tests.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 projects.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",
  72. project=self.project,
  73. timespan_minutes=1,
  74. quantity=1,
  75. )
  76. issue = baker.make("issues.Issue", project=self.project)
  77. baker.make("events.Event", issue=issue)
  78. process_event_alerts()
  79. self.assertEqual(Notification.objects.count(), 1)
  80. def test_alert_on_regression(self):
  81. baker.make(
  82. "alerts.ProjectAlert",
  83. project=self.project,
  84. timespan_minutes=1,
  85. quantity=1,
  86. )
  87. # Make event
  88. with open(
  89. "events/test_data/incoming_events/very_small_event.json"
  90. ) as json_file:
  91. data = json.load(json_file)
  92. projectkey = self.project.projectkey_set.first()
  93. params = f"?sentry_key={projectkey.public_key}"
  94. url = reverse("event_store", args=[self.project.id]) + params
  95. self.client.post(url, data, format="json")
  96. # First alert
  97. process_event_alerts()
  98. self.assertEqual(len(mail.outbox), 1)
  99. # Mark resolved
  100. issue = Issue.objects.first()
  101. issue.status = EventStatus.RESOLVED
  102. issue.save()
  103. # Send a second event
  104. data["event_id"] = "cf536c31b68a473f97e579507ce155e4"
  105. self.client.post(url, data, format="json")
  106. process_event_alerts()
  107. self.assertEqual(len(mail.outbox), 2)
  108. def test_alert_subscription_default_scope(self):
  109. """Subscribe by default should not result in alert emails for non-team members"""
  110. baker.make(
  111. "alerts.ProjectAlert",
  112. project=self.project,
  113. timespan_minutes=1,
  114. quantity=1,
  115. )
  116. # user2 is an org member but not in a relevant team, should not receive alerts
  117. user2 = baker.make("users.user")
  118. org_user2 = self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  119. team2 = baker.make("teams.Team", organization=self.organization)
  120. team2.members.add(org_user2)
  121. # user3 is in team3 which should receive alerts
  122. user3 = baker.make("users.user")
  123. org_user3 = self.organization.add_user(user3, OrganizationUserRole.MEMBER)
  124. self.team.members.add(org_user3)
  125. team3 = baker.make("teams.Team", organization=self.organization)
  126. team3.members.add(org_user3)
  127. team3.projects.add(self.project)
  128. baker.make("events.Event", issue__project=self.project)
  129. process_event_alerts()
  130. self.assertNotIn(user2.email, mail.outbox[0].to)
  131. self.assertIn(user3.email, mail.outbox[0].to)
  132. self.assertEqual(len(mail.outbox[0].to), 2) # Ensure no duplicate emails
  133. class AlertWithUserProjectAlert(GlitchTipTestCase):
  134. def setUp(self):
  135. self.create_user_and_project()
  136. self.now = timezone.now()
  137. def test_alert_enabled_user_project_alert_disabled(self):
  138. """A user should be able to disable their own notifications"""
  139. baker.make(
  140. "alerts.ProjectAlert",
  141. project=self.project,
  142. timespan_minutes=1,
  143. quantity=1,
  144. )
  145. baker.make(
  146. "projects.UserProjectAlert",
  147. user=self.user,
  148. project=self.project,
  149. status=ProjectAlertStatus.OFF,
  150. )
  151. user2 = baker.make("users.user")
  152. org_user2 = self.organization.add_user(user2, OrganizationUserRole.ADMIN)
  153. self.team.members.add(org_user2)
  154. baker.make(
  155. "projects.UserProjectAlert",
  156. user=user2,
  157. status=ProjectAlertStatus.ON,
  158. )
  159. user3 = baker.make("users.user")
  160. org_user3 = self.organization.add_user(user3, OrganizationUserRole.ADMIN)
  161. self.team.members.add(org_user3)
  162. baker.make(
  163. "projects.UserProjectAlert",
  164. user=user3,
  165. project=self.project,
  166. status=ProjectAlertStatus.ON,
  167. )
  168. baker.make("events.Event", issue__project=self.project)
  169. process_event_alerts()
  170. self.assertEqual(len(mail.outbox), 1)
  171. self.assertEqual(len(mail.outbox[0].merge_data), 2)
  172. def test_alert_enabled_subscribe_by_default(self):
  173. self.user.subscribe_by_default = False
  174. self.user.save()
  175. baker.make(
  176. "alerts.ProjectAlert",
  177. project=self.project,
  178. timespan_minutes=1,
  179. quantity=1,
  180. )
  181. baker.make("events.Event", issue__project=self.project)
  182. process_event_alerts()
  183. self.assertEqual(len(mail.outbox), 0)
  184. def test_alert_enabled_subscribe_by_default_override_false(self):
  185. self.user.subscribe_by_default = False
  186. self.user.save()
  187. baker.make(
  188. "alerts.ProjectAlert",
  189. project=self.project,
  190. timespan_minutes=1,
  191. quantity=1,
  192. )
  193. baker.make(
  194. "projects.UserProjectAlert",
  195. user=self.user,
  196. project=self.project,
  197. status=ProjectAlertStatus.ON,
  198. )
  199. baker.make("events.Event", issue__project=self.project)
  200. process_event_alerts()
  201. self.assertEqual(len(mail.outbox), 1)
  202. def test_user_project_alert_scope(self):
  203. """User project alert should not result in alert emails for non-team members"""
  204. baker.make(
  205. "alerts.ProjectAlert",
  206. project=self.project,
  207. timespan_minutes=1,
  208. quantity=1,
  209. )
  210. user2 = baker.make("users.user")
  211. self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  212. baker.make(
  213. "projects.UserProjectAlert",
  214. user=user2,
  215. project=self.project,
  216. status=ProjectAlertStatus.ON,
  217. )
  218. baker.make("events.Event", issue__project=self.project)
  219. process_event_alerts()
  220. self.assertNotIn(user2.email, mail.outbox[0].to)