Skip to content

Commit

Permalink
Merge pull request #494 from arachnys/fix-alert-test-duty
Browse files Browse the repository at this point in the history
Fix alert tests alerting the duty officer
  • Loading branch information
frankh authored Apr 21, 2017
2 parents cfc0222 + c2de2c3 commit 2dc92a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
29 changes: 25 additions & 4 deletions cabot/cabotapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -708,15 +728,16 @@ def post(self, request):

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)

return JsonResponse({"result": "ok"})
return JsonResponse({"result": "error"}, status=400)


class AlertTestPluginView(LoginRequiredMixin, View):
class AlertTestPluginView(AlertTestView):
def post(self, request):
form = AlertTestPluginForm(request.POST)

Expand All @@ -732,13 +753,13 @@ def post(self, request):
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)

Expand Down

0 comments on commit 2dc92a6

Please sign in to comment.