From c2de2c3b198aeb294c9c0dada3cbc87d184bad95 Mon Sep 17 00:00:00 2001 From: Frank Hamand Date: Fri, 21 Apr 2017 11:59:38 +0100 Subject: [PATCH] Fix alert tests alerting the duty officer --- CHANGES | 2 ++ cabot/cabotapp/views.py | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 490daeb..980a9bd 100644 --- a/CHANGES +++ b/CHANGES @@ -2,6 +2,8 @@ master ------ * Fix basic auth passwords getting reset when editing checks +* Fix plugin alert tests alerting the current duty officer + - They should now always alert only the user that runs the test Version 0.10.3 -------------- diff --git a/cabot/cabotapp/views.py b/cabot/cabotapp/views.py index 43da072..ff0f327 100644 --- a/cabot/cabotapp/views.py +++ b/cabot/cabotapp/views.py @@ -692,6 +692,26 @@ class AlertTestPluginForm(AlertTestForm): class AlertTestView(LoginRequiredMixin, View): + def trigger_alert_to_user(self, service, user): + """ + Clear out all service users and duty shifts, and disable all fallback users. + Then add a single shift for this user, and add this user to users-to-notify. + + This should ensure we never alert anyone except the user triggering the alert test. + """ + service.users_to_notify.clear() + service.users_to_notify.add(user) + Shift.objects.update(deleted=True) + UserProfile.objects.update(fallback_alert_user=False) + Shift( + start=timezone.now() - timedelta(days=1), + end=timezone.now() + timedelta(days=1), + uid='test-shift', + last_modified=timezone.now(), + user=user + ).save() + service.alert() + def post(self, request): form = AlertTestForm(request.POST) @@ -708,7 +728,8 @@ class AlertTestView(LoginRequiredMixin, View): service.overall_status = data['new_status'] service.old_overall_status = data['old_status'] - service.alert() + + self.trigger_alert_to_user(service, request.user) transaction.savepoint_rollback(sid) @@ -716,7 +737,7 @@ class AlertTestView(LoginRequiredMixin, View): return JsonResponse({"result": "error"}, status=400) -class AlertTestPluginView(LoginRequiredMixin, View): +class AlertTestPluginView(AlertTestView): def post(self, request): form = AlertTestPluginForm(request.POST) @@ -732,13 +753,13 @@ class AlertTestPluginView(LoginRequiredMixin, View): check = StatusCheck(name='ALERT_TEST', calculated_status=data['new_status']) check.save() service.status_checks.add(check) - service.users_to_notify.add(request.user) service.alerts.add(data['alert_plugin']) service.update_status() service.overall_status = data['new_status'] service.old_overall_status = data['old_status'] - service.alert() + + self.trigger_alert_to_user(service, request.user) transaction.savepoint_rollback(sid)