test_tasks.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.test_utils.test_case import GlitchTipTestCase
  9. from issues.models import EventStatus, Issue
  10. from organizations_ext.models import OrganizationUserRole
  11. from projects.models import ProjectAlertStatus
  12. from ..models import Notification
  13. from ..tasks import process_event_alerts
  14. class AlertTestCase(GlitchTipTestCase):
  15. def setUp(self):
  16. self.create_user_and_project()
  17. self.now = timezone.now()
  18. def test_alerts(self):
  19. baker.make(
  20. "alerts.ProjectAlert",
  21. project=self.project,
  22. timespan_minutes=10,
  23. quantity=10,
  24. )
  25. issue = baker.make("issues.Issue", project=self.project)
  26. baker.make("events.Event", issue=issue)
  27. # Not sufficient events to create alert
  28. process_event_alerts()
  29. self.assertEqual(Notification.objects.count(), 0)
  30. baker.make("events.Event", issue=issue, _quantity=9)
  31. process_event_alerts()
  32. self.assertEqual(Notification.objects.count(), 1)
  33. # Notifications have a cooldown time equal to alert timespan
  34. process_event_alerts()
  35. self.assertEqual(Notification.objects.count(), 1)
  36. # Notifications should not happen again for same issue
  37. with freeze_time(self.now + timedelta(minutes=11)):
  38. baker.make("events.Event", issue=issue, _quantity=10)
  39. process_event_alerts()
  40. self.assertEqual(Notification.objects.count(), 1)
  41. self.assertEqual(len(mail.outbox), 1)
  42. def test_multiple_alerts(self):
  43. baker.make(
  44. "alerts.ProjectAlert",
  45. project=self.project,
  46. timespan_minutes=10,
  47. quantity=1,
  48. )
  49. baker.make(
  50. "alerts.ProjectAlert",
  51. project=self.project,
  52. timespan_minutes=10,
  53. quantity=2,
  54. )
  55. issue1 = baker.make("issues.Issue", project=self.project)
  56. baker.make("events.Event", issue=issue1)
  57. process_event_alerts()
  58. self.assertEqual(Notification.objects.count(), 1)
  59. issue2 = baker.make("issues.Issue", project=self.project)
  60. baker.make("events.Event", issue=issue2, _quantity=2)
  61. process_event_alerts()
  62. # Trigger both alerts, as both meet criteria, total of 3
  63. self.assertEqual(Notification.objects.count(), 3)
  64. def test_alert_timing(self):
  65. baker.make(
  66. "alerts.ProjectAlert",
  67. project=self.project,
  68. timespan_minutes=10,
  69. quantity=10,
  70. )
  71. issue = baker.make("issues.Issue", project=self.project)
  72. # time 0: 4 events
  73. # time 5: 4 more events (8 total)
  74. # time 11: 4 more events (12 total)
  75. baker.make("events.Event", issue=issue, _quantity=4)
  76. with freeze_time(self.now + timedelta(minutes=5)):
  77. baker.make("events.Event", issue=issue, _quantity=4)
  78. process_event_alerts()
  79. with freeze_time(self.now + timedelta(minutes=11)):
  80. baker.make("events.Event", issue=issue, _quantity=4)
  81. process_event_alerts()
  82. # Not sufficient rate of events to trigger alert.
  83. self.assertEqual(Notification.objects.count(), 0)
  84. # time 12: 4 more events (16 total, 12 in past 10 minutes)
  85. with freeze_time(self.now + timedelta(minutes=12)):
  86. baker.make("events.Event", issue=issue, _quantity=4)
  87. process_event_alerts()
  88. self.assertEqual(Notification.objects.count(), 1)
  89. def test_alert_one_event(self):
  90. """Use same logic to send alert for every new issue"""
  91. baker.make(
  92. "alerts.ProjectAlert",
  93. project=self.project,
  94. timespan_minutes=1,
  95. quantity=1,
  96. )
  97. issue = baker.make("issues.Issue", project=self.project)
  98. baker.make("events.Event", issue=issue)
  99. process_event_alerts()
  100. self.assertEqual(Notification.objects.count(), 1)
  101. def test_alert_on_regression(self):
  102. baker.make(
  103. "alerts.ProjectAlert",
  104. project=self.project,
  105. timespan_minutes=1,
  106. quantity=1,
  107. )
  108. # Make event
  109. with open(
  110. "events/test_data/incoming_events/very_small_event.json"
  111. ) as json_file:
  112. data = json.load(json_file)
  113. projectkey = self.project.projectkey_set.first()
  114. params = f"?sentry_key={projectkey.public_key}"
  115. url = reverse("event_store", args=[self.project.id]) + params
  116. self.client.post(url, data, format="json")
  117. # First alert
  118. process_event_alerts()
  119. self.assertEqual(len(mail.outbox), 1)
  120. # Mark resolved
  121. issue = Issue.objects.first()
  122. issue.status = EventStatus.RESOLVED
  123. issue.save()
  124. # Send a second event
  125. data["event_id"] = "cf536c31b68a473f97e579507ce155e4"
  126. self.client.post(url, data, format="json")
  127. process_event_alerts()
  128. self.assertEqual(len(mail.outbox), 2)
  129. def test_alert_subscription_default_scope(self):
  130. """Subscribe by default should not result in alert emails for non-team members"""
  131. baker.make(
  132. "alerts.ProjectAlert",
  133. project=self.project,
  134. timespan_minutes=1,
  135. quantity=1,
  136. )
  137. # user2 is an org member but not in a relevant team, should not receive alerts
  138. user2 = baker.make("users.user")
  139. org_user2 = self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  140. team2 = baker.make("teams.Team", organization=self.organization)
  141. team2.members.add(org_user2)
  142. # user3 is in team3 which should receive alerts
  143. user3 = baker.make("users.user")
  144. org_user3 = self.organization.add_user(user3, OrganizationUserRole.MEMBER)
  145. self.team.members.add(org_user3)
  146. team3 = baker.make("teams.Team", organization=self.organization)
  147. team3.members.add(org_user3)
  148. team3.projects.add(self.project)
  149. baker.make("events.Event", issue__project=self.project)
  150. process_event_alerts()
  151. self.assertNotIn(user2.email, mail.outbox[0].to)
  152. self.assertIn(user3.email, mail.outbox[0].to)
  153. self.assertEqual(len(mail.outbox[0].to), 2) # Ensure no duplicate emails
  154. def test_alert_queries(self):
  155. project2 = baker.make("projects.Project", organization=self.organization)
  156. project2.team_set.add(self.team)
  157. project3 = baker.make("projects.Project", organization=self.organization)
  158. baker.make(
  159. "alerts.ProjectAlert",
  160. project=self.project,
  161. timespan_minutes=1,
  162. quantity=1,
  163. )
  164. baker.make(
  165. "alerts.ProjectAlert",
  166. project=project2,
  167. timespan_minutes=1,
  168. quantity=1,
  169. )
  170. baker.make(
  171. "alerts.ProjectAlert",
  172. project=project2,
  173. timespan_minutes=1,
  174. quantity=2,
  175. )
  176. baker.make(
  177. "alerts.ProjectAlert",
  178. project=project3,
  179. timespan_minutes=1,
  180. quantity=1,
  181. )
  182. with self.assertNumQueries(1):
  183. process_event_alerts()
  184. class AlertWithUserProjectAlert(GlitchTipTestCase):
  185. def setUp(self):
  186. self.create_user_and_project()
  187. self.now = timezone.now()
  188. def test_alert_enabled_user_project_alert_disabled(self):
  189. """A user should be able to disable their own notifications"""
  190. baker.make(
  191. "alerts.ProjectAlert",
  192. project=self.project,
  193. timespan_minutes=1,
  194. quantity=1,
  195. )
  196. baker.make(
  197. "projects.UserProjectAlert",
  198. user=self.user,
  199. project=self.project,
  200. status=ProjectAlertStatus.OFF,
  201. )
  202. user2 = baker.make("users.user")
  203. org_user2 = self.organization.add_user(user2, OrganizationUserRole.ADMIN)
  204. self.team.members.add(org_user2)
  205. baker.make(
  206. "projects.UserProjectAlert",
  207. user=user2,
  208. status=ProjectAlertStatus.ON,
  209. )
  210. user3 = baker.make("users.user")
  211. org_user3 = self.organization.add_user(user3, OrganizationUserRole.ADMIN)
  212. self.team.members.add(org_user3)
  213. baker.make(
  214. "projects.UserProjectAlert",
  215. user=user3,
  216. project=self.project,
  217. status=ProjectAlertStatus.ON,
  218. )
  219. baker.make("events.Event", issue__project=self.project)
  220. process_event_alerts()
  221. self.assertEqual(len(mail.outbox), 1)
  222. self.assertEqual(len(mail.outbox[0].merge_data), 2)
  223. def test_alert_enabled_subscribe_by_default(self):
  224. self.user.subscribe_by_default = False
  225. self.user.save()
  226. baker.make(
  227. "alerts.ProjectAlert",
  228. project=self.project,
  229. timespan_minutes=1,
  230. quantity=1,
  231. )
  232. baker.make("events.Event", issue__project=self.project)
  233. process_event_alerts()
  234. self.assertEqual(len(mail.outbox), 0)
  235. def test_alert_enabled_subscribe_by_default_override_false(self):
  236. self.user.subscribe_by_default = False
  237. self.user.save()
  238. baker.make(
  239. "alerts.ProjectAlert",
  240. project=self.project,
  241. timespan_minutes=1,
  242. quantity=1,
  243. )
  244. baker.make(
  245. "projects.UserProjectAlert",
  246. user=self.user,
  247. project=self.project,
  248. status=ProjectAlertStatus.ON,
  249. )
  250. baker.make("events.Event", issue__project=self.project)
  251. process_event_alerts()
  252. self.assertEqual(len(mail.outbox), 1)
  253. def test_user_project_alert_scope(self):
  254. """User project alert should not result in alert emails for non-team members"""
  255. baker.make(
  256. "alerts.ProjectAlert",
  257. project=self.project,
  258. timespan_minutes=1,
  259. quantity=1,
  260. )
  261. user2 = baker.make("users.user")
  262. self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  263. baker.make(
  264. "projects.UserProjectAlert",
  265. user=user2,
  266. project=self.project,
  267. status=ProjectAlertStatus.ON,
  268. )
  269. baker.make("events.Event", issue__project=self.project)
  270. process_event_alerts()
  271. self.assertNotIn(user2.email, mail.outbox[0].to)