From 9c86eab14c44f58b28e9c05bc98568d55c06a60e Mon Sep 17 00:00:00 2001 From: reverend Date: Sat, 15 May 2021 23:27:25 +0200 Subject: [PATCH] Testing home view --- .../tests/views/test_place_views.py | 68 +++++------ .../lostplaces/tests/views/test_views.py | 112 ++++++++++++++++++ 2 files changed, 146 insertions(+), 34 deletions(-) create mode 100644 django_lostplaces/lostplaces/tests/views/test_views.py diff --git a/django_lostplaces/lostplaces/tests/views/test_place_views.py b/django_lostplaces/lostplaces/tests/views/test_place_views.py index eb5c363..2880893 100644 --- a/django_lostplaces/lostplaces/tests/views/test_place_views.py +++ b/django_lostplaces/lostplaces/tests/views/test_place_views.py @@ -17,10 +17,43 @@ from lostplaces.forms import PlaceImageForm, PlaceForm from lostplaces.tests.views import ( ViewTestCase, TaggableViewTestCaseMixin, - MapableViewTestCaseMixin + MapableViewTestCaseMixin, + GlobalTemplateTestCaseMixin ) +class TestPlaceListView(GlobalTemplateTestCaseMixin, ViewTestCase): + view = PlaceListView + + @classmethod + def setUpTestData(cls): + user = User.objects.create_user( + username='testpeter', + password='Develop123' + ) + + place = Place.objects.create( + name='Im a place', + submitted_when=timezone.now(), + submitted_by=user.explorer, + location='Testtown', + latitude=50.5, + longitude=7.0, + description='This is just a test, do not worry' + ) + place.tags.add('I a tag', 'testlocation') + place.save() + + def setUp(self): + self.client = Client() + + def test_list_view(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.get(reverse('place_list')) + + self.assertContext(response, 'mapping_config') + self.assertGlobal(response) + class TestPlaceCreateView(ViewTestCase): view = PlaceCreateView @@ -53,39 +86,6 @@ class TestPlaceCreateView(ViewTestCase): self.assertHasForm(response, 'place_image_form', PlaceImageForm) self.assertHasForm(response, 'place_form', PlaceForm) - -class TestPlaceListView(ViewTestCase): - view = PlaceListView - - @classmethod - def setUpTestData(cls): - user = User.objects.create_user( - username='testpeter', - password='Develop123' - ) - - place = Place.objects.create( - name='Im a place', - submitted_when=timezone.now(), - submitted_by=user.explorer, - location='Testtown', - latitude=50.5, - longitude=7.0, - description='This is just a test, do not worry' - ) - place.tags.add('I a tag', 'testlocation') - place.save() - - def setUp(self): - self.client = Client() - - def test_list_view(self): - self.client.login(username='testpeter', password='Develop123') - response = self.client.get(reverse('place_list')) - - self.assertContext(response, 'mapping_config') - - class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase): view = PlaceDetailView diff --git a/django_lostplaces/lostplaces/tests/views/test_views.py b/django_lostplaces/lostplaces/tests/views/test_views.py new file mode 100644 index 0000000..91e7ab4 --- /dev/null +++ b/django_lostplaces/lostplaces/tests/views/test_views.py @@ -0,0 +1,112 @@ +import re + +from django.test import TestCase, Client +from django.urls import reverse +from django.contrib.auth.models import User +from django.utils import timezone + + +from lostplaces.models import Place + +from lostplaces.tests.views import ( + ViewTestCase, + GlobalTemplateTestCaseMixin +) + +class TestHomeView(GlobalTemplateTestCaseMixin, ViewTestCase): + + @classmethod + def setUpTestData(cls): + user = User.objects.create_user( + username='testpeter', + password='Develop123' + ) + + place = Place.objects.create( + name='Im a place', + submitted_when=timezone.now(), + submitted_by=user.explorer, + location='Testtown', + latitude=50.5, + longitude=7.0, + description='This is just a test, do not worry' + ) + place.tags.add('I a tag', 'testlocation') + place.save() + + def setUp(self): + self.client = Client() + + def test_global(self): + response = self.client.get(reverse('lostplaces_home')) + self.assertGlobal(response) + + def test_place_list_authenticated(self): + """ + Testing there is the place list containing the name, + location and description of the latest place for + authenticated users. + """ + self.client.login(username='testpeter', password='Develop123') + response = self.client.get(reverse('lostplaces_home')) + self.assertNotEqual( + None, + re.search( + """Im a place.*Testtown.*This is just a test, do not worry""", + response.content.decode().replace('\n', '') + ), + msg='Expecting the test place to show up on the homepage' + ) + + def test_place_list_unauthenticated(self): + """ + Testing there is a place list of the lates places + containing the place names only for unauthenticated users. + """ + response = self.client.get(reverse('lostplaces_home')) + self.assertNotEqual( + None, + re.search( + """Im a place""", + response.content.decode().replace('\n', '') + ), + msg='Expecting the test place to show up on the homepage' + ) + self.assertEqual( + None, + re.search( + """Testtown.*This is just a test, do not worry""", + response.content.decode().replace('\n', '') + ), + msg='Expecting the test place to show up on the homepage' + ) + + def test_map_authenticated(self): + """ + Testing there is a map showing all the lates places + on a map for authenticated users. + """ + self.client.login(username='testpeter', password='Develop123') + response = self.client.get(reverse('lostplaces_home')) + self.assertNotEqual( + None, + re.search( + """map.*7.0,.*50.5.*Im a place""", + response.content.decode().replace('\n', '') + ), + msg='Expecting the test place to show up on the map' + ) + + def test_map_unauthenticated(self): + """ + Testing there is no map for unauthenticated users. + """ + response = self.client.get(reverse('lostplaces_home')) + self.assertEqual( + None, + re.search( + """7.0,.*50.5.*Im a place""", + response.content.decode().replace('\n', '') + ), + msg='Expecting the test place to show up on the map' + ) \ No newline at end of file