Debian Codegraph @

tests/test_urlpatterns.py

raw
1 from collections import namedtuple
2
3 from django.test import TestCase
4 from django.urls import Resolver404, URLResolver, include, path, re_path
5 from django.urls.resolvers import RegexPattern
6
7 from rest_framework.test import APIRequestFactory
8 from rest_framework.urlpatterns import format_suffix_patterns
9
10 # A container class for test paths for the test case
11 URLTestPath = namedtuple('URLTestPath', ['path', 'args', 'kwargs'])
12
13
14 def dummy_view(request, *args, **kwargs):
15 pass
16
17
18 class FormatSuffixTests(TestCase):
19 """
20 Tests `format_suffix_patterns` against different URLPatterns to ensure the
21 URLs still resolve properly, including any captured parameters.
22 """
23 def _resolve_urlpatterns(self, urlpatterns, test_paths, allowed=None):
24 factory = APIRequestFactory()
25 try:
26 urlpatterns = format_suffix_patterns(urlpatterns, allowed=allowed)
27 except Exception:
28 self.fail("Failed to apply `format_suffix_patterns` on the supplied urlpatterns")
29 resolver = URLResolver(RegexPattern(r'^/'), urlpatterns)
30 for test_path in test_paths:
31 try:
32 test_path, expected_resolved = test_path
33 except (TypeError, ValueError):
34 expected_resolved = True
35
36 request = factory.get(test_path.path)
37 try:
38 callback, callback_args, callback_kwargs = resolver.resolve(request.path_info)
39 except Resolver404:
40 callback, callback_args, callback_kwargs = (None, None, None)
41 if expected_resolved:
42 raise
43 except Exception:
44 self.fail("Failed to resolve URL: %s" % request.path_info)
45
46 if not expected_resolved:
47 assert callback is None
48 continue
49
50 assert callback_args == test_path.args
51 assert callback_kwargs == test_path.kwargs
52
53 def _test_trailing_slash(self, urlpatterns):
54 test_paths = [
55 (URLTestPath('/test.api', (), {'format': 'api'}), True),
56 (URLTestPath('/test/.api', (), {'format': 'api'}), False),
57 (URLTestPath('/test.api/', (), {'format': 'api'}), True),
58 ]
59 self._resolve_urlpatterns(urlpatterns, test_paths)
60
61 def test_trailing_slash(self):
62 urlpatterns = [
63 path('test/', dummy_view),
64 ]
65 self._test_trailing_slash(urlpatterns)
66
67 def test_trailing_slash_django2(self):
68 urlpatterns = [
69 path('test/', dummy_view),
70 ]
71 self._test_trailing_slash(urlpatterns)
72
73 def _test_format_suffix(self, urlpatterns):
74 test_paths = [
75 URLTestPath('/test', (), {}),
76 URLTestPath('/test.api', (), {'format': 'api'}),
77 URLTestPath('/test.asdf', (), {'format': 'asdf'}),
78 ]
79 self._resolve_urlpatterns(urlpatterns, test_paths)
80
81 def test_format_suffix(self):
82 urlpatterns = [
83 path('test', dummy_view),
84 ]
85 self._test_format_suffix(urlpatterns)
86
87 def test_format_suffix_django2(self):
88 urlpatterns = [
89 path('test', dummy_view),
90 ]
91 self._test_format_suffix(urlpatterns)
92
93 def test_format_suffix_django2_args(self):
94 urlpatterns = [
95 path('convtest/<int:pk>', dummy_view),
96 re_path(r'^retest/(?P<pk>[1-9]{2})$', dummy_view),
97 ]
98 test_paths = [
99 URLTestPath('/convtest/42', (), {'pk': 42}),
100 URLTestPath('/convtest/42.api', (), {'pk': 42, 'format': 'api'}),
101 URLTestPath('/convtest/42.asdf', (), {'pk': 42, 'format': 'asdf'}),
102 URLTestPath('/retest/42', (), {'pk': '42'}),
103 URLTestPath('/retest/42.api', (), {'pk': '42', 'format': 'api'}),
104 URLTestPath('/retest/42.asdf', (), {'pk': '42', 'format': 'asdf'}),
105 ]
106 self._resolve_urlpatterns(urlpatterns, test_paths)
107
108 def _test_default_args(self, urlpatterns):
109 test_paths = [
110 URLTestPath('/test', (), {'foo': 'bar', }),
111 URLTestPath('/test.api', (), {'foo': 'bar', 'format': 'api'}),
112 URLTestPath('/test.asdf', (), {'foo': 'bar', 'format': 'asdf'}),
113 ]
114 self._resolve_urlpatterns(urlpatterns, test_paths)
115
116 def test_default_args(self):
117 urlpatterns = [
118 path('test', dummy_view, {'foo': 'bar'}),
119 ]
120 self._test_default_args(urlpatterns)
121
122 def test_default_args_django2(self):
123 urlpatterns = [
124 path('test', dummy_view, {'foo': 'bar'}),
125 ]
126 self._test_default_args(urlpatterns)
127
128 def _test_included_urls(self, urlpatterns):
129 test_paths = [
130 URLTestPath('/test/path', (), {'foo': 'bar', }),
131 URLTestPath('/test/path.api', (), {'foo': 'bar', 'format': 'api'}),
132 URLTestPath('/test/path.asdf', (), {'foo': 'bar', 'format': 'asdf'}),
133 ]
134 self._resolve_urlpatterns(urlpatterns, test_paths)
135
136 def test_included_urls(self):
137 nested_patterns = [
138 path('path', dummy_view)
139 ]
140 urlpatterns = [
141 path('test/', include(nested_patterns), {'foo': 'bar'}),
142 ]
143 self._test_included_urls(urlpatterns)
144
145 def test_included_urls_mixed(self):
146 nested_patterns = [
147 path('path/<int:child>', dummy_view),
148 re_path(r'^re_path/(?P<child>[0-9]{2})$', dummy_view)
149 ]
150 urlpatterns = [
151 re_path(r'^pre_path/(?P<parent>[0-9]{2})/', include(nested_patterns), {'foo': 'bar'}),
152 path('ppath/<int:parent>/', include(nested_patterns), {'foo': 'bar'}),
153 ]
154 test_paths = [
155 # parent re_path() nesting child path()
156 URLTestPath('/pre_path/87/path/42', (), {'parent': '87', 'child': 42, 'foo': 'bar', }),
157 URLTestPath('/pre_path/87/path/42.api', (), {'parent': '87', 'child': 42, 'foo': 'bar', 'format': 'api'}),
158 URLTestPath('/pre_path/87/path/42.asdf', (), {'parent': '87', 'child': 42, 'foo': 'bar', 'format': 'asdf'}),
159
160 # parent path() nesting child re_path()
161 URLTestPath('/ppath/87/re_path/42', (), {'parent': 87, 'child': '42', 'foo': 'bar', }),
162 URLTestPath('/ppath/87/re_path/42.api', (), {'parent': 87, 'child': '42', 'foo': 'bar', 'format': 'api'}),
163 URLTestPath('/ppath/87/re_path/42.asdf', (), {'parent': 87, 'child': '42', 'foo': 'bar', 'format': 'asdf'}),
164
165 # parent path() nesting child path()
166 URLTestPath('/ppath/87/path/42', (), {'parent': 87, 'child': 42, 'foo': 'bar', }),
167 URLTestPath('/ppath/87/path/42.api', (), {'parent': 87, 'child': 42, 'foo': 'bar', 'format': 'api'}),
168 URLTestPath('/ppath/87/path/42.asdf', (), {'parent': 87, 'child': 42, 'foo': 'bar', 'format': 'asdf'}),
169
170 # parent re_path() nesting child re_path()
171 URLTestPath('/pre_path/87/re_path/42', (), {'parent': '87', 'child': '42', 'foo': 'bar', }),
172 URLTestPath('/pre_path/87/re_path/42.api', (), {'parent': '87', 'child': '42', 'foo': 'bar', 'format': 'api'}),
173 URLTestPath('/pre_path/87/re_path/42.asdf', (), {'parent': '87', 'child': '42', 'foo': 'bar', 'format': 'asdf'}),
174 ]
175 self._resolve_urlpatterns(urlpatterns, test_paths)
176
177 def _test_allowed_formats(self, urlpatterns):
178 allowed_formats = ['good', 'ugly']
179 test_paths = [
180 (URLTestPath('/test.good/', (), {'format': 'good'}), True),
181 (URLTestPath('/test.bad', (), {}), False),
182 (URLTestPath('/test.ugly', (), {'format': 'ugly'}), True),
183 ]
184 self._resolve_urlpatterns(urlpatterns, test_paths, allowed=allowed_formats)
185
186 def test_allowed_formats_re_path(self):
187 urlpatterns = [
188 re_path(r'^[tes]{4}$', dummy_view),
189 ]
190 self._test_allowed_formats(urlpatterns)
191
192 def test_allowed_formats_path(self):
193 urlpatterns = [
194 path('test', dummy_view),
195 ]
196 self._test_allowed_formats(urlpatterns)