Browse Source

Add "channels" attribute to the Check API resource

pull/211/head
Pēteris Caune 6 years ago
parent
commit
8889cfe993
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 33 additions and 20 deletions
  1. +2
    -0
      CHANGELOG.md
  2. +3
    -1
      hc/api/models.py
  3. +28
    -19
      hc/api/tests/test_list_checks.py

+ 2
- 0
CHANGELOG.md View File

@ -11,6 +11,8 @@ All notable changes to this project will be documented in this file.
- Add read-only API key support - Add read-only API key support
- Remove Profile.bill_to field (obsolete) - Remove Profile.bill_to field (obsolete)
- Show a warning when running with DEBUG=True - 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 ### Bug Fixes
- During DST transition, handle ambiguous dates as pre-transition - During DST transition, handle ambiguous dates as pre-transition


+ 3
- 1
hc/api/models.py View File

@ -164,6 +164,7 @@ class Check(models.Model):
def to_dict(self): def to_dict(self):
update_rel_url = reverse("hc-api-update", args=[self.code]) update_rel_url = reverse("hc-api-update", args=[self.code])
pause_rel_url = reverse("hc-api-pause", 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 = { result = {
"name": self.name, "name": self.name,
@ -173,7 +174,8 @@ class Check(models.Model):
"tags": self.tags, "tags": self.tags,
"grace": int(self.grace.total_seconds()), "grace": int(self.grace.total_seconds()),
"n_pings": self.n_pings, "n_pings": self.n_pings,
"status": self.get_status()
"status": self.get_status(),
"channels": ",".join(sorted(channel_codes))
} }
if self.kind == "simple": if self.kind == "simple":


+ 28
- 19
hc/api/tests/test_list_checks.py View File

@ -3,7 +3,7 @@ from datetime import timedelta as td
from django.utils.timezone import now from django.utils.timezone import now
from django.conf import settings from django.conf import settings
from hc.api.models import Check
from hc.api.models import Channel, Check
from hc.test import BaseTestCase from hc.test import BaseTestCase
@ -31,6 +31,9 @@ class ListChecksTestCase(BaseTestCase):
self.a2.tags = "a2-tag" self.a2.tags = "a2-tag"
self.a2.save() self.a2.save()
self.c1 = Channel.objects.create(user=self.alice)
self.a1.channel_set.add(self.c1)
def get(self): def get(self):
return self.client.get("/api/v1/checks/", HTTP_X_API_KEY="X" * 32) 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) self.assertEqual(r.status_code, 200)
doc = r.json() 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 update_url = settings.SITE_ROOT + "/api/v1/checks/%s" % self.a1.code
pause_url = update_url + "/pause" 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) 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): def test_it_shows_only_users_checks(self):
bobs_check = Check(user=self.bob, name="Bob 1") bobs_check = Check(user=self.bob, name="Bob 1")


Loading…
Cancel
Save