You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

82 lines
2.7 KiB

from django.test import Client
from hc.api.models import Check, Ping
from hc.test import BaseTestCase
class PingBySlugTestCase(BaseTestCase):
def setUp(self):
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):
r = self.client.get(self.url)
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):
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")
self.assertEqual(ping.body, "hello world")
def test_head_works(self):
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_handles_missing_check(self):
r = self.client.get(f"/ping/{self.project.ping_key}/bar")
self.assertEqual(r.status_code, 404)
def test_it_never_caches(self):
r = self.client.get(self.url)
assert "no-cache" in r.get("Cache-Control")
def test_fail_endpoint_works(self):
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):
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):
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):
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):
# 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):
r = self.client.get("/ping/rrrrrrrrrrrrrrrrrrrrrr/foo")
self.assertEqual(r.status_code, 404)