tests.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_event_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_event_alerts()
  30. self.assertEqual(Notification.objects.count(), 0)
  31. baker.make("events.Event", issue=issue, _quantity=9)
  32. process_event_alerts()
  33. self.assertEqual(Notification.objects.count(), 1)
  34. # Notifications have a cooldown time equal to alert timespan
  35. process_event_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_event_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_event_alerts()
  58. with freeze_time(self.now + timedelta(minutes=11)):
  59. baker.make("events.Event", issue=issue, _quantity=4)
  60. process_event_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_event_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_event_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_event_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_event_alerts()
  101. self.assertEqual(len(mail.outbox), 2)
  102. def test_alert_subscription_default_scope(self):
  103. """ Subscribe by default should not result in alert emails for non-team members """
  104. baker.make(
  105. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  106. )
  107. # user2 is an org member but not in a relevant team, should not receive alerts
  108. user2 = baker.make("users.user")
  109. org_user2 = self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  110. team2 = baker.make("teams.Team", organization=self.organization)
  111. team2.members.add(org_user2)
  112. # user3 is in team3 which should receive alerts
  113. user3 = baker.make("users.user")
  114. org_user3 = self.organization.add_user(user3, OrganizationUserRole.MEMBER)
  115. self.team.members.add(org_user3)
  116. team3 = baker.make("teams.Team", organization=self.organization)
  117. team3.members.add(org_user3)
  118. team3.projects.add(self.project)
  119. baker.make("events.Event", issue__project=self.project)
  120. process_event_alerts()
  121. self.assertNotIn(user2.email, mail.outbox[0].to)
  122. self.assertIn(user3.email, mail.outbox[0].to)
  123. self.assertEqual(len(mail.outbox[0].to), 2) # Ensure no duplicate emails
  124. class AlertWithUserProjectAlert(GlitchTipTestCase):
  125. def setUp(self):
  126. self.create_user_and_project()
  127. self.now = timezone.now()
  128. def test_alert_enabled_user_project_alert_disabled(self):
  129. """ A user should be able to disable their own notifications """
  130. baker.make(
  131. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  132. )
  133. baker.make(
  134. "users.UserProjectAlert",
  135. user=self.user,
  136. project=self.project,
  137. status=ProjectAlertStatus.OFF,
  138. )
  139. user2 = baker.make("users.user")
  140. org_user2 = self.organization.add_user(user2, OrganizationUserRole.ADMIN)
  141. self.team.members.add(org_user2)
  142. baker.make(
  143. "users.UserProjectAlert", user=user2, status=ProjectAlertStatus.ON,
  144. )
  145. user3 = baker.make("users.user")
  146. org_user3 = self.organization.add_user(user3, OrganizationUserRole.ADMIN)
  147. self.team.members.add(org_user3)
  148. baker.make(
  149. "users.UserProjectAlert",
  150. user=user3,
  151. project=self.project,
  152. status=ProjectAlertStatus.ON,
  153. )
  154. baker.make("events.Event", issue__project=self.project)
  155. process_event_alerts()
  156. self.assertEqual(len(mail.outbox), 1)
  157. self.assertEqual(len(mail.outbox[0].merge_data), 2)
  158. def test_alert_enabled_subscribe_by_default(self):
  159. self.user.subscribe_by_default = False
  160. self.user.save()
  161. baker.make(
  162. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  163. )
  164. baker.make("events.Event", issue__project=self.project)
  165. process_event_alerts()
  166. self.assertEqual(len(mail.outbox), 0)
  167. def test_alert_enabled_subscribe_by_default_override_false(self):
  168. self.user.subscribe_by_default = False
  169. self.user.save()
  170. baker.make(
  171. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  172. )
  173. baker.make(
  174. "users.UserProjectAlert",
  175. user=self.user,
  176. project=self.project,
  177. status=ProjectAlertStatus.ON,
  178. )
  179. baker.make("events.Event", issue__project=self.project)
  180. process_event_alerts()
  181. self.assertEqual(len(mail.outbox), 1)
  182. def test_user_project_alert_scope(self):
  183. """ User project alert should not result in alert emails for non-team members """
  184. baker.make(
  185. "alerts.ProjectAlert", project=self.project, timespan_minutes=1, quantity=1,
  186. )
  187. user2 = baker.make("users.user")
  188. self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  189. baker.make(
  190. "users.UserProjectAlert",
  191. user=user2,
  192. project=self.project,
  193. status=ProjectAlertStatus.ON,
  194. )
  195. baker.make("events.Event", issue__project=self.project)
  196. process_event_alerts()
  197. self.assertNotIn(user2.email, mail.outbox[0].to)