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.

77 lines
2.7 KiB

  1. import json
  2. from hc.api.models import Channel, Check
  3. from hc.test import BaseTestCase
  4. class CreateCheckTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(CreateCheckTestCase, self).setUp()
  7. def post(self, url, data):
  8. return self.client.post(url, json.dumps(data),
  9. content_type="application/json")
  10. def test_it_works(self):
  11. r = self.post("/api/v1/checks/", {
  12. "api_key": "abc",
  13. "name": "Foo",
  14. "tags": "bar,baz",
  15. "timeout": 3600,
  16. "grace": 60
  17. })
  18. self.assertEqual(r.status_code, 201)
  19. self.assertTrue("ping_url" in r.json())
  20. self.assertEqual(Check.objects.count(), 1)
  21. check = Check.objects.get()
  22. self.assertEqual(check.name, "Foo")
  23. self.assertEqual(check.tags, "bar,baz")
  24. self.assertEqual(check.timeout.total_seconds(), 3600)
  25. self.assertEqual(check.grace.total_seconds(), 60)
  26. def test_it_assigns_channels(self):
  27. channel = Channel(user=self.alice)
  28. channel.save()
  29. r = self.post("/api/v1/checks/", {
  30. "api_key": "abc",
  31. "channels": "*"
  32. })
  33. self.assertEqual(r.status_code, 201)
  34. check = Check.objects.get()
  35. self.assertEqual(check.channel_set.get(), channel)
  36. def test_it_handles_missing_request_body(self):
  37. r = self.client.post("/api/v1/checks/",
  38. content_type="application/json")
  39. self.assertEqual(r.status_code, 400)
  40. self.assertEqual(r.json()["error"], "wrong api_key")
  41. def test_it_rejects_wrong_api_key(self):
  42. r = self.post("/api/v1/checks/", {"api_key": "wrong"})
  43. self.assertEqual(r.json()["error"], "wrong api_key")
  44. def test_it_handles_invalid_json(self):
  45. r = self.client.post("/api/v1/checks/", "this is not json",
  46. content_type="application/json")
  47. self.assertEqual(r.json()["error"], "could not parse request body")
  48. def test_it_rejects_small_timeout(self):
  49. r = self.post("/api/v1/checks/", {"api_key": "abc", "timeout": 0})
  50. self.assertEqual(r.json()["error"], "timeout is too small")
  51. def test_it_rejects_large_timeout(self):
  52. r = self.post("/api/v1/checks/", {"api_key": "abc", "timeout": 604801})
  53. self.assertEqual(r.json()["error"], "timeout is too large")
  54. def test_it_rejects_non_number_timeout(self):
  55. r = self.post("/api/v1/checks/", {"api_key": "abc", "timeout": "oops"})
  56. self.assertEqual(r.json()["error"], "timeout is not a number")
  57. def test_it_rejects_non_string_name(self):
  58. r = self.post("/api/v1/checks/", {"api_key": "abc", "name": False})
  59. self.assertEqual(r.json()["error"], "name is not a string")