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.

89 lines
3.1 KiB

  1. from datetime import timedelta as td
  2. from django.utils.timezone import now
  3. from hc.api.models import Channel, Check
  4. from hc.test import BaseTestCase
  5. class GetCheckTestCase(BaseTestCase):
  6. def setUp(self):
  7. super().setUp()
  8. self.now = now().replace(microsecond=0)
  9. self.a1 = Check(project=self.project)
  10. self.a1.set_name_slug("Alice 1")
  11. self.a1.timeout = td(seconds=3600)
  12. self.a1.grace = td(seconds=900)
  13. self.a1.n_pings = 0
  14. self.a1.status = "new"
  15. self.a1.tags = "a1-tag a1-additional-tag"
  16. self.a1.desc = "This is description"
  17. self.a1.save()
  18. self.c1 = Channel.objects.create(project=self.project)
  19. self.a1.channel_set.add(self.c1)
  20. def get(self, code, api_key="X" * 32):
  21. url = "/api/v1/checks/%s" % code
  22. return self.client.get(url, HTTP_X_API_KEY=api_key)
  23. def test_it_works(self):
  24. r = self.get(self.a1.code)
  25. self.assertEqual(r.status_code, 200)
  26. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  27. doc = r.json()
  28. self.assertEqual(len(doc), 16)
  29. self.assertEqual(doc["slug"], "alice-1")
  30. self.assertEqual(doc["timeout"], 3600)
  31. self.assertEqual(doc["grace"], 900)
  32. self.assertEqual(doc["ping_url"], self.a1.url())
  33. self.assertEqual(doc["last_ping"], None)
  34. self.assertEqual(doc["n_pings"], 0)
  35. self.assertEqual(doc["status"], "new")
  36. self.assertEqual(doc["channels"], str(self.c1.code))
  37. self.assertEqual(doc["desc"], "This is description")
  38. self.assertFalse(doc["manual_resume"])
  39. self.assertEqual(doc["methods"], "")
  40. def test_it_handles_invalid_uuid(self):
  41. r = self.get("not-an-uuid")
  42. self.assertEqual(r.status_code, 404)
  43. def test_it_handles_missing_check(self):
  44. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  45. r = self.get(made_up_code)
  46. self.assertEqual(r.status_code, 404)
  47. def test_it_handles_unique_key(self):
  48. r = self.get(self.a1.unique_key)
  49. self.assertEqual(r.status_code, 200)
  50. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  51. doc = r.json()
  52. self.assertEqual(len(doc), 16)
  53. self.assertEqual(doc["timeout"], 3600)
  54. self.assertEqual(doc["grace"], 900)
  55. self.assertEqual(doc["ping_url"], self.a1.url())
  56. self.assertEqual(doc["last_ping"], None)
  57. self.assertEqual(doc["n_pings"], 0)
  58. self.assertEqual(doc["status"], "new")
  59. self.assertEqual(doc["channels"], str(self.c1.code))
  60. self.assertEqual(doc["desc"], "This is description")
  61. def test_it_rejects_post_unique_key(self):
  62. r = self.csrf_client.post(f"/api/v1/checks/{self.a1.unique_key}")
  63. self.assertEqual(r.status_code, 405)
  64. def test_readonly_key_works(self):
  65. self.project.api_key_readonly = "R" * 32
  66. self.project.save()
  67. r = self.get(self.a1.code, api_key=self.project.api_key_readonly)
  68. self.assertEqual(r.status_code, 200)
  69. # When using readonly keys, the ping URLs should not be exposed:
  70. self.assertNotContains(r, self.a1.url())