tests.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import json
  2. from datetime import timedelta
  3. from django.core import mail
  4. from django.shortcuts import reverse
  5. from django.utils import timezone
  6. from freezegun import freeze_time
  7. from model_bakery import baker
  8. from glitchtip import test_utils # pylint: disable=unused-import
  9. from glitchtip.test_utils.test_case import GlitchTipTestCase
  10. from issues.models import EventStatus, Issue
  11. from organizations_ext.models import OrganizationUserRole
  12. from users.models import ProjectAlertStatus
  13. from ..models import Notification
  14. from ..tasks import process_alerts
  15. class AlertTestCase(GlitchTipTestCase):
  16. def setUp(self):
  17. self.create_user_and_project()
  18. self.now = timezone.now()
  19. def test_alerts(self):
  20. baker.make(
  21. "alerts.ProjectAlert",
  22. project=self.project,
  23. timespan_minutes=10,
  24. quantity=10,
  25. )
  26. issue = baker.make("issues.Issue", project=self.project)
  27. baker.make("events.Event", issue=issue)
  28. # Not sufficient events to create alert
  29. process_alerts()
  30. self.assertEqual(Notification.objects.count(), 0)
  31. baker.make("events.Event", issue=issue, _quantity=9)
  32. process_alerts()
  33. self.assertEqual(Notification.objects.count(), 1)
  34. # Notifications have a cooldown time equal to alert timespan
  35. process_alerts()
  36. self.assertEqual(Notification.objects.count(), 1)
  37. # Notifications should not happen again for same issue
  38. with freeze_time(self.now + timedelta(minutes=11)):
  39. baker.make("events.Event", issue=issue, _quantity=10)
  40. process_alerts()
  41. self.assertEqual(Notification.objects.count(), 1)
  42. self.assertEqual(len(mail.outbox), 1)
  43. def test_alert_timing(self):
  44. baker.make(
  45. "alerts.ProjectAlert",
  46. project=self.project,
  47. timespan_minutes=10,
  48. quantity=10,
  49. )
  50. issue = baker.make("issues.Issue", project=self.project)
  51. # time 0: 4 events
  52. # time 5: 4 more events (8 total)
  53. # time 11: 4 more events (12 total)
  54. baker.make("events.Event", issue=issue, _quantity=4)
  55. with freeze_time(self.now + timedelta(minutes=5)):
  56. baker.make("events.Event", issue=issue, _quantity=4)
  57. process_alerts()
  58. with freeze_time(self.now + timedelta(minutes=11)):
  59. baker.make("events.Event", issue=issue, _quantity=4)
  60. process_alerts()
  61. # Not sufficient rate of events to trigger alert.
  62. self.assertEqual(Notification.objects.count(), 0)
  63. # time 12: 4 more events (16 total, 12 in past 10 minutes)
  64. with freeze_time(self.now + timedelta(minutes=12)):
  65. baker.make("events.Event", issue=issue, _quantity=4)
  66. process_alerts()
  67. self.assertEqual(Notification.objects.count(), 1)
  68. def test_alert_one_event(self):
  69. """ Use same logic to send alert for every new issue """
  70. baker.make(
  71. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  72. )
  73. issue = baker.make("issues.Issue", project=self.project)
  74. baker.make("events.Event", issue=issue)
  75. process_alerts()
  76. self.assertEqual(Notification.objects.count(), 1)
  77. def test_alert_on_regression(self):
  78. baker.make(
  79. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  80. )
  81. # Make event
  82. with open(
  83. "events/test_data/incoming_events/very_small_event.json"
  84. ) as json_file:
  85. data = json.load(json_file)
  86. projectkey = self.project.projectkey_set.first()
  87. params = f"?sentry_key={projectkey.public_key}"
  88. url = reverse("event_store", args=[self.project.id]) + params
  89. self.client.post(url, data, format="json")
  90. # First alert
  91. process_alerts()
  92. self.assertEqual(len(mail.outbox), 1)
  93. # Mark resolved
  94. issue = Issue.objects.first()
  95. issue.status = EventStatus.RESOLVED
  96. issue.save()
  97. # Send a second event
  98. data["event_id"] = "cf536c31b68a473f97e579507ce155e4"
  99. self.client.post(url, data, format="json")
  100. process_alerts()
  101. self.assertEqual(len(mail.outbox), 2)
  102. class AlertWithUserProjectAlert(GlitchTipTestCase):
  103. def setUp(self):
  104. self.create_user_and_project()
  105. self.now = timezone.now()
  106. def test_alert_enabled_user_project_alert_disabled(self):
  107. """ A user should be able to disable their own notifications """
  108. baker.make(
  109. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  110. )
  111. baker.make(
  112. "users.UserProjectAlert",
  113. user=self.user,
  114. project=self.project,
  115. status=ProjectAlertStatus.OFF,
  116. )
  117. user2 = baker.make("users.user")
  118. org_user2 = self.organization.add_user(user2, OrganizationUserRole.ADMIN)
  119. self.team.members.add(org_user2)
  120. baker.make(
  121. "users.UserProjectAlert", user=user2, status=ProjectAlertStatus.ON,
  122. )
  123. user3 = baker.make("users.user")
  124. org_user3 = self.organization.add_user(user3, OrganizationUserRole.ADMIN)
  125. self.team.members.add(org_user3)
  126. baker.make(
  127. "users.UserProjectAlert",
  128. user=user3,
  129. project=self.project,
  130. status=ProjectAlertStatus.ON,
  131. )
  132. baker.make("events.Event", issue__project=self.project)
  133. process_alerts()
  134. self.assertEqual(len(mail.outbox), 1)
  135. self.assertEqual(len(mail.outbox[0].merge_data), 2)
  136. def test_alert_enabled_subscribe_by_default(self):
  137. self.user.subscribe_by_default = False
  138. self.user.save()
  139. baker.make(
  140. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  141. )
  142. baker.make("events.Event", issue__project=self.project)
  143. process_alerts()
  144. self.assertEqual(len(mail.outbox), 0)
  145. def test_alert_enabled_subscribe_by_default_override_false(self):
  146. self.user.subscribe_by_default = False
  147. self.user.save()
  148. baker.make(
  149. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  150. )
  151. baker.make(
  152. "users.UserProjectAlert",
  153. user=self.user,
  154. project=self.project,
  155. status=ProjectAlertStatus.ON,
  156. )
  157. baker.make("events.Event", issue__project=self.project)
  158. process_alerts()
  159. self.assertEqual(len(mail.outbox), 1)