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.

155 lines
5.2 KiB

  1. import json
  2. from datetime import timedelta as td
  3. from django.utils.timezone import now
  4. from django.conf import settings
  5. from hc.api.models import Channel, Check
  6. from hc.test import BaseTestCase
  7. class ListChecksTestCase(BaseTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.now = now().replace(microsecond=0)
  11. self.a1 = Check(project=self.project, name="Alice 1")
  12. self.a1.timeout = td(seconds=3600)
  13. self.a1.grace = td(seconds=900)
  14. self.a1.n_pings = 0
  15. self.a1.status = "new"
  16. self.a1.tags = "a1-tag a1-additional-tag"
  17. self.a1.desc = "This is description"
  18. self.a1.save()
  19. self.a2 = Check(project=self.project, name="Alice 2")
  20. self.a2.timeout = td(seconds=86400)
  21. self.a2.grace = td(seconds=3600)
  22. self.a2.last_ping = self.now
  23. self.a2.status = "up"
  24. self.a2.tags = "a2-tag"
  25. self.a2.save()
  26. self.c1 = Channel.objects.create(project=self.project)
  27. self.a1.channel_set.add(self.c1)
  28. def get(self):
  29. return self.client.get("/api/v1/checks/", HTTP_X_API_KEY="X" * 32)
  30. def test_it_works(self):
  31. # Expect 3 queries:
  32. # * check API key
  33. # * retrieve checks
  34. # * retrieve channel codes
  35. with self.assertNumQueries(3):
  36. r = self.get()
  37. self.assertEqual(r.status_code, 200)
  38. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  39. doc = r.json()
  40. self.assertEqual(len(doc["checks"]), 2)
  41. by_name = {}
  42. for check in doc["checks"]:
  43. by_name[check["name"]] = check
  44. a1 = by_name["Alice 1"]
  45. self.assertEqual(a1["timeout"], 3600)
  46. self.assertEqual(a1["grace"], 900)
  47. self.assertEqual(a1["ping_url"], self.a1.url())
  48. self.assertEqual(a1["last_ping"], None)
  49. self.assertEqual(a1["n_pings"], 0)
  50. self.assertEqual(a1["status"], "new")
  51. self.assertEqual(a1["channels"], str(self.c1.code))
  52. self.assertEqual(a1["desc"], "This is description")
  53. update_url = settings.SITE_ROOT + "/api/v1/checks/%s" % self.a1.code
  54. pause_url = update_url + "/pause"
  55. self.assertEqual(a1["update_url"], update_url)
  56. self.assertEqual(a1["pause_url"], pause_url)
  57. self.assertEqual(a1["next_ping"], None)
  58. a2 = by_name["Alice 2"]
  59. self.assertEqual(a2["timeout"], 86400)
  60. self.assertEqual(a2["grace"], 3600)
  61. self.assertEqual(a2["ping_url"], self.a2.url())
  62. self.assertEqual(a2["status"], "up")
  63. next_ping = self.now + td(seconds=86400)
  64. self.assertEqual(a2["last_ping"], self.now.isoformat())
  65. self.assertEqual(a2["next_ping"], next_ping.isoformat())
  66. def test_it_handles_options(self):
  67. r = self.client.options("/api/v1/checks/")
  68. self.assertEqual(r.status_code, 204)
  69. self.assertIn("GET", r["Access-Control-Allow-Methods"])
  70. def test_it_shows_only_users_checks(self):
  71. Check.objects.create(project=self.bobs_project, name="Bob 1")
  72. r = self.get()
  73. data = r.json()
  74. self.assertEqual(len(data["checks"]), 2)
  75. for check in data["checks"]:
  76. self.assertNotEqual(check["name"], "Bob 1")
  77. def test_it_works_with_tags_param(self):
  78. r = self.client.get("/api/v1/checks/?tag=a2-tag", HTTP_X_API_KEY="X" * 32)
  79. self.assertEqual(r.status_code, 200)
  80. doc = r.json()
  81. self.assertTrue("checks" in doc)
  82. self.assertEqual(len(doc["checks"]), 1)
  83. check = doc["checks"][0]
  84. self.assertEqual(check["name"], "Alice 2")
  85. self.assertEqual(check["tags"], "a2-tag")
  86. def test_it_filters_with_multiple_tags_param(self):
  87. r = self.client.get(
  88. "/api/v1/checks/?tag=a1-tag&tag=a1-additional-tag", HTTP_X_API_KEY="X" * 32
  89. )
  90. self.assertEqual(r.status_code, 200)
  91. doc = r.json()
  92. self.assertTrue("checks" in doc)
  93. self.assertEqual(len(doc["checks"]), 1)
  94. check = doc["checks"][0]
  95. self.assertEqual(check["name"], "Alice 1")
  96. self.assertEqual(check["tags"], "a1-tag a1-additional-tag")
  97. def test_it_does_not_match_tag_partially(self):
  98. r = self.client.get("/api/v1/checks/?tag=tag", HTTP_X_API_KEY="X" * 32)
  99. self.assertEqual(r.status_code, 200)
  100. doc = r.json()
  101. self.assertTrue("checks" in doc)
  102. self.assertEqual(len(doc["checks"]), 0)
  103. def test_non_existing_tags_filter_returns_empty_result(self):
  104. r = self.client.get(
  105. "/api/v1/checks/?tag=non_existing_tag_with_no_checks",
  106. HTTP_X_API_KEY="X" * 32,
  107. )
  108. self.assertEqual(r.status_code, 200)
  109. doc = r.json()
  110. self.assertTrue("checks" in doc)
  111. self.assertEqual(len(doc["checks"]), 0)
  112. def test_readonly_key_works(self):
  113. self.project.api_key_readonly = "R" * 32
  114. self.project.save()
  115. # Expect a query to check the API key, and a query to retrieve checks
  116. with self.assertNumQueries(2):
  117. r = self.client.get("/api/v1/checks/", HTTP_X_API_KEY="R" * 32)
  118. self.assertEqual(r.status_code, 200)
  119. # When using readonly keys, the ping URLs should not be exposed:
  120. self.assertNotContains(r, self.a1.url())