tests.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import unicode_literals
  2. from django.contrib.auth import get_user_model
  3. from django.contrib.auth.tokens import default_token_generator
  4. from django.core import mail
  5. from django.core.urlresolvers import reverse
  6. from django.forms.fields import DateField, DateTimeField
  7. from django.utils.http import int_to_base36
  8. from mezzanine.accounts import ProfileNotConfigured
  9. from mezzanine.accounts.forms import ProfileForm
  10. from mezzanine.conf import settings
  11. from mezzanine.utils.tests import TestCase
  12. User = get_user_model()
  13. class AccountsTests(TestCase):
  14. def account_data(self, test_value):
  15. """
  16. Returns a dict with test data for all the user/profile fields.
  17. """
  18. # User fields
  19. data = {"email": test_value + "@example.com"}
  20. for field in ("first_name", "last_name", "username",
  21. "password1", "password2"):
  22. if field.startswith("password"):
  23. value = "x" * settings.ACCOUNTS_MIN_PASSWORD_LENGTH
  24. else:
  25. value = test_value
  26. data[field] = value
  27. # Profile fields
  28. try:
  29. profile_form = ProfileForm()
  30. ProfileFieldsForm = profile_form.get_profile_fields_form()
  31. for name, field in ProfileFieldsForm().fields.items():
  32. if name != "id":
  33. if hasattr(field, "choices"):
  34. value = list(field.choices)[0][0]
  35. elif isinstance(field, (DateField, DateTimeField)):
  36. value = "9001-04-20"
  37. else:
  38. value = test_value
  39. data[name] = value
  40. except ProfileNotConfigured:
  41. pass
  42. return data
  43. def test_account(self):
  44. """
  45. Test account creation.
  46. """
  47. # Verification not required - test an active user is created.
  48. data = self.account_data("test1")
  49. settings.ACCOUNTS_VERIFICATION_REQUIRED = False
  50. response = self.client.post(reverse("signup"), data, follow=True)
  51. self.assertEqual(response.status_code, 200)
  52. users = User.objects.filter(email=data["email"], is_active=True)
  53. self.assertEqual(len(users), 1)
  54. # Verification required - test an inactive user is created,
  55. settings.ACCOUNTS_VERIFICATION_REQUIRED = True
  56. data = self.account_data("test2")
  57. emails = len(mail.outbox)
  58. response = self.client.post(reverse("signup"), data, follow=True)
  59. self.assertEqual(response.status_code, 200)
  60. users = User.objects.filter(email=data["email"], is_active=False)
  61. self.assertEqual(len(users), 1)
  62. # Test the verification email.
  63. self.assertEqual(len(mail.outbox), emails + 1)
  64. self.assertEqual(len(mail.outbox[0].to), 1)
  65. self.assertEqual(mail.outbox[0].to[0], data["email"])
  66. # Test the verification link.
  67. new_user = users[0]
  68. verification_url = reverse("signup_verify", kwargs={
  69. "uidb36": int_to_base36(new_user.id),
  70. "token": default_token_generator.make_token(new_user),
  71. })
  72. response = self.client.get(verification_url, follow=True)
  73. self.assertEqual(response.status_code, 200)
  74. users = User.objects.filter(email=data["email"], is_active=True)
  75. self.assertEqual(len(users), 1)