From cde1f50ac2ccedddb4391ddf30142cdb043d9279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 19 Feb 2020 12:45:33 +0200 Subject: [PATCH] API: update check's "alert_after" field when changing schedule --- CHANGELOG.md | 5 +++-- hc/api/tests/test_update_check.py | 11 ++++++++++- hc/api/views.py | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e363f17..5060cf61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,9 @@ All notable changes to this project will be documented in this file. ### Bug Fixes - The "render_docs" command checks if markdown and pygments is installed (#329) - The team size limit is applied to the n. of distinct users across all projects (#332) -- Don't let SuspiciousOperation bubble up when validating channel ids in API -- Security: check channel ownership when setting check's channels via API +- API: don't let SuspiciousOperation bubble up when validating channel ids +- API security: check channel ownership when setting check's channels +- API: update check's "alert_after" field when changing schedule ## v1.13.0 - 2020-02-13 diff --git a/hc/api/tests/test_update_check.py b/hc/api/tests/test_update_check.py index 24b6da63..f689f193 100644 --- a/hc/api/tests/test_update_check.py +++ b/hc/api/tests/test_update_check.py @@ -1,5 +1,7 @@ +from datetime import timedelta as td import uuid +from django.utils.timezone import now from hc.api.models import Channel, Check from hc.test import BaseTestCase @@ -14,6 +16,10 @@ class UpdateCheckTestCase(BaseTestCase): return self.client.post(url, data, content_type="application/json") def test_it_works(self): + self.check.last_ping = now() + self.check.status = "up" + self.check.save() + r = self.post( self.check.code, { @@ -34,7 +40,6 @@ class UpdateCheckTestCase(BaseTestCase): self.assertEqual(doc["name"], "Foo") self.assertEqual(doc["tags"], "bar,baz") self.assertEqual(doc["desc"], "My description") - self.assertEqual(doc["last_ping"], None) self.assertEqual(doc["n_pings"], 0) self.assertTrue("schedule" not in doc) @@ -48,6 +53,10 @@ class UpdateCheckTestCase(BaseTestCase): self.assertEqual(self.check.timeout.total_seconds(), 3600) self.assertEqual(self.check.grace.total_seconds(), 60) + # alert_after should be updated too + expected_aa = self.check.last_ping + td(seconds=3600 + 60) + self.assertEqual(self.check.alert_after, expected_aa) + def test_it_handles_options(self): r = self.client.options("/api/v1/checks/%s" % self.check.code) self.assertEqual(r.status_code, 204) diff --git a/hc/api/views.py b/hc/api/views.py index d7662443..abcb67a8 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -89,6 +89,7 @@ def _update(check, spec): if "tz" in spec: check.tz = spec["tz"] + check.alert_after = check.going_down_after() check.save() # This needs to be done after saving the check, because of