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.

214 lines
6.8 KiB

6 years ago
6 years ago
  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. self.assertEqual(check.project, self.project)
  39. def test_it_handles_options(self):
  40. r = self.client.options(self.URL)
  41. self.assertEqual(r.status_code, 204)
  42. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  43. def test_30_days_works(self):
  44. r = self.post({
  45. "api_key": "X" * 32,
  46. "name": "Foo",
  47. "timeout": 2592000,
  48. "grace": 2592000
  49. })
  50. self.assertEqual(r.status_code, 201)
  51. check = Check.objects.get()
  52. self.assertEqual(check.timeout.total_seconds(), 2592000)
  53. self.assertEqual(check.grace.total_seconds(), 2592000)
  54. def test_it_accepts_api_key_in_header(self):
  55. payload = json.dumps({"name": "Foo"})
  56. r = self.client.post(self.URL, payload,
  57. content_type="application/json",
  58. HTTP_X_API_KEY="X" * 32)
  59. self.assertEqual(r.status_code, 201)
  60. def test_it_assigns_channels(self):
  61. channel = Channel(user=self.alice, project=self.project)
  62. channel.save()
  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_supports_unique(self):
  68. existing = Check(user=self.alice, name="Foo", project=self.project)
  69. existing.save()
  70. r = self.post({
  71. "api_key": "X" * 32,
  72. "name": "Foo",
  73. "unique": ["name"]
  74. })
  75. # Expect 200 instead of 201
  76. self.assertEqual(r.status_code, 200)
  77. # And there should be only one check in the database:
  78. self.assertEqual(Check.objects.count(), 1)
  79. def test_it_handles_missing_request_body(self):
  80. r = self.client.post(self.URL, content_type="application/json")
  81. self.assertEqual(r.status_code, 401)
  82. self.assertEqual(r.json()["error"], "missing api key")
  83. def test_it_handles_invalid_json(self):
  84. r = self.client.post(self.URL, "this is not json",
  85. content_type="application/json")
  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({"api_key": "X" * 32, "timeout": 0},
  93. expected_fragment="timeout is too small")
  94. def test_it_rejects_large_timeout(self):
  95. self.post({"api_key": "X" * 32, "timeout": 2592001},
  96. expected_fragment="timeout is too large")
  97. def test_it_rejects_non_number_timeout(self):
  98. self.post({"api_key": "X" * 32, "timeout": "oops"},
  99. expected_fragment="timeout is not a number")
  100. def test_it_rejects_non_string_name(self):
  101. self.post({"api_key": "X" * 32, "name": False},
  102. expected_fragment="name is not a string")
  103. def test_it_rejects_long_name(self):
  104. self.post({"api_key": "X" * 32, "name": "01234567890" * 20},
  105. expected_fragment="name is too long")
  106. def test_unique_accepts_only_whitelisted_values(self):
  107. existing = Check(user=self.alice, name="Foo", project=self.project)
  108. existing.save()
  109. self.post({
  110. "api_key": "X" * 32,
  111. "name": "Foo",
  112. "unique": ["status"]
  113. }, expected_fragment="unexpected value")
  114. def test_it_rejects_bad_unique_values(self):
  115. self.post({
  116. "api_key": "X" * 32,
  117. "name": "Foo",
  118. "unique": "not a list"
  119. }, expected_fragment="not an array")
  120. def test_it_supports_cron_syntax(self):
  121. r = self.post({
  122. "api_key": "X" * 32,
  123. "schedule": "5 * * * *",
  124. "tz": "Europe/Riga",
  125. "grace": 60
  126. })
  127. self.assertEqual(r.status_code, 201)
  128. doc = r.json()
  129. self.assertEqual(doc["schedule"], "5 * * * *")
  130. self.assertEqual(doc["tz"], "Europe/Riga")
  131. self.assertEqual(doc["grace"], 60)
  132. self.assertTrue("timeout" not in doc)
  133. def test_it_validates_cron_expression(self):
  134. r = self.post({
  135. "api_key": "X" * 32,
  136. "schedule": "not-a-cron-expression",
  137. "tz": "Europe/Riga",
  138. "grace": 60
  139. })
  140. self.assertEqual(r.status_code, 400)
  141. def test_it_validates_timezone(self):
  142. r = self.post({
  143. "api_key": "X" * 32,
  144. "schedule": "* * * * *",
  145. "tz": "not-a-timezone",
  146. "grace": 60
  147. })
  148. self.assertEqual(r.status_code, 400)
  149. def test_it_sets_default_timeout(self):
  150. r = self.post({"api_key": "X" * 32})
  151. self.assertEqual(r.status_code, 201)
  152. doc = r.json()
  153. self.assertEqual(doc["timeout"], 86400)
  154. def test_it_obeys_check_limit(self):
  155. self.profile.check_limit = 0
  156. self.profile.save()
  157. r = self.post({"api_key": "X" * 32})
  158. self.assertEqual(r.status_code, 403)
  159. def test_readonly_key_does_not_work(self):
  160. self.project.api_key_readonly = "R" * 32
  161. self.project.save()
  162. r = self.post({"api_key": "R" * 32, "name": "Foo"})
  163. self.assertEqual(r.status_code, 401)