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.

80 lines
2.9 KiB

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