Compare commits

..

No commits in common. "0a5d22d4d9a6b3b5822a24d3def56851ebee10ee" and "10c08a2f5e25e8565fb95b0f4a8e4bf0081d1d27" have entirely different histories.

5 changed files with 39 additions and 235 deletions

View File

@ -1,6 +1,5 @@
#!/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
@ -88,7 +87,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 instead' % ( msg='Expecting the response to redirect to %s, where redirected to %s instea' % (
str(redirect_to), str(redirect_to),
str(response['location']) str(response['location'])
) )
@ -114,84 +113,6 @@ 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:

View File

@ -1,4 +1,4 @@
23238#!/usr/bin/env python #!/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,7 +6,6 @@ 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
@ -34,14 +33,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('/') request = RequestFactory().get('/someurl1234')
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=/'])) self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
response = self.client.get(response['Location']) response = self.client.get(response['Location'])
self.assertTrue(len(messages) > 0) self.assertTrue(len(messages) > 0)
@ -87,5 +86,4 @@ class TestIsPlaceSubmitterMixin(TestCase):
response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1})) response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1}))
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,43 +17,10 @@ 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
@ -86,6 +53,39 @@ 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

@ -1,112 +0,0 @@
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,9 +61,6 @@ 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 = ''