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.

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