test.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. from django.core import mail
  2. from django.test import override_settings
  3. from django.shortcuts import reverse
  4. from rest_framework.test import APITestCase
  5. from model_bakery import baker
  6. from glitchtip import test_utils # pylint: disable=unused-import
  7. from glitchtip.test_utils.test_case import GlitchTipTestCase
  8. from organizations_ext.models import OrganizationUserRole
  9. from ..models import UserProjectAlert, 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_user_and_project()
  54. def test_list(self):
  55. url = reverse("user-list")
  56. res = self.client.get(url)
  57. self.assertContains(res, self.user.email)
  58. def test_retrieve(self):
  59. url = reverse("user-detail", args=["me"])
  60. res = self.client.get(url)
  61. self.assertContains(res, self.user.email)
  62. def test_destroy(self):
  63. other_user = baker.make("users.user")
  64. url = reverse("user-detail", args=[other_user.pk])
  65. res = self.client.delete(url)
  66. self.assertEqual(
  67. res.status_code, 404, "User should not be able to delete other users"
  68. )
  69. url = reverse("user-detail", args=[self.user.pk])
  70. res = self.client.delete(url)
  71. self.assertEqual(res.status_code, 204)
  72. self.assertFalse(User.objects.filter(pk=self.user.pk).exists())
  73. def test_organization_members_list(self):
  74. other_user = baker.make("users.user")
  75. other_organization = baker.make("organizations_ext.Organization")
  76. other_organization.add_user(other_user, OrganizationUserRole.ADMIN)
  77. user2 = baker.make("users.User")
  78. self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  79. url = reverse("organization-members-list", args=[self.organization.slug])
  80. res = self.client.get(url)
  81. self.assertContains(res, user2.email)
  82. self.assertNotContains(res, other_user.email)
  83. # Can't view members of groups you don't belong to
  84. url = reverse("organization-members-list", args=[other_organization.slug])
  85. res = self.client.get(url)
  86. self.assertNotContains(res, other_user.email)
  87. def test_emails_retrieve(self):
  88. email_address = baker.make("account.EmailAddress", user=self.user)
  89. another_user = baker.make("users.user")
  90. another_email_address = baker.make("account.EmailAddress", user=another_user)
  91. url = reverse("user-emails-list", args=["me"])
  92. res = self.client.get(url)
  93. self.assertContains(res, email_address.email)
  94. self.assertNotContains(res, another_email_address.email)
  95. def test_emails_confirm(self):
  96. email_address = baker.make("account.EmailAddress", user=self.user)
  97. url = reverse("user-emails-list", args=["me"]) + "confirm/"
  98. data = {"email": email_address.email}
  99. res = self.client.post(url, data)
  100. self.assertEqual(res.status_code, 204)
  101. self.assertEqual(len(mail.outbox), 1)
  102. def test_emails_create(self):
  103. url = reverse("user-emails-list", args=["me"])
  104. new_email = "new@exmaple.com"
  105. data = {"email": new_email}
  106. res = self.client.post(url, data)
  107. self.assertContains(res, new_email, status_code=201)
  108. self.assertTrue(
  109. self.user.emailaddress_set.filter(email=new_email, verified=False).exists()
  110. )
  111. self.assertEqual(len(mail.outbox), 1)
  112. # Ensure token is valid and can verify email
  113. body = mail.outbox[0].body
  114. key = body[body.find("confirm-email") :].split("/")[1]
  115. url = reverse("rest_verify_email")
  116. data = {"key": key}
  117. res = self.client.post(url, data)
  118. self.assertTrue(
  119. self.user.emailaddress_set.filter(email=new_email, verified=True).exists()
  120. )
  121. def test_emails_create_dupe_email(self):
  122. url = reverse("user-emails-list", args=["me"])
  123. email_address = baker.make("account.EmailAddress", user=self.user)
  124. data = {"email": email_address.email}
  125. res = self.client.post(url, data)
  126. self.assertContains(res, "this account", status_code=400)
  127. def test_emails_create_dupe_email_other_user(self):
  128. url = reverse("user-emails-list", args=["me"])
  129. email_address = baker.make("account.EmailAddress")
  130. data = {"email": email_address.email}
  131. res = self.client.post(url, data)
  132. self.assertContains(res, "another account", status_code=400)
  133. def test_emails_set_primary(self):
  134. url = reverse("user-emails-list", args=["me"])
  135. email_address = baker.make(
  136. "account.EmailAddress", verified=True, user=self.user
  137. )
  138. data = {"email": email_address.email}
  139. res = self.client.put(url, data)
  140. self.assertContains(res, email_address.email, status_code=200)
  141. self.assertTrue(
  142. self.user.emailaddress_set.filter(
  143. email=email_address.email, primary=True
  144. ).exists()
  145. )
  146. extra_email = baker.make("account.EmailAddress", verified=True, user=self.user)
  147. data = {"email": extra_email.email}
  148. res = self.client.put(url, data)
  149. self.assertEqual(self.user.emailaddress_set.filter(primary=True).count(), 1)
  150. def test_emails_destroy(self):
  151. url = reverse("user-emails-list", args=["me"])
  152. email_address = baker.make(
  153. "account.EmailAddress", verified=True, primary=False, user=self.user
  154. )
  155. data = {"email": email_address.email}
  156. res = self.client.delete(url, data)
  157. self.assertEqual(res.status_code, 204)
  158. self.assertFalse(
  159. self.user.emailaddress_set.filter(email=email_address.email).exists()
  160. )
  161. def test_notifications_retrieve(self):
  162. url = reverse("user-detail", args=["me"]) + "notifications/"
  163. res = self.client.get(url)
  164. self.assertContains(res, "subscribeByDefault")
  165. def test_notifications_update(self):
  166. url = reverse("user-detail", args=["me"]) + "notifications/"
  167. data = {"subscribeByDefault": False}
  168. res = self.client.put(url, data)
  169. self.assertFalse(res.data.get("subscribeByDefault"))
  170. self.user.refresh_from_db()
  171. self.assertFalse(self.user.subscribe_by_default)
  172. def test_alerts_retrieve(self):
  173. url = reverse("user-detail", args=["me"]) + "notifications/alerts/"
  174. alert = baker.make(
  175. "users.UserProjectAlert", user=self.user, project=self.project
  176. )
  177. res = self.client.get(url)
  178. self.assertContains(res, self.project.id)
  179. self.assertEqual(res.data[self.project.id], alert.status)
  180. def test_alerts_update(self):
  181. url = reverse("user-detail", args=["me"]) + "notifications/alerts/"
  182. # Set to alert to On
  183. data = '{"' + str(self.project.id) + '":1}'
  184. res = self.client.put(url, data, content_type="application/json")
  185. self.assertEqual(res.status_code, 204)
  186. self.assertEqual(UserProjectAlert.objects.all().count(), 1)
  187. self.assertEqual(UserProjectAlert.objects.first().status, 1)
  188. # Set to alert to Off
  189. data = '{"' + str(self.project.id) + '":0}'
  190. res = self.client.put(url, data, content_type="application/json")
  191. self.assertEqual(res.status_code, 204)
  192. self.assertEqual(UserProjectAlert.objects.first().status, 0)
  193. # Set to alert to "default"
  194. data = '{"' + str(self.project.id) + '":-1}'
  195. res = self.client.put(url, data, content_type="application/json")
  196. self.assertEqual(res.status_code, 204)
  197. # Default deletes the row
  198. self.assertEqual(UserProjectAlert.objects.all().count(), 0)
  199. def test_reset_password(self):
  200. url = reverse("rest_password_reset")
  201. # Normal behavior
  202. res = self.client.post(url, {"email": self.user.email})
  203. self.assertEqual(len(mail.outbox), 1)
  204. """
  205. Social accounts weren't getting reset password emails. This
  206. approximates the issue by testing an account that has an
  207. unusable password.
  208. """
  209. user_without_password = baker.make("users.User")
  210. user_without_password.set_unusable_password()
  211. user_without_password.save()
  212. self.assertFalse(user_without_password.has_usable_password())
  213. res = self.client.post(url, {"email": user_without_password.email})
  214. self.assertEqual(len(mail.outbox), 2)