test.py 9.8 KB

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