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.

301 lines
9.8 KiB

  1. import json
  2. from hc.api.models import Channel, Check
  3. from hc.test import BaseTestCase
  4. class CreateCheckTestCase(BaseTestCase):
  5. URL = "/api/v1/checks/"
  6. def post(self, data, expected_error=None, expected_fragment=None):
  7. r = self.client.post(self.URL, data, content_type="application/json")
  8. if expected_error:
  9. self.assertEqual(r.status_code, 400)
  10. self.assertEqual(r.json()["error"], expected_error)
  11. if expected_fragment:
  12. self.assertEqual(r.status_code, 400)
  13. self.assertIn(expected_fragment, r.json()["error"])
  14. return r
  15. def test_it_works(self):
  16. r = self.post(
  17. {
  18. "api_key": "X" * 32,
  19. "name": "Foo",
  20. "tags": "bar,baz",
  21. "timeout": 3600,
  22. "grace": 60,
  23. }
  24. )
  25. self.assertEqual(r.status_code, 201)
  26. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  27. doc = r.json()
  28. assert "ping_url" in doc
  29. self.assertEqual(doc["name"], "Foo")
  30. self.assertEqual(doc["tags"], "bar,baz")
  31. self.assertEqual(doc["last_ping"], None)
  32. self.assertEqual(doc["n_pings"], 0)
  33. self.assertEqual(doc["methods"], "")
  34. self.assertTrue("schedule" not in doc)
  35. self.assertTrue("tz" not in doc)
  36. check = Check.objects.get()
  37. self.assertEqual(check.name, "Foo")
  38. self.assertEqual(check.tags, "bar,baz")
  39. self.assertEqual(check.methods, "")
  40. self.assertEqual(check.timeout.total_seconds(), 3600)
  41. self.assertEqual(check.grace.total_seconds(), 60)
  42. self.assertEqual(check.project, self.project)
  43. def test_it_handles_options(self):
  44. r = self.client.options(self.URL)
  45. self.assertEqual(r.status_code, 204)
  46. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  47. def test_30_days_works(self):
  48. r = self.post(
  49. {"api_key": "X" * 32, "name": "Foo", "timeout": 2592000, "grace": 2592000}
  50. )
  51. self.assertEqual(r.status_code, 201)
  52. check = Check.objects.get()
  53. self.assertEqual(check.timeout.total_seconds(), 2592000)
  54. self.assertEqual(check.grace.total_seconds(), 2592000)
  55. def test_it_accepts_api_key_in_header(self):
  56. payload = json.dumps({"name": "Foo"})
  57. r = self.client.post(
  58. self.URL, payload, content_type="application/json", HTTP_X_API_KEY="X" * 32
  59. )
  60. self.assertEqual(r.status_code, 201)
  61. def test_it_assigns_channels(self):
  62. channel = Channel.objects.create(project=self.project)
  63. r = self.post({"api_key": "X" * 32, "channels": "*"})
  64. self.assertEqual(r.status_code, 201)
  65. check = Check.objects.get()
  66. self.assertEqual(check.channel_set.get(), channel)
  67. def test_it_sets_channel_by_name(self):
  68. channel = Channel.objects.create(project=self.project, name="alerts")
  69. r = self.post({"api_key": "X" * 32, "channels": "alerts"})
  70. self.assertEqual(r.status_code, 201)
  71. check = Check.objects.get()
  72. assigned_channel = check.channel_set.get()
  73. self.assertEqual(assigned_channel, channel)
  74. def test_it_sets_channel_by_name_formatted_as_uuid(self):
  75. name = "102eaa82-a274-4b15-a499-c1bb6bbcd7b6"
  76. channel = Channel.objects.create(project=self.project, name=name)
  77. r = self.post({"api_key": "X" * 32, "channels": name})
  78. self.assertEqual(r.status_code, 201)
  79. check = Check.objects.get()
  80. assigned_channel = check.channel_set.get()
  81. self.assertEqual(assigned_channel, channel)
  82. def test_it_handles_channel_lookup_by_name_with_no_results(self):
  83. r = self.post({"api_key": "X" * 32, "channels": "abc"})
  84. self.assertEqual(r.status_code, 400)
  85. self.assertEqual(r.json()["error"], "invalid channel identifier: abc")
  86. # The check should not have been saved
  87. self.assertFalse(Check.objects.exists())
  88. def test_it_handles_channel_lookup_by_name_with_multiple_results(self):
  89. Channel.objects.create(project=self.project, name="foo")
  90. Channel.objects.create(project=self.project, name="foo")
  91. r = self.post({"api_key": "X" * 32, "channels": "foo"})
  92. self.assertEqual(r.status_code, 400)
  93. self.assertEqual(r.json()["error"], "non-unique channel identifier: foo")
  94. # The check should not have been saved
  95. self.assertFalse(Check.objects.exists())
  96. def test_it_rejects_multiple_empty_channel_names(self):
  97. Channel.objects.create(project=self.project, name="")
  98. r = self.post({"api_key": "X" * 32, "channels": ","})
  99. self.assertEqual(r.status_code, 400)
  100. self.assertEqual(r.json()["error"], "empty channel identifier")
  101. # The check should not have been saved
  102. self.assertFalse(Check.objects.exists())
  103. def test_it_supports_unique(self):
  104. check = Check.objects.create(project=self.project, name="Foo")
  105. r = self.post(
  106. {"api_key": "X" * 32, "name": "Foo", "tags": "bar", "unique": ["name"]}
  107. )
  108. # Expect 200 instead of 201
  109. self.assertEqual(r.status_code, 200)
  110. # And there should be only one check in the database:
  111. self.assertEqual(Check.objects.count(), 1)
  112. # The tags field should have a value now:
  113. check.refresh_from_db()
  114. self.assertEqual(check.tags, "bar")
  115. def test_it_handles_missing_request_body(self):
  116. r = self.client.post(self.URL, content_type="application/json")
  117. self.assertEqual(r.status_code, 401)
  118. self.assertEqual(r.json()["error"], "missing api key")
  119. def test_it_handles_invalid_json(self):
  120. r = self.client.post(
  121. self.URL, "this is not json", content_type="application/json"
  122. )
  123. self.assertEqual(r.status_code, 400)
  124. self.assertEqual(r.json()["error"], "could not parse request body")
  125. def test_it_rejects_wrong_api_key(self):
  126. r = self.post({"api_key": "Y" * 32})
  127. self.assertEqual(r.status_code, 401)
  128. def test_it_rejects_small_timeout(self):
  129. self.post(
  130. {"api_key": "X" * 32, "timeout": 0},
  131. expected_fragment="timeout is too small",
  132. )
  133. def test_it_rejects_large_timeout(self):
  134. self.post(
  135. {"api_key": "X" * 32, "timeout": 2592001},
  136. expected_fragment="timeout is too large",
  137. )
  138. def test_it_rejects_non_number_timeout(self):
  139. self.post(
  140. {"api_key": "X" * 32, "timeout": "oops"},
  141. expected_fragment="timeout is not a number",
  142. )
  143. def test_it_rejects_non_string_name(self):
  144. self.post(
  145. {"api_key": "X" * 32, "name": False},
  146. expected_fragment="name is not a string",
  147. )
  148. def test_it_rejects_long_name(self):
  149. self.post(
  150. {"api_key": "X" * 32, "name": "01234567890" * 20},
  151. expected_fragment="name is too long",
  152. )
  153. def test_unique_accepts_only_specific_values(self):
  154. self.post(
  155. {"api_key": "X" * 32, "name": "Foo", "unique": ["status"]},
  156. expected_fragment="unexpected value",
  157. )
  158. def test_it_rejects_bad_unique_values(self):
  159. self.post(
  160. {"api_key": "X" * 32, "name": "Foo", "unique": "not a list"},
  161. expected_fragment="not an array",
  162. )
  163. def test_it_supports_cron_syntax(self):
  164. r = self.post(
  165. {
  166. "api_key": "X" * 32,
  167. "schedule": "5 * * * *",
  168. "tz": "Europe/Riga",
  169. "grace": 60,
  170. }
  171. )
  172. self.assertEqual(r.status_code, 201)
  173. doc = r.json()
  174. self.assertEqual(doc["schedule"], "5 * * * *")
  175. self.assertEqual(doc["tz"], "Europe/Riga")
  176. self.assertEqual(doc["grace"], 60)
  177. self.assertTrue("timeout" not in doc)
  178. def test_it_validates_cron_expression(self):
  179. r = self.post(
  180. {
  181. "api_key": "X" * 32,
  182. "schedule": "not-a-cron-expression",
  183. "tz": "Europe/Riga",
  184. "grace": 60,
  185. }
  186. )
  187. self.assertEqual(r.status_code, 400)
  188. def test_it_validates_timezone(self):
  189. r = self.post(
  190. {
  191. "api_key": "X" * 32,
  192. "schedule": "* * * * *",
  193. "tz": "not-a-timezone",
  194. "grace": 60,
  195. }
  196. )
  197. self.assertEqual(r.status_code, 400)
  198. def test_it_sets_default_timeout(self):
  199. r = self.post({"api_key": "X" * 32})
  200. self.assertEqual(r.status_code, 201)
  201. doc = r.json()
  202. self.assertEqual(doc["timeout"], 86400)
  203. def test_it_obeys_check_limit(self):
  204. self.profile.check_limit = 0
  205. self.profile.save()
  206. r = self.post({"api_key": "X" * 32})
  207. self.assertEqual(r.status_code, 403)
  208. def test_it_rejects_readonly_key(self):
  209. self.project.api_key_readonly = "R" * 32
  210. self.project.save()
  211. r = self.post({"api_key": "R" * 32, "name": "Foo"})
  212. self.assertEqual(r.status_code, 401)
  213. def test_it_sets_manual_resume(self):
  214. r = self.post({"api_key": "X" * 32, "manual_resume": True})
  215. self.assertEqual(r.status_code, 201)
  216. check = Check.objects.get()
  217. self.assertTrue(check.manual_resume)
  218. def test_it_rejects_non_boolean_manual_resume(self):
  219. r = self.post({"api_key": "X" * 32, "manual_resume": "surprise"})
  220. self.assertEqual(r.status_code, 400)
  221. def test_it_sets_methods(self):
  222. r = self.post({"api_key": "X" * 32, "methods": "POST"})
  223. self.assertEqual(r.status_code, 201)
  224. check = Check.objects.get()
  225. self.assertEqual(check.methods, "POST")
  226. def test_it_rejects_bad_methods_value(self):
  227. r = self.post({"api_key": "X" * 32, "methods": "bad-value"})
  228. self.assertEqual(r.status_code, 400)