test_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. from django.core import mail
  2. from django.test import override_settings
  3. from django.urls import reverse
  4. from model_bakery import baker
  5. from rest_framework.test import APITestCase
  6. from apps.organizations_ext.models import OrganizationUserRole
  7. from apps.projects.models import UserProjectAlert
  8. from glitchtip.test_utils.test_case import GlitchTipTestCase
  9. from ..models import User
  10. class UserRegistrationTestCase(APITestCase):
  11. def test_create_user(self):
  12. url = reverse("rest_register")
  13. data = {
  14. "email": "test@example.com",
  15. "password1": "hunter222",
  16. "password2": "hunter222",
  17. }
  18. res = self.client.post(url, data)
  19. self.assertEqual(res.status_code, 204)
  20. def test_create_user_with_tags(self):
  21. url = reverse("rest_register")
  22. data = {
  23. "email": "test@example.com",
  24. "password1": "hunter222",
  25. "password2": "hunter222",
  26. "tags": "?utm_campaign=test&utm_source=test&utm_medium=test&utm_medium=test",
  27. }
  28. res = self.client.post(url, data)
  29. self.assertEqual(res.status_code, 204)
  30. self.assertTrue(
  31. User.objects.filter(analytics__register__utm_campaign="test").exists()
  32. )
  33. def test_closed_registration(self):
  34. """Only first user may register"""
  35. url = reverse("rest_register")
  36. user1_data = {
  37. "email": "test1@example.com",
  38. "password1": "hunter222",
  39. "password2": "hunter222",
  40. }
  41. user2_data = {
  42. "email": "test2@example.com",
  43. "password1": "hunter222",
  44. "password2": "hunter222",
  45. }
  46. with override_settings(ENABLE_USER_REGISTRATION=False):
  47. res = self.client.post(url, user1_data)
  48. self.assertEqual(res.status_code, 204)
  49. res = self.client.post(url, user2_data)
  50. self.assertEqual(res.status_code, 403)
  51. class UsersTestCase(GlitchTipTestCase):
  52. def setUp(self):
  53. self.create_logged_in_user()
  54. def test_list(self):
  55. url = reverse("api:list_users")
  56. res = self.client.get(url)
  57. self.assertContains(res, self.user.email)
  58. def test_retrieve(self):
  59. url = reverse("api:get_user", args=["me"])
  60. res = self.client.get(url)
  61. self.assertContains(res, self.user.email)
  62. url = reverse("api:get_user", args=[self.user.id])
  63. res = self.client.get(url)
  64. self.assertContains(res, self.user.email)
  65. def test_destroy(self):
  66. other_user = baker.make("users.user")
  67. url = reverse("api:delete_user", args=[other_user.pk])
  68. res = self.client.delete(url)
  69. self.assertEqual(
  70. res.status_code, 404, "User should not be able to delete other users"
  71. )
  72. url = reverse("api:delete_user", args=[self.user.pk])
  73. res = self.client.delete(url)
  74. self.assertEqual(
  75. res.status_code, 400, "Not allowed to destroy owned organization"
  76. )
  77. # Delete organization to allow user deletion
  78. self.organization.delete()
  79. res = self.client.delete(url)
  80. self.assertEqual(res.status_code, 204)
  81. self.assertFalse(User.objects.filter(pk=self.user.pk).exists())
  82. def test_update(self):
  83. url = reverse("api:update_user", args=["me"])
  84. data = {"name": "new", "options": {"foo": "bar"}}
  85. res = self.client.put(url, data, format="json")
  86. self.assertContains(res, data["name"])
  87. self.assertContains(res, data["options"]["foo"])
  88. self.assertTrue(User.objects.filter(name=data["name"]).exists())
  89. def test_organization_members_list(self):
  90. other_user = baker.make("users.user")
  91. other_organization = baker.make("organizations_ext.Organization")
  92. other_organization.add_user(other_user, OrganizationUserRole.ADMIN)
  93. user2 = baker.make("users.User")
  94. self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  95. url = reverse("organization-members-list", args=[self.organization.slug])
  96. res = self.client.get(url)
  97. self.assertContains(res, user2.email)
  98. self.assertNotContains(res, other_user.email)
  99. # Can't view members of groups you don't belong to
  100. url = reverse("organization-members-list", args=[other_organization.slug])
  101. res = self.client.get(url)
  102. self.assertNotContains(res, other_user.email)
  103. def test_emails_retrieve(self):
  104. email_address = baker.make("account.EmailAddress", user=self.user)
  105. another_user = baker.make("users.user")
  106. another_email_address = baker.make("account.EmailAddress", user=another_user)
  107. url = reverse("user-emails-list", args=["me"])
  108. res = self.client.get(url)
  109. self.assertContains(res, email_address.email)
  110. self.assertNotContains(res, another_email_address.email)
  111. def test_emails_confirm(self):
  112. email_address = baker.make("account.EmailAddress", user=self.user)
  113. url = reverse("user-emails-list", args=["me"]) + "confirm/"
  114. data = {"email": email_address.email}
  115. res = self.client.post(url, data)
  116. self.assertEqual(res.status_code, 204)
  117. self.assertEqual(len(mail.outbox), 1)
  118. def test_emails_create(self):
  119. url = reverse("user-emails-list", args=["me"])
  120. new_email = "new@exmaple.com"
  121. data = {"email": new_email}
  122. res = self.client.post(url, data)
  123. self.assertContains(res, new_email, status_code=201)
  124. self.assertTrue(
  125. self.user.emailaddress_set.filter(email=new_email, verified=False).exists()
  126. )
  127. self.assertEqual(len(mail.outbox), 1)
  128. # Ensure token is valid and can verify email
  129. body = mail.outbox[0].body
  130. key = body[body.find("confirm-email") :].split("/")[1]
  131. url = reverse("rest_verify_email")
  132. data = {"key": key}
  133. res = self.client.post(url, data)
  134. self.assertTrue(
  135. self.user.emailaddress_set.filter(email=new_email, verified=True).exists()
  136. )
  137. def test_emails_create_dupe_email(self):
  138. url = reverse("user-emails-list", args=["me"])
  139. email_address = baker.make(
  140. "account.EmailAddress",
  141. user=self.user,
  142. email="something@example.com",
  143. )
  144. data = {"email": email_address.email}
  145. res = self.client.post(url, data)
  146. self.assertContains(res, "this account", status_code=400)
  147. def test_emails_create_dupe_email_other_user(self):
  148. url = reverse("user-emails-list", args=["me"])
  149. email_address = baker.make("account.EmailAddress", email="a@example.com")
  150. data = {"email": email_address.email}
  151. res = self.client.post(url, data)
  152. self.assertContains(res, "another account", status_code=400)
  153. def test_emails_set_primary(self):
  154. url = reverse("user-emails-list", args=["me"])
  155. email_address = baker.make(
  156. "account.EmailAddress", verified=True, user=self.user
  157. )
  158. data = {"email": email_address.email}
  159. res = self.client.put(url, data)
  160. self.assertContains(res, email_address.email, status_code=200)
  161. self.assertTrue(
  162. self.user.emailaddress_set.filter(
  163. email=email_address.email, primary=True
  164. ).exists()
  165. )
  166. extra_email = baker.make("account.EmailAddress", verified=True, user=self.user)
  167. data = {"email": extra_email.email}
  168. res = self.client.put(url, data)
  169. self.assertEqual(self.user.emailaddress_set.filter(primary=True).count(), 1)
  170. def test_emails_destroy(self):
  171. url = reverse("user-emails-list", args=["me"])
  172. email_address = baker.make(
  173. "account.EmailAddress", verified=True, primary=False, user=self.user
  174. )
  175. data = {"email": email_address.email}
  176. res = self.client.delete(url, data)
  177. self.assertEqual(res.status_code, 204)
  178. self.assertFalse(
  179. self.user.emailaddress_set.filter(email=email_address.email).exists()
  180. )
  181. def test_notifications_retrieve(self):
  182. url = reverse("user-detail", args=["me"]) + "notifications/"
  183. res = self.client.get(url)
  184. self.assertContains(res, "subscribeByDefault")
  185. def test_notifications_update(self):
  186. url = reverse("user-detail", args=["me"]) + "notifications/"
  187. data = {"subscribeByDefault": False}
  188. res = self.client.put(url, data)
  189. self.assertFalse(res.data.get("subscribeByDefault"))
  190. self.user.refresh_from_db()
  191. self.assertFalse(self.user.subscribe_by_default)
  192. def test_alerts_retrieve(self):
  193. url = reverse("user-detail", args=["me"]) + "notifications/alerts/"
  194. alert = baker.make(
  195. "projects.UserProjectAlert", user=self.user, project=self.project
  196. )
  197. res = self.client.get(url)
  198. self.assertContains(res, self.project.id)
  199. self.assertEqual(res.data[self.project.id], alert.status)
  200. def test_alerts_update(self):
  201. url = reverse("user-detail", args=["me"]) + "notifications/alerts/"
  202. # Set to alert to On
  203. data = '{"' + str(self.project.id) + '":1}'
  204. res = self.client.put(url, data, content_type="application/json")
  205. self.assertEqual(res.status_code, 204)
  206. self.assertEqual(UserProjectAlert.objects.all().count(), 1)
  207. self.assertEqual(UserProjectAlert.objects.first().status, 1)
  208. # Set to alert to Off
  209. data = '{"' + str(self.project.id) + '":0}'
  210. res = self.client.put(url, data, content_type="application/json")
  211. self.assertEqual(res.status_code, 204)
  212. self.assertEqual(UserProjectAlert.objects.first().status, 0)
  213. # Set to alert to "default"
  214. data = '{"' + str(self.project.id) + '":-1}'
  215. res = self.client.put(url, data, content_type="application/json")
  216. self.assertEqual(res.status_code, 204)
  217. # Default deletes the row
  218. self.assertEqual(UserProjectAlert.objects.all().count(), 0)
  219. def test_reset_password(self):
  220. """
  221. Social accounts weren't getting reset password emails. This
  222. approximates the issue by testing an account that has an
  223. unusable password.
  224. """
  225. url = reverse("rest_password_reset")
  226. # Normal behavior
  227. self.client.post(url, {"email": self.user.email})
  228. self.assertEqual(len(mail.outbox), 1)
  229. user_without_password = baker.make("users.User")
  230. user_without_password.set_unusable_password()
  231. user_without_password.save()
  232. self.assertFalse(user_without_password.has_usable_password())
  233. self.client.post(url, {"email": user_without_password.email})
  234. self.assertEqual(len(mail.outbox), 2)