Browse Source

Tag labels in "My Checks" page and SVG badges should ignore the "started" state.

pull/211/head
Pēteris Caune 6 years ago
parent
commit
93405cc286
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 72 additions and 9 deletions
  1. +2
    -2
      hc/api/models.py
  2. +37
    -5
      hc/api/tests/test_badge.py
  3. +1
    -1
      hc/api/views.py
  4. +31
    -0
      hc/front/tests/test_my_checks.py
  5. +1
    -1
      hc/front/views.py

+ 2
- 2
hc/api/models.py View File

@ -166,13 +166,13 @@ class Check(models.Model):
return timezone.now() >= down_after 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. """ """ Return current status for display. """
if now is None: if now is None:
now = timezone.now() now = timezone.now()
if self.last_start:
if self.last_start and with_started:
if now >= self.last_start + self.grace: if now >= self.last_start + self.grace:
return "down" return "down"
else: else:


+ 37
- 5
hc/api/tests/test_badge.py View File

@ -1,5 +1,8 @@
from datetime import timedelta as td
from django.conf import settings from django.conf import settings
from django.core.signing import base64_hmac from django.core.signing import base64_hmac
from django.utils.timezone import now
from hc.api.models import Check from hc.api.models import Check
from hc.test import BaseTestCase from hc.test import BaseTestCase
@ -11,16 +14,17 @@ class BadgeTestCase(BaseTestCase):
super(BadgeTestCase, self).setUp() super(BadgeTestCase, self).setUp()
self.check = Check.objects.create(user=self.alice, tags="foo bar") 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): def test_it_rejects_bad_signature(self):
r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username) r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username)
assert r.status_code == 404 assert r.status_code == 404
def test_it_returns_svg(self): 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.assertEqual(r["Access-Control-Allow-Origin"], "*")
self.assertContains(r, "#4c1") self.assertContains(r, "#4c1")
@ -32,3 +36,31 @@ class BadgeTestCase(BaseTestCase):
r = self.client.options(url) r = self.client.options(url)
self.assertEqual(r.status_code, 204) self.assertEqual(r.status_code, 204)
self.assertEqual(r["Access-Control-Allow-Origin"], "*") 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")

+ 1
- 1
hc/api/views.py View File

@ -211,7 +211,7 @@ def badge(request, username, signature, tag, format="svg"):
if tag != "*" and tag not in check.tags_list(): if tag != "*" and tag not in check.tags_list():
continue continue
check_status = check.get_status()
check_status = check.get_status(with_started=False)
if status == "up" and check_status == "grace": if status == "up" and check_status == "grace":
status = "late" status = "late"


+ 31
- 0
hc/front/tests/test_my_checks.py View File

@ -65,3 +65,34 @@ class MyChecksTestCase(BaseTestCase):
self.profile.refresh_from_db() self.profile.refresh_from_db()
self.assertEqual(self.profile.sort, "created") 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="[email protected]", password="password")
r = self.client.get("/checks/")
self.assertContains(r, """<div class="btn btn-xs down ">foo</div>""")
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="[email protected]", password="password")
r = self.client.get("/checks/")
self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")
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="[email protected]", password="password")
r = self.client.get("/checks/")
self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")

+ 1
- 1
hc/front/views.py View File

@ -42,7 +42,7 @@ EVENTS_TMPL = get_template("front/details_events.html")
def _tags_statuses(checks): def _tags_statuses(checks):
tags, down, grace, num_down = {}, {}, {}, 0 tags, down, grace, num_down = {}, {}, {}, 0
for check in checks: for check in checks:
status = check.get_status()
status = check.get_status(with_started=False)
if status == "down": if status == "down":
num_down += 1 num_down += 1


Loading…
Cancel
Save