test_user_create_api.py 6.0 KB

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