Compare commits

..

4 Commits

Author SHA1 Message Date
0a5d22d4d9 More comments 2021-05-15 23:27:33 +02:00
9c86eab14c Testing home view 2021-05-15 23:27:25 +02:00
29d067002e Mixin Test does not rely on actual place view 2021-05-15 23:26:27 +02:00
f5f5980f9b Tests for global templates 2021-05-15 23:26:02 +02:00
5 changed files with 235 additions and 39 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import re
from django.test import TestCase from django.test import TestCase
@ -87,7 +88,7 @@ class ViewTestCase(TestCase):
self.assertEqual( self.assertEqual(
response['location'], response['location'],
redirect_to, redirect_to,
msg='Expecting the response to redirect to %s, where redirected to %s instea' % ( msg='Expecting the response to redirect to %s, where redirected to %s instead' % (
str(redirect_to), str(redirect_to),
str(response['location']) str(response['location'])
) )
@ -114,6 +115,84 @@ class ViewTestCase(TestCase):
def assertHttpMethodNotAllowed(self, response): def assertHttpMethodNotAllowed(self, response):
self.assertHttpCode(response, 405) self.assertHttpCode(response, 405)
class GlobalTemplateTestCaseMixin:
def assertGlobal(self, response):
self.assertLogo(response)
self.assertMenu(response)
self.assertProfileMenu(response)
def assertLogo(self, response):
"""
Looks for an image tag with with 'logo' or 'Logo' in the file name
all within an <header> tag.
"""
self.assertNotEqual(
None,
re.search(
"""<header.*>.*<img.*src=("|').*(logo|Logo).*("|').*>.*</header>""",
response.content.decode().replace('\n', '')
),
msg='Expecting a header containing a logo with \'logo\' in the source name'
)
def assertMenu(self, response):
"""
Looks for an '<nav>' tag containing a link to / and a link labeled 'urbex code.*'
"""
self.assertNotEqual(
None,
re.search(
"""<nav.*>.*<a.*href=("|')/("|').*>.*<a.*>.*urbex code.*</nav>""",
response.content.decode().replace('\n', '').lower()
),
msg='Expecting an menu containing a link to the homepage and a link to the urbex codex'
)
def assertMenuItem(self, response, label=None, url=None):
"""
Checks for an additional menu item by label or url. Regex can be passed.
"""
if label is None and url is None:
raise ValueError("Either a label or url has to be provided")
found_label = None
if label != None:
label = label.lower()
found_label = re.search(
"""<nav.*>.*<a.*>.*%s.*</a>.*</nav>""" % label,
response.content.decode().replace('\n', '').lower()
)
found_url = None
if url != None:
url = url.lower()
found_url = re.search(
"""<nav.*>.*<a.*href=("|')%s("|')>.*</a>.*</nav>""" % url,
response.content.decode().replace('\n', '')
)
self.assertTrue(
found_label != None and found_url != None,
msg='Expecting an menu item with either label \'%s\' or url \'%s\'' % (
label,
url
)
)
def assertProfileMenu(self, response):
"""
Looks for a logout or login+signup link within the an header tag
"""
self.assertNotEqual(
None,
re.search(
"""<header.*>.*(<a.*href=("|').*logout.*("|')|<a.*href=("|').*login.*("|').*<a.*href=("|').*signup.*("|')).*</header>""",
response.content.decode().replace('\n', '').lower()
),
msg='Expecting a profile menu containing either a logout link or a login and then a signup link'
)
class TaggableViewTestCaseMixin: class TaggableViewTestCaseMixin:
def assertTaggableContext(self, context): def assertTaggableContext(self, context):

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python 23238#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.test import TestCase, RequestFactory, Client from django.test import TestCase, RequestFactory, Client
@ -6,6 +6,7 @@ from django.urls import reverse_lazy
from django.contrib.auth.models import User, AnonymousUser from django.contrib.auth.models import User, AnonymousUser
from django.contrib.messages.storage.fallback import FallbackStorage from django.contrib.messages.storage.fallback import FallbackStorage
from django.utils import timezone from django.utils import timezone
from django.shortcuts import render
from lostplaces.models import Place from lostplaces.models import Place
from lostplaces.views import IsAuthenticatedMixin from lostplaces.views import IsAuthenticatedMixin
@ -33,14 +34,14 @@ class TestIsAuthenticated(ViewTestCase):
self.assertHttpMethodNotAllowed(response) self.assertHttpMethodNotAllowed(response)
def test_not_logged_in(self): def test_not_logged_in(self):
request = RequestFactory().get('/someurl1234') request = RequestFactory().get('/')
request.user = AnonymousUser() request.user = AnonymousUser()
request.session = 'session' request.session = 'session'
messages = FallbackStorage(request) messages = FallbackStorage(request)
request._messages = messages request._messages = messages
response = IsAuthenticatedMixin.as_view()(request) response = IsAuthenticatedMixin.as_view()(request)
self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234'])) self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/']))
response = self.client.get(response['Location']) response = self.client.get(response['Location'])
self.assertTrue(len(messages) > 0) self.assertTrue(len(messages) > 0)
@ -87,3 +88,4 @@ class TestIsPlaceSubmitterMixin(TestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assertTrue(response.context['messages']) self.assertTrue(response.context['messages'])
self.assertTrue(len(response.context['messages']) > 0) self.assertTrue(len(response.context['messages']) > 0)

View File

@ -17,10 +17,43 @@ from lostplaces.forms import PlaceImageForm, PlaceForm
from lostplaces.tests.views import ( from lostplaces.tests.views import (
ViewTestCase, ViewTestCase,
TaggableViewTestCaseMixin, 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): class TestPlaceCreateView(ViewTestCase):
view = PlaceCreateView view = PlaceCreateView
@ -53,39 +86,6 @@ class TestPlaceCreateView(ViewTestCase):
self.assertHasForm(response, 'place_image_form', PlaceImageForm) self.assertHasForm(response, 'place_image_form', PlaceImageForm)
self.assertHasForm(response, 'place_form', PlaceForm) 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): class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase):
view = PlaceDetailView view = PlaceDetailView

View File

@ -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'
)

View File

@ -61,6 +61,9 @@ class IsPlaceSubmitterMixin(UserPassesTestMixin, View):
return False return False
class PlaceAssetCreateView(IsAuthenticatedMixin, SuccessMessageMixin, CreateView): class PlaceAssetCreateView(IsAuthenticatedMixin, SuccessMessageMixin, CreateView):
"""
Abstract View for creating a place asset (i.e. PlaceImage)
"""
model = None model = None
template_name = '' template_name = ''
success_message = '' success_message = ''