test_tasks.py 10 KB

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