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.9 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, name="Alice 1")
  10. self.a1.timeout = td(seconds=3600)
  11. self.a1.grace = td(seconds=900)
  12. self.a1.n_pings = 0
  13. self.a1.status = "new"
  14. self.a1.tags = "a1-tag a1-additional-tag"
  15. self.a1.desc = "This is description"
  16. self.a1.save()
  17. self.c1 = Channel.objects.create(project=self.project)
  18. self.a1.channel_set.add(self.c1)
  19. def get(self, code, api_key="X" * 32):
  20. url = "/api/v1/checks/%s" % code
  21. return self.client.get(url, HTTP_X_API_KEY=api_key)
  22. def test_it_works(self):
  23. r = self.get(self.a1.code)
  24. self.assertEqual(r.status_code, 200)
  25. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  26. doc = r.json()
  27. self.assertEqual(len(doc), 15)
  28. self.assertEqual(doc["timeout"], 3600)
  29. self.assertEqual(doc["grace"], 900)
  30. self.assertEqual(doc["ping_url"], self.a1.url())
  31. self.assertEqual(doc["last_ping"], None)
  32. self.assertEqual(doc["n_pings"], 0)
  33. self.assertEqual(doc["status"], "new")
  34. self.assertEqual(doc["channels"], str(self.c1.code))
  35. self.assertEqual(doc["desc"], "This is description")
  36. self.assertFalse(doc["manual_resume"])
  37. self.assertEqual(doc["methods"], "")
  38. def test_it_handles_invalid_uuid(self):
  39. r = self.get("not-an-uuid")
  40. self.assertEqual(r.status_code, 404)
  41. def test_it_handles_missing_check(self):
  42. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  43. r = self.get(made_up_code)
  44. self.assertEqual(r.status_code, 404)
  45. def test_it_handles_unique_key(self):
  46. r = self.get(self.a1.unique_key)
  47. self.assertEqual(r.status_code, 200)
  48. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  49. doc = r.json()
  50. self.assertEqual(len(doc), 15)
  51. self.assertEqual(doc["timeout"], 3600)
  52. self.assertEqual(doc["grace"], 900)
  53. self.assertEqual(doc["ping_url"], self.a1.url())
  54. self.assertEqual(doc["last_ping"], None)
  55. self.assertEqual(doc["n_pings"], 0)
  56. self.assertEqual(doc["status"], "new")
  57. self.assertEqual(doc["channels"], str(self.c1.code))
  58. self.assertEqual(doc["desc"], "This is description")
  59. def test_readonly_key_works(self):
  60. self.project.api_key_readonly = "R" * 32
  61. self.project.save()
  62. r = self.get(self.a1.code, api_key=self.project.api_key_readonly)
  63. self.assertEqual(r.status_code, 200)
  64. # When using readonly keys, the ping URLs should not be exposed:
  65. self.assertNotContains(r, self.a1.url())