PageRenderTime 17ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/views/tests/defaults.py

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