test_views.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from django.shortcuts import reverse
  2. from model_bakery import baker
  3. from glitchtip.test_utils.test_case import GlitchTipTestCase
  4. from organizations_ext.models import OrganizationUserRole
  5. from ..models import ProjectAlert
  6. class AlertAPITestCase(GlitchTipTestCase):
  7. def setUp(self):
  8. self.create_user_and_project()
  9. def test_project_alerts_retrieve(self):
  10. alert = baker.make(
  11. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  12. )
  13. url = reverse(
  14. "project-alerts-list",
  15. kwargs={
  16. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  17. },
  18. )
  19. res = self.client.get(url)
  20. self.assertContains(res, alert.timespan_minutes)
  21. def test_retrieve_with_second_team(self):
  22. baker.make("alerts.ProjectAlert", project=self.project, timespan_minutes=60)
  23. url = reverse(
  24. "project-alerts-list",
  25. kwargs={
  26. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  27. },
  28. )
  29. team2 = baker.make("teams.Team", organization=self.organization)
  30. team2.members.add(self.org_user)
  31. self.project.team_set.add(team2)
  32. res = self.client.get(url)
  33. self.assertEqual(len(res.json()), 1)
  34. def test_project_alerts_create(self):
  35. url = reverse(
  36. "project-alerts-list",
  37. kwargs={
  38. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  39. },
  40. )
  41. data = {
  42. "name": "foo",
  43. "timespan_minutes": 60,
  44. "quantity": 2,
  45. "uptime": True,
  46. "alertRecipients": [{"recipientType": "email", "url": "example.com"}],
  47. }
  48. res = self.client.post(url, data)
  49. self.assertEqual(res.status_code, 201)
  50. project_alert = ProjectAlert.objects.filter(name="foo", uptime=True).first()
  51. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  52. self.assertEqual(project_alert.project, self.project)
  53. def test_project_alerts_create_permissions(self):
  54. user = baker.make("users.user")
  55. org_user = self.organization.add_user(user, OrganizationUserRole.MEMBER)
  56. self.client.force_login(user)
  57. url = reverse(
  58. "project-alerts-list",
  59. kwargs={
  60. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  61. },
  62. )
  63. data = {
  64. "name": "foo",
  65. "timespan_minutes": 60,
  66. "quantity": 2,
  67. "uptime": True,
  68. "alertRecipients": [{"recipientType": "email", "url": "example.com"}],
  69. }
  70. res = self.client.post(url, data)
  71. # Member without project team membership cannot create alerts
  72. self.assertEqual(res.status_code, 400)
  73. org_user.role = OrganizationUserRole.ADMIN
  74. org_user.save()
  75. res = self.client.post(url, data)
  76. self.assertEqual(res.status_code, 201)
  77. org_user.role = OrganizationUserRole.MEMBER
  78. org_user.save()
  79. res = self.client.get(url)
  80. # Members can still view alerts
  81. self.assertEqual(len(res.data), 1)
  82. def test_create_with_second_team(self):
  83. team2 = baker.make("teams.Team", organization=self.organization)
  84. team2.members.add(self.org_user)
  85. self.project.team_set.add(team2)
  86. url = reverse(
  87. "project-alerts-list",
  88. kwargs={
  89. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  90. },
  91. )
  92. data = {
  93. "name": "foo",
  94. "timespan_minutes": 60,
  95. "quantity": 2,
  96. "uptime": True,
  97. "alertRecipients": [{"recipientType": "email", "url": "example.com"}],
  98. }
  99. res = self.client.post(url, data)
  100. self.assertEqual(res.status_code, 201)
  101. project_alert = ProjectAlert.objects.filter(name="foo", uptime=True).first()
  102. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  103. self.assertEqual(project_alert.project, self.project)
  104. def test_project_alerts_update(self):
  105. alert = baker.make(
  106. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  107. )
  108. url = reverse(
  109. "project-alerts-detail",
  110. kwargs={
  111. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  112. "pk": alert.pk,
  113. },
  114. )
  115. # Test put
  116. data = {
  117. "timespan_minutes": 500,
  118. "quantity": 2,
  119. "alertRecipients": [
  120. {"recipientType": "email", "url": "https://example.com"}
  121. ],
  122. }
  123. res = self.client.put(url, data, format="json")
  124. self.assertEqual(res.status_code, 200)
  125. project_alert = ProjectAlert.objects.all().first()
  126. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  127. # Test patch
  128. data = {"timespan_minutes": 30}
  129. res = self.client.patch(url, data)
  130. self.assertEqual(res.status_code, 200)
  131. project_alert.refresh_from_db()
  132. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  133. self.assertEqual(project_alert.quantity, 2)
  134. def test_project_alerts_update_auth(self):
  135. """Cannot update alert on project that user does not belong to"""
  136. alert = baker.make("alerts.ProjectAlert", timespan_minutes=60)
  137. url = reverse(
  138. "project-alerts-detail",
  139. kwargs={
  140. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  141. "pk": alert.pk,
  142. },
  143. )
  144. data = {"timespan_minutes": 500, "quantity": 2}
  145. res = self.client.put(url, data)
  146. self.assertEqual(res.status_code, 404)
  147. def test_project_alerts_delete(self):
  148. alert = baker.make(
  149. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  150. )
  151. url = reverse(
  152. "project-alerts-detail",
  153. kwargs={
  154. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  155. "pk": alert.pk,
  156. },
  157. )
  158. res = self.client.delete(url)
  159. self.assertEqual(res.status_code, 204)
  160. self.assertEqual(ProjectAlert.objects.count(), 0)
  161. def test_delete_with_second_team(self):
  162. alert = baker.make(
  163. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  164. )
  165. url = reverse(
  166. "project-alerts-detail",
  167. kwargs={
  168. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  169. "pk": alert.pk,
  170. },
  171. )
  172. team2 = baker.make("teams.Team", organization=self.organization)
  173. team2.members.add(self.org_user)
  174. self.project.team_set.add(team2)
  175. res = self.client.delete(url)
  176. self.assertEqual(res.status_code, 204)
  177. self.assertEqual(ProjectAlert.objects.count(), 0)