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.

322 lines
12 KiB

  1. from datetime import timedelta as td
  2. import uuid
  3. from django.utils.timezone import now
  4. from hc.api.models import Channel, Check
  5. from hc.test import BaseTestCase
  6. class UpdateCheckTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.check = Check.objects.create(project=self.project)
  10. def post(self, code, data):
  11. url = "/api/v1/checks/%s" % code
  12. return self.client.post(url, data, content_type="application/json")
  13. def test_it_works(self):
  14. self.check.last_ping = now()
  15. self.check.status = "up"
  16. self.check.save()
  17. r = self.post(
  18. self.check.code,
  19. {
  20. "api_key": "X" * 32,
  21. "name": "Foo",
  22. "tags": "bar,baz",
  23. "desc": "My description",
  24. "timeout": 3600,
  25. "grace": 60,
  26. },
  27. )
  28. self.assertEqual(r.status_code, 200)
  29. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  30. doc = r.json()
  31. assert "ping_url" in doc
  32. self.assertEqual(doc["name"], "Foo")
  33. self.assertEqual(doc["tags"], "bar,baz")
  34. self.assertEqual(doc["desc"], "My description")
  35. self.assertEqual(doc["n_pings"], 0)
  36. self.assertTrue("schedule" not in doc)
  37. self.assertTrue("tz" not in doc)
  38. self.assertEqual(Check.objects.count(), 1)
  39. self.check.refresh_from_db()
  40. self.assertEqual(self.check.name, "Foo")
  41. self.assertEqual(self.check.tags, "bar,baz")
  42. self.assertEqual(self.check.timeout.total_seconds(), 3600)
  43. self.assertEqual(self.check.grace.total_seconds(), 60)
  44. # alert_after should be updated too
  45. expected_aa = self.check.last_ping + td(seconds=3600 + 60)
  46. self.assertEqual(self.check.alert_after, expected_aa)
  47. def test_it_handles_options(self):
  48. r = self.client.options("/api/v1/checks/%s" % self.check.code)
  49. self.assertEqual(r.status_code, 204)
  50. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  51. def test_it_unassigns_channels(self):
  52. Channel.objects.create(project=self.project)
  53. self.check.assign_all_channels()
  54. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": ""})
  55. self.assertEqual(r.status_code, 200)
  56. check = Check.objects.get()
  57. self.assertEqual(check.channel_set.count(), 0)
  58. def test_it_handles_invalid_uuid(self):
  59. r = self.post("not-an-uuid", {"api_key": "X" * 32})
  60. self.assertEqual(r.status_code, 404)
  61. def test_it_handles_missing_check(self):
  62. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  63. r = self.post(made_up_code, {"api_key": "X" * 32})
  64. self.assertEqual(r.status_code, 404)
  65. def test_it_validates_ownership(self):
  66. check = Check.objects.create(project=self.bobs_project, status="up")
  67. r = self.post(check.code, {"api_key": "X" * 32})
  68. self.assertEqual(r.status_code, 403)
  69. def test_it_updates_cron_to_simple(self):
  70. self.check.kind = "cron"
  71. self.check.schedule = "5 * * * *"
  72. self.check.save()
  73. r = self.post(self.check.code, {"api_key": "X" * 32, "timeout": 3600})
  74. self.assertEqual(r.status_code, 200)
  75. self.check.refresh_from_db()
  76. self.assertEqual(self.check.kind, "simple")
  77. def test_it_sets_single_channel(self):
  78. channel = Channel.objects.create(project=self.project)
  79. # Create another channel so we can test that only the first one
  80. # gets assigned:
  81. Channel.objects.create(project=self.project)
  82. r = self.post(
  83. self.check.code, {"api_key": "X" * 32, "channels": str(channel.code)}
  84. )
  85. self.assertEqual(r.status_code, 200)
  86. self.check.refresh_from_db()
  87. self.assertEqual(self.check.channel_set.count(), 1)
  88. self.assertEqual(self.check.channel_set.first().code, channel.code)
  89. def test_it_sets_the_channel_only_once(self):
  90. channel = Channel.objects.create(project=self.project)
  91. duplicates = "%s,%s" % (channel.code, channel.code)
  92. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": duplicates})
  93. self.assertEqual(r.status_code, 200)
  94. self.check.refresh_from_db()
  95. self.assertEqual(self.check.channel_set.count(), 1)
  96. def test_it_sets_channel_by_name(self):
  97. channel = Channel.objects.create(project=self.project, name="alerts")
  98. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "alerts"})
  99. self.assertEqual(r.status_code, 200)
  100. self.check.refresh_from_db()
  101. self.assertEqual(self.check.channel_set.count(), 1)
  102. self.assertEqual(self.check.channel_set.first().code, channel.code)
  103. def test_it_sets_channel_by_name_formatted_as_uuid(self):
  104. name = "102eaa82-a274-4b15-a499-c1bb6bbcd7b6"
  105. channel = Channel.objects.create(project=self.project, name=name)
  106. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": name})
  107. self.assertEqual(r.status_code, 200)
  108. self.check.refresh_from_db()
  109. self.assertEqual(self.check.channel_set.count(), 1)
  110. self.assertEqual(self.check.channel_set.first().code, channel.code)
  111. def test_it_handles_comma_separated_channel_codes(self):
  112. c1 = Channel.objects.create(project=self.project)
  113. c2 = Channel.objects.create(project=self.project)
  114. r = self.post(
  115. self.check.code,
  116. {"api_key": "X" * 32, "channels": "%s,%s" % (c1.code, c2.code)},
  117. )
  118. self.assertEqual(r.status_code, 200)
  119. self.check.refresh_from_db()
  120. self.assertEqual(self.check.channel_set.count(), 2)
  121. def test_it_handles_asterix(self):
  122. Channel.objects.create(project=self.project)
  123. Channel.objects.create(project=self.project)
  124. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "*"})
  125. self.assertEqual(r.status_code, 200)
  126. self.check.refresh_from_db()
  127. self.assertEqual(self.check.channel_set.count(), 2)
  128. def test_it_ignores_channels_if_channels_key_missing(self):
  129. Channel.objects.create(project=self.project)
  130. self.check.assign_all_channels()
  131. r = self.post(self.check.code, {"api_key": "X" * 32})
  132. self.assertEqual(r.status_code, 200)
  133. check = Check.objects.get()
  134. self.assertEqual(check.channel_set.count(), 1)
  135. def test_it_rejects_bad_channel_code(self):
  136. payload = {"api_key": "X" * 32, "channels": "abc", "name": "New Name"}
  137. r = self.post(self.check.code, payload,)
  138. self.assertEqual(r.status_code, 400)
  139. self.assertEqual(r.json()["error"], "invalid channel identifier: abc")
  140. # The name should be unchanged
  141. self.check.refresh_from_db()
  142. self.assertEqual(self.check.name, "")
  143. def test_it_rejects_missing_channel(self):
  144. code = str(uuid.uuid4())
  145. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": code})
  146. self.assertEqual(r.status_code, 400)
  147. self.assertEqual(r.json()["error"], "invalid channel identifier: " + code)
  148. self.check.refresh_from_db()
  149. self.assertEqual(self.check.channel_set.count(), 0)
  150. def test_it_rejects_channel_from_another_project(self):
  151. charlies_channel = Channel.objects.create(project=self.charlies_project)
  152. code = str(charlies_channel.code)
  153. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": code})
  154. self.assertEqual(r.status_code, 400)
  155. self.assertEqual(r.json()["error"], "invalid channel identifier: " + code)
  156. self.check.refresh_from_db()
  157. self.assertEqual(self.check.channel_set.count(), 0)
  158. def test_it_handles_channel_lookup_by_name_with_no_results(self):
  159. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "foo"})
  160. self.assertEqual(r.status_code, 400)
  161. self.assertEqual(r.json()["error"], "invalid channel identifier: foo")
  162. self.check.refresh_from_db()
  163. self.assertEqual(self.check.channel_set.count(), 0)
  164. def test_it_handles_channel_lookup_by_name_with_multiple_results(self):
  165. Channel.objects.create(project=self.project, name="foo")
  166. Channel.objects.create(project=self.project, name="foo")
  167. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "foo"})
  168. self.assertEqual(r.status_code, 400)
  169. self.assertEqual(r.json()["error"], "non-unique channel identifier: foo")
  170. self.check.refresh_from_db()
  171. self.assertEqual(self.check.channel_set.count(), 0)
  172. def test_it_rejects_multiple_empty_channel_names(self):
  173. Channel.objects.create(project=self.project, name="")
  174. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": ","})
  175. self.assertEqual(r.status_code, 400)
  176. self.assertEqual(r.json()["error"], "empty channel identifier")
  177. self.check.refresh_from_db()
  178. self.assertEqual(self.check.channel_set.count(), 0)
  179. def test_it_rejects_non_string_channels_key(self):
  180. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": None})
  181. self.assertEqual(r.status_code, 400)
  182. def test_it_rejects_non_string_desc(self):
  183. r = self.post(self.check.code, {"api_key": "X" * 32, "desc": 123})
  184. self.assertEqual(r.status_code, 400)
  185. def test_it_validates_cron_expression(self):
  186. self.check.kind = "cron"
  187. self.check.schedule = "5 * * * *"
  188. self.check.save()
  189. samples = ["* invalid *", "1,2 61 * * *", "0 0 31 2 *"]
  190. for sample in samples:
  191. r = self.post(self.check.code, {"api_key": "X" * 32, "schedule": sample})
  192. self.assertEqual(r.status_code, 400, "Did not reject '%s'" % sample)
  193. # Schedule should be unchanged
  194. self.check.refresh_from_db()
  195. self.assertEqual(self.check.schedule, "5 * * * *")
  196. def test_it_rejects_readonly_key(self):
  197. self.project.api_key_readonly = "R" * 32
  198. self.project.save()
  199. r = self.post(self.check.code, {"api_key": "R" * 32, "name": "Foo"})
  200. self.assertEqual(r.status_code, 401)
  201. def test_it_sets_manual_resume_to_true(self):
  202. r = self.post(self.check.code, {"api_key": "X" * 32, "manual_resume": True})
  203. self.assertEqual(r.status_code, 200)
  204. self.check.refresh_from_db()
  205. self.assertTrue(self.check.manual_resume)
  206. def test_it_sets_manual_resume_to_false(self):
  207. self.check.manual_resume = True
  208. self.check.save()
  209. r = self.post(self.check.code, {"api_key": "X" * 32, "manual_resume": False})
  210. self.assertEqual(r.status_code, 200)
  211. self.check.refresh_from_db()
  212. self.assertFalse(self.check.manual_resume)
  213. def test_it_sets_methods(self):
  214. r = self.post(self.check.code, {"api_key": "X" * 32, "methods": "POST"})
  215. self.assertEqual(r.status_code, 200)
  216. self.check.refresh_from_db()
  217. self.assertEqual(self.check.methods, "POST")
  218. def test_it_clears_methods(self):
  219. self.check.methods = "POST"
  220. self.check.save()
  221. # Client supplies an empty string: we should save it
  222. r = self.post(self.check.code, {"api_key": "X" * 32, "methods": ""})
  223. self.assertEqual(r.status_code, 200)
  224. self.check.refresh_from_db()
  225. self.assertEqual(self.check.methods, "")
  226. def test_it_leaves_methods_unchanged(self):
  227. self.check.methods = "POST"
  228. self.check.save()
  229. # Client omits the methods key: we should leave it unchanged
  230. r = self.post(self.check.code, {"api_key": "X" * 32})
  231. self.assertEqual(r.status_code, 200)
  232. self.check.refresh_from_db()
  233. self.assertEqual(self.check.methods, "POST")
  234. def test_it_rejects_bad_methods_value(self):
  235. r = self.post(self.check.code, {"api_key": "X" * 32, "methods": "bad-value"})
  236. self.assertEqual(r.status_code, 400)