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.

201 lines
6.2 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, json.dumps(data),
  8. content_type="application/json")
  9. if expected_error:
  10. self.assertEqual(r.status_code, 400)
  11. self.assertEqual(r.json()["error"], expected_error)
  12. if expected_fragment:
  13. self.assertEqual(r.status_code, 400)
  14. self.assertIn(expected_fragment, r.json()["error"])
  15. return r
  16. def test_it_works(self):
  17. r = self.post({
  18. "api_key": "abc",
  19. "name": "Foo",
  20. "tags": "bar,baz",
  21. "timeout": 3600,
  22. "grace": 60
  23. })
  24. self.assertEqual(r.status_code, 201)
  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_30_days_works(self):
  39. r = self.post({
  40. "api_key": "abc",
  41. "name": "Foo",
  42. "timeout": 2592000,
  43. "grace": 2592000
  44. })
  45. self.assertEqual(r.status_code, 201)
  46. check = Check.objects.get()
  47. self.assertEqual(check.timeout.total_seconds(), 2592000)
  48. self.assertEqual(check.grace.total_seconds(), 2592000)
  49. def test_it_accepts_api_key_in_header(self):
  50. payload = json.dumps({"name": "Foo"})
  51. r = self.client.post(self.URL, payload,
  52. content_type="application/json",
  53. HTTP_X_API_KEY="abc")
  54. self.assertEqual(r.status_code, 201)
  55. def test_it_assigns_channels(self):
  56. channel = Channel(user=self.alice)
  57. channel.save()
  58. r = self.post({"api_key": "abc", "channels": "*"})
  59. self.assertEqual(r.status_code, 201)
  60. check = Check.objects.get()
  61. self.assertEqual(check.channel_set.get(), channel)
  62. def test_it_supports_unique(self):
  63. existing = Check(user=self.alice, name="Foo")
  64. existing.save()
  65. r = self.post({
  66. "api_key": "abc",
  67. "name": "Foo",
  68. "unique": ["name"]
  69. })
  70. # Expect 200 instead of 201
  71. self.assertEqual(r.status_code, 200)
  72. # And there should be only one check in the database:
  73. self.assertEqual(Check.objects.count(), 1)
  74. def test_it_handles_missing_request_body(self):
  75. r = self.client.post(self.URL, content_type="application/json")
  76. self.assertEqual(r.status_code, 400)
  77. self.assertEqual(r.json()["error"], "wrong api_key")
  78. def test_it_handles_invalid_json(self):
  79. r = self.client.post(self.URL, "this is not json",
  80. content_type="application/json")
  81. self.assertEqual(r.status_code, 400)
  82. self.assertEqual(r.json()["error"], "could not parse request body")
  83. def test_it_rejects_wrong_api_key(self):
  84. r = self.post({"api_key": "wrong"})
  85. self.assertEqual(r.status_code, 403)
  86. def test_it_rejects_small_timeout(self):
  87. self.post({"api_key": "abc", "timeout": 0},
  88. expected_fragment="timeout is too small")
  89. def test_it_rejects_large_timeout(self):
  90. self.post({"api_key": "abc", "timeout": 2592001},
  91. expected_fragment="timeout is too large")
  92. def test_it_rejects_non_number_timeout(self):
  93. self.post({"api_key": "abc", "timeout": "oops"},
  94. expected_fragment="timeout is not a number")
  95. def test_it_rejects_non_string_name(self):
  96. self.post({"api_key": "abc", "name": False},
  97. expected_fragment="name is not a string")
  98. def test_it_rejects_long_name(self):
  99. self.post({"api_key": "abc", "name": "01234567890" * 20},
  100. expected_fragment="name is too long")
  101. def test_unique_accepts_only_whitelisted_values(self):
  102. existing = Check(user=self.alice, name="Foo")
  103. existing.save()
  104. self.post({
  105. "api_key": "abc",
  106. "name": "Foo",
  107. "unique": ["status"]
  108. }, expected_fragment="unexpected value")
  109. def test_it_rejects_bad_unique_values(self):
  110. self.post({
  111. "api_key": "abc",
  112. "name": "Foo",
  113. "unique": "not a list"
  114. }, expected_fragment="not an array")
  115. def test_it_supports_cron_syntax(self):
  116. r = self.post({
  117. "api_key": "abc",
  118. "schedule": "5 * * * *",
  119. "tz": "Europe/Riga",
  120. "grace": 60
  121. })
  122. self.assertEqual(r.status_code, 201)
  123. doc = r.json()
  124. self.assertEqual(doc["schedule"], "5 * * * *")
  125. self.assertEqual(doc["tz"], "Europe/Riga")
  126. self.assertEqual(doc["grace"], 60)
  127. self.assertTrue("timeout" not in doc)
  128. def test_it_validates_cron_expression(self):
  129. r = self.post({
  130. "api_key": "abc",
  131. "schedule": "not-a-cron-expression",
  132. "tz": "Europe/Riga",
  133. "grace": 60
  134. })
  135. self.assertEqual(r.status_code, 400)
  136. def test_it_validates_timezone(self):
  137. r = self.post({
  138. "api_key": "abc",
  139. "schedule": "* * * * *",
  140. "tz": "not-a-timezone",
  141. "grace": 60
  142. })
  143. self.assertEqual(r.status_code, 400)
  144. def test_it_sets_default_timeout(self):
  145. r = self.post({"api_key": "abc"})
  146. self.assertEqual(r.status_code, 201)
  147. doc = r.json()
  148. self.assertEqual(doc["timeout"], 86400)
  149. def test_it_obeys_check_limit(self):
  150. self.profile.check_limit = 0
  151. self.profile.save()
  152. r = self.post({"api_key": "abc"})
  153. self.assertEqual(r.status_code, 403)