test_tasks.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. def test_alert_queries(self):
  134. project2 = baker.make("projects.Project", organization=self.organization)
  135. project2.team_set.add(self.team)
  136. project3 = baker.make("projects.Project", organization=self.organization)
  137. baker.make(
  138. "alerts.ProjectAlert",
  139. project=self.project,
  140. timespan_minutes=1,
  141. quantity=1,
  142. )
  143. baker.make(
  144. "alerts.ProjectAlert",
  145. project=project2,
  146. timespan_minutes=1,
  147. quantity=1,
  148. )
  149. baker.make(
  150. "alerts.ProjectAlert",
  151. project=project2,
  152. timespan_minutes=1,
  153. quantity=2,
  154. )
  155. baker.make(
  156. "alerts.ProjectAlert",
  157. project=project3,
  158. timespan_minutes=1,
  159. quantity=1,
  160. )
  161. with self.assertNumQueries(5):
  162. process_event_alerts()
  163. class AlertWithUserProjectAlert(GlitchTipTestCase):
  164. def setUp(self):
  165. self.create_user_and_project()
  166. self.now = timezone.now()
  167. def test_alert_enabled_user_project_alert_disabled(self):
  168. """A user should be able to disable their own notifications"""
  169. baker.make(
  170. "alerts.ProjectAlert",
  171. project=self.project,
  172. timespan_minutes=1,
  173. quantity=1,
  174. )
  175. baker.make(
  176. "projects.UserProjectAlert",
  177. user=self.user,
  178. project=self.project,
  179. status=ProjectAlertStatus.OFF,
  180. )
  181. user2 = baker.make("users.user")
  182. org_user2 = self.organization.add_user(user2, OrganizationUserRole.ADMIN)
  183. self.team.members.add(org_user2)
  184. baker.make(
  185. "projects.UserProjectAlert",
  186. user=user2,
  187. status=ProjectAlertStatus.ON,
  188. )
  189. user3 = baker.make("users.user")
  190. org_user3 = self.organization.add_user(user3, OrganizationUserRole.ADMIN)
  191. self.team.members.add(org_user3)
  192. baker.make(
  193. "projects.UserProjectAlert",
  194. user=user3,
  195. project=self.project,
  196. status=ProjectAlertStatus.ON,
  197. )
  198. baker.make("events.Event", issue__project=self.project)
  199. process_event_alerts()
  200. self.assertEqual(len(mail.outbox), 1)
  201. self.assertEqual(len(mail.outbox[0].merge_data), 2)
  202. def test_alert_enabled_subscribe_by_default(self):
  203. self.user.subscribe_by_default = False
  204. self.user.save()
  205. baker.make(
  206. "alerts.ProjectAlert",
  207. project=self.project,
  208. timespan_minutes=1,
  209. quantity=1,
  210. )
  211. baker.make("events.Event", issue__project=self.project)
  212. process_event_alerts()
  213. self.assertEqual(len(mail.outbox), 0)
  214. def test_alert_enabled_subscribe_by_default_override_false(self):
  215. self.user.subscribe_by_default = False
  216. self.user.save()
  217. baker.make(
  218. "alerts.ProjectAlert",
  219. project=self.project,
  220. timespan_minutes=1,
  221. quantity=1,
  222. )
  223. baker.make(
  224. "projects.UserProjectAlert",
  225. user=self.user,
  226. project=self.project,
  227. status=ProjectAlertStatus.ON,
  228. )
  229. baker.make("events.Event", issue__project=self.project)
  230. process_event_alerts()
  231. self.assertEqual(len(mail.outbox), 1)
  232. def test_user_project_alert_scope(self):
  233. """User project alert should not result in alert emails for non-team members"""
  234. baker.make(
  235. "alerts.ProjectAlert",
  236. project=self.project,
  237. timespan_minutes=1,
  238. quantity=1,
  239. )
  240. user2 = baker.make("users.user")
  241. self.organization.add_user(user2, OrganizationUserRole.MEMBER)
  242. baker.make(
  243. "projects.UserProjectAlert",
  244. user=user2,
  245. project=self.project,
  246. status=ProjectAlertStatus.ON,
  247. )
  248. baker.make("events.Event", issue__project=self.project)
  249. process_event_alerts()
  250. self.assertNotIn(user2.email, mail.outbox[0].to)