Tests for global templates

This commit is contained in:
reverend 2021-05-15 23:26:02 +02:00
parent 10c08a2f5e
commit f5f5980f9b

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):