From 2ea04440dbf16535da1bfb03726bcbc1e2adc653 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 19:26:43 +0200 Subject: [PATCH] events: optimise marking events as seen (cherry-pick #11297) (#11299) events: optimise marking events as seen (#11297) * events: optimise marking events as seen * add tests --------- Signed-off-by: Jens Langhammer Co-authored-by: Jens L. --- authentik/events/api/notifications.py | 5 +---- authentik/events/tests/test_notifications.py | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/authentik/events/api/notifications.py b/authentik/events/api/notifications.py index 35cbd88252..37b8ca14ca 100644 --- a/authentik/events/api/notifications.py +++ b/authentik/events/api/notifications.py @@ -69,8 +69,5 @@ class NotificationViewSet( @action(detail=False, methods=["post"]) def mark_all_seen(self, request: Request) -> Response: """Mark all the user's notifications as seen""" - notifications = Notification.objects.filter(user=request.user) - for notification in notifications: - notification.seen = True - Notification.objects.bulk_update(notifications, ["seen"]) + Notification.objects.filter(user=request.user, seen=False).update(seen=True) return Response({}, status=204) diff --git a/authentik/events/tests/test_notifications.py b/authentik/events/tests/test_notifications.py index 027383c7fa..527d7e5cd6 100644 --- a/authentik/events/tests/test_notifications.py +++ b/authentik/events/tests/test_notifications.py @@ -2,7 +2,8 @@ from unittest.mock import MagicMock, patch -from django.test import TestCase +from django.urls import reverse +from rest_framework.test import APITestCase from authentik.core.models import Group, User from authentik.events.models import ( @@ -10,6 +11,7 @@ from authentik.events.models import ( EventAction, Notification, NotificationRule, + NotificationSeverity, NotificationTransport, NotificationWebhookMapping, TransportMode, @@ -20,7 +22,7 @@ from authentik.policies.exceptions import PolicyException from authentik.policies.models import PolicyBinding -class TestEventsNotifications(TestCase): +class TestEventsNotifications(APITestCase): """Test Event Notifications""" def setUp(self) -> None: @@ -131,3 +133,15 @@ class TestEventsNotifications(TestCase): Notification.objects.all().delete() Event.new(EventAction.CUSTOM_PREFIX).save() self.assertEqual(Notification.objects.first().body, "foo") + + def test_api_mark_all_seen(self): + """Test mark_all_seen""" + self.client.force_login(self.user) + + Notification.objects.create( + severity=NotificationSeverity.NOTICE, body="foo", user=self.user, seen=False + ) + + response = self.client.post(reverse("authentik_api:notification-mark-all-seen")) + self.assertEqual(response.status_code, 204) + self.assertFalse(Notification.objects.filter(body="foo", seen=False).exists())