test.py 9.9 KB

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