test_views.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from django.shortcuts import reverse
  2. from model_bakery import baker
  3. from glitchtip import test_utils # pylint: disable=unused-import
  4. from glitchtip.test_utils.test_case import GlitchTipTestCase
  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_create_with_second_team(self):
  54. team2 = baker.make("teams.Team", organization=self.organization)
  55. team2.members.add(self.org_user)
  56. self.project.team_set.add(team2)
  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. self.assertEqual(res.status_code, 201)
  72. project_alert = ProjectAlert.objects.filter(name="foo", uptime=True).first()
  73. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  74. self.assertEqual(project_alert.project, self.project)
  75. def test_project_alerts_update(self):
  76. alert = baker.make(
  77. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  78. )
  79. url = reverse(
  80. "project-alerts-detail",
  81. kwargs={
  82. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  83. "pk": alert.pk,
  84. },
  85. )
  86. # Test put
  87. data = {
  88. "timespan_minutes": 500,
  89. "quantity": 2,
  90. "alertRecipients": [
  91. {"recipientType": "email", "url": "https://example.com"}
  92. ],
  93. }
  94. res = self.client.put(url, data, format="json")
  95. self.assertEqual(res.status_code, 200)
  96. project_alert = ProjectAlert.objects.all().first()
  97. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  98. # Test patch
  99. data = {"timespan_minutes": 30}
  100. res = self.client.patch(url, data)
  101. self.assertEqual(res.status_code, 200)
  102. project_alert.refresh_from_db()
  103. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  104. self.assertEqual(project_alert.quantity, 2)
  105. def test_project_alerts_update_auth(self):
  106. """Cannot update alert on project that user does not belong to"""
  107. alert = baker.make("alerts.ProjectAlert", timespan_minutes=60)
  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. data = {"timespan_minutes": 500, "quantity": 2}
  116. res = self.client.put(url, data)
  117. self.assertEqual(res.status_code, 404)
  118. def test_project_alerts_delete(self):
  119. alert = baker.make(
  120. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  121. )
  122. url = reverse(
  123. "project-alerts-detail",
  124. kwargs={
  125. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  126. "pk": alert.pk,
  127. },
  128. )
  129. res = self.client.delete(url)
  130. self.assertEqual(res.status_code, 204)
  131. self.assertEqual(ProjectAlert.objects.count(), 0)
  132. def test_delete_with_second_team(self):
  133. alert = baker.make(
  134. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  135. )
  136. url = reverse(
  137. "project-alerts-detail",
  138. kwargs={
  139. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  140. "pk": alert.pk,
  141. },
  142. )
  143. team2 = baker.make("teams.Team", organization=self.organization)
  144. team2.members.add(self.org_user)
  145. self.project.team_set.add(team2)
  146. res = self.client.delete(url)
  147. self.assertEqual(res.status_code, 204)
  148. self.assertEqual(ProjectAlert.objects.count(), 0)