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.

95 lines
3.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 setUp(self):
  7. super(CreateCheckTestCase, self).setUp()
  8. def post(self, data, expected_error=None):
  9. r = self.client.post(self.URL, json.dumps(data),
  10. content_type="application/json")
  11. if expected_error:
  12. self.assertEqual(r.status_code, 400)
  13. self.assertEqual(r.json()["error"], expected_error)
  14. return r
  15. def test_it_works(self):
  16. r = self.post({
  17. "api_key": "abc",
  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.assertEqual(Check.objects.count(), 1)
  31. check = Check.objects.get()
  32. self.assertEqual(check.name, "Foo")
  33. self.assertEqual(check.tags, "bar,baz")
  34. self.assertEqual(check.timeout.total_seconds(), 3600)
  35. self.assertEqual(check.grace.total_seconds(), 60)
  36. def test_it_accepts_api_key_in_header(self):
  37. payload = json.dumps({"name": "Foo"})
  38. r = self.client.post(self.URL, payload,
  39. content_type="application/json",
  40. HTTP_X_API_KEY="abc")
  41. self.assertEqual(r.status_code, 201)
  42. def test_it_assigns_channels(self):
  43. channel = Channel(user=self.alice)
  44. channel.save()
  45. r = self.post({"api_key": "abc", "channels": "*"})
  46. self.assertEqual(r.status_code, 201)
  47. check = Check.objects.get()
  48. self.assertEqual(check.channel_set.get(), channel)
  49. def test_it_handles_missing_request_body(self):
  50. r = self.client.post(self.URL, content_type="application/json")
  51. self.assertEqual(r.status_code, 400)
  52. self.assertEqual(r.json()["error"], "wrong api_key")
  53. def test_it_handles_invalid_json(self):
  54. r = self.client.post(self.URL, "this is not json",
  55. content_type="application/json")
  56. self.assertEqual(r.status_code, 400)
  57. self.assertEqual(r.json()["error"], "could not parse request body")
  58. def test_it_rejects_wrong_api_key(self):
  59. self.post({"api_key": "wrong"},
  60. expected_error="wrong api_key")
  61. def test_it_rejects_small_timeout(self):
  62. self.post({"api_key": "abc", "timeout": 0},
  63. expected_error="timeout is too small")
  64. def test_it_rejects_large_timeout(self):
  65. self.post({"api_key": "abc", "timeout": 604801},
  66. expected_error="timeout is too large")
  67. def test_it_rejects_non_number_timeout(self):
  68. self.post({"api_key": "abc", "timeout": "oops"},
  69. expected_error="timeout is not a number")
  70. def test_it_rejects_non_string_name(self):
  71. self.post({"api_key": "abc", "name": False},
  72. expected_error="name is not a string")