/apps/userprofile/tests/test_views.py

https://github.com/ikicic/skoljka · Python · 142 lines · 109 code · 18 blank · 15 comment · 1 complexity · 32ee5a48a0049344f3c42cda34cadc57 MD5 · raw file

  1. from __future__ import print_function
  2. from django.contrib import auth
  3. from django.contrib.auth.models import Group, User
  4. from django.core import mail
  5. from django.test import TestCase
  6. from django.test.client import Client
  7. import re
  8. from userprofile.models import UserProfile
  9. class UserViewsTestCase(TestCase):
  10. assertRegex = TestCase.assertRegexpMatches
  11. def test_registration_and_login(self):
  12. def has_form_error(response):
  13. return 'class="errorlist"' in response.content
  14. # Check everything is clean at the start.
  15. self.assertEqual(User.objects.all().count(), 0)
  16. self.assertEqual(Group.objects.all().count(), 0)
  17. self.assertEqual(UserProfile.objects.all().count(), 0)
  18. self.assertEqual(len(mail.outbox), 0)
  19. c = Client()
  20. # Test the page without POST arguments.
  21. response = c.get('/accounts/register/')
  22. self.assertEqual(response.status_code, 200)
  23. # Test reject if TOU not accepted.
  24. response = c.post('/accounts/register/', {
  25. 'username': 'testaccount',
  26. 'email': 'dummy@example.com',
  27. 'password1': 'testpwd',
  28. 'password2': 'testpwd',
  29. })
  30. self.assertEqual(User.objects.all().count(), 0)
  31. self.assertTrue(has_form_error(response))
  32. # Test invalid email.
  33. response = c.post('/accounts/register/', {
  34. 'username': 'testaccount',
  35. 'email': 'this-is-not-an-email',
  36. 'password1': 'testpwd',
  37. 'password2': 'testpwd',
  38. 'tou': 'on',
  39. })
  40. self.assertEqual(User.objects.all().count(), 0)
  41. self.assertTrue(has_form_error(response))
  42. # Test mismatched passwords.
  43. response = c.post('/accounts/register/', {
  44. 'username': 'testaccount',
  45. 'email': 'test@example.com',
  46. 'password1': 'testpwd',
  47. 'password2': 'testpwdwrong',
  48. 'tou': 'on',
  49. })
  50. self.assertEqual(User.objects.all().count(), 0)
  51. self.assertTrue(has_form_error(response))
  52. # Test valid registration. <--------------
  53. response = c.post('/accounts/register/', {
  54. 'username': 'testaccount',
  55. 'email': 'test@example.com',
  56. 'password1': 'testpwd',
  57. 'password2': 'testpwd',
  58. 'tou': 'on',
  59. })
  60. self.assertEqual(User.objects.all().count(), 1)
  61. self.assertFalse(has_form_error(response))
  62. self.assertEqual(len(mail.outbox), 1)
  63. # Test used username.
  64. response = c.post('/accounts/register/', {
  65. 'username': 'testaccount',
  66. 'email': 'anothertest@example.com',
  67. 'password1': 'testpwd',
  68. 'password2': 'testpwd',
  69. 'tou': 'on',
  70. })
  71. self.assertEqual(User.objects.all().count(), 1)
  72. self.assertTrue(has_form_error(response))
  73. self.assertEqual(len(mail.outbox), 1)
  74. # Test used email.
  75. response = c.post('/accounts/register/', {
  76. 'username': 'anothertestaccount',
  77. 'email': 'test@example.com',
  78. 'password1': 'testpwd',
  79. 'password2': 'testpwd',
  80. 'tou': 'on',
  81. })
  82. self.assertEqual(User.objects.all().count(), 1)
  83. self.assertTrue(has_form_error(response))
  84. self.assertEqual(len(mail.outbox), 1)
  85. # Continue testing the registered user.
  86. user = User.objects.get(username='testaccount')
  87. self.assertEqual(UserProfile.objects.all().count(), 1)
  88. profile = user.get_profile() # Test profile created.
  89. self.assertEqual(Group.objects.all().count(), 1)
  90. group = Group.objects.get(name='testaccount')
  91. self.assertEqual(list(group.user_set.all()), [user],
  92. "User's private group should point to the user.")
  93. # Test that the user is not active before confirming the email.
  94. self.assertFalse(user.is_active, "The user should not be active yet.")
  95. self.assertEqual(user.email, 'test@example.com')
  96. response = c.post('/accounts/login/', { # Inactive users cannot login.
  97. 'username': 'testaccount',
  98. 'password': 'testpwd',
  99. })
  100. # FIXME: Invalid username or password message is missing!
  101. self.assertFalse(auth.get_user(c).is_authenticated())
  102. # Test confirmation email and confirming the email address.
  103. match = re.search(r'(/accounts/activate/.*)', mail.outbox[0].body)
  104. self.assertIsNotNone(match)
  105. # FIXME: Add a trailing slash in the email.
  106. response = c.get(match.group(0) + '/', follow=True)
  107. self.assertEqual(response.status_code, 200)
  108. self.assertEqual(len(response.redirect_chain), 1)
  109. self.assertTrue(response.redirect_chain[0][0].endswith('/accounts/activate/complete/'))
  110. user = User.objects.get(username='testaccount')
  111. self.assertTrue(user.is_active)
  112. self.assertEqual(auth.get_user(c), user,
  113. "User is automatically logged in after confirmation.")
  114. # Test logout.
  115. response = c.get('/accounts/logout/', follow=True)
  116. self.assertEqual(len(response.redirect_chain), 1)
  117. self.assertRegex(response.redirect_chain[0][0], r'https?://[a-z.]+/', "Should go to home page.")
  118. # Test login after confirming the email.
  119. self.assertIsNone(auth.get_user(c).id)
  120. response = c.post('/accounts/login/', {
  121. 'username': 'testaccount',
  122. 'password': 'testpwd',
  123. })
  124. self.assertEqual(auth.get_user(c), user)