test_user_create_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. from django.contrib.auth import get_user_model
  2. from django.core import mail
  3. from django.test import override_settings
  4. from django.urls import reverse
  5. from misago.conf import settings
  6. from misago.users.models import Ban, Online
  7. from misago.users.testutils import UserTestCase
  8. UserModel = get_user_model()
  9. class UserCreateTests(UserTestCase):
  10. """tests for new user registration (POST to /api/users/)"""
  11. def setUp(self):
  12. super(UserCreateTests, self).setUp()
  13. self.api_link = '/api/users/'
  14. def test_empty_request(self):
  15. """empty request errors with code 400"""
  16. response = self.client.post(self.api_link)
  17. self.assertEqual(response.status_code, 400)
  18. def test_invalid_data(self):
  19. """invalid request data errors with code 400"""
  20. response = self.client.post(
  21. self.api_link,
  22. 'false',
  23. content_type="application/json",
  24. )
  25. self.assertEqual(response.status_code, 400)
  26. def test_authenticated_request(self):
  27. """authentiated user request errors with code 403"""
  28. self.login_user(self.get_authenticated_user())
  29. response = self.client.post(self.api_link)
  30. self.assertEqual(response.status_code, 403)
  31. def test_registration_off_request(self):
  32. """registrations off request errors with code 403"""
  33. settings.override_setting('account_activation', 'closed')
  34. response = self.client.post(self.api_link)
  35. self.assertContains(response, 'closed', status_code=403)
  36. def test_registration_validates_ip_ban(self):
  37. """api validates ip ban"""
  38. Ban.objects.create(
  39. check_type=Ban.IP,
  40. banned_value='127.*',
  41. user_message="You can't register account like this.",
  42. )
  43. response = self.client.post(
  44. self.api_link,
  45. data={
  46. 'username': 'totallyNew',
  47. 'email': 'loremipsum@dolor.met',
  48. 'password': 'LoremP4ssword',
  49. },
  50. )
  51. self.assertEqual(response.status_code, 403)
  52. def test_registration_validates_ip_registration_ban(self):
  53. """api validates ip registration-only ban"""
  54. Ban.objects.create(
  55. check_type=Ban.IP,
  56. banned_value='127.*',
  57. user_message="You can't register account like this.",
  58. registration_only=True,
  59. )
  60. response = self.client.post(
  61. self.api_link,
  62. data={
  63. 'username': 'totallyNew',
  64. 'email': 'loremipsum@dolor.met',
  65. 'password': 'LoremP4ssword',
  66. },
  67. )
  68. self.assertEqual(response.status_code, 403)
  69. self.assertEqual(
  70. response.json(), {
  71. 'detail': {
  72. 'html': '<p>You can&#39;t register account like this.</p>',
  73. 'plain': "You can't register account like this.",
  74. },
  75. 'expires_on': None,
  76. }
  77. )
  78. def test_registration_validates_username(self):
  79. """api validates usernames"""
  80. user = self.get_authenticated_user()
  81. response = self.client.post(
  82. self.api_link,
  83. data={
  84. 'username': user.username,
  85. 'email': 'loremipsum@dolor.met',
  86. 'password': 'LoremP4ssword',
  87. },
  88. )
  89. self.assertEqual(response.status_code, 400)
  90. self.assertEqual(response.json(), {
  91. 'username': ["This username is not available."],
  92. })
  93. def test_registration_validates_username_ban(self):
  94. """api validates username ban"""
  95. Ban.objects.create(
  96. banned_value='totally*',
  97. user_message="You can't register account like this.",
  98. )
  99. response = self.client.post(
  100. self.api_link,
  101. data={
  102. 'username': 'totallyNew',
  103. 'email': 'loremipsum@dolor.met',
  104. 'password': 'LoremP4ssword',
  105. },
  106. )
  107. self.assertEqual(response.status_code, 400)
  108. self.assertEqual(
  109. response.json(), {
  110. 'username': ["You can't register account like this."],
  111. }
  112. )
  113. def test_registration_validates_username_registration_ban(self):
  114. """api validates username registration-only ban"""
  115. Ban.objects.create(
  116. banned_value='totally*',
  117. user_message="You can't register account like this.",
  118. registration_only=True,
  119. )
  120. response = self.client.post(
  121. self.api_link,
  122. data={
  123. 'username': 'totallyNew',
  124. 'email': 'loremipsum@dolor.met',
  125. 'password': 'LoremP4ssword',
  126. },
  127. )
  128. self.assertEqual(response.status_code, 400)
  129. self.assertEqual(
  130. response.json(), {
  131. 'username': ["You can't register account like this."],
  132. }
  133. )
  134. def test_registration_validates_email(self):
  135. """api validates usernames"""
  136. user = self.get_authenticated_user()
  137. response = self.client.post(
  138. self.api_link,
  139. data={
  140. 'username': 'totallyNew',
  141. 'email': user.email,
  142. 'password': 'LoremP4ssword',
  143. },
  144. )
  145. self.assertEqual(response.status_code, 400)
  146. self.assertEqual(response.json(), {
  147. 'email': ["This e-mail address is not available."],
  148. })
  149. def test_registration_validates_email_ban(self):
  150. """api validates email ban"""
  151. Ban.objects.create(
  152. check_type=Ban.EMAIL,
  153. banned_value='lorem*',
  154. user_message="You can't register account like this.",
  155. )
  156. response = self.client.post(
  157. self.api_link,
  158. data={
  159. 'username': 'totallyNew',
  160. 'email': 'loremipsum@dolor.met',
  161. 'password': 'LoremP4ssword',
  162. },
  163. )
  164. self.assertEqual(response.status_code, 400)
  165. self.assertEqual(response.json(), {
  166. 'email': ["You can't register account like this."],
  167. })
  168. def test_registration_validates_email_registration_ban(self):
  169. """api validates email registration-only ban"""
  170. Ban.objects.create(
  171. check_type=Ban.EMAIL,
  172. banned_value='lorem*',
  173. user_message="You can't register account like this.",
  174. registration_only=True,
  175. )
  176. response = self.client.post(
  177. self.api_link,
  178. data={
  179. 'username': 'totallyNew',
  180. 'email': 'loremipsum@dolor.met',
  181. 'password': 'LoremP4ssword',
  182. },
  183. )
  184. self.assertEqual(response.status_code, 400)
  185. self.assertEqual(response.json(), {
  186. 'email': ["You can't register account like this."],
  187. })
  188. def test_registration_validates_password(self):
  189. """api uses django's validate_password to validate registrations"""
  190. response = self.client.post(
  191. self.api_link,
  192. data={
  193. 'username': 'Bob',
  194. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  195. 'password': '123',
  196. },
  197. )
  198. self.assertContains(response, "password is too short", status_code=400)
  199. self.assertContains(response, "password is entirely numeric", status_code=400)
  200. self.assertContains(response, "email is not allowed", status_code=400)
  201. def test_registration_validates_password_similiarity(self):
  202. """api uses validate_password to validate registrations"""
  203. response = self.client.post(
  204. self.api_link,
  205. data={
  206. 'username': 'BobBoberson',
  207. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  208. 'password': 'BobBoberson',
  209. },
  210. )
  211. self.assertContains(response, "password is too similar to the username", status_code=400)
  212. @override_settings(captcha_type='qa', qa_question='Test', qa_answers='Lorem\nIpsum')
  213. def test_registration_validates_captcha(self):
  214. """api validates captcha"""
  215. response = self.client.post(
  216. self.api_link,
  217. data={
  218. 'username': 'totallyNew',
  219. 'email': 'loremipsum@dolor.met',
  220. 'password': 'LoremP4ssword',
  221. 'captcha': 'dolor'
  222. },
  223. )
  224. self.assertEqual(response.status_code, 400)
  225. self.assertEqual(
  226. response.json(), {
  227. 'captcha': ['Entered answer is incorrect.'],
  228. }
  229. )
  230. # valid captcha
  231. response = self.client.post(
  232. self.api_link,
  233. data={
  234. 'username': 'totallyNew',
  235. 'email': 'loremipsum@dolor.met',
  236. 'password': 'LoremP4ssword',
  237. 'captcha': 'ipSUM'
  238. },
  239. )
  240. self.assertEqual(response.status_code, 200)
  241. def test_registration_calls_validate_new_registration(self):
  242. """api uses validate_new_registration to validate registrations"""
  243. response = self.client.post(
  244. self.api_link,
  245. data={
  246. 'username': 'Bob',
  247. 'email': 'l.o.r.e.m.i.p.s.u.m@gmail.com',
  248. 'password': 'pass1234',
  249. },
  250. )
  251. self.assertEqual(response.status_code, 400)
  252. self.assertEqual(response.json(), {
  253. 'email': ["This email is not allowed."]
  254. })
  255. def test_registration_creates_active_user(self):
  256. """api creates active and signed in user on POST"""
  257. settings.override_setting('account_activation', 'none')
  258. response = self.client.post(
  259. self.api_link,
  260. data={
  261. 'username': 'Bob',
  262. 'email': 'bob@bob.com',
  263. 'password': 'pass123',
  264. },
  265. )
  266. self.assertEqual(response.json(), {
  267. 'activation': None,
  268. 'username': 'Bob',
  269. 'email': 'bob@bob.com',
  270. })
  271. UserModel.objects.get_by_username('Bob')
  272. test_user = UserModel.objects.get_by_email('bob@bob.com')
  273. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  274. self.assertTrue(test_user.check_password('pass123'))
  275. response = self.client.get(reverse('misago:index'))
  276. self.assertContains(response, 'Bob')
  277. self.assertIn('Welcome', mail.outbox[0].subject)
  278. def test_registration_creates_inactive_user(self):
  279. """api creates inactive user on POST"""
  280. settings.override_setting('account_activation', 'user')
  281. response = self.client.post(
  282. self.api_link,
  283. data={
  284. 'username': 'Bob',
  285. 'email': 'bob@bob.com',
  286. 'password': 'pass123',
  287. },
  288. )
  289. self.assertEqual(response.json(), {
  290. 'activation': 'user',
  291. 'username': 'Bob',
  292. 'email': 'bob@bob.com',
  293. })
  294. UserModel.objects.get_by_username('Bob')
  295. UserModel.objects.get_by_email('bob@bob.com')
  296. self.assertIn('Welcome', mail.outbox[0].subject)
  297. def test_registration_creates_admin_activated_user(self):
  298. """api creates admin activated user on POST"""
  299. settings.override_setting('account_activation', 'admin')
  300. response = self.client.post(
  301. self.api_link,
  302. data={
  303. 'username': 'Bob',
  304. 'email': 'bob@bob.com',
  305. 'password': 'pass123',
  306. },
  307. )
  308. self.assertEqual(response.json(), {
  309. 'activation': 'admin',
  310. 'username': 'Bob',
  311. 'email': 'bob@bob.com',
  312. })
  313. UserModel.objects.get_by_username('Bob')
  314. UserModel.objects.get_by_email('bob@bob.com')
  315. self.assertIn('Welcome', mail.outbox[0].subject)
  316. def test_registration_creates_user_with_whitespace_password(self):
  317. """api creates user with spaces around password"""
  318. settings.override_setting('account_activation', 'none')
  319. response = self.client.post(
  320. self.api_link,
  321. data={
  322. 'username': 'Bob',
  323. 'email': 'bob@bob.com',
  324. 'password': ' pass123 ',
  325. },
  326. )
  327. self.assertEqual(response.json(), {
  328. 'activation': None,
  329. 'username': 'Bob',
  330. 'email': 'bob@bob.com',
  331. })
  332. UserModel.objects.get_by_username('Bob')
  333. test_user = UserModel.objects.get_by_email('bob@bob.com')
  334. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  335. self.assertTrue(test_user.check_password(' pass123 '))
  336. response = self.client.get(reverse('misago:index'))
  337. self.assertContains(response, 'Bob')
  338. self.assertIn('Welcome', mail.outbox[0].subject)