123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- import json
- from datetime import timedelta
- from django.core import mail
- from django.shortcuts import reverse
- from django.utils import timezone
- from freezegun import freeze_time
- from model_bakery import baker
- from glitchtip.test_utils.test_case import GlitchTipTestCase
- from issues.models import EventStatus, Issue
- from organizations_ext.models import OrganizationUserRole
- from projects.models import ProjectAlertStatus
- from ..models import Notification
- from ..tasks import process_event_alerts
- class AlertTestCase(GlitchTipTestCase):
- def setUp(self):
- self.create_user_and_project()
- self.now = timezone.now()
- def test_alerts(self):
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=10,
- quantity=10,
- )
- issue = baker.make("issues.Issue", project=self.project)
- baker.make("events.Event", issue=issue)
- # Not sufficient events to create alert
- process_event_alerts()
- self.assertEqual(Notification.objects.count(), 0)
- baker.make("events.Event", issue=issue, _quantity=9)
- process_event_alerts()
- self.assertEqual(Notification.objects.count(), 1)
- # Notifications have a cooldown time equal to alert timespan
- process_event_alerts()
- self.assertEqual(Notification.objects.count(), 1)
- # Notifications should not happen again for same issue
- with freeze_time(self.now + timedelta(minutes=11)):
- baker.make("events.Event", issue=issue, _quantity=10)
- process_event_alerts()
- self.assertEqual(Notification.objects.count(), 1)
- self.assertEqual(len(mail.outbox), 1)
- def test_multiple_alerts(self):
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=10,
- quantity=1,
- )
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=10,
- quantity=2,
- )
- issue1 = baker.make("issues.Issue", project=self.project)
- baker.make("events.Event", issue=issue1)
- process_event_alerts()
- self.assertEqual(Notification.objects.count(), 1)
- issue2 = baker.make("issues.Issue", project=self.project)
- baker.make("events.Event", issue=issue2, _quantity=2)
- process_event_alerts()
- # Trigger both alerts, as both meet criteria, total of 3
- self.assertEqual(Notification.objects.count(), 3)
- def test_alert_timing(self):
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=10,
- quantity=10,
- )
- issue = baker.make("issues.Issue", project=self.project)
- # time 0: 4 events
- # time 5: 4 more events (8 total)
- # time 11: 4 more events (12 total)
- baker.make("events.Event", issue=issue, _quantity=4)
- with freeze_time(self.now + timedelta(minutes=5)):
- baker.make("events.Event", issue=issue, _quantity=4)
- process_event_alerts()
- with freeze_time(self.now + timedelta(minutes=11)):
- baker.make("events.Event", issue=issue, _quantity=4)
- process_event_alerts()
- # Not sufficient rate of events to trigger alert.
- self.assertEqual(Notification.objects.count(), 0)
- # time 12: 4 more events (16 total, 12 in past 10 minutes)
- with freeze_time(self.now + timedelta(minutes=12)):
- baker.make("events.Event", issue=issue, _quantity=4)
- process_event_alerts()
- self.assertEqual(Notification.objects.count(), 1)
- def test_alert_one_event(self):
- """Use same logic to send alert for every new issue"""
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- issue = baker.make("issues.Issue", project=self.project)
- baker.make("events.Event", issue=issue)
- process_event_alerts()
- self.assertEqual(Notification.objects.count(), 1)
- def test_alert_on_regression(self):
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- # Make event
- with open(
- "events/test_data/incoming_events/very_small_event.json"
- ) as json_file:
- data = json.load(json_file)
- projectkey = self.project.projectkey_set.first()
- params = f"?sentry_key={projectkey.public_key}"
- url = reverse("event_store", args=[self.project.id]) + params
- self.client.post(url, data, format="json")
- # First alert
- process_event_alerts()
- self.assertEqual(len(mail.outbox), 1)
- # Mark resolved
- issue = Issue.objects.first()
- issue.status = EventStatus.RESOLVED
- issue.save()
- # Send a second event
- data["event_id"] = "cf536c31b68a473f97e579507ce155e4"
- self.client.post(url, data, format="json")
- process_event_alerts()
- self.assertEqual(len(mail.outbox), 2)
- def test_alert_subscription_default_scope(self):
- """Subscribe by default should not result in alert emails for non-team members"""
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- # user2 is an org member but not in a relevant team, should not receive alerts
- user2 = baker.make("users.user")
- org_user2 = self.organization.add_user(user2, OrganizationUserRole.MEMBER)
- team2 = baker.make("teams.Team", organization=self.organization)
- team2.members.add(org_user2)
- # user3 is in team3 which should receive alerts
- user3 = baker.make("users.user")
- org_user3 = self.organization.add_user(user3, OrganizationUserRole.MEMBER)
- self.team.members.add(org_user3)
- team3 = baker.make("teams.Team", organization=self.organization)
- team3.members.add(org_user3)
- team3.projects.add(self.project)
- baker.make("events.Event", issue__project=self.project)
- process_event_alerts()
- self.assertNotIn(user2.email, mail.outbox[0].to)
- self.assertIn(user3.email, mail.outbox[0].to)
- self.assertEqual(len(mail.outbox[0].to), 2) # Ensure no duplicate emails
- def test_alert_queries(self):
- project2 = baker.make("projects.Project", organization=self.organization)
- project2.team_set.add(self.team)
- project3 = baker.make("projects.Project", organization=self.organization)
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- baker.make(
- "alerts.ProjectAlert",
- project=project2,
- timespan_minutes=1,
- quantity=1,
- )
- baker.make(
- "alerts.ProjectAlert",
- project=project2,
- timespan_minutes=1,
- quantity=2,
- )
- baker.make(
- "alerts.ProjectAlert",
- project=project3,
- timespan_minutes=1,
- quantity=1,
- )
- with self.assertNumQueries(1):
- process_event_alerts()
- class AlertWithUserProjectAlert(GlitchTipTestCase):
- def setUp(self):
- self.create_user_and_project()
- self.now = timezone.now()
- def test_alert_enabled_user_project_alert_disabled(self):
- """A user should be able to disable their own notifications"""
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- baker.make(
- "projects.UserProjectAlert",
- user=self.user,
- project=self.project,
- status=ProjectAlertStatus.OFF,
- )
- user2 = baker.make("users.user")
- org_user2 = self.organization.add_user(user2, OrganizationUserRole.ADMIN)
- self.team.members.add(org_user2)
- baker.make(
- "projects.UserProjectAlert",
- user=user2,
- status=ProjectAlertStatus.ON,
- )
- user3 = baker.make("users.user")
- org_user3 = self.organization.add_user(user3, OrganizationUserRole.ADMIN)
- self.team.members.add(org_user3)
- baker.make(
- "projects.UserProjectAlert",
- user=user3,
- project=self.project,
- status=ProjectAlertStatus.ON,
- )
- baker.make("events.Event", issue__project=self.project)
- process_event_alerts()
- self.assertEqual(len(mail.outbox), 1)
- self.assertEqual(len(mail.outbox[0].merge_data), 2)
- def test_alert_enabled_subscribe_by_default(self):
- self.user.subscribe_by_default = False
- self.user.save()
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- baker.make("events.Event", issue__project=self.project)
- process_event_alerts()
- self.assertEqual(len(mail.outbox), 0)
- def test_alert_enabled_subscribe_by_default_override_false(self):
- self.user.subscribe_by_default = False
- self.user.save()
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- baker.make(
- "projects.UserProjectAlert",
- user=self.user,
- project=self.project,
- status=ProjectAlertStatus.ON,
- )
- baker.make("events.Event", issue__project=self.project)
- process_event_alerts()
- self.assertEqual(len(mail.outbox), 1)
- def test_user_project_alert_scope(self):
- """User project alert should not result in alert emails for non-team members"""
- baker.make(
- "alerts.ProjectAlert",
- project=self.project,
- timespan_minutes=1,
- quantity=1,
- )
- user2 = baker.make("users.user")
- self.organization.add_user(user2, OrganizationUserRole.MEMBER)
- baker.make(
- "projects.UserProjectAlert",
- user=user2,
- project=self.project,
- status=ProjectAlertStatus.ON,
- )
- baker.make("events.Event", issue__project=self.project)
- process_event_alerts()
- self.assertNotIn(user2.email, mail.outbox[0].to)
|