test_user_create_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 Ban, 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_ip_ban(self):
  28. """api validates ip ban"""
  29. Ban.objects.create(
  30. check_type=Ban.IP,
  31. banned_value='127.*',
  32. user_message="You can't register account like this.",
  33. )
  34. response = self.client.post(
  35. self.api_link,
  36. data={
  37. 'username': 'totallyNew',
  38. 'email': 'loremipsum@dolor.met',
  39. 'password': 'LoremP4ssword',
  40. },
  41. )
  42. self.assertEqual(response.status_code, 403)
  43. def test_registration_validates_ip_registration_ban(self):
  44. """api validates ip registration-only ban"""
  45. Ban.objects.create(
  46. check_type=Ban.IP,
  47. banned_value='127.*',
  48. user_message="You can't register account like this.",
  49. registration_only=True,
  50. )
  51. response = self.client.post(
  52. self.api_link,
  53. data={
  54. 'username': 'totallyNew',
  55. 'email': 'loremipsum@dolor.met',
  56. 'password': 'LoremP4ssword',
  57. },
  58. )
  59. self.assertEqual(response.status_code, 400)
  60. self.assertEqual(
  61. response.json(), {
  62. '__all__': ["You can't register account like this."],
  63. }
  64. )
  65. def test_registration_validates_username(self):
  66. """api validates usernames"""
  67. user = self.get_authenticated_user()
  68. response = self.client.post(
  69. self.api_link,
  70. data={
  71. 'username': user.username,
  72. 'email': 'loremipsum@dolor.met',
  73. 'password': 'LoremP4ssword',
  74. },
  75. )
  76. self.assertEqual(response.status_code, 400)
  77. self.assertEqual(response.json(), {
  78. 'username': ["This username is not available."],
  79. })
  80. def test_registration_validates_username_ban(self):
  81. """api validates username ban"""
  82. Ban.objects.create(
  83. banned_value='totally*',
  84. user_message="You can't register account like this.",
  85. )
  86. response = self.client.post(
  87. self.api_link,
  88. data={
  89. 'username': 'totallyNew',
  90. 'email': 'loremipsum@dolor.met',
  91. 'password': 'LoremP4ssword',
  92. },
  93. )
  94. self.assertEqual(response.status_code, 400)
  95. self.assertEqual(
  96. response.json(), {
  97. 'username': ["You can't register account like this."],
  98. }
  99. )
  100. def test_registration_validates_username_registration_ban(self):
  101. """api validates username registration-only ban"""
  102. Ban.objects.create(
  103. banned_value='totally*',
  104. user_message="You can't register account like this.",
  105. registration_only=True,
  106. )
  107. response = self.client.post(
  108. self.api_link,
  109. data={
  110. 'username': 'totallyNew',
  111. 'email': 'loremipsum@dolor.met',
  112. 'password': 'LoremP4ssword',
  113. },
  114. )
  115. self.assertEqual(response.status_code, 400)
  116. self.assertEqual(
  117. response.json(), {
  118. 'username': ["You can't register account like this."],
  119. }
  120. )
  121. def test_registration_validates_email(self):
  122. """api validates usernames"""
  123. user = self.get_authenticated_user()
  124. response = self.client.post(
  125. self.api_link,
  126. data={
  127. 'username': 'totallyNew',
  128. 'email': user.email,
  129. 'password': 'LoremP4ssword',
  130. },
  131. )
  132. self.assertEqual(response.status_code, 400)
  133. self.assertEqual(response.json(), {
  134. 'email': ["This e-mail address is not available."],
  135. })
  136. def test_registration_validates_email_ban(self):
  137. """api validates email ban"""
  138. Ban.objects.create(
  139. check_type=Ban.EMAIL,
  140. banned_value='lorem*',
  141. user_message="You can't register account like this.",
  142. )
  143. response = self.client.post(
  144. self.api_link,
  145. data={
  146. 'username': 'totallyNew',
  147. 'email': 'loremipsum@dolor.met',
  148. 'password': 'LoremP4ssword',
  149. },
  150. )
  151. self.assertEqual(response.status_code, 400)
  152. self.assertEqual(response.json(), {
  153. 'email': ["You can't register account like this."],
  154. })
  155. def test_registration_validates_email_registration_ban(self):
  156. """api validates email registration-only ban"""
  157. Ban.objects.create(
  158. check_type=Ban.EMAIL,
  159. banned_value='lorem*',
  160. user_message="You can't register account like this.",
  161. registration_only=True,
  162. )
  163. response = self.client.post(
  164. self.api_link,
  165. data={
  166. 'username': 'totallyNew',
  167. 'email': 'loremipsum@dolor.met',
  168. 'password': 'LoremP4ssword',
  169. },
  170. )
  171. self.assertEqual(response.status_code, 400)
  172. self.assertEqual(response.json(), {
  173. 'email': ["You can't register account like this."],
  174. })
  175. def test_registration_validates_password(self):
  176. """api uses django's validate_password to validate registrations"""
  177. response = self.client.post(
  178. self.api_link,
  179. data={
  180. 'username': 'Bob',
  181. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  182. 'password': '123',
  183. },
  184. )
  185. self.assertContains(response, "password is too short", status_code=400)
  186. self.assertContains(response, "password is entirely numeric", status_code=400)
  187. self.assertContains(response, "email is not allowed", status_code=400)
  188. def test_registration_validates_password_similiarity(self):
  189. """api uses validate_password to validate registrations"""
  190. response = self.client.post(
  191. self.api_link,
  192. data={
  193. 'username': 'BobBoberson',
  194. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  195. 'password': 'BobBoberson',
  196. },
  197. )
  198. self.assertContains(response, "password is too similar to the username", status_code=400)
  199. def test_registration_calls_validate_new_registration(self):
  200. """api uses validate_new_registration to validate registrations"""
  201. response = self.client.post(
  202. self.api_link,
  203. data={
  204. 'username': 'Bob',
  205. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  206. 'password': 'pas123',
  207. },
  208. )
  209. self.assertContains(response, "email is not allowed", status_code=400)
  210. def test_registration_creates_active_user(self):
  211. """api creates active and signed in user on POST"""
  212. settings.override_setting('account_activation', 'none')
  213. response = self.client.post(
  214. self.api_link,
  215. data={
  216. 'username': 'Bob',
  217. 'email': 'bob@bob.com',
  218. 'password': 'pass123',
  219. },
  220. )
  221. self.assertContains(response, 'active')
  222. self.assertContains(response, 'Bob')
  223. self.assertContains(response, 'bob@bob.com')
  224. UserModel.objects.get_by_username('Bob')
  225. test_user = UserModel.objects.get_by_email('bob@bob.com')
  226. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  227. self.assertTrue(test_user.check_password('pass123'))
  228. response = self.client.get(reverse('misago:index'))
  229. self.assertContains(response, 'Bob')
  230. self.assertIn('Welcome', mail.outbox[0].subject)
  231. def test_registration_creates_inactive_user(self):
  232. """api creates inactive user on POST"""
  233. settings.override_setting('account_activation', 'user')
  234. response = self.client.post(
  235. self.api_link,
  236. data={
  237. 'username': 'Bob',
  238. 'email': 'bob@bob.com',
  239. 'password': 'pass123',
  240. },
  241. )
  242. self.assertContains(response, 'user')
  243. self.assertContains(response, 'Bob')
  244. self.assertContains(response, 'bob@bob.com')
  245. UserModel.objects.get_by_username('Bob')
  246. UserModel.objects.get_by_email('bob@bob.com')
  247. self.assertIn('Welcome', mail.outbox[0].subject)
  248. def test_registration_creates_admin_activated_user(self):
  249. """api creates admin activated user on POST"""
  250. settings.override_setting('account_activation', 'admin')
  251. response = self.client.post(
  252. self.api_link,
  253. data={
  254. 'username': 'Bob',
  255. 'email': 'bob@bob.com',
  256. 'password': 'pass123',
  257. },
  258. )
  259. self.assertContains(response, 'admin')
  260. self.assertContains(response, 'Bob')
  261. self.assertContains(response, 'bob@bob.com')
  262. UserModel.objects.get_by_username('Bob')
  263. UserModel.objects.get_by_email('bob@bob.com')
  264. self.assertIn('Welcome', mail.outbox[0].subject)
  265. def test_registration_creates_user_with_whitespace_password(self):
  266. """api creates user with spaces around password"""
  267. settings.override_setting('account_activation', 'none')
  268. response = self.client.post(
  269. self.api_link,
  270. data={
  271. 'username': 'Bob',
  272. 'email': 'bob@bob.com',
  273. 'password': ' pass123 ',
  274. },
  275. )
  276. self.assertContains(response, 'active')
  277. self.assertContains(response, 'Bob')
  278. self.assertContains(response, 'bob@bob.com')
  279. UserModel.objects.get_by_username('Bob')
  280. test_user = UserModel.objects.get_by_email('bob@bob.com')
  281. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  282. self.assertTrue(test_user.check_password(' pass123 '))
  283. response = self.client.get(reverse('misago:index'))
  284. self.assertContains(response, 'Bob')
  285. self.assertIn('Welcome', mail.outbox[0].subject)