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.

219 lines
6.7 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_supports_unique(self):
  66. Check.objects.create(project=self.project, name="Foo")
  67. r = self.post({"api_key": "X" * 32, "name": "Foo", "unique": ["name"]})
  68. # Expect 200 instead of 201
  69. self.assertEqual(r.status_code, 200)
  70. # And there should be only one check in the database:
  71. self.assertEqual(Check.objects.count(), 1)
  72. def test_it_handles_missing_request_body(self):
  73. r = self.client.post(self.URL, content_type="application/json")
  74. self.assertEqual(r.status_code, 401)
  75. self.assertEqual(r.json()["error"], "missing api key")
  76. def test_it_handles_invalid_json(self):
  77. r = self.client.post(
  78. self.URL, "this is not json", content_type="application/json"
  79. )
  80. self.assertEqual(r.status_code, 400)
  81. self.assertEqual(r.json()["error"], "could not parse request body")
  82. def test_it_rejects_wrong_api_key(self):
  83. r = self.post({"api_key": "Y" * 32})
  84. self.assertEqual(r.status_code, 401)
  85. def test_it_rejects_small_timeout(self):
  86. self.post(
  87. {"api_key": "X" * 32, "timeout": 0},
  88. expected_fragment="timeout is too small",
  89. )
  90. def test_it_rejects_large_timeout(self):
  91. self.post(
  92. {"api_key": "X" * 32, "timeout": 2592001},
  93. expected_fragment="timeout is too large",
  94. )
  95. def test_it_rejects_non_number_timeout(self):
  96. self.post(
  97. {"api_key": "X" * 32, "timeout": "oops"},
  98. expected_fragment="timeout is not a number",
  99. )
  100. def test_it_rejects_non_string_name(self):
  101. self.post(
  102. {"api_key": "X" * 32, "name": False},
  103. expected_fragment="name is not a string",
  104. )
  105. def test_it_rejects_long_name(self):
  106. self.post(
  107. {"api_key": "X" * 32, "name": "01234567890" * 20},
  108. expected_fragment="name is too long",
  109. )
  110. def test_unique_accepts_only_whitelisted_values(self):
  111. self.post(
  112. {"api_key": "X" * 32, "name": "Foo", "unique": ["status"]},
  113. expected_fragment="unexpected value",
  114. )
  115. def test_it_rejects_bad_unique_values(self):
  116. self.post(
  117. {"api_key": "X" * 32, "name": "Foo", "unique": "not a list"},
  118. expected_fragment="not an array",
  119. )
  120. def test_it_supports_cron_syntax(self):
  121. r = self.post(
  122. {
  123. "api_key": "X" * 32,
  124. "schedule": "5 * * * *",
  125. "tz": "Europe/Riga",
  126. "grace": 60,
  127. }
  128. )
  129. self.assertEqual(r.status_code, 201)
  130. doc = r.json()
  131. self.assertEqual(doc["schedule"], "5 * * * *")
  132. self.assertEqual(doc["tz"], "Europe/Riga")
  133. self.assertEqual(doc["grace"], 60)
  134. self.assertTrue("timeout" not in doc)
  135. def test_it_validates_cron_expression(self):
  136. r = self.post(
  137. {
  138. "api_key": "X" * 32,
  139. "schedule": "not-a-cron-expression",
  140. "tz": "Europe/Riga",
  141. "grace": 60,
  142. }
  143. )
  144. self.assertEqual(r.status_code, 400)
  145. def test_it_validates_timezone(self):
  146. r = self.post(
  147. {
  148. "api_key": "X" * 32,
  149. "schedule": "* * * * *",
  150. "tz": "not-a-timezone",
  151. "grace": 60,
  152. }
  153. )
  154. self.assertEqual(r.status_code, 400)
  155. def test_it_sets_default_timeout(self):
  156. r = self.post({"api_key": "X" * 32})
  157. self.assertEqual(r.status_code, 201)
  158. doc = r.json()
  159. self.assertEqual(doc["timeout"], 86400)
  160. def test_it_obeys_check_limit(self):
  161. self.profile.check_limit = 0
  162. self.profile.save()
  163. r = self.post({"api_key": "X" * 32})
  164. self.assertEqual(r.status_code, 403)
  165. def test_readonly_key_does_not_work(self):
  166. self.project.api_key_readonly = "R" * 32
  167. self.project.save()
  168. r = self.post({"api_key": "R" * 32, "name": "Foo"})
  169. self.assertEqual(r.status_code, 401)