Files
pretix_original/src/tests/base/test_urls.py
Tim Freund 1c01e23867 Name presale index + unit test for URL names (#784)
* Name the default URL

If metrics collection is enabled, the index page of the site will fail
to load: without a name, the metrics middleware throws a TypeError.

* Test for names on all URLs

This test passes if all URLs have names. Without names, URLs will cause
the optional metrics middleware to throw a TypeError.
2018-02-26 10:17:42 +01:00

42 lines
1.2 KiB
Python

from importlib import import_module
from django.conf import settings
from django.test import TestCase
class URLTestCase(TestCase):
"""
This test case tests for a name string on all URLs. Unnamed
URLs will cause a TypeError in the metrics middleware.
"""
pattern_attrs = ['urlpatterns', 'url_patterns']
def test_url_names(self):
urlconf = import_module(settings.ROOT_URLCONF)
nameless = self.find_nameless_urls(urlconf)
message = "URL regexes missing names: %s" % " ".join([n.regex.pattern for n in nameless])
self.assertIs(len(nameless), 0, message)
def find_nameless_urls(self, conf):
nameless = []
patterns = self.get_patterns(conf)
for u in patterns:
if self.has_patterns(u):
nameless.extend(self.find_nameless_urls(u))
else:
if u.name is None:
nameless.append(u)
return nameless
def get_patterns(self, conf):
for pa in self.pattern_attrs:
if hasattr(conf, pa):
return getattr(conf, pa)
return []
def has_patterns(self, conf):
for pa in self.pattern_attrs:
if hasattr(conf, pa):
return True
return False