Skip to content

Commit

Permalink
[BUGFIX] Fix notification splay #494
Browse files Browse the repository at this point in the history
  • Loading branch information
kfdm authored Apr 11, 2024
2 parents 3d1ac57 + 4bf29e2 commit 387e2c7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions promgen/notification/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class NotificationUser(NotificationBase):

form = FormUser

def splay(self, address):
def splay(self, address, **kwargs):
try:
user = User.objects.get(username=address)
except User.DoesNotExist:
logger.error("Missing user %s", address)
else:
yield from models.Sender.objects.filter(obj=user)
yield from models.Sender.objects.filter(obj=user, **kwargs)

def _send(self, address, data):
user = User.objects.get(username=address)
Expand Down
2 changes: 2 additions & 0 deletions promgen/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def check_user_subscription(sender, instance, created, request):


@receiver(post_save, sender=models.Service)
@skip_raw
def add_default_service_subscription(instance, created, **kwargs):
if created and instance.owner:
sender, new_notifier = models.Sender.objects.get_or_create(
Expand All @@ -323,6 +324,7 @@ def add_default_service_subscription(instance, created, **kwargs):


@receiver(post_save, sender=models.Project)
@skip_raw
def add_default_project_subscription(instance, created, **kwargs):
if created and instance.owner:
sender, new_notifier = models.Sender.objects.get_or_create(
Expand Down
2 changes: 1 addition & 1 deletion promgen/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def process_alert(alert_pk):
logger.debug("Filtered out sender %s", sender)
continue
if hasattr(sender.driver, "splay"):
for splay in sender.driver.splay(sender.value):
for splay in sender.driver.splay(sender.value, enabled=True):
senders[splay.sender].add(splay.value)
else:
senders[sender.sender].add(sender.value)
Expand Down
20 changes: 20 additions & 0 deletions promgen/tests/notification/test_user_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ def test_failed_user(self, mock_email):
self.assertCount(models.AlertError, 0, "No failed alerts")

self.assertEqual(mock_email.call_count, 1, "Still called email")

@override_settings(PROMGEN=tests.SETTINGS)
@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
@mock.patch("promgen.notification.email.send_mail")
def test_enabled(self, mock_email):
one = models.Service.objects.get(pk=1)

# This notification is direct and disabled
NotificationEmail.create(obj=one, value="[email protected]", enabled=False)
# Our parent notification is enabled
NotificationUser.create(obj=one, value=one.owner.username)
# But the child notifier is disabled and shouldn't fire
NotificationEmail.create(obj=one.owner, value="[email protected]", enabled=False)

response = self.fireAlert()
self.assertRoute(response, rest.AlertReceiver, 202)
self.assertCount(models.Alert, 1, "Alert Queued")
self.assertCount(models.AlertError, 0, "No failed alerts")

self.assertEqual(mock_email.call_count, 0, "Should not call email")

0 comments on commit 387e2c7

Please sign in to comment.