PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/test_client_regress/models.py

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