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.

141 lines
4.7 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.last_ping = self.now
  15. self.a1.n_pings = 1
  16. self.a1.status = "new"
  17. self.a1.tags = "a1-tag a1-additional-tag"
  18. self.a1.save()
  19. self.a2 = Check(user=self.alice, 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(user=self.alice)
  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. r = self.get()
  32. self.assertEqual(r.status_code, 200)
  33. doc = r.json()
  34. self.assertEqual(len(doc["checks"]), 2)
  35. a1 = None
  36. a2 = None
  37. for check in doc["checks"]:
  38. if check["name"] == "Alice 1":
  39. a1 = check
  40. if check["name"] == "Alice 2":
  41. a2 = check
  42. self.assertEqual(a1["timeout"], 3600)
  43. self.assertEqual(a1["grace"], 900)
  44. self.assertEqual(a1["ping_url"], self.a1.url())
  45. self.assertEqual(a1["last_ping"], self.now.isoformat())
  46. self.assertEqual(a1["n_pings"], 1)
  47. self.assertEqual(a1["status"], "new")
  48. self.assertEqual(a1["channels"], str(self.c1.code))
  49. update_url = settings.SITE_ROOT + "/api/v1/checks/%s" % self.a1.code
  50. pause_url = update_url + "/pause"
  51. self.assertEqual(a1["update_url"], update_url)
  52. self.assertEqual(a1["pause_url"], pause_url)
  53. next_ping = self.now + td(seconds=3600)
  54. self.assertEqual(a1["next_ping"], next_ping.isoformat())
  55. self.assertEqual(a2["timeout"], 86400)
  56. self.assertEqual(a2["grace"], 3600)
  57. self.assertEqual(a2["ping_url"], self.a2.url())
  58. self.assertEqual(a2["status"], "up")
  59. def test_it_shows_only_users_checks(self):
  60. bobs_check = Check(user=self.bob, name="Bob 1")
  61. bobs_check.save()
  62. r = self.get()
  63. data = r.json()
  64. self.assertEqual(len(data["checks"]), 2)
  65. for check in data["checks"]:
  66. self.assertNotEqual(check["name"], "Bob 1")
  67. def test_it_accepts_api_key_from_request_body(self):
  68. payload = json.dumps({"api_key": "X" * 32})
  69. r = self.client.generic("GET", "/api/v1/checks/", payload,
  70. content_type="application/json")
  71. self.assertEqual(r.status_code, 200)
  72. self.assertContains(r, "Alice")
  73. def test_it_works_with_tags_param(self):
  74. r = self.client.get("/api/v1/checks/?tag=a2-tag", HTTP_X_API_KEY="X" * 32)
  75. self.assertEqual(r.status_code, 200)
  76. doc = r.json()
  77. self.assertTrue("checks" in doc)
  78. self.assertEqual(len(doc["checks"]), 1)
  79. check = doc["checks"][0]
  80. self.assertEqual(check["name"], "Alice 2")
  81. self.assertEqual(check["tags"], "a2-tag")
  82. def test_it_filters_with_multiple_tags_param(self):
  83. r = self.client.get("/api/v1/checks/?tag=a1-tag&tag=a1-additional-tag", HTTP_X_API_KEY="X" * 32)
  84. self.assertEqual(r.status_code, 200)
  85. doc = r.json()
  86. self.assertTrue("checks" in doc)
  87. self.assertEqual(len(doc["checks"]), 1)
  88. check = doc["checks"][0]
  89. self.assertEqual(check["name"], "Alice 1")
  90. self.assertEqual(check["tags"], "a1-tag a1-additional-tag")
  91. def test_it_does_not_match_tag_partially(self):
  92. r = self.client.get("/api/v1/checks/?tag=tag", HTTP_X_API_KEY="X" * 32)
  93. self.assertEqual(r.status_code, 200)
  94. doc = r.json()
  95. self.assertTrue("checks" in doc)
  96. self.assertEqual(len(doc["checks"]), 0)
  97. def test_non_existing_tags_filter_returns_empty_result(self):
  98. r = self.client.get("/api/v1/checks/?tag=non_existing_tag_with_no_checks", 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_readonly_key_works(self):
  104. self.profile.api_key_readonly = "R" * 32
  105. self.profile.save()
  106. r = self.client.get("/api/v1/checks/", HTTP_X_API_KEY="R" * 32)
  107. self.assertEqual(r.status_code, 200)