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.

213 lines
6.6 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. "api_key": "X" * 32,
  18. "name": "Foo",
  19. "tags": "bar,baz",
  20. "timeout": 3600,
  21. "grace": 60
  22. })
  23. self.assertEqual(r.status_code, 201)
  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["last_ping"], None)
  30. self.assertEqual(doc["n_pings"], 0)
  31. self.assertTrue("schedule" not in doc)
  32. self.assertTrue("tz" not in doc)
  33. check = Check.objects.get()
  34. self.assertEqual(check.name, "Foo")
  35. self.assertEqual(check.tags, "bar,baz")
  36. self.assertEqual(check.timeout.total_seconds(), 3600)
  37. self.assertEqual(check.grace.total_seconds(), 60)
  38. def test_it_handles_options(self):
  39. r = self.client.options(self.URL)
  40. self.assertEqual(r.status_code, 204)
  41. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  42. def test_30_days_works(self):
  43. r = self.post({
  44. "api_key": "X" * 32,
  45. "name": "Foo",
  46. "timeout": 2592000,
  47. "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(self.URL, payload,
  56. content_type="application/json",
  57. HTTP_X_API_KEY="X" * 32)
  58. self.assertEqual(r.status_code, 201)
  59. def test_it_assigns_channels(self):
  60. channel = Channel(user=self.alice)
  61. channel.save()
  62. r = self.post({"api_key": "X" * 32, "channels": "*"})
  63. self.assertEqual(r.status_code, 201)
  64. check = Check.objects.get()
  65. self.assertEqual(check.channel_set.get(), channel)
  66. def test_it_supports_unique(self):
  67. existing = Check(user=self.alice, name="Foo")
  68. existing.save()
  69. r = self.post({
  70. "api_key": "X" * 32,
  71. "name": "Foo",
  72. "unique": ["name"]
  73. })
  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(self.URL, "this is not json",
  84. content_type="application/json")
  85. self.assertEqual(r.status_code, 400)
  86. self.assertEqual(r.json()["error"], "could not parse request body")
  87. def test_it_rejects_wrong_api_key(self):
  88. r = self.post({"api_key": "Y" * 32})
  89. self.assertEqual(r.status_code, 401)
  90. def test_it_rejects_small_timeout(self):
  91. self.post({"api_key": "X" * 32, "timeout": 0},
  92. expected_fragment="timeout is too small")
  93. def test_it_rejects_large_timeout(self):
  94. self.post({"api_key": "X" * 32, "timeout": 2592001},
  95. expected_fragment="timeout is too large")
  96. def test_it_rejects_non_number_timeout(self):
  97. self.post({"api_key": "X" * 32, "timeout": "oops"},
  98. expected_fragment="timeout is not a number")
  99. def test_it_rejects_non_string_name(self):
  100. self.post({"api_key": "X" * 32, "name": False},
  101. expected_fragment="name is not a string")
  102. def test_it_rejects_long_name(self):
  103. self.post({"api_key": "X" * 32, "name": "01234567890" * 20},
  104. expected_fragment="name is too long")
  105. def test_unique_accepts_only_whitelisted_values(self):
  106. existing = Check(user=self.alice, name="Foo")
  107. existing.save()
  108. self.post({
  109. "api_key": "X" * 32,
  110. "name": "Foo",
  111. "unique": ["status"]
  112. }, expected_fragment="unexpected value")
  113. def test_it_rejects_bad_unique_values(self):
  114. self.post({
  115. "api_key": "X" * 32,
  116. "name": "Foo",
  117. "unique": "not a list"
  118. }, expected_fragment="not an array")
  119. def test_it_supports_cron_syntax(self):
  120. r = self.post({
  121. "api_key": "X" * 32,
  122. "schedule": "5 * * * *",
  123. "tz": "Europe/Riga",
  124. "grace": 60
  125. })
  126. self.assertEqual(r.status_code, 201)
  127. doc = r.json()
  128. self.assertEqual(doc["schedule"], "5 * * * *")
  129. self.assertEqual(doc["tz"], "Europe/Riga")
  130. self.assertEqual(doc["grace"], 60)
  131. self.assertTrue("timeout" not in doc)
  132. def test_it_validates_cron_expression(self):
  133. r = self.post({
  134. "api_key": "X" * 32,
  135. "schedule": "not-a-cron-expression",
  136. "tz": "Europe/Riga",
  137. "grace": 60
  138. })
  139. self.assertEqual(r.status_code, 400)
  140. def test_it_validates_timezone(self):
  141. r = self.post({
  142. "api_key": "X" * 32,
  143. "schedule": "* * * * *",
  144. "tz": "not-a-timezone",
  145. "grace": 60
  146. })
  147. self.assertEqual(r.status_code, 400)
  148. def test_it_sets_default_timeout(self):
  149. r = self.post({"api_key": "X" * 32})
  150. self.assertEqual(r.status_code, 201)
  151. doc = r.json()
  152. self.assertEqual(doc["timeout"], 86400)
  153. def test_it_obeys_check_limit(self):
  154. self.profile.check_limit = 0
  155. self.profile.save()
  156. r = self.post({"api_key": "X" * 32})
  157. self.assertEqual(r.status_code, 403)
  158. def test_readonly_key_does_not_work(self):
  159. self.profile.api_key_readonly = "R" * 32
  160. self.profile.save()
  161. r = self.post({"api_key": "R" * 32, "name": "Foo"})
  162. self.assertEqual(r.status_code, 401)