test_user_create_api.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(), {
  42. 'username': ["This username is not available."],
  43. })
  44. def test_registration_validates_email(self):
  45. """api validates usernames"""
  46. user = self.get_authenticated_user()
  47. response = self.client.post(
  48. self.api_link,
  49. data={
  50. 'username': 'totallyNew',
  51. 'email': user.email,
  52. 'password': 'LoremP4ssword',
  53. },
  54. )
  55. self.assertEqual(response.status_code, 400)
  56. self.assertEqual(response.json(), {
  57. 'email': ["This e-mail address is not available."],
  58. })
  59. def test_registration_validates_password(self):
  60. """api uses django's validate_password to validate registrations"""
  61. response = self.client.post(
  62. self.api_link,
  63. data={
  64. 'username': 'Bob',
  65. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  66. 'password': '123',
  67. },
  68. )
  69. self.assertContains(response, "password is too short", status_code=400)
  70. self.assertContains(response, "password is entirely numeric", status_code=400)
  71. self.assertContains(response, "email is not allowed", status_code=400)
  72. def test_registration_validates_password_similiarity(self):
  73. """api uses validate_password to validate registrations"""
  74. response = self.client.post(
  75. self.api_link,
  76. data={
  77. 'username': 'BobBoberson',
  78. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  79. 'password': 'BobBoberson',
  80. },
  81. )
  82. self.assertContains(response, "password is too similar to the username", status_code=400)
  83. def test_registration_calls_validate_new_registration(self):
  84. """api uses validate_new_registration to validate registrations"""
  85. response = self.client.post(
  86. self.api_link,
  87. data={
  88. 'username': 'Bob',
  89. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  90. 'password': 'pas123',
  91. },
  92. )
  93. self.assertContains(response, "email is not allowed", status_code=400)
  94. def test_registration_creates_active_user(self):
  95. """api creates active and signed in user on POST"""
  96. settings.override_setting('account_activation', 'none')
  97. response = self.client.post(
  98. self.api_link,
  99. data={
  100. 'username': 'Bob',
  101. 'email': 'bob@bob.com',
  102. 'password': 'pass123',
  103. },
  104. )
  105. self.assertContains(response, 'active')
  106. self.assertContains(response, 'Bob')
  107. self.assertContains(response, 'bob@bob.com')
  108. UserModel.objects.get_by_username('Bob')
  109. test_user = UserModel.objects.get_by_email('bob@bob.com')
  110. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  111. response = self.client.get(reverse('misago:index'))
  112. self.assertContains(response, 'Bob')
  113. self.assertIn('Welcome', mail.outbox[0].subject)
  114. def test_registration_creates_inactive_user(self):
  115. """api creates inactive user on POST"""
  116. settings.override_setting('account_activation', 'user')
  117. response = self.client.post(
  118. self.api_link,
  119. data={
  120. 'username': 'Bob',
  121. 'email': 'bob@bob.com',
  122. 'password': 'pass123',
  123. },
  124. )
  125. self.assertContains(response, 'user')
  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)
  131. def test_registration_creates_admin_activated_user(self):
  132. """api creates admin activated user on POST"""
  133. settings.override_setting('account_activation', 'admin')
  134. response = self.client.post(
  135. self.api_link,
  136. data={
  137. 'username': 'Bob',
  138. 'email': 'bob@bob.com',
  139. 'password': 'pass123',
  140. },
  141. )
  142. self.assertContains(response, 'admin')
  143. self.assertContains(response, 'Bob')
  144. self.assertContains(response, 'bob@bob.com')
  145. UserModel.objects.get_by_username('Bob')
  146. UserModel.objects.get_by_email('bob@bob.com')
  147. self.assertIn('Welcome', mail.outbox[0].subject)