@@ -530,47 +530,73 @@ def test_send_bundle_notification(
530
530
alert_group_1 = make_alert_group (alert_receive_channel = alert_receive_channel )
531
531
alert_group_2 = make_alert_group (alert_receive_channel = alert_receive_channel )
532
532
alert_group_3 = make_alert_group (alert_receive_channel = alert_receive_channel )
533
+
534
+ task_id = "test_task_id"
533
535
notification_bundle = make_user_notification_bundle (
534
- user , UserNotificationPolicy .NotificationChannel .SMS , notification_task_id = "test_task_id" , eta = timezone .now ()
536
+ user , UserNotificationPolicy .NotificationChannel .SMS , notification_task_id = task_id , eta = timezone .now ()
535
537
)
538
+
536
539
notification_bundle .append_notification (alert_group_1 , notification_policy )
537
540
notification_bundle .append_notification (alert_group_2 , notification_policy )
538
541
notification_bundle .append_notification (alert_group_3 , notification_policy )
542
+
539
543
assert notification_bundle .notifications .filter (bundle_uuid__isnull = True ).count () == 3
544
+
540
545
alert_group_3 .resolve ()
541
- with patch ("apps.alerts.tasks.notify_user.compare_escalations" , return_value = True ):
542
- # send notification for 2 active alert groups
543
- send_bundled_notification (notification_bundle .id )
544
- assert f"alert_group { alert_group_3 .id } is not active, skip notification" in caplog .text
545
- assert "perform bundled notification for alert groups with ids:" in caplog .text
546
- # check bundle_uuid was set, notification for resolved alert group was deleted
547
- assert notification_bundle .notifications .filter (bundle_uuid__isnull = True ).count () == 0
548
- assert notification_bundle .notifications .all ().count () == 2
549
- assert not notification_bundle .notifications .filter (alert_group = alert_group_3 ).exists ()
550
-
551
- # send notification for 1 active alert group
552
- notification_bundle .notifications .update (bundle_uuid = None )
553
- alert_group_2 .resolve ()
554
- send_bundled_notification (notification_bundle .id )
555
- assert f"alert_group { alert_group_2 .id } is not active, skip notification" in caplog .text
556
- assert (
557
- f"there is only one alert group in bundled notification, perform regular notification. "
558
- f"alert_group { alert_group_1 .id } "
559
- ) in caplog .text
560
- # check bundle_uuid was set
561
- assert notification_bundle .notifications .filter (bundle_uuid__isnull = True ).count () == 0
562
- assert notification_bundle .notifications .all ().count () == 1
563
- # cleanup notifications
564
- notification_bundle .notifications .all ().delete ()
565
-
566
- # send notification for 0 active alert group
567
- notification_bundle .append_notification (alert_group_1 , notification_policy )
568
- alert_group_1 .resolve ()
569
- send_bundled_notification (notification_bundle .id )
570
- assert f"alert_group { alert_group_1 .id } is not active, skip notification" in caplog .text
571
- assert f"no alert groups to notify about or notification is not allowed for user { user .id } " in caplog .text
572
- # check all notifications were deleted
573
- assert notification_bundle .notifications .all ().count () == 0
546
+
547
+ # send notification for 2 active alert groups
548
+ send_bundled_notification .apply ((notification_bundle .id ,), task_id = task_id )
549
+
550
+ assert f"alert_group { alert_group_3 .id } is not active, skip notification" in caplog .text
551
+ assert "perform bundled notification for alert groups with ids:" in caplog .text
552
+
553
+ # check bundle_uuid was set, notification for resolved alert group was deleted
554
+ assert notification_bundle .notifications .filter (bundle_uuid__isnull = True ).count () == 0
555
+ assert notification_bundle .notifications .all ().count () == 2
556
+ assert not notification_bundle .notifications .filter (alert_group = alert_group_3 ).exists ()
557
+
558
+ # send notification for 1 active alert group
559
+ notification_bundle .notifications .update (bundle_uuid = None )
560
+
561
+ # since we're calling send_bundled_notification several times within this test, we need to reset task_id
562
+ # because it gets set to None after the first call
563
+ notification_bundle .notification_task_id = task_id
564
+ notification_bundle .save ()
565
+
566
+ alert_group_2 .resolve ()
567
+
568
+ send_bundled_notification .apply ((notification_bundle .id ,), task_id = task_id )
569
+
570
+ assert f"alert_group { alert_group_2 .id } is not active, skip notification" in caplog .text
571
+ assert (
572
+ f"there is only one alert group in bundled notification, perform regular notification. "
573
+ f"alert_group { alert_group_1 .id } "
574
+ ) in caplog .text
575
+
576
+ # check bundle_uuid was set
577
+ assert notification_bundle .notifications .filter (bundle_uuid__isnull = True ).count () == 0
578
+ assert notification_bundle .notifications .all ().count () == 1
579
+
580
+ # cleanup notifications
581
+ notification_bundle .notifications .all ().delete ()
582
+
583
+ # send notification for 0 active alert group
584
+ notification_bundle .append_notification (alert_group_1 , notification_policy )
585
+
586
+ # since we're calling send_bundled_notification several times within this test, we need to reset task_id
587
+ # because it gets set to None after the first call
588
+ notification_bundle .notification_task_id = task_id
589
+ notification_bundle .save ()
590
+
591
+ alert_group_1 .resolve ()
592
+
593
+ send_bundled_notification .apply ((notification_bundle .id ,), task_id = task_id )
594
+
595
+ assert f"alert_group { alert_group_1 .id } is not active, skip notification" in caplog .text
596
+ assert f"no alert groups to notify about or notification is not allowed for user { user .id } " in caplog .text
597
+
598
+ # check all notifications were deleted
599
+ assert notification_bundle .notifications .all ().count () == 0
574
600
575
601
576
602
@pytest .mark .django_db
0 commit comments