test_views.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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={"project_pk": f"{self.organization.slug}/{self.project.slug}",},
  16. )
  17. res = self.client.get(url)
  18. self.assertContains(res, alert.timespan_minutes)
  19. def test_retrieve_with_second_team(self):
  20. baker.make(
  21. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  22. )
  23. url = reverse(
  24. "project-alerts-list",
  25. kwargs={"project_pk": f"{self.organization.slug}/{self.project.slug}",},
  26. )
  27. team2 = baker.make("teams.Team", organization=self.organization)
  28. team2.members.add(self.org_user)
  29. self.project.team_set.add(team2)
  30. res = self.client.get(url)
  31. self.assertEqual(len(res.json()), 1)
  32. def test_project_alerts_create(self):
  33. url = reverse(
  34. "project-alerts-list",
  35. kwargs={"project_pk": f"{self.organization.slug}/{self.project.slug}",},
  36. )
  37. data = {
  38. "name": "foo",
  39. "timespan_minutes": 60,
  40. "quantity": 2,
  41. "uptime": True,
  42. "alertRecipients": [{"recipientType": "email", "url": "example.com"}],
  43. }
  44. res = self.client.post(url, data)
  45. self.assertEqual(res.status_code, 201)
  46. project_alert = ProjectAlert.objects.filter(name="foo", uptime=True).first()
  47. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  48. self.assertEqual(project_alert.project, self.project)
  49. def test_create_with_second_team(self):
  50. team2 = baker.make("teams.Team", organization=self.organization)
  51. team2.members.add(self.org_user)
  52. self.project.team_set.add(team2)
  53. url = reverse(
  54. "project-alerts-list",
  55. kwargs={"project_pk": f"{self.organization.slug}/{self.project.slug}",},
  56. )
  57. data = {
  58. "name": "foo",
  59. "timespan_minutes": 60,
  60. "quantity": 2,
  61. "uptime": True,
  62. "alertRecipients": [{"recipientType": "email", "url": "example.com"}],
  63. }
  64. res = self.client.post(url, data)
  65. self.assertEqual(res.status_code, 201)
  66. project_alert = ProjectAlert.objects.filter(name="foo", uptime=True).first()
  67. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  68. self.assertEqual(project_alert.project, self.project)
  69. def test_project_alerts_update(self):
  70. alert = baker.make(
  71. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  72. )
  73. url = reverse(
  74. "project-alerts-detail",
  75. kwargs={
  76. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  77. "pk": alert.pk,
  78. },
  79. )
  80. # Test put
  81. data = {
  82. "timespan_minutes": 500,
  83. "quantity": 2,
  84. "alertRecipients": [
  85. {"recipientType": "email", "url": "https://example.com"}
  86. ],
  87. }
  88. res = self.client.put(url, data, format="json")
  89. self.assertEqual(res.status_code, 200)
  90. project_alert = ProjectAlert.objects.all().first()
  91. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  92. # Test patch
  93. data = {"timespan_minutes": 30}
  94. res = self.client.patch(url, data)
  95. self.assertEqual(res.status_code, 200)
  96. project_alert.refresh_from_db()
  97. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  98. self.assertEqual(project_alert.quantity, 2)
  99. def test_project_alerts_update_auth(self):
  100. """ Cannot update alert on project that user does not belong to """
  101. alert = baker.make("alerts.ProjectAlert", timespan_minutes=60)
  102. url = reverse(
  103. "project-alerts-detail",
  104. kwargs={
  105. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  106. "pk": alert.pk,
  107. },
  108. )
  109. data = {"timespan_minutes": 500, "quantity": 2}
  110. res = self.client.put(url, data)
  111. self.assertEqual(res.status_code, 404)
  112. def test_project_alerts_delete(self):
  113. alert = baker.make(
  114. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  115. )
  116. url = reverse(
  117. "project-alerts-detail",
  118. kwargs={
  119. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  120. "pk": alert.pk,
  121. },
  122. )
  123. res = self.client.delete(url)
  124. self.assertEqual(res.status_code, 204)
  125. self.assertEqual(ProjectAlert.objects.count(), 0)
  126. def test_delete_with_second_team(self):
  127. alert = baker.make(
  128. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  129. )
  130. url = reverse(
  131. "project-alerts-detail",
  132. kwargs={
  133. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  134. "pk": alert.pk,
  135. },
  136. )
  137. team2 = baker.make("teams.Team", organization=self.organization)
  138. team2.members.add(self.org_user)
  139. self.project.team_set.add(team2)
  140. res = self.client.delete(url)
  141. self.assertEqual(res.status_code, 204)
  142. self.assertEqual(ProjectAlert.objects.count(), 0)