Changeset 6661
- Timestamp:
- 11/10/07 21:54:21 (10 months ago)
- Files:
-
- django/trunk/django/test/testcases.py (modified) (3 diffs)
- django/trunk/tests/modeltests/test_client/models.py (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/testcases.py
r6625 r6661 1 1 import re 2 2 import unittest 3 from urlparse import urlsplit 3 from urlparse import urlsplit, urlunsplit 4 4 5 5 from django.http import QueryDict … … 75 75 76 76 def assertRedirects(self, response, expected_url, status_code=302, 77 target_status_code=200 ):77 target_status_code=200, host=None): 78 78 """Asserts that a response redirected to a specific URL, and that the 79 79 redirect URL can be loaded. … … 87 87 url = response['Location'] 88 88 scheme, netloc, path, query, fragment = urlsplit(url) 89 e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url) 90 if not (e_scheme or e_netloc): 91 expected_url = urlunsplit(('http', host or 'testserver', e_path, 92 e_query, e_fragment)) 89 93 self.assertEqual(url, expected_url, 90 94 "Response redirected to '%s', expected '%s'" % (url, expected_url)) django/trunk/tests/modeltests/test_client/models.py
r6658 r6661 5 5 The test client is a class that can act like a simple 6 6 browser for testing purposes. 7 7 8 8 It allows the user to compose GET and POST requests, and 9 9 obtain the response that the server gave to those requests. … … 16 16 17 17 This is not intended as a replacement for Twill,Selenium, or 18 other browser automation frameworks - it is here to allow 19 testing against the contexts and templates produced by a view, 18 other browser automation frameworks - it is here to allow 19 testing against the contexts and templates produced by a view, 20 20 rather than the HTML rendered to the end-user. 21 21 … … 26 26 class ClientTest(TestCase): 27 27 fixtures = ['testdata.json'] 28 28 29 29 def test_get_view(self): 30 30 "GET a view" … … 33 33 data = {'var': u'\xf2'} 34 34 response = self.client.get('/test_client/get_view/', data) 35 35 36 36 # Check some response details 37 37 self.assertContains(response, 'This is a test') … … 42 42 "GET a view that normally expects POSTs" 43 43 response = self.client.get('/test_client/post_view/', {}) 44 44 45 45 # Check some response details 46 46 self.assertEqual(response.status_code, 200) … … 48 48 self.assertTemplateUsed(response, 'Empty GET Template') 49 49 self.assertTemplateNotUsed(response, 'Empty POST Template') 50 50 51 51 def test_empty_post(self): 52 52 "POST an empty dictionary to a view" 53 53 response = self.client.post('/test_client/post_view/', {}) 54 54 55 55 # Check some response details 56 56 self.assertEqual(response.status_code, 200) … … 58 58 self.assertTemplateNotUsed(response, 'Empty GET Template') 59 59 self.assertTemplateUsed(response, 'Empty POST Template') 60 60 61 61 def test_post(self): 62 62 "POST some data to a view" … … 65 65 } 66 66 response = self.client.post('/test_client/post_view/', post_data) 67 67 68 68 # Check some response details 69 69 self.assertEqual(response.status_code, 200) … … 71 71 self.assertEqual(response.template.name, 'POST Template') 72 72 self.failUnless('Data received' in response.content) 73 73 74 74 def test_raw_post(self): 75 75 "POST raw data (with a content type) to a view" … … 84 84 "GET a URL that redirects elsewhere" 85 85 response = self.client.get('/test_client/redirect_view/') 86 # Check that the response was a 302 (redirect) 87 self.assertRedirects(response, 'http://testserver/test_client/get_view/') 88 89 client_providing_host = Client(HTTP_HOST='django.testserver') 86 # Check that the response was a 302 (redirect) and that 87 # assertRedirect() understands to put an implicit http://testserver/ in 88 # front of non-absolute URLs. 89 self.assertRedirects(response, '/test_client/get_view/') 90 91 host = 'django.testserver' 92 client_providing_host = Client(HTTP_HOST=host) 90 93 response = client_providing_host.get('/test_client/redirect_view/') 91 94 # Check that the response was a 302 (redirect) with absolute URI 92 self.assertRedirects(response, ' http://django.testserver/test_client/get_view/')93 95 self.assertRedirects(response, '/test_client/get_view/', host=host) 96 94 97 def test_redirect_with_query(self): 95 98 "GET a URL that redirects with given GET parameters" 96 99 response = self.client.get('/test_client/redirect_view/', {'var': 'value'}) 97 100 98 101 # Check if parameters are intact 99 102 self.assertRedirects(response, 'http://testserver/test_client/get_view/?var=value') … … 113 116 "GET a URL that redirects to a non-200 page" 114 117 response = self.client.get('/test_client/double_redirect_view/') 115 118 116 119 # Check that the response was a 302, and that 117 120 # the attempt to get the redirection location returned 301 when retrieved … … 121 124 "GET a URL that responds as '404:Not Found'" 122 125 response = self.client.get('/test_client/bad_view/') 123 126 124 127 # Check that the response was a 404, and that the content contains MAGIC 125 128 self.assertContains(response, 'MAGIC', status_code=404) … … 149 152 # Check that the multi-value data has been rolled out ok 150 153 self.assertContains(response, 'Select a valid choice.', 0) 151 154 152 155 def test_incomplete_data_form(self): 153 156 "POST incomplete data to a form" 154 157 post_data = { 155 158 'text': 'Hello World', 156 'value': 37 159 'value': 37 157 160 } 158 161 response = self.client.post('/test_client/form_view/', post_data) … … 199 202 post_data = { 200 203 'text': 'Hello World', 201 'value': 37 204 'value': 37 202 205 } 203 206 response = self.client.post('/test_client/form_view_with_template/', post_data) … … 227 230 228 231 self.assertFormError(response, 'form', 'email', 'Enter a valid e-mail address.') 229 232 230 233 def test_unknown_page(self): 231 234 "GET an invalid URL" 232 235 response = self.client.get('/test_client/unknown_view/') 233 236 234 237 # Check that the response was a 404 235 238 self.assertEqual(response.status_code, 404) 236 239 237 240 def test_view_with_login(self): 238 241 "Request a page that is protected with @login_required" 239 242 240 243 # Get the page without logging in. Should result in 302. 241 244 response = self.client.get('/test_client/login_protected_view/') 242 245 self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/') 243 246 244 247 # Log in 245 248 login = self.client.login(username='testclient', password='password') … … 253 256 def test_view_with_method_login(self): 254 257 "Request a page that is protected with a @login_required method" 255 258 256 259 # Get the page without logging in. Should result in 302. 257 260 response = self.client.get('/test_client/login_protected_method_view/') 258 261 self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_method_view/') 259 262 260 263 # Log in 261 264 login = self.client.login(username='testclient', password='password') … … 269 272 def test_view_with_login_and_custom_redirect(self): 270 273 "Request a page that is protected with @login_required(redirect_field_name='redirect_to')" 271 274 272 275 # Get the page without logging in. Should result in 302. 273 276 response = self.client.get('/test_client/login_protected_view_custom_redirect/') … … 314 317 def test_view_with_permissions(self): 315 318 "Request a page that is protected with @permission_required" 316 319 317 320 # Get the page without logging in. Should result in 302. 318 321 response = self.client.get('/test_client/permission_protected_view/') 319 322 self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/') 320 323 321 324 # Log in 322 325 login = self.client.login(username='testclient', password='password') … … 331 334 def test_view_with_method_permissions(self): 332 335 "Request a page that is protected with a @permission_required method" 333 336 334 337 # Get the page without logging in. Should result in 302. 335 338 response = self.client.get('/test_client/permission_protected_method_view/') 336 339 self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_method_view/') 337 340 338 341 # Log in 339 342 login = self.client.login(username='testclient', password='password') … … 354 357 except KeyError: 355 358 pass 356 359 357 360 from django.contrib.sessions.models import Session 358 361 response = self.client.post('/test_client/session_view/') 359 362 360 363 # Check that the session was modified 361 364 self.assertEquals(self.client.session['tobacconist'], 'hovercraft') … … 364 367 "Request a page that is known to throw an error" 365 368 self.assertRaises(KeyError, self.client.get, "/test_client/broken_view/") 366 369 367 370 #Try the same assertion, a different way 368 371 try: … … 371 374 except KeyError: 372 375 pass 373 376 374 377 def test_mail_sending(self): 375 378 "Test that mail is redirected to a dummy outbox during test setup" 376 379 377 380 response = self.client.get('/test_client/mail_sending_view/') 378 381 self.assertEqual(response.status_code, 200) 379 382 380 383 self.assertEqual(len(mail.outbox), 1) 381 384 self.assertEqual(mail.outbox[0].subject, 'Test message') 382 385 self.assertEqual(mail.outbox[0].body, 'This is a test email') 383 self.assertEqual(mail.outbox[0].from_email, 'from@example.com') 386 self.assertEqual(mail.outbox[0].from_email, 'from@example.com') 384 387 self.assertEqual(mail.outbox[0].to[0], 'first@example.com') 385 388 self.assertEqual(mail.outbox[0].to[1], 'second@example.com') … … 387 390 def test_mass_mail_sending(self): 388 391 "Test that mass mail is redirected to a dummy outbox during test setup" 389 392 390 393 response = self.client.get('/test_client/mass_mail_sending_view/') 391 394 self.assertEqual(response.status_code, 200) 392 395 393 396 self.assertEqual(len(mail.outbox), 2) 394 397 self.assertEqual(mail.outbox[0].subject, 'First Test message') 395 398 self.assertEqual(mail.outbox[0].body, 'This is the first test email') 396 self.assertEqual(mail.outbox[0].from_email, 'from@example.com') 399 self.assertEqual(mail.outbox[0].from_email, 'from@example.com') 397 400 self.assertEqual(mail.outbox[0].to[0], 'first@example.com') 398 401 self.assertEqual(mail.outbox[0].to[1], 'second@example.com') … … 400 403 self.assertEqual(mail.outbox[1].subject, 'Second Test message') 401 404 self.assertEqual(mail.outbox[1].body, 'This is the second test email') 402 self.assertEqual(mail.outbox[1].from_email, 'from@example.com') 405 self.assertEqual(mail.outbox[1].from_email, 'from@example.com') 403 406 self.assertEqual(mail.outbox[1].to[0], 'second@example.com') 404 407 self.assertEqual(mail.outbox[1].to[1], 'third@example.com') 405 408
