/src/libs/registration/tests/views.py

https://github.com/kawazrepos/Kawaz · Python · 245 lines · 160 code · 24 blank · 61 comment · 0 complexity · 165fb40b1d4107363278f71fee5c4f65 MD5 · raw file

  1. import datetime
  2. from django.conf import settings
  3. from django.contrib.auth.models import User
  4. from django.core import mail
  5. from django.core.urlresolvers import reverse
  6. from django.test import TestCase
  7. from registration import forms
  8. from registration.models import RegistrationProfile
  9. class RegistrationViewTests(TestCase):
  10. """
  11. Test the registration views.
  12. """
  13. urls = 'registration.tests.urls'
  14. def setUp(self):
  15. """
  16. Set ``REGISTRATION_BACKEND`` to the default backend, and store
  17. the original value to be restored later.
  18. """
  19. self.old_backend = getattr(settings, 'REGISTRATION_BACKEND', None)
  20. settings.REGISTRATION_BACKEND = 'registration.backends.default.DefaultBackend'
  21. self.old_activation = getattr(settings, 'ACCOUNT_ACTIVATION_DAYS', None)
  22. settings.ACCOUNT_ACTIVATION_DAYS = 7
  23. def tearDown(self):
  24. """
  25. Restore the original value of ``REGISTRATION_BACKEND``.
  26. """
  27. settings.REGISTRATION_BACKEND = self.old_backend
  28. settings.ACCOUNT_ACTIVATION_DAYS = self.old_activation
  29. def test_registration_view_initial(self):
  30. """
  31. A ``GET`` to the ``register`` view uses the appropriate
  32. template and populates the registration form into the context.
  33. """
  34. response = self.client.get(reverse('registration_register'))
  35. self.assertEqual(response.status_code, 200)
  36. self.assertTemplateUsed(response,
  37. 'registration/registration_form.html')
  38. self.failUnless(isinstance(response.context['form'],
  39. forms.RegistrationForm))
  40. def test_registration_view_success(self):
  41. """
  42. A ``POST`` to the ``register`` view with valid data properly
  43. creates a new user and issues a redirect.
  44. """
  45. response = self.client.post(reverse('registration_register'),
  46. data={'username': 'alice',
  47. 'email': 'alice@example.com',
  48. 'password1': 'swordfish',
  49. 'password2': 'swordfish'})
  50. self.assertRedirects(response,
  51. 'http://testserver%s' % reverse('registration_complete'))
  52. self.assertEqual(len(mail.outbox), 1)
  53. def test_registration_view_failure(self):
  54. """
  55. A ``POST`` to the ``register`` view with invalid data does not
  56. create a user, and displays appropriate error messages.
  57. """
  58. response = self.client.post(reverse('registration_register'),
  59. data={'username': 'bob',
  60. 'email': 'bobe@example.com',
  61. 'password1': 'foo',
  62. 'password2': 'bar'})
  63. self.assertEqual(response.status_code, 200)
  64. self.failIf(response.context['form'].is_valid())
  65. self.assertFormError(response, 'form', field=None,
  66. errors=u"The two password fields didn't match.")
  67. self.assertEqual(len(mail.outbox), 0)
  68. def test_registration_view_closed(self):
  69. """
  70. Any attempt to access the ``register`` view when registration
  71. is closed fails and redirects.
  72. """
  73. old_allowed = getattr(settings, 'REGISTRATION_OPEN', True)
  74. settings.REGISTRATION_OPEN = False
  75. closed_redirect = 'http://testserver%s' % reverse('registration_disallowed')
  76. response = self.client.get(reverse('registration_register'))
  77. self.assertRedirects(response, closed_redirect)
  78. # Even if valid data is posted, it still shouldn't work.
  79. response = self.client.post(reverse('registration_register'),
  80. data={'username': 'alice',
  81. 'email': 'alice@example.com',
  82. 'password1': 'swordfish',
  83. 'password2': 'swordfish'})
  84. self.assertRedirects(response, closed_redirect)
  85. self.assertEqual(RegistrationProfile.objects.count(), 0)
  86. settings.REGISTRATION_OPEN = old_allowed
  87. def test_registration_template_name(self):
  88. """
  89. Passing ``template_name`` to the ``register`` view will result
  90. in that template being used.
  91. """
  92. response = self.client.get(reverse('registration_test_register_template_name'))
  93. self.assertTemplateUsed(response,
  94. 'registration/test_template_name.html')
  95. def test_registration_extra_context(self):
  96. """
  97. Passing ``extra_context`` to the ``register`` view will
  98. correctly populate the context.
  99. """
  100. response = self.client.get(reverse('registration_test_register_extra_context'))
  101. self.assertEqual(response.context['foo'], 'bar')
  102. # Callables in extra_context are called to obtain the value.
  103. self.assertEqual(response.context['callable'], 'called')
  104. def test_registration_disallowed_url(self):
  105. """
  106. Passing ``disallowed_url`` to the ``register`` view will
  107. result in a redirect to that URL when registration is closed.
  108. """
  109. old_allowed = getattr(settings, 'REGISTRATION_OPEN', True)
  110. settings.REGISTRATION_OPEN = False
  111. closed_redirect = 'http://testserver%s' % reverse('registration_test_custom_disallowed')
  112. response = self.client.get(reverse('registration_test_register_disallowed_url'))
  113. self.assertRedirects(response, closed_redirect)
  114. settings.REGISTRATION_OPEN = old_allowed
  115. def test_registration_success_url(self):
  116. """
  117. Passing ``success_url`` to the ``register`` view will result
  118. in a redirect to that URL when registration is successful.
  119. """
  120. success_redirect = 'http://testserver%s' % reverse('registration_test_custom_success_url')
  121. response = self.client.post(reverse('registration_test_register_success_url'),
  122. data={'username': 'alice',
  123. 'email': 'alice@example.com',
  124. 'password1': 'swordfish',
  125. 'password2': 'swordfish'})
  126. self.assertRedirects(response, success_redirect)
  127. def test_valid_activation(self):
  128. """
  129. Test that the ``activate`` view properly handles a valid
  130. activation (in this case, based on the default backend's
  131. activation window).
  132. """
  133. success_redirect = 'http://testserver%s' % reverse('registration_activation_complete')
  134. # First, register an account.
  135. self.client.post(reverse('registration_register'),
  136. data={'username': 'alice',
  137. 'email': 'alice@example.com',
  138. 'password1': 'swordfish',
  139. 'password2': 'swordfish'})
  140. profile = RegistrationProfile.objects.get(user__username='alice')
  141. response = self.client.get(reverse('registration_activate',
  142. kwargs={'activation_key': profile.activation_key}))
  143. self.assertRedirects(response, success_redirect)
  144. self.failUnless(User.objects.get(username='alice').is_active)
  145. def test_invalid_activation(self):
  146. """
  147. Test that the ``activate`` view properly handles an invalid
  148. activation (in this case, based on the default backend's
  149. activation window).
  150. """
  151. # Register an account and reset its date_joined to be outside
  152. # the activation window.
  153. self.client.post(reverse('registration_register'),
  154. data={'username': 'bob',
  155. 'email': 'bob@example.com',
  156. 'password1': 'secret',
  157. 'password2': 'secret'})
  158. expired_user = User.objects.get(username='bob')
  159. expired_user.date_joined = expired_user.date_joined - datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
  160. expired_user.save()
  161. expired_profile = RegistrationProfile.objects.get(user=expired_user)
  162. response = self.client.get(reverse('registration_activate',
  163. kwargs={'activation_key': expired_profile.activation_key}))
  164. self.assertEqual(response.status_code, 200)
  165. self.assertEqual(response.context['activation_key'],
  166. expired_profile.activation_key)
  167. self.failIf(User.objects.get(username='bob').is_active)
  168. def test_activation_success_url(self):
  169. """
  170. Passing ``success_url`` to the ``activate`` view and
  171. successfully activating will result in that URL being used for
  172. the redirect.
  173. """
  174. success_redirect = 'http://testserver%s' % reverse('registration_test_custom_success_url')
  175. self.client.post(reverse('registration_register'),
  176. data={'username': 'alice',
  177. 'email': 'alice@example.com',
  178. 'password1': 'swordfish',
  179. 'password2': 'swordfish'})
  180. profile = RegistrationProfile.objects.get(user__username='alice')
  181. response = self.client.get(reverse('registration_test_activate_success_url',
  182. kwargs={'activation_key': profile.activation_key}))
  183. self.assertRedirects(response, success_redirect)
  184. def test_activation_template_name(self):
  185. """
  186. Passing ``template_name`` to the ``activate`` view will result
  187. in that template being used.
  188. """
  189. response = self.client.get(reverse('registration_test_activate_template_name',
  190. kwargs={'activation_key': 'foo'}))
  191. self.assertTemplateUsed(response, 'registration/test_template_name.html')
  192. def test_activation_extra_context(self):
  193. """
  194. Passing ``extra_context`` to the ``activate`` view will
  195. correctly populate the context.
  196. """
  197. response = self.client.get(reverse('registration_test_activate_extra_context',
  198. kwargs={'activation_key': 'foo'}))
  199. self.assertEqual(response.context['foo'], 'bar')
  200. # Callables in extra_context are called to obtain the value.
  201. self.assertEqual(response.context['callable'], 'called')