/apps/user_registration/tests/views.py

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