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
2.9 KiB

  1. import json
  2. from hc.api.models import Channel, Check
  3. from hc.test import BaseTestCase
  4. class UpdateCheckTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(UpdateCheckTestCase, self).setUp()
  7. self.check = Check(user=self.alice)
  8. self.check.save()
  9. def post(self, code, data):
  10. url = "/api/v1/checks/%s" % code
  11. r = self.client.post(url, json.dumps(data),
  12. content_type="application/json")
  13. return r
  14. def test_it_works(self):
  15. r = self.post(self.check.code, {
  16. "api_key": "abc",
  17. "name": "Foo",
  18. "tags": "bar,baz",
  19. "timeout": 3600,
  20. "grace": 60
  21. })
  22. self.assertEqual(r.status_code, 200)
  23. doc = r.json()
  24. assert "ping_url" in doc
  25. self.assertEqual(doc["name"], "Foo")
  26. self.assertEqual(doc["tags"], "bar,baz")
  27. self.assertEqual(doc["last_ping"], None)
  28. self.assertEqual(doc["n_pings"], 0)
  29. self.assertTrue("schedule" not in doc)
  30. self.assertTrue("tz" not in doc)
  31. self.assertEqual(Check.objects.count(), 1)
  32. self.check.refresh_from_db()
  33. self.assertEqual(self.check.name, "Foo")
  34. self.assertEqual(self.check.tags, "bar,baz")
  35. self.assertEqual(self.check.timeout.total_seconds(), 3600)
  36. self.assertEqual(self.check.grace.total_seconds(), 60)
  37. def test_it_unassigns_channels(self):
  38. channel = Channel(user=self.alice)
  39. channel.save()
  40. self.check.assign_all_channels()
  41. r = self.post(self.check.code, {
  42. "api_key": "abc",
  43. "channels": ""
  44. })
  45. self.assertEqual(r.status_code, 200)
  46. check = Check.objects.get()
  47. self.assertEqual(check.channel_set.count(), 0)
  48. def test_it_requires_post(self):
  49. url = "/api/v1/checks/%s" % self.check.code
  50. r = self.client.get(url, HTTP_X_API_KEY="abc")
  51. self.assertEqual(r.status_code, 405)
  52. def test_it_handles_invalid_uuid(self):
  53. r = self.post("not-an-uuid", {"api_key": "abc"})
  54. self.assertEqual(r.status_code, 404)
  55. def test_it_handles_missing_check(self):
  56. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  57. r = self.post(made_up_code, {"api_key": "abc"})
  58. self.assertEqual(r.status_code, 404)
  59. def test_it_validates_ownership(self):
  60. check = Check(user=self.bob, status="up")
  61. check.save()
  62. r = self.post(check.code, {"api_key": "abc"})
  63. self.assertEqual(r.status_code, 403)
  64. def test_it_updates_cron_to_simple(self):
  65. self.check.kind = "cron"
  66. self.check.schedule = "5 * * * *"
  67. self.check.save()
  68. r = self.post(self.check.code, {"api_key": "abc", "timeout": 3600})
  69. self.assertEqual(r.status_code, 200)
  70. self.check.refresh_from_db()
  71. self.assertEqual(self.check.kind, "simple")