forked from CGM_Public/pretix_original
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.
This commit is contained in:
committed by
Raphael Michel
parent
f763a8694b
commit
1c01e23867
@@ -15,7 +15,7 @@ presale_patterns_main = [
|
|||||||
url(r'', include((locale_patterns + [
|
url(r'', include((locale_patterns + [
|
||||||
url(r'^(?P<organizer>[^/]+)/', include(organizer_patterns)),
|
url(r'^(?P<organizer>[^/]+)/', include(organizer_patterns)),
|
||||||
url(r'^(?P<organizer>[^/]+)/(?P<event>[^/]+)/', include(event_patterns)),
|
url(r'^(?P<organizer>[^/]+)/(?P<event>[^/]+)/', include(event_patterns)),
|
||||||
url(r'^$', TemplateView.as_view(template_name='pretixpresale/index.html'))
|
url(r'^$', TemplateView.as_view(template_name='pretixpresale/index.html'), name="index")
|
||||||
], 'presale')))
|
], 'presale')))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
41
src/tests/base/test_urls.py
Normal file
41
src/tests/base/test_urls.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
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
|
||||||
Reference in New Issue
Block a user