test_api.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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("account.EmailAddress", user=self.user)
  140. data = {"email": email_address.email}
  141. res = self.client.post(url, data)
  142. self.assertContains(res, "this account", status_code=400)
  143. def test_emails_create_dupe_email_other_user(self):
  144. url = reverse("user-emails-list", args=["me"])
  145. email_address = baker.make("account.EmailAddress")
  146. data = {"email": email_address.email}
  147. res = self.client.post(url, data)
  148. self.assertContains(res, "another account", status_code=400)
  149. def test_emails_set_primary(self):
  150. url = reverse("user-emails-list", args=["me"])
  151. email_address = baker.make(
  152. "account.EmailAddress", verified=True, user=self.user
  153. )
  154. data = {"email": email_address.email}
  155. res = self.client.put(url, data)
  156. self.assertContains(res, email_address.email, status_code=200)
  157. self.assertTrue(
  158. self.user.emailaddress_set.filter(
  159. email=email_address.email, primary=True
  160. ).exists()
  161. )
  162. extra_email = baker.make("account.EmailAddress", verified=True, user=self.user)
  163. data = {"email": extra_email.email}
  164. res = self.client.put(url, data)
  165. self.assertEqual(self.user.emailaddress_set.filter(primary=True).count(), 1)
  166. def test_emails_destroy(self):
  167. url = reverse("user-emails-list", args=["me"])
  168. email_address = baker.make(
  169. "account.EmailAddress", verified=True, primary=False, user=self.user
  170. )
  171. data = {"email": email_address.email}
  172. res = self.client.delete(url, data)
  173. self.assertEqual(res.status_code, 204)
  174. self.assertFalse(
  175. self.user.emailaddress_set.filter(email=email_address.email).exists()
  176. )
  177. def test_notifications_retrieve(self):
  178. url = reverse("user-detail", args=["me"]) + "notifications/"
  179. res = self.client.get(url)
  180. self.assertContains(res, "subscribeByDefault")
  181. def test_notifications_update(self):
  182. url = reverse("user-detail", args=["me"]) + "notifications/"
  183. data = {"subscribeByDefault": False}
  184. res = self.client.put(url, data)
  185. self.assertFalse(res.data.get("subscribeByDefault"))
  186. self.user.refresh_from_db()
  187. self.assertFalse(self.user.subscribe_by_default)
  188. def test_alerts_retrieve(self):
  189. url = reverse("user-detail", args=["me"]) + "notifications/alerts/"
  190. alert = baker.make(
  191. "projects.UserProjectAlert", user=self.user, project=self.project
  192. )
  193. res = self.client.get(url)
  194. self.assertContains(res, self.project.id)
  195. self.assertEqual(res.data[self.project.id], alert.status)
  196. def test_alerts_update(self):
  197. url = reverse("user-detail", args=["me"]) + "notifications/alerts/"
  198. # Set to alert to On
  199. data = '{"' + str(self.project.id) + '":1}'
  200. res = self.client.put(url, data, content_type="application/json")
  201. self.assertEqual(res.status_code, 204)
  202. self.assertEqual(UserProjectAlert.objects.all().count(), 1)
  203. self.assertEqual(UserProjectAlert.objects.first().status, 1)
  204. # Set to alert to Off
  205. data = '{"' + str(self.project.id) + '":0}'
  206. res = self.client.put(url, data, content_type="application/json")
  207. self.assertEqual(res.status_code, 204)
  208. self.assertEqual(UserProjectAlert.objects.first().status, 0)
  209. # Set to alert to "default"
  210. data = '{"' + str(self.project.id) + '":-1}'
  211. res = self.client.put(url, data, content_type="application/json")
  212. self.assertEqual(res.status_code, 204)
  213. # Default deletes the row
  214. self.assertEqual(UserProjectAlert.objects.all().count(), 0)
  215. def test_reset_password(self):
  216. """
  217. Social accounts weren't getting reset password emails. This
  218. approximates the issue by testing an account that has an
  219. unusable password.
  220. """
  221. url = reverse("rest_password_reset")
  222. # Normal behavior
  223. self.client.post(url, {"email": self.user.email})
  224. self.assertEqual(len(mail.outbox), 1)
  225. user_without_password = baker.make("users.User")
  226. user_without_password.set_unusable_password()
  227. user_without_password.save()
  228. self.assertFalse(user_without_password.has_usable_password())
  229. self.client.post(url, {"email": user_without_password.email})
  230. self.assertEqual(len(mail.outbox), 2)