Skip to content

Commit fb24e27

Browse files
committed
fix empty notification middleware query param
1 parent a8d3ad9 commit fb24e27

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

ephios/extra/middleware.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,22 @@ def __init__(self, get_response):
2222
self.get_response = get_response
2323

2424
def __call__(self, request):
25-
from ephios.core.models import Notification
26-
2725
response = self.get_response(request)
28-
if NOTIFICATION_READ_PARAM_NAME in request.GET:
26+
if getattr(request.user, "is_authenticated", False) and (
27+
notification_id := request.GET.get(NOTIFICATION_READ_PARAM_NAME)
28+
):
29+
from ephios.core.models import Notification
30+
2931
try:
3032
notification = Notification.objects.get(
31-
pk=request.GET[NOTIFICATION_READ_PARAM_NAME]
33+
pk=notification_id,
34+
user=request.user,
35+
read=False,
3236
)
33-
if notification.user == request.user and not notification.read:
34-
notification.read = True
35-
notification.save()
36-
except Notification.DoesNotExist:
37+
except (Notification.DoesNotExist, ValueError):
38+
# ValueError if `notification_id` is not an integer
3739
pass
40+
else:
41+
notification.read = True
42+
notification.save(update_fields=["read"])
3843
return response

tests/core/test_notifications.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ephios.core.models import AbstractParticipation, LocalParticipation, Notification
88
from ephios.core.services.notifications.backends import EmailNotificationBackend
99
from ephios.core.services.notifications.types import (
10+
NOTIFICATION_READ_PARAM_NAME,
1011
ConsequenceApprovedNotification,
1112
ConsequenceDeniedNotification,
1213
CustomEventParticipantNotification,
@@ -201,11 +202,34 @@ def test_middleware_marks_notification_as_read(django_app, qualified_volunteer,
201202
user=planner, slug=ResponsibleParticipationAwaitsDispositionNotification.slug
202203
)
203204
assert not notification.read
204-
response = django_app.get(notification.get_actions()[0][1], user=planner)
205+
django_app.get(notification.get_actions()[0][1], user=planner)
205206
notification.refresh_from_db()
206207
assert notification.read
207208

208209

210+
def test_broken_middleware_query_param(django_app, planner):
211+
django_app.get(
212+
f"/?{NOTIFICATION_READ_PARAM_NAME}=1",
213+
user=None, # anonymous user
214+
)
215+
django_app.get(
216+
f"/?{NOTIFICATION_READ_PARAM_NAME}",
217+
user=planner,
218+
)
219+
django_app.get(
220+
f"/?{NOTIFICATION_READ_PARAM_NAME}=123",
221+
user=planner,
222+
)
223+
django_app.get(
224+
f"/?{NOTIFICATION_READ_PARAM_NAME}=abc",
225+
user=planner,
226+
)
227+
django_app.get(
228+
f"/?{NOTIFICATION_READ_PARAM_NAME}=1&{NOTIFICATION_READ_PARAM_NAME}=2",
229+
user=planner,
230+
)
231+
232+
209233
def test_notification_doesnotexist_gets_deleted(django_app, qualified_volunteer, event):
210234
participation = LocalParticipation.objects.create(
211235
shift=event.shifts.first(),

0 commit comments

Comments
 (0)