|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 Regression tests for the Test Client, especially the customized assertions. |
|
4 """ |
|
5 import os |
|
6 |
|
7 from django.conf import settings |
|
8 from django.core.exceptions import SuspiciousOperation |
|
9 from django.core.urlresolvers import reverse |
|
10 from django.template import (TemplateDoesNotExist, TemplateSyntaxError, |
|
11 Context, loader) |
|
12 from django.test import TestCase, Client |
|
13 from django.test.client import encode_file |
|
14 from django.test.utils import ContextList |
|
15 |
|
16 |
|
17 class AssertContainsTests(TestCase): |
|
18 def setUp(self): |
|
19 self.old_templates = settings.TEMPLATE_DIRS |
|
20 settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),) |
|
21 |
|
22 def tearDown(self): |
|
23 settings.TEMPLATE_DIRS = self.old_templates |
|
24 |
|
25 def test_contains(self): |
|
26 "Responses can be inspected for content, including counting repeated substrings" |
|
27 response = self.client.get('/test_client_regress/no_template_view/') |
|
28 |
|
29 self.assertNotContains(response, 'never') |
|
30 self.assertContains(response, 'never', 0) |
|
31 self.assertContains(response, 'once') |
|
32 self.assertContains(response, 'once', 1) |
|
33 self.assertContains(response, 'twice') |
|
34 self.assertContains(response, 'twice', 2) |
|
35 |
|
36 try: |
|
37 self.assertContains(response, 'text', status_code=999) |
|
38 except AssertionError, e: |
|
39 self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)") |
|
40 try: |
|
41 self.assertContains(response, 'text', status_code=999, msg_prefix='abc') |
|
42 except AssertionError, e: |
|
43 self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)") |
|
44 |
|
45 try: |
|
46 self.assertNotContains(response, 'text', status_code=999) |
|
47 except AssertionError, e: |
|
48 self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)") |
|
49 try: |
|
50 self.assertNotContains(response, 'text', status_code=999, msg_prefix='abc') |
|
51 except AssertionError, e: |
|
52 self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)") |
|
53 |
|
54 try: |
|
55 self.assertNotContains(response, 'once') |
|
56 except AssertionError, e: |
|
57 self.assertEquals(str(e), "Response should not contain 'once'") |
|
58 try: |
|
59 self.assertNotContains(response, 'once', msg_prefix='abc') |
|
60 except AssertionError, e: |
|
61 self.assertEquals(str(e), "abc: Response should not contain 'once'") |
|
62 |
|
63 try: |
|
64 self.assertContains(response, 'never', 1) |
|
65 except AssertionError, e: |
|
66 self.assertEquals(str(e), "Found 0 instances of 'never' in response (expected 1)") |
|
67 try: |
|
68 self.assertContains(response, 'never', 1, msg_prefix='abc') |
|
69 except AssertionError, e: |
|
70 self.assertEquals(str(e), "abc: Found 0 instances of 'never' in response (expected 1)") |
|
71 |
|
72 try: |
|
73 self.assertContains(response, 'once', 0) |
|
74 except AssertionError, e: |
|
75 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 0)") |
|
76 try: |
|
77 self.assertContains(response, 'once', 0, msg_prefix='abc') |
|
78 except AssertionError, e: |
|
79 self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 0)") |
|
80 |
|
81 try: |
|
82 self.assertContains(response, 'once', 2) |
|
83 except AssertionError, e: |
|
84 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)") |
|
85 try: |
|
86 self.assertContains(response, 'once', 2, msg_prefix='abc') |
|
87 except AssertionError, e: |
|
88 self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 2)") |
|
89 |
|
90 try: |
|
91 self.assertContains(response, 'twice', 1) |
|
92 except AssertionError, e: |
|
93 self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)") |
|
94 try: |
|
95 self.assertContains(response, 'twice', 1, msg_prefix='abc') |
|
96 except AssertionError, e: |
|
97 self.assertEquals(str(e), "abc: Found 2 instances of 'twice' in response (expected 1)") |
|
98 |
|
99 try: |
|
100 self.assertContains(response, 'thrice') |
|
101 except AssertionError, e: |
|
102 self.assertEquals(str(e), "Couldn't find 'thrice' in response") |
|
103 try: |
|
104 self.assertContains(response, 'thrice', msg_prefix='abc') |
|
105 except AssertionError, e: |
|
106 self.assertEquals(str(e), "abc: Couldn't find 'thrice' in response") |
|
107 |
|
108 try: |
|
109 self.assertContains(response, 'thrice', 3) |
|
110 except AssertionError, e: |
|
111 self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)") |
|
112 try: |
|
113 self.assertContains(response, 'thrice', 3, msg_prefix='abc') |
|
114 except AssertionError, e: |
|
115 self.assertEquals(str(e), "abc: Found 0 instances of 'thrice' in response (expected 3)") |
|
116 |
|
117 def test_unicode_contains(self): |
|
118 "Unicode characters can be found in template context" |
|
119 #Regression test for #10183 |
|
120 r = self.client.get('/test_client_regress/check_unicode/') |
|
121 self.assertContains(r, u'さかき') |
|
122 self.assertContains(r, '\xe5\xb3\xa0'.decode('utf-8')) |
|
123 |
|
124 def test_unicode_not_contains(self): |
|
125 "Unicode characters can be searched for, and not found in template context" |
|
126 #Regression test for #10183 |
|
127 r = self.client.get('/test_client_regress/check_unicode/') |
|
128 self.assertNotContains(r, u'はたけ') |
|
129 self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8')) |
|
130 |
|
131 |
|
132 class AssertTemplateUsedTests(TestCase): |
|
133 fixtures = ['testdata.json'] |
|
134 |
|
135 def test_no_context(self): |
|
136 "Template usage assertions work then templates aren't in use" |
|
137 response = self.client.get('/test_client_regress/no_template_view/') |
|
138 |
|
139 # Check that the no template case doesn't mess with the template assertions |
|
140 self.assertTemplateNotUsed(response, 'GET Template') |
|
141 |
|
142 try: |
|
143 self.assertTemplateUsed(response, 'GET Template') |
|
144 except AssertionError, e: |
|
145 self.assertEquals(str(e), "No templates used to render the response") |
|
146 |
|
147 try: |
|
148 self.assertTemplateUsed(response, 'GET Template', msg_prefix='abc') |
|
149 except AssertionError, e: |
|
150 self.assertEquals(str(e), "abc: No templates used to render the response") |
|
151 |
|
152 def test_single_context(self): |
|
153 "Template assertions work when there is a single context" |
|
154 response = self.client.get('/test_client/post_view/', {}) |
|
155 |
|
156 try: |
|
157 self.assertTemplateNotUsed(response, 'Empty GET Template') |
|
158 except AssertionError, e: |
|
159 self.assertEquals(str(e), "Template 'Empty GET Template' was used unexpectedly in rendering the response") |
|
160 |
|
161 try: |
|
162 self.assertTemplateNotUsed(response, 'Empty GET Template', msg_prefix='abc') |
|
163 except AssertionError, e: |
|
164 self.assertEquals(str(e), "abc: Template 'Empty GET Template' was used unexpectedly in rendering the response") |
|
165 |
|
166 try: |
|
167 self.assertTemplateUsed(response, 'Empty POST Template') |
|
168 except AssertionError, e: |
|
169 self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template") |
|
170 |
|
171 try: |
|
172 self.assertTemplateUsed(response, 'Empty POST Template', msg_prefix='abc') |
|
173 except AssertionError, e: |
|
174 self.assertEquals(str(e), "abc: Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template") |
|
175 |
|
176 def test_multiple_context(self): |
|
177 "Template assertions work when there are multiple contexts" |
|
178 post_data = { |
|
179 'text': 'Hello World', |
|
180 'email': 'foo@example.com', |
|
181 'value': 37, |
|
182 'single': 'b', |
|
183 'multi': ('b','c','e') |
|
184 } |
|
185 response = self.client.post('/test_client/form_view_with_template/', post_data) |
|
186 self.assertContains(response, 'POST data OK') |
|
187 try: |
|
188 self.assertTemplateNotUsed(response, "form_view.html") |
|
189 except AssertionError, e: |
|
190 self.assertEquals(str(e), "Template 'form_view.html' was used unexpectedly in rendering the response") |
|
191 |
|
192 try: |
|
193 self.assertTemplateNotUsed(response, 'base.html') |
|
194 except AssertionError, e: |
|
195 self.assertEquals(str(e), "Template 'base.html' was used unexpectedly in rendering the response") |
|
196 |
|
197 try: |
|
198 self.assertTemplateUsed(response, "Valid POST Template") |
|
199 except AssertionError, e: |
|
200 self.assertEquals(str(e), "Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html") |
|
201 |
|
202 class AssertRedirectsTests(TestCase): |
|
203 def test_redirect_page(self): |
|
204 "An assertion is raised if the original page couldn't be retrieved as expected" |
|
205 # This page will redirect with code 301, not 302 |
|
206 response = self.client.get('/test_client/permanent_redirect_view/') |
|
207 try: |
|
208 self.assertRedirects(response, '/test_client/get_view/') |
|
209 except AssertionError, e: |
|
210 self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)") |
|
211 |
|
212 try: |
|
213 self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc') |
|
214 except AssertionError, e: |
|
215 self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 301 (expected 302)") |
|
216 |
|
217 def test_lost_query(self): |
|
218 "An assertion is raised if the redirect location doesn't preserve GET parameters" |
|
219 response = self.client.get('/test_client/redirect_view/', {'var': 'value'}) |
|
220 try: |
|
221 self.assertRedirects(response, '/test_client/get_view/') |
|
222 except AssertionError, e: |
|
223 self.assertEquals(str(e), "Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'") |
|
224 |
|
225 try: |
|
226 self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc') |
|
227 except AssertionError, e: |
|
228 self.assertEquals(str(e), "abc: Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'") |
|
229 |
|
230 def test_incorrect_target(self): |
|
231 "An assertion is raised if the response redirects to another target" |
|
232 response = self.client.get('/test_client/permanent_redirect_view/') |
|
233 try: |
|
234 # Should redirect to get_view |
|
235 self.assertRedirects(response, '/test_client/some_view/') |
|
236 except AssertionError, e: |
|
237 self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)") |
|
238 |
|
239 def test_target_page(self): |
|
240 "An assertion is raised if the response redirect target cannot be retrieved as expected" |
|
241 response = self.client.get('/test_client/double_redirect_view/') |
|
242 try: |
|
243 # The redirect target responds with a 301 code, not 200 |
|
244 self.assertRedirects(response, 'http://testserver/test_client/permanent_redirect_view/') |
|
245 except AssertionError, e: |
|
246 self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)") |
|
247 |
|
248 try: |
|
249 # The redirect target responds with a 301 code, not 200 |
|
250 self.assertRedirects(response, 'http://testserver/test_client/permanent_redirect_view/', msg_prefix='abc') |
|
251 except AssertionError, e: |
|
252 self.assertEquals(str(e), "abc: Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)") |
|
253 |
|
254 def test_redirect_chain(self): |
|
255 "You can follow a redirect chain of multiple redirects" |
|
256 response = self.client.get('/test_client_regress/redirects/further/more/', {}, follow=True) |
|
257 self.assertRedirects(response, '/test_client_regress/no_template_view/', |
|
258 status_code=301, target_status_code=200) |
|
259 |
|
260 self.assertEquals(len(response.redirect_chain), 1) |
|
261 self.assertEquals(response.redirect_chain[0], ('http://testserver/test_client_regress/no_template_view/', 301)) |
|
262 |
|
263 def test_multiple_redirect_chain(self): |
|
264 "You can follow a redirect chain of multiple redirects" |
|
265 response = self.client.get('/test_client_regress/redirects/', {}, follow=True) |
|
266 self.assertRedirects(response, '/test_client_regress/no_template_view/', |
|
267 status_code=301, target_status_code=200) |
|
268 |
|
269 self.assertEquals(len(response.redirect_chain), 3) |
|
270 self.assertEquals(response.redirect_chain[0], ('http://testserver/test_client_regress/redirects/further/', 301)) |
|
271 self.assertEquals(response.redirect_chain[1], ('http://testserver/test_client_regress/redirects/further/more/', 301)) |
|
272 self.assertEquals(response.redirect_chain[2], ('http://testserver/test_client_regress/no_template_view/', 301)) |
|
273 |
|
274 def test_redirect_chain_to_non_existent(self): |
|
275 "You can follow a chain to a non-existent view" |
|
276 response = self.client.get('/test_client_regress/redirect_to_non_existent_view2/', {}, follow=True) |
|
277 self.assertRedirects(response, '/test_client_regress/non_existent_view/', |
|
278 status_code=301, target_status_code=404) |
|
279 |
|
280 def test_redirect_chain_to_self(self): |
|
281 "Redirections to self are caught and escaped" |
|
282 response = self.client.get('/test_client_regress/redirect_to_self/', {}, follow=True) |
|
283 # The chain of redirects stops once the cycle is detected. |
|
284 self.assertRedirects(response, '/test_client_regress/redirect_to_self/', |
|
285 status_code=301, target_status_code=301) |
|
286 self.assertEquals(len(response.redirect_chain), 2) |
|
287 |
|
288 def test_circular_redirect(self): |
|
289 "Circular redirect chains are caught and escaped" |
|
290 response = self.client.get('/test_client_regress/circular_redirect_1/', {}, follow=True) |
|
291 # The chain of redirects will get back to the starting point, but stop there. |
|
292 self.assertRedirects(response, '/test_client_regress/circular_redirect_2/', |
|
293 status_code=301, target_status_code=301) |
|
294 self.assertEquals(len(response.redirect_chain), 4) |
|
295 |
|
296 def test_redirect_chain_post(self): |
|
297 "A redirect chain will be followed from an initial POST post" |
|
298 response = self.client.post('/test_client_regress/redirects/', |
|
299 {'nothing': 'to_send'}, follow=True) |
|
300 self.assertRedirects(response, |
|
301 '/test_client_regress/no_template_view/', 301, 200) |
|
302 self.assertEquals(len(response.redirect_chain), 3) |
|
303 |
|
304 def test_redirect_chain_head(self): |
|
305 "A redirect chain will be followed from an initial HEAD request" |
|
306 response = self.client.head('/test_client_regress/redirects/', |
|
307 {'nothing': 'to_send'}, follow=True) |
|
308 self.assertRedirects(response, |
|
309 '/test_client_regress/no_template_view/', 301, 200) |
|
310 self.assertEquals(len(response.redirect_chain), 3) |
|
311 |
|
312 def test_redirect_chain_options(self): |
|
313 "A redirect chain will be followed from an initial OPTIONS request" |
|
314 response = self.client.options('/test_client_regress/redirects/', |
|
315 {'nothing': 'to_send'}, follow=True) |
|
316 self.assertRedirects(response, |
|
317 '/test_client_regress/no_template_view/', 301, 200) |
|
318 self.assertEquals(len(response.redirect_chain), 3) |
|
319 |
|
320 def test_redirect_chain_put(self): |
|
321 "A redirect chain will be followed from an initial PUT request" |
|
322 response = self.client.put('/test_client_regress/redirects/', |
|
323 {'nothing': 'to_send'}, follow=True) |
|
324 self.assertRedirects(response, |
|
325 '/test_client_regress/no_template_view/', 301, 200) |
|
326 self.assertEquals(len(response.redirect_chain), 3) |
|
327 |
|
328 def test_redirect_chain_delete(self): |
|
329 "A redirect chain will be followed from an initial DELETE request" |
|
330 response = self.client.delete('/test_client_regress/redirects/', |
|
331 {'nothing': 'to_send'}, follow=True) |
|
332 self.assertRedirects(response, |
|
333 '/test_client_regress/no_template_view/', 301, 200) |
|
334 self.assertEquals(len(response.redirect_chain), 3) |
|
335 |
|
336 def test_redirect_chain_on_non_redirect_page(self): |
|
337 "An assertion is raised if the original page couldn't be retrieved as expected" |
|
338 # This page will redirect with code 301, not 302 |
|
339 response = self.client.get('/test_client/get_view/', follow=True) |
|
340 try: |
|
341 self.assertRedirects(response, '/test_client/get_view/') |
|
342 except AssertionError, e: |
|
343 self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)") |
|
344 |
|
345 try: |
|
346 self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc') |
|
347 except AssertionError, e: |
|
348 self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 200 (expected 302)") |
|
349 |
|
350 def test_redirect_on_non_redirect_page(self): |
|
351 "An assertion is raised if the original page couldn't be retrieved as expected" |
|
352 # This page will redirect with code 301, not 302 |
|
353 response = self.client.get('/test_client/get_view/') |
|
354 try: |
|
355 self.assertRedirects(response, '/test_client/get_view/') |
|
356 except AssertionError, e: |
|
357 self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)") |
|
358 |
|
359 try: |
|
360 self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc') |
|
361 except AssertionError, e: |
|
362 self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 200 (expected 302)") |
|
363 |
|
364 |
|
365 class AssertFormErrorTests(TestCase): |
|
366 def test_unknown_form(self): |
|
367 "An assertion is raised if the form name is unknown" |
|
368 post_data = { |
|
369 'text': 'Hello World', |
|
370 'email': 'not an email address', |
|
371 'value': 37, |
|
372 'single': 'b', |
|
373 'multi': ('b','c','e') |
|
374 } |
|
375 response = self.client.post('/test_client/form_view/', post_data) |
|
376 self.assertEqual(response.status_code, 200) |
|
377 self.assertTemplateUsed(response, "Invalid POST Template") |
|
378 |
|
379 try: |
|
380 self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.') |
|
381 except AssertionError, e: |
|
382 self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response") |
|
383 try: |
|
384 self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.', msg_prefix='abc') |
|
385 except AssertionError, e: |
|
386 self.assertEqual(str(e), "abc: The form 'wrong_form' was not used to render the response") |
|
387 |
|
388 def test_unknown_field(self): |
|
389 "An assertion is raised if the field name is unknown" |
|
390 post_data = { |
|
391 'text': 'Hello World', |
|
392 'email': 'not an email address', |
|
393 'value': 37, |
|
394 'single': 'b', |
|
395 'multi': ('b','c','e') |
|
396 } |
|
397 response = self.client.post('/test_client/form_view/', post_data) |
|
398 self.assertEqual(response.status_code, 200) |
|
399 self.assertTemplateUsed(response, "Invalid POST Template") |
|
400 |
|
401 try: |
|
402 self.assertFormError(response, 'form', 'some_field', 'Some error.') |
|
403 except AssertionError, e: |
|
404 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'") |
|
405 try: |
|
406 self.assertFormError(response, 'form', 'some_field', 'Some error.', msg_prefix='abc') |
|
407 except AssertionError, e: |
|
408 self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the field 'some_field'") |
|
409 |
|
410 def test_noerror_field(self): |
|
411 "An assertion is raised if the field doesn't have any errors" |
|
412 post_data = { |
|
413 'text': 'Hello World', |
|
414 'email': 'not an email address', |
|
415 'value': 37, |
|
416 'single': 'b', |
|
417 'multi': ('b','c','e') |
|
418 } |
|
419 response = self.client.post('/test_client/form_view/', post_data) |
|
420 self.assertEqual(response.status_code, 200) |
|
421 self.assertTemplateUsed(response, "Invalid POST Template") |
|
422 |
|
423 try: |
|
424 self.assertFormError(response, 'form', 'value', 'Some error.') |
|
425 except AssertionError, e: |
|
426 self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors") |
|
427 try: |
|
428 self.assertFormError(response, 'form', 'value', 'Some error.', msg_prefix='abc') |
|
429 except AssertionError, e: |
|
430 self.assertEqual(str(e), "abc: The field 'value' on form 'form' in context 0 contains no errors") |
|
431 |
|
432 def test_unknown_error(self): |
|
433 "An assertion is raised if the field doesn't contain the provided error" |
|
434 post_data = { |
|
435 'text': 'Hello World', |
|
436 'email': 'not an email address', |
|
437 'value': 37, |
|
438 'single': 'b', |
|
439 'multi': ('b','c','e') |
|
440 } |
|
441 response = self.client.post('/test_client/form_view/', post_data) |
|
442 self.assertEqual(response.status_code, 200) |
|
443 self.assertTemplateUsed(response, "Invalid POST Template") |
|
444 |
|
445 try: |
|
446 self.assertFormError(response, 'form', 'email', 'Some error.') |
|
447 except AssertionError, e: |
|
448 self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])") |
|
449 try: |
|
450 self.assertFormError(response, 'form', 'email', 'Some error.', msg_prefix='abc') |
|
451 except AssertionError, e: |
|
452 self.assertEqual(str(e), "abc: The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])") |
|
453 |
|
454 def test_unknown_nonfield_error(self): |
|
455 """ |
|
456 Checks that an assertion is raised if the form's non field errors |
|
457 doesn't contain the provided error. |
|
458 """ |
|
459 post_data = { |
|
460 'text': 'Hello World', |
|
461 'email': 'not an email address', |
|
462 'value': 37, |
|
463 'single': 'b', |
|
464 'multi': ('b','c','e') |
|
465 } |
|
466 response = self.client.post('/test_client/form_view/', post_data) |
|
467 self.assertEqual(response.status_code, 200) |
|
468 self.assertTemplateUsed(response, "Invalid POST Template") |
|
469 |
|
470 try: |
|
471 self.assertFormError(response, 'form', None, 'Some error.') |
|
472 except AssertionError, e: |
|
473 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )") |
|
474 try: |
|
475 self.assertFormError(response, 'form', None, 'Some error.', msg_prefix='abc') |
|
476 except AssertionError, e: |
|
477 self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )") |
|
478 |
|
479 class LoginTests(TestCase): |
|
480 fixtures = ['testdata'] |
|
481 |
|
482 def test_login_different_client(self): |
|
483 "Check that using a different test client doesn't violate authentication" |
|
484 |
|
485 # Create a second client, and log in. |
|
486 c = Client() |
|
487 login = c.login(username='testclient', password='password') |
|
488 self.assertTrue(login, 'Could not log in') |
|
489 |
|
490 # Get a redirection page with the second client. |
|
491 response = c.get("/test_client_regress/login_protected_redirect_view/") |
|
492 |
|
493 # At this points, the self.client isn't logged in. |
|
494 # Check that assertRedirects uses the original client, not the |
|
495 # default client. |
|
496 self.assertRedirects(response, "http://testserver/test_client_regress/get_view/") |
|
497 |
|
498 |
|
499 class SessionEngineTests(TestCase): |
|
500 fixtures = ['testdata'] |
|
501 |
|
502 def setUp(self): |
|
503 self.old_SESSION_ENGINE = settings.SESSION_ENGINE |
|
504 settings.SESSION_ENGINE = 'regressiontests.test_client_regress.session' |
|
505 |
|
506 def tearDown(self): |
|
507 settings.SESSION_ENGINE = self.old_SESSION_ENGINE |
|
508 |
|
509 def test_login(self): |
|
510 "A session engine that modifies the session key can be used to log in" |
|
511 login = self.client.login(username='testclient', password='password') |
|
512 self.assertTrue(login, 'Could not log in') |
|
513 |
|
514 # Try to access a login protected page. |
|
515 response = self.client.get("/test_client/login_protected_view/") |
|
516 self.assertEqual(response.status_code, 200) |
|
517 self.assertEqual(response.context['user'].username, 'testclient') |
|
518 |
|
519 class URLEscapingTests(TestCase): |
|
520 def test_simple_argument_get(self): |
|
521 "Get a view that has a simple string argument" |
|
522 response = self.client.get(reverse('arg_view', args=['Slartibartfast'])) |
|
523 self.assertEqual(response.status_code, 200) |
|
524 self.assertEqual(response.content, 'Howdy, Slartibartfast') |
|
525 |
|
526 def test_argument_with_space_get(self): |
|
527 "Get a view that has a string argument that requires escaping" |
|
528 response = self.client.get(reverse('arg_view', args=['Arthur Dent'])) |
|
529 self.assertEqual(response.status_code, 200) |
|
530 self.assertEqual(response.content, 'Hi, Arthur') |
|
531 |
|
532 def test_simple_argument_post(self): |
|
533 "Post for a view that has a simple string argument" |
|
534 response = self.client.post(reverse('arg_view', args=['Slartibartfast'])) |
|
535 self.assertEqual(response.status_code, 200) |
|
536 self.assertEqual(response.content, 'Howdy, Slartibartfast') |
|
537 |
|
538 def test_argument_with_space_post(self): |
|
539 "Post for a view that has a string argument that requires escaping" |
|
540 response = self.client.post(reverse('arg_view', args=['Arthur Dent'])) |
|
541 self.assertEqual(response.status_code, 200) |
|
542 self.assertEqual(response.content, 'Hi, Arthur') |
|
543 |
|
544 class ExceptionTests(TestCase): |
|
545 fixtures = ['testdata.json'] |
|
546 |
|
547 def test_exception_cleared(self): |
|
548 "#5836 - A stale user exception isn't re-raised by the test client." |
|
549 |
|
550 login = self.client.login(username='testclient',password='password') |
|
551 self.assertTrue(login, 'Could not log in') |
|
552 try: |
|
553 response = self.client.get("/test_client_regress/staff_only/") |
|
554 self.fail("General users should not be able to visit this page") |
|
555 except SuspiciousOperation: |
|
556 pass |
|
557 |
|
558 # At this point, an exception has been raised, and should be cleared. |
|
559 |
|
560 # This next operation should be successful; if it isn't we have a problem. |
|
561 login = self.client.login(username='staff', password='password') |
|
562 self.assertTrue(login, 'Could not log in') |
|
563 try: |
|
564 self.client.get("/test_client_regress/staff_only/") |
|
565 except SuspiciousOperation: |
|
566 self.fail("Staff should be able to visit this page") |
|
567 |
|
568 class TemplateExceptionTests(TestCase): |
|
569 def setUp(self): |
|
570 # Reset the loaders so they don't try to render cached templates. |
|
571 if loader.template_source_loaders is not None: |
|
572 for template_loader in loader.template_source_loaders: |
|
573 if hasattr(template_loader, 'reset'): |
|
574 template_loader.reset() |
|
575 self.old_templates = settings.TEMPLATE_DIRS |
|
576 settings.TEMPLATE_DIRS = () |
|
577 |
|
578 def tearDown(self): |
|
579 settings.TEMPLATE_DIRS = self.old_templates |
|
580 |
|
581 def test_no_404_template(self): |
|
582 "Missing templates are correctly reported by test client" |
|
583 try: |
|
584 response = self.client.get("/no_such_view/") |
|
585 self.fail("Should get error about missing template") |
|
586 except TemplateDoesNotExist: |
|
587 pass |
|
588 |
|
589 def test_bad_404_template(self): |
|
590 "Errors found when rendering 404 error templates are re-raised" |
|
591 settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'bad_templates'),) |
|
592 try: |
|
593 response = self.client.get("/no_such_view/") |
|
594 self.fail("Should get error about syntax error in template") |
|
595 except TemplateSyntaxError: |
|
596 pass |
|
597 |
|
598 # We need two different tests to check URLconf substitution - one to check |
|
599 # it was changed, and another one (without self.urls) to check it was reverted on |
|
600 # teardown. This pair of tests relies upon the alphabetical ordering of test execution. |
|
601 class UrlconfSubstitutionTests(TestCase): |
|
602 urls = 'regressiontests.test_client_regress.urls' |
|
603 |
|
604 def test_urlconf_was_changed(self): |
|
605 "TestCase can enforce a custom URLconf on a per-test basis" |
|
606 url = reverse('arg_view', args=['somename']) |
|
607 self.assertEquals(url, '/arg_view/somename/') |
|
608 |
|
609 # This test needs to run *after* UrlconfSubstitutionTests; the zz prefix in the |
|
610 # name is to ensure alphabetical ordering. |
|
611 class zzUrlconfSubstitutionTests(TestCase): |
|
612 def test_urlconf_was_reverted(self): |
|
613 "URLconf is reverted to original value after modification in a TestCase" |
|
614 url = reverse('arg_view', args=['somename']) |
|
615 self.assertEquals(url, '/test_client_regress/arg_view/somename/') |
|
616 |
|
617 class ContextTests(TestCase): |
|
618 fixtures = ['testdata'] |
|
619 |
|
620 def test_single_context(self): |
|
621 "Context variables can be retrieved from a single context" |
|
622 response = self.client.get("/test_client_regress/request_data/", data={'foo':'whiz'}) |
|
623 self.assertEqual(response.context.__class__, Context) |
|
624 self.assertTrue('get-foo' in response.context) |
|
625 self.assertEqual(response.context['get-foo'], 'whiz') |
|
626 self.assertEqual(response.context['request-foo'], 'whiz') |
|
627 self.assertEqual(response.context['data'], 'sausage') |
|
628 |
|
629 try: |
|
630 response.context['does-not-exist'] |
|
631 self.fail('Should not be able to retrieve non-existent key') |
|
632 except KeyError, e: |
|
633 self.assertEquals(e.args[0], 'does-not-exist') |
|
634 |
|
635 def test_inherited_context(self): |
|
636 "Context variables can be retrieved from a list of contexts" |
|
637 response = self.client.get("/test_client_regress/request_data_extended/", data={'foo':'whiz'}) |
|
638 self.assertEqual(response.context.__class__, ContextList) |
|
639 self.assertEqual(len(response.context), 2) |
|
640 self.assertTrue('get-foo' in response.context) |
|
641 self.assertEqual(response.context['get-foo'], 'whiz') |
|
642 self.assertEqual(response.context['request-foo'], 'whiz') |
|
643 self.assertEqual(response.context['data'], 'bacon') |
|
644 |
|
645 try: |
|
646 response.context['does-not-exist'] |
|
647 self.fail('Should not be able to retrieve non-existent key') |
|
648 except KeyError, e: |
|
649 self.assertEquals(e.args[0], 'does-not-exist') |
|
650 |
|
651 |
|
652 class SessionTests(TestCase): |
|
653 fixtures = ['testdata.json'] |
|
654 |
|
655 def test_session(self): |
|
656 "The session isn't lost if a user logs in" |
|
657 # The session doesn't exist to start. |
|
658 response = self.client.get('/test_client_regress/check_session/') |
|
659 self.assertEqual(response.status_code, 200) |
|
660 self.assertEqual(response.content, 'NO') |
|
661 |
|
662 # This request sets a session variable. |
|
663 response = self.client.get('/test_client_regress/set_session/') |
|
664 self.assertEqual(response.status_code, 200) |
|
665 self.assertEqual(response.content, 'set_session') |
|
666 |
|
667 # Check that the session has been modified |
|
668 response = self.client.get('/test_client_regress/check_session/') |
|
669 self.assertEqual(response.status_code, 200) |
|
670 self.assertEqual(response.content, 'YES') |
|
671 |
|
672 # Log in |
|
673 login = self.client.login(username='testclient',password='password') |
|
674 self.assertTrue(login, 'Could not log in') |
|
675 |
|
676 # Session should still contain the modified value |
|
677 response = self.client.get('/test_client_regress/check_session/') |
|
678 self.assertEqual(response.status_code, 200) |
|
679 self.assertEqual(response.content, 'YES') |
|
680 |
|
681 def test_logout(self): |
|
682 """Logout should work whether the user is logged in or not (#9978).""" |
|
683 self.client.logout() |
|
684 login = self.client.login(username='testclient',password='password') |
|
685 self.assertTrue(login, 'Could not log in') |
|
686 self.client.logout() |
|
687 self.client.logout() |
|
688 |
|
689 class RequestMethodTests(TestCase): |
|
690 def test_get(self): |
|
691 "Request a view via request method GET" |
|
692 response = self.client.get('/test_client_regress/request_methods/') |
|
693 self.assertEqual(response.status_code, 200) |
|
694 self.assertEqual(response.content, 'request method: GET') |
|
695 |
|
696 def test_post(self): |
|
697 "Request a view via request method POST" |
|
698 response = self.client.post('/test_client_regress/request_methods/') |
|
699 self.assertEqual(response.status_code, 200) |
|
700 self.assertEqual(response.content, 'request method: POST') |
|
701 |
|
702 def test_head(self): |
|
703 "Request a view via request method HEAD" |
|
704 response = self.client.head('/test_client_regress/request_methods/') |
|
705 self.assertEqual(response.status_code, 200) |
|
706 # A HEAD request doesn't return any content. |
|
707 self.assertNotEqual(response.content, 'request method: HEAD') |
|
708 self.assertEqual(response.content, '') |
|
709 |
|
710 def test_options(self): |
|
711 "Request a view via request method OPTIONS" |
|
712 response = self.client.options('/test_client_regress/request_methods/') |
|
713 self.assertEqual(response.status_code, 200) |
|
714 self.assertEqual(response.content, 'request method: OPTIONS') |
|
715 |
|
716 def test_put(self): |
|
717 "Request a view via request method PUT" |
|
718 response = self.client.put('/test_client_regress/request_methods/') |
|
719 self.assertEqual(response.status_code, 200) |
|
720 self.assertEqual(response.content, 'request method: PUT') |
|
721 |
|
722 def test_delete(self): |
|
723 "Request a view via request method DELETE" |
|
724 response = self.client.delete('/test_client_regress/request_methods/') |
|
725 self.assertEqual(response.status_code, 200) |
|
726 self.assertEqual(response.content, 'request method: DELETE') |
|
727 |
|
728 class RequestMethodStringDataTests(TestCase): |
|
729 def test_post(self): |
|
730 "Request a view with string data via request method POST" |
|
731 # Regression test for #11371 |
|
732 data = u'{"test": "json"}' |
|
733 response = self.client.post('/test_client_regress/request_methods/', data=data, content_type='application/json') |
|
734 self.assertEqual(response.status_code, 200) |
|
735 self.assertEqual(response.content, 'request method: POST') |
|
736 |
|
737 def test_put(self): |
|
738 "Request a view with string data via request method PUT" |
|
739 # Regression test for #11371 |
|
740 data = u'{"test": "json"}' |
|
741 response = self.client.put('/test_client_regress/request_methods/', data=data, content_type='application/json') |
|
742 self.assertEqual(response.status_code, 200) |
|
743 self.assertEqual(response.content, 'request method: PUT') |
|
744 |
|
745 class QueryStringTests(TestCase): |
|
746 def test_get_like_requests(self): |
|
747 for method_name in ('get','head','options','put','delete'): |
|
748 # A GET-like request can pass a query string as data |
|
749 method = getattr(self.client, method_name) |
|
750 response = method("/test_client_regress/request_data/", data={'foo':'whiz'}) |
|
751 self.assertEqual(response.context['get-foo'], 'whiz') |
|
752 self.assertEqual(response.context['request-foo'], 'whiz') |
|
753 |
|
754 # A GET-like request can pass a query string as part of the URL |
|
755 response = method("/test_client_regress/request_data/?foo=whiz") |
|
756 self.assertEqual(response.context['get-foo'], 'whiz') |
|
757 self.assertEqual(response.context['request-foo'], 'whiz') |
|
758 |
|
759 # Data provided in the URL to a GET-like request is overridden by actual form data |
|
760 response = method("/test_client_regress/request_data/?foo=whiz", data={'foo':'bang'}) |
|
761 self.assertEqual(response.context['get-foo'], 'bang') |
|
762 self.assertEqual(response.context['request-foo'], 'bang') |
|
763 |
|
764 response = method("/test_client_regress/request_data/?foo=whiz", data={'bar':'bang'}) |
|
765 self.assertEqual(response.context['get-foo'], None) |
|
766 self.assertEqual(response.context['get-bar'], 'bang') |
|
767 self.assertEqual(response.context['request-foo'], None) |
|
768 self.assertEqual(response.context['request-bar'], 'bang') |
|
769 |
|
770 def test_post_like_requests(self): |
|
771 # A POST-like request can pass a query string as data |
|
772 response = self.client.post("/test_client_regress/request_data/", data={'foo':'whiz'}) |
|
773 self.assertEqual(response.context['get-foo'], None) |
|
774 self.assertEqual(response.context['post-foo'], 'whiz') |
|
775 |
|
776 # A POST-like request can pass a query string as part of the URL |
|
777 response = self.client.post("/test_client_regress/request_data/?foo=whiz") |
|
778 self.assertEqual(response.context['get-foo'], 'whiz') |
|
779 self.assertEqual(response.context['post-foo'], None) |
|
780 self.assertEqual(response.context['request-foo'], 'whiz') |
|
781 |
|
782 # POST data provided in the URL augments actual form data |
|
783 response = self.client.post("/test_client_regress/request_data/?foo=whiz", data={'foo':'bang'}) |
|
784 self.assertEqual(response.context['get-foo'], 'whiz') |
|
785 self.assertEqual(response.context['post-foo'], 'bang') |
|
786 self.assertEqual(response.context['request-foo'], 'bang') |
|
787 |
|
788 response = self.client.post("/test_client_regress/request_data/?foo=whiz", data={'bar':'bang'}) |
|
789 self.assertEqual(response.context['get-foo'], 'whiz') |
|
790 self.assertEqual(response.context['get-bar'], None) |
|
791 self.assertEqual(response.context['post-foo'], None) |
|
792 self.assertEqual(response.context['post-bar'], 'bang') |
|
793 self.assertEqual(response.context['request-foo'], 'whiz') |
|
794 self.assertEqual(response.context['request-bar'], 'bang') |
|
795 |
|
796 class UnicodePayloadTests(TestCase): |
|
797 def test_simple_unicode_payload(self): |
|
798 "A simple ASCII-only unicode JSON document can be POSTed" |
|
799 # Regression test for #10571 |
|
800 json = u'{"english": "mountain pass"}' |
|
801 response = self.client.post("/test_client_regress/parse_unicode_json/", json, |
|
802 content_type="application/json") |
|
803 self.assertEqual(response.content, json) |
|
804 |
|
805 def test_unicode_payload_utf8(self): |
|
806 "A non-ASCII unicode data encoded as UTF-8 can be POSTed" |
|
807 # Regression test for #10571 |
|
808 json = u'{"dog": "собака"}' |
|
809 response = self.client.post("/test_client_regress/parse_unicode_json/", json, |
|
810 content_type="application/json; charset=utf-8") |
|
811 self.assertEqual(response.content, json.encode('utf-8')) |
|
812 |
|
813 def test_unicode_payload_utf16(self): |
|
814 "A non-ASCII unicode data encoded as UTF-16 can be POSTed" |
|
815 # Regression test for #10571 |
|
816 json = u'{"dog": "собака"}' |
|
817 response = self.client.post("/test_client_regress/parse_unicode_json/", json, |
|
818 content_type="application/json; charset=utf-16") |
|
819 self.assertEqual(response.content, json.encode('utf-16')) |
|
820 |
|
821 def test_unicode_payload_non_utf(self): |
|
822 "A non-ASCII unicode data as a non-UTF based encoding can be POSTed" |
|
823 #Regression test for #10571 |
|
824 json = u'{"dog": "собака"}' |
|
825 response = self.client.post("/test_client_regress/parse_unicode_json/", json, |
|
826 content_type="application/json; charset=koi8-r") |
|
827 self.assertEqual(response.content, json.encode('koi8-r')) |
|
828 |
|
829 class DummyFile(object): |
|
830 def __init__(self, filename): |
|
831 self.name = filename |
|
832 def read(self): |
|
833 return 'TEST_FILE_CONTENT' |
|
834 |
|
835 class UploadedFileEncodingTest(TestCase): |
|
836 def test_file_encoding(self): |
|
837 encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin')) |
|
838 self.assertEqual('--TEST_BOUNDARY', encoded_file[0]) |
|
839 self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1]) |
|
840 self.assertEqual('TEST_FILE_CONTENT', encoded_file[-1]) |
|
841 |
|
842 def test_guesses_content_type_on_file_encoding(self): |
|
843 self.assertEqual('Content-Type: application/octet-stream', |
|
844 encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2]) |
|
845 self.assertEqual('Content-Type: text/plain', |
|
846 encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2]) |
|
847 self.assertEqual('Content-Type: application/zip', |
|
848 encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2]) |
|
849 self.assertEqual('Content-Type: application/octet-stream', |
|
850 encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2]) |
|
851 |
|
852 class RequestHeadersTest(TestCase): |
|
853 def test_client_headers(self): |
|
854 "A test client can receive custom headers" |
|
855 response = self.client.get("/test_client_regress/check_headers/", HTTP_X_ARG_CHECK='Testing 123') |
|
856 self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123") |
|
857 self.assertEquals(response.status_code, 200) |
|
858 |
|
859 def test_client_headers_redirect(self): |
|
860 "Test client headers are preserved through redirects" |
|
861 response = self.client.get("/test_client_regress/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123') |
|
862 self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123") |
|
863 self.assertRedirects(response, '/test_client_regress/check_headers/', |
|
864 status_code=301, target_status_code=200) |