test_user_create_api.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_password(self):
  30. """api uses django's validate_password to validate registrations"""
  31. response = self.client.post(self.api_link, data={
  32. 'username': 'Bob',
  33. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  34. 'password': '123'
  35. })
  36. self.assertContains(response, "password is too short", status_code=400)
  37. self.assertContains(response, "password is entirely numeric", status_code=400)
  38. self.assertContains(response, "email is not allowed", status_code=400)
  39. def test_registration_validates_password_similiarity(self):
  40. """api uses validate_password to validate registrations"""
  41. response = self.client.post(self.api_link, data={
  42. 'username': 'BobBoberson',
  43. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  44. 'password': 'BobBoberson'
  45. })
  46. self.assertContains(response, "password is too similar to the username", status_code=400)
  47. def test_registration_calls_validate_new_registration(self):
  48. """api uses validate_new_registration to validate registrations"""
  49. response = self.client.post(self.api_link, data={
  50. 'username': 'Bob',
  51. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  52. 'password': 'pas123'
  53. })
  54. self.assertContains(response, "email is not allowed", status_code=400)
  55. def test_registration_creates_active_user(self):
  56. """api creates active and signed in user on POST"""
  57. settings.override_setting('account_activation', 'none')
  58. response = self.client.post(self.api_link, data={
  59. 'username': 'Bob',
  60. 'email': 'bob@bob.com',
  61. 'password': 'pass123'
  62. })
  63. self.assertContains(response, 'active')
  64. self.assertContains(response, 'Bob')
  65. self.assertContains(response, 'bob@bob.com')
  66. UserModel.objects.get_by_username('Bob')
  67. test_user = UserModel.objects.get_by_email('bob@bob.com')
  68. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  69. response = self.client.get(reverse('misago:index'))
  70. self.assertContains(response, 'Bob')
  71. self.assertIn('Welcome', mail.outbox[0].subject)
  72. def test_registration_creates_inactive_user(self):
  73. """api creates inactive user on POST"""
  74. settings.override_setting('account_activation', 'user')
  75. response = self.client.post(self.api_link, data={
  76. 'username': 'Bob',
  77. 'email': 'bob@bob.com',
  78. 'password': 'pass123'
  79. })
  80. self.assertContains(response, 'user')
  81. self.assertContains(response, 'Bob')
  82. self.assertContains(response, 'bob@bob.com')
  83. UserModel.objects.get_by_username('Bob')
  84. UserModel.objects.get_by_email('bob@bob.com')
  85. self.assertIn('Welcome', mail.outbox[0].subject)
  86. def test_registration_creates_admin_activated_user(self):
  87. """api creates admin activated user on POST"""
  88. settings.override_setting('account_activation', 'admin')
  89. response = self.client.post(self.api_link, data={
  90. 'username': 'Bob',
  91. 'email': 'bob@bob.com',
  92. 'password': 'pass123'
  93. })
  94. self.assertContains(response, 'admin')
  95. self.assertContains(response, 'Bob')
  96. self.assertContains(response, 'bob@bob.com')
  97. UserModel.objects.get_by_username('Bob')
  98. UserModel.objects.get_by_email('bob@bob.com')
  99. self.assertIn('Welcome', mail.outbox[0].subject)