test_user_create_api.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. from django.contrib.auth import get_user_model
  2. from django.core import mail
  3. from django.urls import reverse
  4. from misago.conf import settings
  5. from misago.users.models import Online
  6. from misago.users.testutils import UserTestCase
  7. UserModel = get_user_model()
  8. class UserCreateTests(UserTestCase):
  9. """
  10. tests for new user registration (POST to /api/users/)
  11. """
  12. def setUp(self):
  13. super(UserCreateTests, self).setUp()
  14. self.api_link = '/api/users/'
  15. def test_empty_request(self):
  16. """empty request errors with code 400"""
  17. response = self.client.post(self.api_link)
  18. self.assertEqual(response.status_code, 400)
  19. def test_authenticated_request(self):
  20. """authentiated user request errors with code 403"""
  21. self.login_user(self.get_authenticated_user())
  22. response = self.client.post(self.api_link)
  23. self.assertEqual(response.status_code, 403)
  24. def test_registration_off_request(self):
  25. """registrations off request errors with code 403"""
  26. settings.override_setting('account_activation', 'closed')
  27. response = self.client.post(self.api_link)
  28. self.assertContains(response, 'closed', status_code=403)
  29. def test_registration_validates_username(self):
  30. """api validates usernames"""
  31. user = self.get_authenticated_user()
  32. response = self.client.post(
  33. self.api_link,
  34. data={
  35. 'username': user.username,
  36. 'email': 'loremipsum@dolor.met',
  37. 'password': 'LoremP4ssword'
  38. }
  39. )
  40. self.assertEqual(response.status_code, 400)
  41. self.assertEqual(response.json(), {'username': ["This username is not available."]})
  42. def test_registration_validates_email(self):
  43. """api validates usernames"""
  44. user = self.get_authenticated_user()
  45. response = self.client.post(
  46. self.api_link,
  47. data={'username': 'totallyNew',
  48. 'email': user.email,
  49. 'password': 'LoremP4ssword'}
  50. )
  51. self.assertEqual(response.status_code, 400)
  52. self.assertEqual(response.json(), {'email': ["This e-mail address is not available."]})
  53. def test_registration_validates_password(self):
  54. """api uses django's validate_password to validate registrations"""
  55. response = self.client.post(
  56. self.api_link,
  57. data={'username': 'Bob',
  58. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  59. 'password': '123'}
  60. )
  61. self.assertContains(response, "password is too short", status_code=400)
  62. self.assertContains(response, "password is entirely numeric", status_code=400)
  63. self.assertContains(response, "email is not allowed", status_code=400)
  64. def test_registration_validates_password_similiarity(self):
  65. """api uses validate_password to validate registrations"""
  66. response = self.client.post(
  67. self.api_link,
  68. data={
  69. 'username': 'BobBoberson',
  70. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  71. 'password': 'BobBoberson'
  72. }
  73. )
  74. self.assertContains(response, "password is too similar to the username", status_code=400)
  75. def test_registration_calls_validate_new_registration(self):
  76. """api uses validate_new_registration to validate registrations"""
  77. response = self.client.post(
  78. self.api_link,
  79. data={
  80. 'username': 'Bob',
  81. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  82. 'password': 'pas123'
  83. }
  84. )
  85. self.assertContains(response, "email is not allowed", status_code=400)
  86. def test_registration_creates_active_user(self):
  87. """api creates active and signed in user on POST"""
  88. settings.override_setting('account_activation', 'none')
  89. response = self.client.post(
  90. self.api_link, data={'username': 'Bob',
  91. 'email': 'bob@bob.com',
  92. 'password': 'pass123'}
  93. )
  94. self.assertContains(response, 'active')
  95. self.assertContains(response, 'Bob')
  96. self.assertContains(response, 'bob@bob.com')
  97. UserModel.objects.get_by_username('Bob')
  98. test_user = UserModel.objects.get_by_email('bob@bob.com')
  99. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  100. response = self.client.get(reverse('misago:index'))
  101. self.assertContains(response, 'Bob')
  102. self.assertIn('Welcome', mail.outbox[0].subject)
  103. def test_registration_creates_inactive_user(self):
  104. """api creates inactive user on POST"""
  105. settings.override_setting('account_activation', 'user')
  106. response = self.client.post(
  107. self.api_link, data={'username': 'Bob',
  108. 'email': 'bob@bob.com',
  109. 'password': 'pass123'}
  110. )
  111. self.assertContains(response, 'user')
  112. self.assertContains(response, 'Bob')
  113. self.assertContains(response, 'bob@bob.com')
  114. UserModel.objects.get_by_username('Bob')
  115. UserModel.objects.get_by_email('bob@bob.com')
  116. self.assertIn('Welcome', mail.outbox[0].subject)
  117. def test_registration_creates_admin_activated_user(self):
  118. """api creates admin activated user on POST"""
  119. settings.override_setting('account_activation', 'admin')
  120. response = self.client.post(
  121. self.api_link, data={'username': 'Bob',
  122. 'email': 'bob@bob.com',
  123. 'password': 'pass123'}
  124. )
  125. self.assertContains(response, 'admin')
  126. self.assertContains(response, 'Bob')
  127. self.assertContains(response, 'bob@bob.com')
  128. UserModel.objects.get_by_username('Bob')
  129. UserModel.objects.get_by_email('bob@bob.com')
  130. self.assertIn('Welcome', mail.outbox[0].subject)