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.

164 lines
5.5 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(ListChecksTestCase, self).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_accepts_api_key_from_request_body(self):
  78. payload = json.dumps({"api_key": "X" * 32})
  79. r = self.client.generic(
  80. "GET", "/api/v1/checks/", payload, content_type="application/json"
  81. )
  82. self.assertEqual(r.status_code, 200)
  83. self.assertContains(r, "Alice")
  84. def test_it_works_with_tags_param(self):
  85. r = self.client.get("/api/v1/checks/?tag=a2-tag", HTTP_X_API_KEY="X" * 32)
  86. self.assertEqual(r.status_code, 200)
  87. doc = r.json()
  88. self.assertTrue("checks" in doc)
  89. self.assertEqual(len(doc["checks"]), 1)
  90. check = doc["checks"][0]
  91. self.assertEqual(check["name"], "Alice 2")
  92. self.assertEqual(check["tags"], "a2-tag")
  93. def test_it_filters_with_multiple_tags_param(self):
  94. r = self.client.get(
  95. "/api/v1/checks/?tag=a1-tag&tag=a1-additional-tag", HTTP_X_API_KEY="X" * 32
  96. )
  97. self.assertEqual(r.status_code, 200)
  98. doc = r.json()
  99. self.assertTrue("checks" in doc)
  100. self.assertEqual(len(doc["checks"]), 1)
  101. check = doc["checks"][0]
  102. self.assertEqual(check["name"], "Alice 1")
  103. self.assertEqual(check["tags"], "a1-tag a1-additional-tag")
  104. def test_it_does_not_match_tag_partially(self):
  105. r = self.client.get("/api/v1/checks/?tag=tag", HTTP_X_API_KEY="X" * 32)
  106. self.assertEqual(r.status_code, 200)
  107. doc = r.json()
  108. self.assertTrue("checks" in doc)
  109. self.assertEqual(len(doc["checks"]), 0)
  110. def test_non_existing_tags_filter_returns_empty_result(self):
  111. r = self.client.get(
  112. "/api/v1/checks/?tag=non_existing_tag_with_no_checks",
  113. HTTP_X_API_KEY="X" * 32,
  114. )
  115. self.assertEqual(r.status_code, 200)
  116. doc = r.json()
  117. self.assertTrue("checks" in doc)
  118. self.assertEqual(len(doc["checks"]), 0)
  119. def test_readonly_key_works(self):
  120. self.project.api_key_readonly = "R" * 32
  121. self.project.save()
  122. # Expect a query to check the API key, and a query to retrieve checks
  123. with self.assertNumQueries(2):
  124. r = self.client.get("/api/v1/checks/", HTTP_X_API_KEY="R" * 32)
  125. self.assertEqual(r.status_code, 200)
  126. # When using readonly keys, the ping URLs should not be exposed:
  127. self.assertNotContains(r, self.a1.url())