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.

146 lines
5.0 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(user=self.alice, 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.save()
  18. self.a2 = Check(user=self.alice, name="Alice 2")
  19. self.a2.timeout = td(seconds=86400)
  20. self.a2.grace = td(seconds=3600)
  21. self.a2.last_ping = self.now
  22. self.a2.status = "up"
  23. self.a2.tags = "a2-tag"
  24. self.a2.save()
  25. self.c1 = Channel.objects.create(user=self.alice)
  26. self.a1.channel_set.add(self.c1)
  27. def get(self):
  28. return self.client.get("/api/v1/checks/", HTTP_X_API_KEY="X" * 32)
  29. def test_it_works(self):
  30. r = self.get()
  31. self.assertEqual(r.status_code, 200)
  32. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  33. doc = r.json()
  34. self.assertEqual(len(doc["checks"]), 2)
  35. by_name = {}
  36. for check in doc["checks"]:
  37. by_name[check["name"]] = check
  38. a1 = by_name["Alice 1"]
  39. self.assertEqual(a1["timeout"], 3600)
  40. self.assertEqual(a1["grace"], 900)
  41. self.assertEqual(a1["ping_url"], self.a1.url())
  42. self.assertEqual(a1["last_ping"], None)
  43. self.assertEqual(a1["n_pings"], 0)
  44. self.assertEqual(a1["status"], "new")
  45. self.assertEqual(a1["channels"], str(self.c1.code))
  46. update_url = settings.SITE_ROOT + "/api/v1/checks/%s" % self.a1.code
  47. pause_url = update_url + "/pause"
  48. self.assertEqual(a1["update_url"], update_url)
  49. self.assertEqual(a1["pause_url"], pause_url)
  50. self.assertEqual(a1["next_ping"], None)
  51. a2 = by_name["Alice 2"]
  52. self.assertEqual(a2["timeout"], 86400)
  53. self.assertEqual(a2["grace"], 3600)
  54. self.assertEqual(a2["ping_url"], self.a2.url())
  55. self.assertEqual(a2["status"], "up")
  56. next_ping = self.now + td(seconds=86400)
  57. self.assertEqual(a2["last_ping"], self.now.isoformat())
  58. self.assertEqual(a2["next_ping"], next_ping.isoformat())
  59. def test_it_handles_options(self):
  60. r = self.client.options("/api/v1/checks/")
  61. self.assertEqual(r.status_code, 204)
  62. self.assertIn("GET", r["Access-Control-Allow-Methods"])
  63. def test_it_shows_only_users_checks(self):
  64. bobs_check = Check(user=self.bob, name="Bob 1")
  65. bobs_check.save()
  66. r = self.get()
  67. data = r.json()
  68. self.assertEqual(len(data["checks"]), 2)
  69. for check in data["checks"]:
  70. self.assertNotEqual(check["name"], "Bob 1")
  71. def test_it_accepts_api_key_from_request_body(self):
  72. payload = json.dumps({"api_key": "X" * 32})
  73. r = self.client.generic("GET", "/api/v1/checks/", payload,
  74. content_type="application/json")
  75. self.assertEqual(r.status_code, 200)
  76. self.assertContains(r, "Alice")
  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("/api/v1/checks/?tag=a1-tag&tag=a1-additional-tag", HTTP_X_API_KEY="X" * 32)
  88. self.assertEqual(r.status_code, 200)
  89. doc = r.json()
  90. self.assertTrue("checks" in doc)
  91. self.assertEqual(len(doc["checks"]), 1)
  92. check = doc["checks"][0]
  93. self.assertEqual(check["name"], "Alice 1")
  94. self.assertEqual(check["tags"], "a1-tag a1-additional-tag")
  95. def test_it_does_not_match_tag_partially(self):
  96. r = self.client.get("/api/v1/checks/?tag=tag", HTTP_X_API_KEY="X" * 32)
  97. self.assertEqual(r.status_code, 200)
  98. doc = r.json()
  99. self.assertTrue("checks" in doc)
  100. self.assertEqual(len(doc["checks"]), 0)
  101. def test_non_existing_tags_filter_returns_empty_result(self):
  102. r = self.client.get("/api/v1/checks/?tag=non_existing_tag_with_no_checks", HTTP_X_API_KEY="X" * 32)
  103. self.assertEqual(r.status_code, 200)
  104. doc = r.json()
  105. self.assertTrue("checks" in doc)
  106. self.assertEqual(len(doc["checks"]), 0)
  107. def test_readonly_key_works(self):
  108. self.profile.api_key_readonly = "R" * 32
  109. self.profile.save()
  110. r = self.client.get("/api/v1/checks/", HTTP_X_API_KEY="R" * 32)
  111. self.assertEqual(r.status_code, 200)