From 44a677f327ac5da162eb9cbc0214c396bb2bf77e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Tue, 9 Feb 2021 14:25:26 +0200 Subject: [PATCH] Fix hc.api.views.notification_status to always return 200 If the notification does not exist, or is more than a hour old, return HTTP 200 (instead of 400 or 404) so the other party doesn't retry over and over again. --- hc/api/tests/test_notification_status.py | 2 +- hc/api/views.py | 12 ++++++------ hc/front/views.py | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/hc/api/tests/test_notification_status.py b/hc/api/tests/test_notification_status.py index 3e311279..bdab0d99 100644 --- a/hc/api/tests/test_notification_status.py +++ b/hc/api/tests/test_notification_status.py @@ -67,7 +67,7 @@ class NotificationStatusTestCase(BaseTestCase): fake_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02" url = f"/api/v1/notifications/{fake_code}/status" r = self.client.post(url, {"MessageStatus": "failed"}) - self.assertEqual(r.status_code, 404) + self.assertEqual(r.status_code, 200) def test_it_requires_post(self): r = self.client.get(self.url) diff --git a/hc/api/views.py b/hc/api/views.py index 3103064e..fbe68b82 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -436,12 +436,12 @@ def badge(request, badge_key, signature, tag, fmt): def notification_status(request, code): """ Handle notification delivery status callbacks. """ - notification = get_object_or_404(Notification, code=code) - - td = timezone.now() - notification.created - if td.total_seconds() > 3600: - # If the webhook is called more than 1 hour after the notification, ignore it. - # Return HTTP 200 so the other party doesn't retry over and over again: + try: + cutoff = timezone.now() - td(hours=1) + notification = Notification.objects.get(code=code, created__gt=cutoff) + except Notification.DoesNotExist: + # If the notification does not exist, or is more than a hour old, + # return HTTP 200 so the other party doesn't retry over and over again: return HttpResponse() error, mark_not_verified = None, False diff --git a/hc/front/views.py b/hc/front/views.py index 78cebcee..acf6d70f 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -477,7 +477,6 @@ def cron_preview(request): for i in range(0, 6): ctx["dates"].append(it.get_next(datetime)) - pass except UnknownTimeZoneError: ctx["bad_tz"] = True except: