/tests/regressiontests/views/tests/defaults.py
Python | 80 lines | 64 code | 11 blank | 5 comment | 8 complexity | 09d98d92ff23931eebd89315f912480e MD5 | raw file
1from os import path 2 3from django.conf import settings 4from django.test import TestCase 5from django.contrib.contenttypes.models import ContentType 6 7from regressiontests.views.models import Author, Article, UrlArticle 8 9class DefaultsTests(TestCase): 10 """Test django views in django/views/defaults.py""" 11 fixtures = ['testdata.json'] 12 non_existing_urls = ['/views/non_existing_url/', # this is in urls.py 13 '/views/other_non_existing_url/'] # this NOT in urls.py 14 15 def test_shortcut_with_absolute_url(self): 16 "Can view a shortcut for an Author object that has a get_absolute_url method" 17 for obj in Author.objects.all(): 18 short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk) 19 response = self.client.get(short_url) 20 self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(), 21 status_code=302, target_status_code=404) 22 23 def test_shortcut_no_absolute_url(self): 24 "Shortcuts for an object that has no get_absolute_url method raises 404" 25 for obj in Article.objects.all(): 26 short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, obj.pk) 27 response = self.client.get(short_url) 28 self.assertEqual(response.status_code, 404) 29 30 def test_wrong_type_pk(self): 31 short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, 'nobody/expects') 32 response = self.client.get(short_url) 33 self.assertEqual(response.status_code, 404) 34 35 def test_shortcut_bad_pk(self): 36 short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, '42424242') 37 response = self.client.get(short_url) 38 self.assertEqual(response.status_code, 404) 39 40 def test_nonint_content_type(self): 41 an_author = Author.objects.all()[0] 42 short_url = '/views/shortcut/%s/%s/' % ('spam', an_author.pk) 43 response = self.client.get(short_url) 44 self.assertEqual(response.status_code, 404) 45 46 def test_bad_content_type(self): 47 an_author = Author.objects.all()[0] 48 short_url = '/views/shortcut/%s/%s/' % (42424242, an_author.pk) 49 response = self.client.get(short_url) 50 self.assertEqual(response.status_code, 404) 51 52 def test_page_not_found(self): 53 "A 404 status is returned by the page_not_found view" 54 for url in self.non_existing_urls: 55 response = self.client.get(url) 56 self.assertEqual(response.status_code, 404) 57 58 def test_csrf_token_in_404(self): 59 """ 60 The 404 page should have the csrf_token available in the context 61 """ 62 # See ticket #14565 63 for url in self.non_existing_urls: 64 response = self.client.get(url) 65 csrf_token = response.context['csrf_token'] 66 self.assertNotEqual(str(csrf_token), 'NOTPROVIDED') 67 self.assertNotEqual(str(csrf_token), '') 68 69 def test_server_error(self): 70 "The server_error view raises a 500 status" 71 response = self.client.get('/views/server_error/') 72 self.assertEqual(response.status_code, 500) 73 74 def test_get_absolute_url_attributes(self): 75 "A model can set attributes on the get_absolute_url method" 76 self.assertTrue(getattr(UrlArticle.get_absolute_url, 'purge', False), 77 'The attributes of the original get_absolute_url must be added.') 78 article = UrlArticle.objects.get(pk=1) 79 self.assertTrue(getattr(article.get_absolute_url, 'purge', False), 80 'The attributes of the original get_absolute_url must be added.')