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.

83 lines
2.8 KiB

  1. from django.test import Client
  2. from hc.api.models import Check, Ping
  3. from hc.test import BaseTestCase
  4. class PingBySlugTestCase(BaseTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.check = Check.objects.create(project=self.project, name="foo", slug="foo")
  8. self.url = f"/ping/{self.project.ping_key}/foo"
  9. def test_it_works(self):
  10. r = self.client.get(self.url)
  11. self.assertEqual(r.status_code, 200)
  12. self.assertEqual(r.headers["Access-Control-Allow-Origin"], "*")
  13. ping = Ping.objects.get()
  14. self.assertEqual(ping.kind, None)
  15. def test_post_works(self):
  16. csrf_client = Client(enforce_csrf_checks=True)
  17. r = csrf_client.post(self.url, "hello world", content_type="text/plain")
  18. self.assertEqual(r.status_code, 200)
  19. ping = Ping.objects.get()
  20. self.assertEqual(ping.method, "POST")
  21. self.assertEqual(ping.body, "hello world")
  22. def test_head_works(self):
  23. csrf_client = Client(enforce_csrf_checks=True)
  24. r = csrf_client.head(self.url)
  25. self.assertEqual(r.status_code, 200)
  26. self.assertEqual(Ping.objects.count(), 1)
  27. def test_it_handles_missing_check(self):
  28. r = self.client.get(f"/ping/{self.project.ping_key}/bar")
  29. self.assertEqual(r.status_code, 404)
  30. self.assertEqual(r.content.decode(), "not found")
  31. def test_it_never_caches(self):
  32. r = self.client.get(self.url)
  33. assert "no-cache" in r.get("Cache-Control")
  34. def test_fail_endpoint_works(self):
  35. r = self.client.get(self.url + "/fail")
  36. self.assertEqual(r.status_code, 200)
  37. ping = Ping.objects.get()
  38. self.assertEqual(ping.kind, "fail")
  39. def test_start_endpoint_works(self):
  40. r = self.client.get(self.url + "/start")
  41. self.assertEqual(r.status_code, 200)
  42. ping = Ping.objects.get()
  43. self.assertEqual(ping.kind, "start")
  44. def test_zero_exit_status_works(self):
  45. r = self.client.get(self.url + "/0")
  46. self.assertEqual(r.status_code, 200)
  47. ping = Ping.objects.get()
  48. self.assertEqual(ping.kind, None)
  49. self.assertEqual(ping.exitstatus, 0)
  50. def test_nonzero_exit_status_works(self):
  51. r = self.client.get(self.url + "/123")
  52. self.assertEqual(r.status_code, 200)
  53. ping = Ping.objects.get()
  54. self.assertEqual(ping.kind, "fail")
  55. self.assertEqual(ping.exitstatus, 123)
  56. def test_it_handles_duplicates(self):
  57. # Another check with the same slug:
  58. Check.objects.create(project=self.project, name="foo", slug="foo")
  59. r = self.client.get(self.url)
  60. self.assertEqual(r.status_code, 409)
  61. def test_it_handles_wrong_ping_key(self):
  62. r = self.client.get("/ping/rrrrrrrrrrrrrrrrrrrrrr/foo")
  63. self.assertEqual(r.status_code, 404)