diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index d3d62026..54932822 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -85,14 +85,24 @@ def num_down_title(num_down): @register.filter def down_title(check): + """ Prepare title tag for the Details page. + + If the check is down, return "DOWN - Name - site_name". + Otherwise, return "Name - site_name". + + """ + s = "%s – %s" % (check.name_then_code(), settings.SITE_NAME) if check.get_status() == "down": s = "DOWN – " + s + return s @register.filter def break_underscore(s): + """ Add non-breaking-space characters after underscores. """ + if len(s) > 30: s = s.replace("_", "_\u200b") diff --git a/hc/front/tests/test_details.py b/hc/front/tests/test_details.py new file mode 100644 index 00000000..e0b5cfa1 --- /dev/null +++ b/hc/front/tests/test_details.py @@ -0,0 +1,38 @@ +from hc.api.models import Check, Ping +from hc.test import BaseTestCase + + +class DetailsTestCase(BaseTestCase): + + def setUp(self): + super(DetailsTestCase, self).setUp() + self.check = Check(user=self.alice) + self.check.save() + + ping = Ping(owner=self.check) + ping.save() + + # Older MySQL versions don't store microseconds. This makes sure + # the ping is older than any notifications we may create later: + ping.created = "2000-01-01T00:00:00+00:00" + ping.save() + + self.url = "/checks/%s/details/" % self.check.code + + def test_it_works(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get(self.url) + self.assertContains(r, "How To Ping", status_code=200) + + def test_it_checks_ownership(self): + self.client.login(username="charlie@example.org", password="password") + r = self.client.get(self.url) + assert r.status_code == 403 + + def test_it_shows_cron_expression(self): + self.check.kind = "cron" + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get(self.url) + self.assertContains(r, "Cron Expression", status_code=200) diff --git a/hc/front/tests/test_log.py b/hc/front/tests/test_log.py index 9a55127d..b671503a 100644 --- a/hc/front/tests/test_log.py +++ b/hc/front/tests/test_log.py @@ -46,13 +46,13 @@ class LogTestCase(BaseTestCase): self.client.login(username="alice@example.org", password="password") r = self.client.get(url) - assert r.status_code == 404 + self.assertEqual(r.status_code, 404) def test_it_checks_ownership(self): url = "/checks/%s/log/" % self.check.code self.client.login(username="charlie@example.org", password="password") r = self.client.get(url) - assert r.status_code == 403 + self.assertEqual(r.status_code, 403) def test_it_shows_pushover_notifications(self): ch = Channel(kind="po", user=self.alice) diff --git a/hc/front/tests/test_status_single.py b/hc/front/tests/test_status_single.py new file mode 100644 index 00000000..1fa2ce4f --- /dev/null +++ b/hc/front/tests/test_status_single.py @@ -0,0 +1,48 @@ +from hc.api.models import Check, Ping +from hc.test import BaseTestCase + + +class StatusSingleTestCase(BaseTestCase): + + def setUp(self): + super(StatusSingleTestCase, self).setUp() + self.check = Check(user=self.alice, name="Alice Was Here") + self.check.save() + + def test_it_works(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/%s/status/" % self.check.code) + doc = r.json() + + self.assertEqual(doc["status"], "new") + self.assertTrue("never received a ping" in doc["status_text"]) + self.assertTrue("not received any pings yet" in doc["events"]) + + def test_it_returns_events(self): + p = Ping.objects.create(owner=self.check, ua="test-user-agent", n=1) + self.check.status = "up" + self.check.last_ping = p.created + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/%s/status/" % self.check.code) + doc = r.json() + + self.assertEqual(doc["status"], "up") + self.assertEqual(doc["updated"], p.created.strftime("%s.%f")) + self.assertTrue("test-user-agent" in doc["events"]) + + def test_it_omits_events(self): + p = Ping.objects.create(owner=self.check, ua="test-user-agent", n=1) + self.check.status = "up" + self.check.last_ping = p.created + self.check.save() + + timestamp = p.created.strftime("%s.%f") + url = "/checks/%s/status/?u=%s" % (self.check.code, timestamp) + + self.client.login(username="alice@example.org", password="password") + r = self.client.get(url) + doc = r.json() + + self.assertFalse("events" in doc) diff --git a/hc/front/tests/test_switch_channel.py b/hc/front/tests/test_switch_channel.py new file mode 100644 index 00000000..58fcf489 --- /dev/null +++ b/hc/front/tests/test_switch_channel.py @@ -0,0 +1,46 @@ +from hc.api.models import Channel, Check +from hc.test import BaseTestCase + + +class SwitchChannelTestCase(BaseTestCase): + + def setUp(self): + super(SwitchChannelTestCase, self).setUp() + self.check = Check(user=self.alice) + self.check.save() + + self.channel = Channel(user=self.alice, kind="email") + self.channel.value = "alice@example.org" + self.channel.save() + + self.url = "/checks/%s/channels/%s/enabled" % (self.check.code, self.channel.code) + + def test_it_enables(self): + self.client.login(username="alice@example.org", password="password") + self.client.post(self.url, {"state": "on"}) + + self.assertTrue(self.channel in self.check.channel_set.all()) + + def test_it_disables(self): + self.check.channel_set.add(self.channel) + + self.client.login(username="alice@example.org", password="password") + self.client.post(self.url, {"state": "off"}) + + self.assertFalse(self.channel in self.check.channel_set.all()) + + def test_it_checks_ownership(self): + self.client.login(username="charlie@example.org", password="password") + r = self.client.post(self.url, {"state": "on"}) + self.assertEqual(r.status_code, 403) + + def test_it_checks_channels_ownership(self): + cc = Check(user=self.charlie) + cc.save() + + # Charlie will try to assign Alice's channel to his check: + self.url = "/checks/%s/channels/%s/enabled" % (cc.code, self.channel.code) + + self.client.login(username="charlie@example.org", password="password") + r = self.client.post(self.url, {"state": "on"}) + self.assertEqual(r.status_code, 403)