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.

207 lines
6.4 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. doc = r.json()
  25. assert "ping_url" in doc
  26. self.assertEqual(doc["name"], "Foo")
  27. self.assertEqual(doc["tags"], "bar,baz")
  28. self.assertEqual(doc["last_ping"], None)
  29. self.assertEqual(doc["n_pings"], 0)
  30. self.assertTrue("schedule" not in doc)
  31. self.assertTrue("tz" not in doc)
  32. check = Check.objects.get()
  33. self.assertEqual(check.name, "Foo")
  34. self.assertEqual(check.tags, "bar,baz")
  35. self.assertEqual(check.timeout.total_seconds(), 3600)
  36. self.assertEqual(check.grace.total_seconds(), 60)
  37. def test_30_days_works(self):
  38. r = self.post({
  39. "api_key": "X" * 32,
  40. "name": "Foo",
  41. "timeout": 2592000,
  42. "grace": 2592000
  43. })
  44. self.assertEqual(r.status_code, 201)
  45. check = Check.objects.get()
  46. self.assertEqual(check.timeout.total_seconds(), 2592000)
  47. self.assertEqual(check.grace.total_seconds(), 2592000)
  48. def test_it_accepts_api_key_in_header(self):
  49. payload = json.dumps({"name": "Foo"})
  50. r = self.client.post(self.URL, payload,
  51. content_type="application/json",
  52. HTTP_X_API_KEY="X" * 32)
  53. self.assertEqual(r.status_code, 201)
  54. def test_it_assigns_channels(self):
  55. channel = Channel(user=self.alice)
  56. channel.save()
  57. r = self.post({"api_key": "X" * 32, "channels": "*"})
  58. self.assertEqual(r.status_code, 201)
  59. check = Check.objects.get()
  60. self.assertEqual(check.channel_set.get(), channel)
  61. def test_it_supports_unique(self):
  62. existing = Check(user=self.alice, name="Foo")
  63. existing.save()
  64. r = self.post({
  65. "api_key": "X" * 32,
  66. "name": "Foo",
  67. "unique": ["name"]
  68. })
  69. # Expect 200 instead of 201
  70. self.assertEqual(r.status_code, 200)
  71. # And there should be only one check in the database:
  72. self.assertEqual(Check.objects.count(), 1)
  73. def test_it_handles_missing_request_body(self):
  74. r = self.client.post(self.URL, content_type="application/json")
  75. self.assertEqual(r.status_code, 401)
  76. self.assertEqual(r.json()["error"], "missing api key")
  77. def test_it_handles_invalid_json(self):
  78. r = self.client.post(self.URL, "this is not json",
  79. content_type="application/json")
  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({"api_key": "X" * 32, "timeout": 0},
  87. expected_fragment="timeout is too small")
  88. def test_it_rejects_large_timeout(self):
  89. self.post({"api_key": "X" * 32, "timeout": 2592001},
  90. expected_fragment="timeout is too large")
  91. def test_it_rejects_non_number_timeout(self):
  92. self.post({"api_key": "X" * 32, "timeout": "oops"},
  93. expected_fragment="timeout is not a number")
  94. def test_it_rejects_non_string_name(self):
  95. self.post({"api_key": "X" * 32, "name": False},
  96. expected_fragment="name is not a string")
  97. def test_it_rejects_long_name(self):
  98. self.post({"api_key": "X" * 32, "name": "01234567890" * 20},
  99. expected_fragment="name is too long")
  100. def test_unique_accepts_only_whitelisted_values(self):
  101. existing = Check(user=self.alice, name="Foo")
  102. existing.save()
  103. self.post({
  104. "api_key": "X" * 32,
  105. "name": "Foo",
  106. "unique": ["status"]
  107. }, expected_fragment="unexpected value")
  108. def test_it_rejects_bad_unique_values(self):
  109. self.post({
  110. "api_key": "X" * 32,
  111. "name": "Foo",
  112. "unique": "not a list"
  113. }, expected_fragment="not an array")
  114. def test_it_supports_cron_syntax(self):
  115. r = self.post({
  116. "api_key": "X" * 32,
  117. "schedule": "5 * * * *",
  118. "tz": "Europe/Riga",
  119. "grace": 60
  120. })
  121. self.assertEqual(r.status_code, 201)
  122. doc = r.json()
  123. self.assertEqual(doc["schedule"], "5 * * * *")
  124. self.assertEqual(doc["tz"], "Europe/Riga")
  125. self.assertEqual(doc["grace"], 60)
  126. self.assertTrue("timeout" not in doc)
  127. def test_it_validates_cron_expression(self):
  128. r = self.post({
  129. "api_key": "X" * 32,
  130. "schedule": "not-a-cron-expression",
  131. "tz": "Europe/Riga",
  132. "grace": 60
  133. })
  134. self.assertEqual(r.status_code, 400)
  135. def test_it_validates_timezone(self):
  136. r = self.post({
  137. "api_key": "X" * 32,
  138. "schedule": "* * * * *",
  139. "tz": "not-a-timezone",
  140. "grace": 60
  141. })
  142. self.assertEqual(r.status_code, 400)
  143. def test_it_sets_default_timeout(self):
  144. r = self.post({"api_key": "X" * 32})
  145. self.assertEqual(r.status_code, 201)
  146. doc = r.json()
  147. self.assertEqual(doc["timeout"], 86400)
  148. def test_it_obeys_check_limit(self):
  149. self.profile.check_limit = 0
  150. self.profile.save()
  151. r = self.post({"api_key": "X" * 32})
  152. self.assertEqual(r.status_code, 403)
  153. def test_readonly_key_does_not_work(self):
  154. self.profile.api_key_readonly = "R" * 32
  155. self.profile.save()
  156. r = self.post({"api_key": "R" * 32, "name": "Foo"})
  157. self.assertEqual(r.status_code, 401)