tests.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import absolute_import, unicode_literals
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.test import TestCase
  4. from .models import Author, Article
  5. class ContentTypesViewsTests(TestCase):
  6. fixtures = ['testdata.json']
  7. urls = 'contenttypes_tests.urls'
  8. def test_shortcut_with_absolute_url(self):
  9. "Can view a shortcut for an Author object that has a get_absolute_url method"
  10. for obj in Author.objects.all():
  11. short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk)
  12. response = self.client.get(short_url)
  13. self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(),
  14. status_code=302, target_status_code=404)
  15. def test_shortcut_no_absolute_url(self):
  16. "Shortcuts for an object that has no get_absolute_url method raises 404"
  17. for obj in Article.objects.all():
  18. short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, obj.pk)
  19. response = self.client.get(short_url)
  20. self.assertEqual(response.status_code, 404)
  21. def test_wrong_type_pk(self):
  22. short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, 'nobody/expects')
  23. response = self.client.get(short_url)
  24. self.assertEqual(response.status_code, 404)
  25. def test_shortcut_bad_pk(self):
  26. short_url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, '42424242')
  27. response = self.client.get(short_url)
  28. self.assertEqual(response.status_code, 404)
  29. def test_nonint_content_type(self):
  30. an_author = Author.objects.all()[0]
  31. short_url = '/shortcut/%s/%s/' % ('spam', an_author.pk)
  32. response = self.client.get(short_url)
  33. self.assertEqual(response.status_code, 404)
  34. def test_bad_content_type(self):
  35. an_author = Author.objects.all()[0]
  36. short_url = '/shortcut/%s/%s/' % (42424242, an_author.pk)
  37. response = self.client.get(short_url)
  38. self.assertEqual(response.status_code, 404)