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.

227 lines
7.0 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.assertTrue("schedule" not in doc)
  34. self.assertTrue("tz" not in doc)
  35. check = Check.objects.get()
  36. self.assertEqual(check.name, "Foo")
  37. self.assertEqual(check.tags, "bar,baz")
  38. self.assertEqual(check.timeout.total_seconds(), 3600)
  39. self.assertEqual(check.grace.total_seconds(), 60)
  40. self.assertEqual(check.project, self.project)
  41. def test_it_handles_options(self):
  42. r = self.client.options(self.URL)
  43. self.assertEqual(r.status_code, 204)
  44. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  45. def test_30_days_works(self):
  46. r = self.post(
  47. {"api_key": "X" * 32, "name": "Foo", "timeout": 2592000, "grace": 2592000}
  48. )
  49. self.assertEqual(r.status_code, 201)
  50. check = Check.objects.get()
  51. self.assertEqual(check.timeout.total_seconds(), 2592000)
  52. self.assertEqual(check.grace.total_seconds(), 2592000)
  53. def test_it_accepts_api_key_in_header(self):
  54. payload = json.dumps({"name": "Foo"})
  55. r = self.client.post(
  56. self.URL, payload, content_type="application/json", HTTP_X_API_KEY="X" * 32
  57. )
  58. self.assertEqual(r.status_code, 201)
  59. def test_it_assigns_channels(self):
  60. channel = Channel.objects.create(project=self.project)
  61. r = self.post({"api_key": "X" * 32, "channels": "*"})
  62. self.assertEqual(r.status_code, 201)
  63. check = Check.objects.get()
  64. self.assertEqual(check.channel_set.get(), channel)
  65. def test_it_rejects_bad_channel_code(self):
  66. r = self.post({"api_key": "X" * 32, "channels": "abc"})
  67. self.assertEqual(r.status_code, 400)
  68. self.assertEqual(r.json()["error"], "invalid channel identifier: abc")
  69. # The check should not have been saved
  70. self.assertFalse(Check.objects.exists())
  71. def test_it_supports_unique(self):
  72. Check.objects.create(project=self.project, name="Foo")
  73. r = self.post({"api_key": "X" * 32, "name": "Foo", "unique": ["name"]})
  74. # Expect 200 instead of 201
  75. self.assertEqual(r.status_code, 200)
  76. # And there should be only one check in the database:
  77. self.assertEqual(Check.objects.count(), 1)
  78. def test_it_handles_missing_request_body(self):
  79. r = self.client.post(self.URL, content_type="application/json")
  80. self.assertEqual(r.status_code, 401)
  81. self.assertEqual(r.json()["error"], "missing api key")
  82. def test_it_handles_invalid_json(self):
  83. r = self.client.post(
  84. self.URL, "this is not json", content_type="application/json"
  85. )
  86. self.assertEqual(r.status_code, 400)
  87. self.assertEqual(r.json()["error"], "could not parse request body")
  88. def test_it_rejects_wrong_api_key(self):
  89. r = self.post({"api_key": "Y" * 32})
  90. self.assertEqual(r.status_code, 401)
  91. def test_it_rejects_small_timeout(self):
  92. self.post(
  93. {"api_key": "X" * 32, "timeout": 0},
  94. expected_fragment="timeout is too small",
  95. )
  96. def test_it_rejects_large_timeout(self):
  97. self.post(
  98. {"api_key": "X" * 32, "timeout": 2592001},
  99. expected_fragment="timeout is too large",
  100. )
  101. def test_it_rejects_non_number_timeout(self):
  102. self.post(
  103. {"api_key": "X" * 32, "timeout": "oops"},
  104. expected_fragment="timeout is not a number",
  105. )
  106. def test_it_rejects_non_string_name(self):
  107. self.post(
  108. {"api_key": "X" * 32, "name": False},
  109. expected_fragment="name is not a string",
  110. )
  111. def test_it_rejects_long_name(self):
  112. self.post(
  113. {"api_key": "X" * 32, "name": "01234567890" * 20},
  114. expected_fragment="name is too long",
  115. )
  116. def test_unique_accepts_only_whitelisted_values(self):
  117. self.post(
  118. {"api_key": "X" * 32, "name": "Foo", "unique": ["status"]},
  119. expected_fragment="unexpected value",
  120. )
  121. def test_it_rejects_bad_unique_values(self):
  122. self.post(
  123. {"api_key": "X" * 32, "name": "Foo", "unique": "not a list"},
  124. expected_fragment="not an array",
  125. )
  126. def test_it_supports_cron_syntax(self):
  127. r = self.post(
  128. {
  129. "api_key": "X" * 32,
  130. "schedule": "5 * * * *",
  131. "tz": "Europe/Riga",
  132. "grace": 60,
  133. }
  134. )
  135. self.assertEqual(r.status_code, 201)
  136. doc = r.json()
  137. self.assertEqual(doc["schedule"], "5 * * * *")
  138. self.assertEqual(doc["tz"], "Europe/Riga")
  139. self.assertEqual(doc["grace"], 60)
  140. self.assertTrue("timeout" not in doc)
  141. def test_it_validates_cron_expression(self):
  142. r = self.post(
  143. {
  144. "api_key": "X" * 32,
  145. "schedule": "not-a-cron-expression",
  146. "tz": "Europe/Riga",
  147. "grace": 60,
  148. }
  149. )
  150. self.assertEqual(r.status_code, 400)
  151. def test_it_validates_timezone(self):
  152. r = self.post(
  153. {
  154. "api_key": "X" * 32,
  155. "schedule": "* * * * *",
  156. "tz": "not-a-timezone",
  157. "grace": 60,
  158. }
  159. )
  160. self.assertEqual(r.status_code, 400)
  161. def test_it_sets_default_timeout(self):
  162. r = self.post({"api_key": "X" * 32})
  163. self.assertEqual(r.status_code, 201)
  164. doc = r.json()
  165. self.assertEqual(doc["timeout"], 86400)
  166. def test_it_obeys_check_limit(self):
  167. self.profile.check_limit = 0
  168. self.profile.save()
  169. r = self.post({"api_key": "X" * 32})
  170. self.assertEqual(r.status_code, 403)
  171. def test_readonly_key_does_not_work(self):
  172. self.project.api_key_readonly = "R" * 32
  173. self.project.save()
  174. r = self.post({"api_key": "R" * 32, "name": "Foo"})
  175. self.assertEqual(r.status_code, 401)