healthchecks_healthchecks/hc/api/tests/test_ping_by_slug.py
2023-09-02 17:19:31 +03:00

105 lines
3.6 KiB
Python

from __future__ import annotations
from django.test import Client
from hc.api.models import Channel, Check, Ping
from hc.test import BaseTestCase
class PingBySlugTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
self.check = Check.objects.create(project=self.project, name="foo", slug="foo")
self.url = f"/ping/{self.project.ping_key}/foo"
def test_it_works(self) -> None:
r = self.client.get(self.url)
self.assertEqual(r.content, b"OK")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.headers["Access-Control-Allow-Origin"], "*")
ping = Ping.objects.get()
self.assertEqual(ping.kind, None)
def test_post_works(self) -> None:
csrf_client = Client(enforce_csrf_checks=True)
r = csrf_client.post(self.url, "hello world", content_type="text/plain")
self.assertEqual(r.status_code, 200)
ping = Ping.objects.get()
self.assertEqual(ping.method, "POST")
assert ping.body_raw
self.assertEqual(bytes(ping.body_raw), b"hello world")
def test_head_works(self) -> None:
csrf_client = Client(enforce_csrf_checks=True)
r = csrf_client.head(self.url)
self.assertEqual(r.status_code, 200)
self.assertEqual(Ping.objects.count(), 1)
def test_it_never_caches(self) -> None:
r = self.client.get(self.url)
assert "no-cache" in r["Cache-Control"]
def test_fail_endpoint_works(self) -> None:
r = self.client.get(self.url + "/fail")
self.assertEqual(r.status_code, 200)
ping = Ping.objects.get()
self.assertEqual(ping.kind, "fail")
def test_start_endpoint_works(self) -> None:
r = self.client.get(self.url + "/start")
self.assertEqual(r.status_code, 200)
ping = Ping.objects.get()
self.assertEqual(ping.kind, "start")
def test_zero_exit_status_works(self) -> None:
r = self.client.get(self.url + "/0")
self.assertEqual(r.status_code, 200)
ping = Ping.objects.get()
self.assertEqual(ping.kind, None)
self.assertEqual(ping.exitstatus, 0)
def test_nonzero_exit_status_works(self) -> None:
r = self.client.get(self.url + "/123")
self.assertEqual(r.status_code, 200)
ping = Ping.objects.get()
self.assertEqual(ping.kind, "fail")
self.assertEqual(ping.exitstatus, 123)
def test_it_handles_duplicates(self) -> None:
# Another check with the same slug:
Check.objects.create(project=self.project, name="foo", slug="foo")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 409)
def test_it_handles_wrong_ping_key(self) -> None:
r = self.client.get("/ping/rrrrrrrrrrrrrrrrrrrrrr/foo")
self.assertEqual(r.status_code, 404)
def test_it_auto_provisions_missing_check(self) -> None:
self.check.delete()
channel = Channel.objects.create(project=self.project)
r = self.client.get(self.url + "?create=1")
self.assertEqual(r.content, b"Created")
self.assertEqual(r.status_code, 201)
check = Check.objects.get()
self.assertEqual(check.name, "foo")
self.assertEqual(check.slug, "foo")
self.assertEqual(check.ping_set.count(), 1)
# It should assign all channels to the new check
self.assertEqual(check.channel_set.get(), channel)
def test_auto_provisioning_is_off_by_default(self) -> None:
self.check.delete()
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)
self.assertFalse(Check.objects.exists())