/envelope/tests/views.py

https://github.com/emperorcezar/django-envelope · Python · 127 lines · 108 code · 18 blank · 1 comment · 1 complexity · b0eae63ea7ee3a93db587804d8f0d327 MD5 · raw file

  1. u"""
  2. Unit tests for ``django-envelope`` views.
  3. """
  4. import warnings
  5. from django.conf import settings
  6. from django.contrib.auth.models import User
  7. from django.core.urlresolvers import reverse
  8. from django.test import TestCase
  9. from django.utils.translation import ugettext_lazy as _
  10. class BaseContactViewTestCase(TestCase):
  11. u"""
  12. Base unit tests for contact form view.
  13. These test cases are common to both class-based and function-based views.
  14. Only the specific subclasses are imported in tests/__init__.py in order
  15. not to duplicate tests.
  16. """
  17. url = reverse('envelope-contact')
  18. def setUp(self):
  19. self.honeypot = getattr(settings, 'HONEYPOT_FIELD_NAME', 'email2')
  20. self.form_data = {
  21. 'sender': 'zbyszek',
  22. 'email': 'test@example.com',
  23. 'category': 10,
  24. 'subject': 'A subject',
  25. 'message': 'Hello there!',
  26. self.honeypot: '',
  27. }
  28. def test_response_data(self):
  29. u"""
  30. A GET request displays the contact form.
  31. """
  32. response = self.client.get(self.url)
  33. self.assertEqual(response.status_code, 200)
  34. self.assertTemplateUsed(response, "envelope/contact.html")
  35. form = response.context['form']
  36. self.assertFalse(form.is_bound)
  37. def test_prefilled_form(self):
  38. u"""
  39. When an authenticated user hits the form view, his email is
  40. automatically filled in the email field.
  41. """
  42. User.objects.create_user('test', 'test@example.org', 'password')
  43. logged_in = self.client.login(username='test', password='password')
  44. self.assertTrue(logged_in)
  45. response = self.client.get(self.url)
  46. self.assertContains(response, 'value="test@example.org"')
  47. def test_honeypot(self):
  48. u"""
  49. If the honeypot field is not empty, keep the spammer off the page.
  50. """
  51. response = self.client.post(self.url, {self.honeypot: 'some value'})
  52. self.assertEqual(response.status_code, 400)
  53. response = self.client.post(self.url, {self.honeypot: ''})
  54. self.assertEqual(response.status_code, 200)
  55. def test_form_successful(self):
  56. u"""
  57. If the data is correct, a message is sent and the user is redirected.
  58. """
  59. response = self.client.post(self.url, self.form_data, follow=True)
  60. self.assertRedirects(response, self.url)
  61. self.assertEquals(len(response.redirect_chain), 1)
  62. flash_error_message = _("There was en error in the contact form.")
  63. self.assertNotContains(response, flash_error_message)
  64. flash_success_message = _("Thank you for your message.")
  65. self.assertContains(response, flash_success_message)
  66. class ClassContactViewTestCase(BaseContactViewTestCase):
  67. u"""
  68. Unit tests for class-based contact form view (the default one).
  69. """
  70. url = reverse('class_contact')
  71. customized_url = reverse('customized_class_contact')
  72. def test_custom_template(self):
  73. u"""
  74. You can change the default template used to render the form.
  75. """
  76. response = self.client.get(self.customized_url)
  77. self.assertTemplateUsed(response, "contact.html")
  78. def test_custom_success_url(self):
  79. u"""
  80. The view redirects to a custom success_url when the form is valid.
  81. """
  82. response = self.client.post(self.customized_url, self.form_data)
  83. self.assertRedirects(response, self.customized_url)
  84. class FunctionContactViewTestCase(BaseContactViewTestCase):
  85. u"""
  86. Unit tests for old-style contact form view (function-based, deprecated).
  87. """
  88. url = reverse('func_contact')
  89. customized_url = reverse('customized_func_contact')
  90. def test_deprecation(self):
  91. u"""
  92. Function-based view is deprecated since 0.3.0.
  93. """
  94. with warnings.catch_warnings(record=True) as warns:
  95. warnings.filterwarnings("always",
  96. category=PendingDeprecationWarning)
  97. self.client.get(self.url)
  98. self.assertEqual(len(warns), 1)
  99. def test_extra_context(self):
  100. u"""
  101. Custom context variables can be supplied to the view.
  102. """
  103. response = self.client.get(self.customized_url)
  104. self.assertIn('foo', response.context)
  105. self.assertEqual(response.context['foo'], 'bar')
  106. # evaluate callables
  107. self.assertIn('spam', response.context)
  108. self.assertEqual(response.context['spam'], 'eggs')