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.

175 lines
5.9 KiB

  1. import uuid
  2. from hc.api.models import Channel, Check
  3. from hc.test import BaseTestCase
  4. class UpdateCheckTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(UpdateCheckTestCase, self).setUp()
  7. self.check = Check.objects.create(project=self.project)
  8. def post(self, code, data):
  9. url = "/api/v1/checks/%s" % code
  10. return self.client.post(url, data, content_type="application/json")
  11. def test_it_works(self):
  12. r = self.post(
  13. self.check.code,
  14. {
  15. "api_key": "X" * 32,
  16. "name": "Foo",
  17. "tags": "bar,baz",
  18. "desc": "My description",
  19. "timeout": 3600,
  20. "grace": 60,
  21. },
  22. )
  23. self.assertEqual(r.status_code, 200)
  24. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  25. doc = r.json()
  26. assert "ping_url" in doc
  27. self.assertEqual(doc["name"], "Foo")
  28. self.assertEqual(doc["tags"], "bar,baz")
  29. self.assertEqual(doc["desc"], "My description")
  30. self.assertEqual(doc["last_ping"], None)
  31. self.assertEqual(doc["n_pings"], 0)
  32. self.assertTrue("schedule" not in doc)
  33. self.assertTrue("tz" not in doc)
  34. self.assertEqual(Check.objects.count(), 1)
  35. self.check.refresh_from_db()
  36. self.assertEqual(self.check.name, "Foo")
  37. self.assertEqual(self.check.tags, "bar,baz")
  38. self.assertEqual(self.check.timeout.total_seconds(), 3600)
  39. self.assertEqual(self.check.grace.total_seconds(), 60)
  40. def test_it_handles_options(self):
  41. r = self.client.options("/api/v1/checks/%s" % self.check.code)
  42. self.assertEqual(r.status_code, 204)
  43. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  44. def test_it_unassigns_channels(self):
  45. Channel.objects.create(project=self.project)
  46. self.check.assign_all_channels()
  47. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": ""})
  48. self.assertEqual(r.status_code, 200)
  49. check = Check.objects.get()
  50. self.assertEqual(check.channel_set.count(), 0)
  51. def test_it_requires_post(self):
  52. url = "/api/v1/checks/%s" % self.check.code
  53. r = self.client.get(url, HTTP_X_API_KEY="X" * 32)
  54. self.assertEqual(r.status_code, 405)
  55. def test_it_handles_invalid_uuid(self):
  56. r = self.post("not-an-uuid", {"api_key": "X" * 32})
  57. self.assertEqual(r.status_code, 404)
  58. def test_it_handles_missing_check(self):
  59. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  60. r = self.post(made_up_code, {"api_key": "X" * 32})
  61. self.assertEqual(r.status_code, 404)
  62. def test_it_validates_ownership(self):
  63. check = Check.objects.create(project=self.bobs_project, status="up")
  64. r = self.post(check.code, {"api_key": "X" * 32})
  65. self.assertEqual(r.status_code, 403)
  66. def test_it_updates_cron_to_simple(self):
  67. self.check.kind = "cron"
  68. self.check.schedule = "5 * * * *"
  69. self.check.save()
  70. r = self.post(self.check.code, {"api_key": "X" * 32, "timeout": 3600})
  71. self.assertEqual(r.status_code, 200)
  72. self.check.refresh_from_db()
  73. self.assertEqual(self.check.kind, "simple")
  74. def test_it_sets_single_channel(self):
  75. channel = Channel.objects.create(project=self.project)
  76. # Create another channel so we can test that only the first one
  77. # gets assigned:
  78. Channel.objects.create(project=self.project)
  79. r = self.post(
  80. self.check.code, {"api_key": "X" * 32, "channels": str(channel.code)}
  81. )
  82. self.assertEqual(r.status_code, 200)
  83. self.check.refresh_from_db()
  84. self.assertEqual(self.check.channel_set.count(), 1)
  85. self.assertEqual(self.check.channel_set.first().code, channel.code)
  86. def test_it_handles_comma_separated_channel_codes(self):
  87. c1 = Channel.objects.create(project=self.project)
  88. c2 = Channel.objects.create(project=self.project)
  89. r = self.post(
  90. self.check.code,
  91. {"api_key": "X" * 32, "channels": "%s,%s" % (c1.code, c2.code)},
  92. )
  93. self.assertEqual(r.status_code, 200)
  94. self.check.refresh_from_db()
  95. self.assertEqual(self.check.channel_set.count(), 2)
  96. def test_it_handles_asterix(self):
  97. Channel.objects.create(project=self.project)
  98. Channel.objects.create(project=self.project)
  99. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "*"})
  100. self.assertEqual(r.status_code, 200)
  101. self.check.refresh_from_db()
  102. self.assertEqual(self.check.channel_set.count(), 2)
  103. def test_it_ignores_channels_if_channels_key_missing(self):
  104. Channel.objects.create(project=self.project)
  105. self.check.assign_all_channels()
  106. r = self.post(self.check.code, {"api_key": "X" * 32})
  107. self.assertEqual(r.status_code, 200)
  108. check = Check.objects.get()
  109. self.assertEqual(check.channel_set.count(), 1)
  110. def test_it_rejects_bad_channel_code(self):
  111. r = self.post(
  112. self.check.code, {"api_key": "X" * 32, "channels": str(uuid.uuid4())}
  113. )
  114. self.assertEqual(r.status_code, 400)
  115. self.check.refresh_from_db()
  116. self.assertEqual(self.check.channel_set.count(), 0)
  117. def test_it_rejects_non_uuid_channel_code(self):
  118. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "foo"})
  119. self.assertEqual(r.status_code, 400)
  120. self.check.refresh_from_db()
  121. self.assertEqual(self.check.channel_set.count(), 0)
  122. def test_it_rejects_non_string_channels_key(self):
  123. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": None})
  124. self.assertEqual(r.status_code, 400)
  125. def test_it_rejects_non_string_desc(self):
  126. r = self.post(
  127. self.check.code, {"api_key": "X" * 32, "desc": 123}
  128. )
  129. self.assertEqual(r.status_code, 400)
  130. self.check.refresh_from_db()