From 93405cc286a6622d732c0d34059e0bc424dd36d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 20 Dec 2018 19:19:46 +0200 Subject: [PATCH] Tag labels in "My Checks" page and SVG badges should ignore the "started" state. --- hc/api/models.py | 4 +-- hc/api/tests/test_badge.py | 42 ++++++++++++++++++++++++++++---- hc/api/views.py | 2 +- hc/front/tests/test_my_checks.py | 31 +++++++++++++++++++++++ hc/front/views.py | 2 +- 5 files changed, 72 insertions(+), 9 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index 7e35d0f9..418c5a65 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -166,13 +166,13 @@ class Check(models.Model): return timezone.now() >= down_after - def get_status(self, now=None): + def get_status(self, now=None, with_started=True): """ Return current status for display. """ if now is None: now = timezone.now() - if self.last_start: + if self.last_start and with_started: if now >= self.last_start + self.grace: return "down" else: diff --git a/hc/api/tests/test_badge.py b/hc/api/tests/test_badge.py index 394d9bcb..6a7039ce 100644 --- a/hc/api/tests/test_badge.py +++ b/hc/api/tests/test_badge.py @@ -1,5 +1,8 @@ +from datetime import timedelta as td + from django.conf import settings from django.core.signing import base64_hmac +from django.utils.timezone import now from hc.api.models import Check from hc.test import BaseTestCase @@ -11,16 +14,17 @@ class BadgeTestCase(BaseTestCase): super(BadgeTestCase, self).setUp() self.check = Check.objects.create(user=self.alice, tags="foo bar") + sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY) + sig = sig[:8] + self.svg_url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig) + self.json_url = "/badge/%s/%s/foo.json" % (self.alice.username, sig) + def test_it_rejects_bad_signature(self): r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username) assert r.status_code == 404 def test_it_returns_svg(self): - sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY) - sig = sig[:8] - url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig) - - r = self.client.get(url) + r = self.client.get(self.svg_url) self.assertEqual(r["Access-Control-Allow-Origin"], "*") self.assertContains(r, "#4c1") @@ -32,3 +36,31 @@ class BadgeTestCase(BaseTestCase): r = self.client.options(url) self.assertEqual(r.status_code, 204) self.assertEqual(r["Access-Control-Allow-Origin"], "*") + + def test_it_handles_started_but_down(self): + self.check.last_start = now() + self.check.tags = "foo" + self.check.status = "down" + self.check.save() + + r = self.client.get(self.json_url) + self.assertContains(r, "down") + + def test_it_shows_grace_badge(self): + self.check.last_ping = now() - td(days=1, minutes=10) + self.check.tags = "foo" + self.check.status = "up" + self.check.save() + + r = self.client.get(self.json_url) + self.assertContains(r, "late") + + def test_it_shows_started_but_grace_badge(self): + self.check.last_start = now() + self.check.last_ping = now() - td(days=1, minutes=10) + self.check.tags = "foo" + self.check.status = "up" + self.check.save() + + r = self.client.get(self.json_url) + self.assertContains(r, "late") diff --git a/hc/api/views.py b/hc/api/views.py index 1a2a620e..d9f5c624 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -211,7 +211,7 @@ def badge(request, username, signature, tag, format="svg"): if tag != "*" and tag not in check.tags_list(): continue - check_status = check.get_status() + check_status = check.get_status(with_started=False) if status == "up" and check_status == "grace": status = "late" diff --git a/hc/front/tests/test_my_checks.py b/hc/front/tests/test_my_checks.py index f966ccda..646ffb1b 100644 --- a/hc/front/tests/test_my_checks.py +++ b/hc/front/tests/test_my_checks.py @@ -65,3 +65,34 @@ class MyChecksTestCase(BaseTestCase): self.profile.refresh_from_db() self.assertEqual(self.profile.sort, "created") + + def test_it_shows_started_but_down_badge(self): + self.check.last_start = timezone.now() + self.check.tags = "foo" + self.check.status = "down" + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/") + self.assertContains(r, """
foo
""") + + def test_it_shows_grace_badge(self): + self.check.last_ping = timezone.now() - td(days=1, minutes=10) + self.check.tags = "foo" + self.check.status = "up" + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/") + self.assertContains(r, """
foo
""") + + def test_it_shows_grace_started_badge(self): + self.check.last_start = timezone.now() + self.check.last_ping = timezone.now() - td(days=1, minutes=10) + self.check.tags = "foo" + self.check.status = "up" + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/") + self.assertContains(r, """
foo
""") diff --git a/hc/front/views.py b/hc/front/views.py index 3d97952f..4efaf7bd 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -42,7 +42,7 @@ EVENTS_TMPL = get_template("front/details_events.html") def _tags_statuses(checks): tags, down, grace, num_down = {}, {}, {}, 0 for check in checks: - status = check.get_status() + status = check.get_status(with_started=False) if status == "down": num_down += 1