test_views.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_project_alerts_create(self):
  20. url = reverse(
  21. "project-alerts-list",
  22. kwargs={"project_pk": f"{self.organization.slug}/{self.project.slug}",},
  23. )
  24. data = {
  25. "name": "foo",
  26. "timespan_minutes": 60,
  27. "quantity": 2,
  28. "uptime": True,
  29. "alertRecipients": [{"recipientType": "email", "url": "example.com"}],
  30. }
  31. res = self.client.post(url, data)
  32. self.assertEqual(res.status_code, 201)
  33. project_alert = ProjectAlert.objects.filter(name="foo", uptime=True).first()
  34. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  35. self.assertEqual(project_alert.project, self.project)
  36. def test_project_alerts_update(self):
  37. alert = baker.make(
  38. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  39. )
  40. url = reverse(
  41. "project-alerts-detail",
  42. kwargs={
  43. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  44. "pk": alert.pk,
  45. },
  46. )
  47. # Test put
  48. data = {
  49. "timespan_minutes": 500,
  50. "quantity": 2,
  51. "alertRecipients": [
  52. {"recipientType": "email", "url": "https://example.com"}
  53. ],
  54. }
  55. res = self.client.put(url, data, format="json")
  56. self.assertEqual(res.status_code, 200)
  57. project_alert = ProjectAlert.objects.all().first()
  58. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  59. # Test patch
  60. data = {"timespan_minutes": 30}
  61. res = self.client.patch(url, data)
  62. self.assertEqual(res.status_code, 200)
  63. project_alert.refresh_from_db()
  64. self.assertEqual(project_alert.timespan_minutes, data["timespan_minutes"])
  65. self.assertEqual(project_alert.quantity, 2)
  66. def test_project_alerts_update_auth(self):
  67. """ Cannot update alert on project that user does not belong to """
  68. alert = baker.make("alerts.ProjectAlert", timespan_minutes=60)
  69. url = reverse(
  70. "project-alerts-detail",
  71. kwargs={
  72. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  73. "pk": alert.pk,
  74. },
  75. )
  76. data = {"timespan_minutes": 500, "quantity": 2}
  77. res = self.client.put(url, data)
  78. self.assertEqual(res.status_code, 404)
  79. def test_project_alerts_delete(self):
  80. alert = baker.make(
  81. "alerts.ProjectAlert", project=self.project, timespan_minutes=60
  82. )
  83. url = reverse(
  84. "project-alerts-detail",
  85. kwargs={
  86. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  87. "pk": alert.pk,
  88. },
  89. )
  90. res = self.client.delete(url)
  91. self.assertEqual(res.status_code, 204)
  92. self.assertEqual(ProjectAlert.objects.count(), 0)