forked from encode/django-rest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
269 lines (211 loc) · 8.25 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from unittest import mock
from django.test import TestCase, override_settings
from django.urls import path
from rest_framework.decorators import action
from rest_framework.routers import SimpleRouter
from rest_framework.serializers import ModelSerializer
from rest_framework.utils import json
from rest_framework.utils.breadcrumbs import get_breadcrumbs
from rest_framework.utils.formatting import lazy_format
from rest_framework.utils.urls import remove_query_param, replace_query_param
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from tests.models import BasicModel
class Root(APIView):
pass
class ResourceRoot(APIView):
pass
class ResourceInstance(APIView):
pass
class NestedResourceRoot(APIView):
pass
class NestedResourceInstance(APIView):
pass
class CustomNameResourceInstance(APIView):
def get_view_name(self):
return "Foo"
class ResourceViewSet(ModelViewSet):
serializer_class = ModelSerializer
queryset = BasicModel.objects.all()
@action(detail=False)
def list_action(self, request, *args, **kwargs):
raise NotImplementedError
@action(detail=True)
def detail_action(self, request, *args, **kwargs):
raise NotImplementedError
@action(detail=True, name='Custom Name')
def named_action(self, request, *args, **kwargs):
raise NotImplementedError
@action(detail=True, suffix='Custom Suffix')
def suffixed_action(self, request, *args, **kwargs):
raise NotImplementedError
router = SimpleRouter()
router.register(r'resources', ResourceViewSet)
urlpatterns = [
path('', Root.as_view()),
path('resource/', ResourceRoot.as_view()),
path('resource/customname', CustomNameResourceInstance.as_view()),
path('resource/<int:key>', ResourceInstance.as_view()),
path('resource/<int:key>/', NestedResourceRoot.as_view()),
path('resource/<int:key>/<str:other>', NestedResourceInstance.as_view()),
]
urlpatterns += router.urls
@override_settings(ROOT_URLCONF='tests.test_utils')
class BreadcrumbTests(TestCase):
"""
Tests the breadcrumb functionality used by the HTML renderer.
"""
def test_root_breadcrumbs(self):
url = '/'
assert get_breadcrumbs(url) == [('Root', '/')]
def test_resource_root_breadcrumbs(self):
url = '/resource/'
assert get_breadcrumbs(url) == [
('Root', '/'), ('Resource Root', '/resource/')
]
def test_resource_instance_breadcrumbs(self):
url = '/resource/123'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource Root', '/resource/'),
('Resource Instance', '/resource/123')
]
def test_resource_instance_customname_breadcrumbs(self):
url = '/resource/customname'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource Root', '/resource/'),
('Foo', '/resource/customname')
]
def test_nested_resource_breadcrumbs(self):
url = '/resource/123/'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource Root', '/resource/'),
('Resource Instance', '/resource/123'),
('Nested Resource Root', '/resource/123/')
]
def test_nested_resource_instance_breadcrumbs(self):
url = '/resource/123/abc'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource Root', '/resource/'),
('Resource Instance', '/resource/123'),
('Nested Resource Root', '/resource/123/'),
('Nested Resource Instance', '/resource/123/abc')
]
def test_broken_url_breadcrumbs_handled_gracefully(self):
url = '/foobar'
assert get_breadcrumbs(url) == [('Root', '/')]
def test_modelviewset_resource_instance_breadcrumbs(self):
url = '/resources/1/'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource List', '/resources/'),
('Resource Instance', '/resources/1/')
]
def test_modelviewset_list_action_breadcrumbs(self):
url = '/resources/list_action/'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource List', '/resources/'),
('List action', '/resources/list_action/'),
]
def test_modelviewset_detail_action_breadcrumbs(self):
url = '/resources/1/detail_action/'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource List', '/resources/'),
('Resource Instance', '/resources/1/'),
('Detail action', '/resources/1/detail_action/'),
]
def test_modelviewset_action_name_kwarg(self):
url = '/resources/1/named_action/'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource List', '/resources/'),
('Resource Instance', '/resources/1/'),
('Custom Name', '/resources/1/named_action/'),
]
def test_modelviewset_action_suffix_kwarg(self):
url = '/resources/1/suffixed_action/'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource List', '/resources/'),
('Resource Instance', '/resources/1/'),
('Resource Custom Suffix', '/resources/1/suffixed_action/'),
]
class JsonFloatTests(TestCase):
"""
Internally, wrapped json functions should adhere to strict float handling
"""
def test_dumps(self):
with self.assertRaises(ValueError):
json.dumps(float('inf'))
with self.assertRaises(ValueError):
json.dumps(float('nan'))
def test_loads(self):
with self.assertRaises(ValueError):
json.loads("Infinity")
with self.assertRaises(ValueError):
json.loads("NaN")
@override_settings(REST_FRAMEWORK={'STRICT_JSON': False})
class NonStrictJsonFloatTests(JsonFloatTests):
"""
'STRICT_JSON = False' should not somehow affect internal json behavior
"""
class UrlsReplaceQueryParamTests(TestCase):
"""
Tests the replace_query_param functionality.
"""
def test_valid_unicode_preserved(self):
# Encoded string: '查询'
q = '/?q=%E6%9F%A5%E8%AF%A2'
new_key = 'page'
new_value = 2
value = '%E6%9F%A5%E8%AF%A2'
assert new_key in replace_query_param(q, new_key, new_value)
assert value in replace_query_param(q, new_key, new_value)
def test_valid_unicode_replaced(self):
q = '/?page=1'
value = '1'
new_key = 'q'
new_value = '%E6%9F%A5%E8%AF%A2'
assert new_key in replace_query_param(q, new_key, new_value)
assert value in replace_query_param(q, new_key, new_value)
def test_invalid_unicode(self):
# Encoded string: '��<script>alert(313)</script>=1'
q = '/e/?%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1'
key = 'from'
value = 'login'
assert key in replace_query_param(q, key, value)
class UrlsRemoveQueryParamTests(TestCase):
"""
Tests the remove_query_param functionality.
"""
def test_valid_unicode_removed(self):
q = '/?page=2345&q=%E6%9F%A5%E8%AF%A2'
key = 'page'
value = '2345'
removed_key = 'q'
assert key in remove_query_param(q, removed_key)
assert value in remove_query_param(q, removed_key)
assert '%' not in remove_query_param(q, removed_key)
def test_invalid_unicode(self):
q = '/?from=login&page=2&%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1'
key = 'from'
removed_key = 'page'
assert key in remove_query_param(q, removed_key)
class LazyFormatTests(TestCase):
def test_it_formats_correctly(self):
formatted = lazy_format('Does {} work? {answer}: %s', 'it', answer='Yes')
assert str(formatted) == 'Does it work? Yes: %s'
assert formatted % 'it does' == 'Does it work? Yes: it does'
def test_it_formats_lazily(self):
message = mock.Mock(wraps='message')
formatted = lazy_format(message)
assert message.format.call_count == 0
str(formatted)
assert message.format.call_count == 1
str(formatted)
assert message.format.call_count == 1