From 8889cfe99320bf6023db874d7cdfaed261e8b51f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 8 Nov 2018 12:13:18 +0200 Subject: [PATCH] Add "channels" attribute to the Check API resource --- CHANGELOG.md | 2 ++ hc/api/models.py | 4 ++- hc/api/tests/test_list_checks.py | 47 +++++++++++++++++++------------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e2f6e9b..4ff3d5c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ All notable changes to this project will be documented in this file. - Add read-only API key support - Remove Profile.bill_to field (obsolete) - Show a warning when running with DEBUG=True +- Add "channels" attribute to the Check API resource +- Can specify channel codes when updating a check via API ### Bug Fixes - During DST transition, handle ambiguous dates as pre-transition diff --git a/hc/api/models.py b/hc/api/models.py index ad8a9871..aeb77287 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -164,6 +164,7 @@ class Check(models.Model): def to_dict(self): update_rel_url = reverse("hc-api-update", args=[self.code]) pause_rel_url = reverse("hc-api-pause", args=[self.code]) + channel_codes = [str(ch.code) for ch in self.channel_set.all()] result = { "name": self.name, @@ -173,7 +174,8 @@ class Check(models.Model): "tags": self.tags, "grace": int(self.grace.total_seconds()), "n_pings": self.n_pings, - "status": self.get_status() + "status": self.get_status(), + "channels": ",".join(sorted(channel_codes)) } if self.kind == "simple": diff --git a/hc/api/tests/test_list_checks.py b/hc/api/tests/test_list_checks.py index 5ed922dc..fac54368 100644 --- a/hc/api/tests/test_list_checks.py +++ b/hc/api/tests/test_list_checks.py @@ -3,7 +3,7 @@ from datetime import timedelta as td from django.utils.timezone import now from django.conf import settings -from hc.api.models import Check +from hc.api.models import Channel, Check from hc.test import BaseTestCase @@ -31,6 +31,9 @@ class ListChecksTestCase(BaseTestCase): self.a2.tags = "a2-tag" self.a2.save() + self.c1 = Channel.objects.create(user=self.alice) + self.a1.channel_set.add(self.c1) + def get(self): return self.client.get("/api/v1/checks/", HTTP_X_API_KEY="X" * 32) @@ -39,30 +42,36 @@ class ListChecksTestCase(BaseTestCase): self.assertEqual(r.status_code, 200) doc = r.json() - self.assertTrue("checks" in doc) - - checks = {check["name"]: check for check in doc["checks"]} - self.assertEqual(len(checks), 2) - - self.assertEqual(checks["Alice 1"]["timeout"], 3600) - self.assertEqual(checks["Alice 1"]["grace"], 900) - self.assertEqual(checks["Alice 1"]["ping_url"], self.a1.url()) - self.assertEqual(checks["Alice 1"]["last_ping"], self.now.isoformat()) - self.assertEqual(checks["Alice 1"]["n_pings"], 1) - self.assertEqual(checks["Alice 1"]["status"], "new") + self.assertEqual(len(doc["checks"]), 2) + + a1 = None + a2 = None + for check in doc["checks"]: + if check["name"] == "Alice 1": + a1 = check + if check["name"] == "Alice 2": + a2 = check + + self.assertEqual(a1["timeout"], 3600) + self.assertEqual(a1["grace"], 900) + self.assertEqual(a1["ping_url"], self.a1.url()) + self.assertEqual(a1["last_ping"], self.now.isoformat()) + self.assertEqual(a1["n_pings"], 1) + self.assertEqual(a1["status"], "new") + self.assertEqual(a1["channels"], str(self.c1.code)) update_url = settings.SITE_ROOT + "/api/v1/checks/%s" % self.a1.code pause_url = update_url + "/pause" - self.assertEqual(checks["Alice 1"]["update_url"], update_url) - self.assertEqual(checks["Alice 1"]["pause_url"], pause_url) + self.assertEqual(a1["update_url"], update_url) + self.assertEqual(a1["pause_url"], pause_url) next_ping = self.now + td(seconds=3600) - self.assertEqual(checks["Alice 1"]["next_ping"], next_ping.isoformat()) + self.assertEqual(a1["next_ping"], next_ping.isoformat()) - self.assertEqual(checks["Alice 2"]["timeout"], 86400) - self.assertEqual(checks["Alice 2"]["grace"], 3600) - self.assertEqual(checks["Alice 2"]["ping_url"], self.a2.url()) - self.assertEqual(checks["Alice 2"]["status"], "up") + self.assertEqual(a2["timeout"], 86400) + self.assertEqual(a2["grace"], 3600) + self.assertEqual(a2["ping_url"], self.a2.url()) + self.assertEqual(a2["status"], "up") def test_it_shows_only_users_checks(self): bobs_check = Check(user=self.bob, name="Bob 1")