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.

164 lines
5.6 KiB

  1. import uuid
  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.objects.create(project=self.project)
  8. def post(self, code, data):
  9. url = "/api/v1/checks/%s" % code
  10. return self.client.post(url, data, content_type="application/json")
  11. def test_it_works(self):
  12. r = self.post(
  13. self.check.code,
  14. {
  15. "api_key": "X" * 32,
  16. "name": "Foo",
  17. "tags": "bar,baz",
  18. "timeout": 3600,
  19. "grace": 60,
  20. },
  21. )
  22. self.assertEqual(r.status_code, 200)
  23. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  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.assertTrue("schedule" not in doc)
  31. self.assertTrue("tz" not in doc)
  32. self.assertEqual(Check.objects.count(), 1)
  33. self.check.refresh_from_db()
  34. self.assertEqual(self.check.name, "Foo")
  35. self.assertEqual(self.check.tags, "bar,baz")
  36. self.assertEqual(self.check.timeout.total_seconds(), 3600)
  37. self.assertEqual(self.check.grace.total_seconds(), 60)
  38. def test_it_handles_options(self):
  39. r = self.client.options("/api/v1/checks/%s" % self.check.code)
  40. self.assertEqual(r.status_code, 204)
  41. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  42. def test_it_unassigns_channels(self):
  43. Channel.objects.create(project=self.project)
  44. self.check.assign_all_channels()
  45. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": ""})
  46. self.assertEqual(r.status_code, 200)
  47. check = Check.objects.get()
  48. self.assertEqual(check.channel_set.count(), 0)
  49. def test_it_requires_post(self):
  50. url = "/api/v1/checks/%s" % self.check.code
  51. r = self.client.get(url, HTTP_X_API_KEY="X" * 32)
  52. self.assertEqual(r.status_code, 405)
  53. def test_it_handles_invalid_uuid(self):
  54. r = self.post("not-an-uuid", {"api_key": "X" * 32})
  55. self.assertEqual(r.status_code, 404)
  56. def test_it_handles_missing_check(self):
  57. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  58. r = self.post(made_up_code, {"api_key": "X" * 32})
  59. self.assertEqual(r.status_code, 404)
  60. def test_it_validates_ownership(self):
  61. check = Check.objects.create(project=self.bobs_project, status="up")
  62. r = self.post(check.code, {"api_key": "X" * 32})
  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": "X" * 32, "timeout": 3600})
  69. self.assertEqual(r.status_code, 200)
  70. self.check.refresh_from_db()
  71. self.assertEqual(self.check.kind, "simple")
  72. def test_it_sets_single_channel(self):
  73. channel = Channel.objects.create(project=self.project)
  74. # Create another channel so we can test that only the first one
  75. # gets assigned:
  76. Channel.objects.create(project=self.project)
  77. r = self.post(
  78. self.check.code, {"api_key": "X" * 32, "channels": str(channel.code)}
  79. )
  80. self.assertEqual(r.status_code, 200)
  81. self.check.refresh_from_db()
  82. self.assertEqual(self.check.channel_set.count(), 1)
  83. self.assertEqual(self.check.channel_set.first().code, channel.code)
  84. def test_it_handles_comma_separated_channel_codes(self):
  85. c1 = Channel.objects.create(project=self.project)
  86. c2 = Channel.objects.create(project=self.project)
  87. r = self.post(
  88. self.check.code,
  89. {"api_key": "X" * 32, "channels": "%s,%s" % (c1.code, c2.code)},
  90. )
  91. self.assertEqual(r.status_code, 200)
  92. self.check.refresh_from_db()
  93. self.assertEqual(self.check.channel_set.count(), 2)
  94. def test_it_handles_asterix(self):
  95. Channel.objects.create(project=self.project)
  96. Channel.objects.create(project=self.project)
  97. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "*"})
  98. self.assertEqual(r.status_code, 200)
  99. self.check.refresh_from_db()
  100. self.assertEqual(self.check.channel_set.count(), 2)
  101. def test_it_ignores_channels_if_channels_key_missing(self):
  102. Channel.objects.create(project=self.project)
  103. self.check.assign_all_channels()
  104. r = self.post(self.check.code, {"api_key": "X" * 32})
  105. self.assertEqual(r.status_code, 200)
  106. check = Check.objects.get()
  107. self.assertEqual(check.channel_set.count(), 1)
  108. def test_it_rejects_bad_channel_code(self):
  109. r = self.post(
  110. self.check.code, {"api_key": "X" * 32, "channels": str(uuid.uuid4())}
  111. )
  112. self.assertEqual(r.status_code, 400)
  113. self.check.refresh_from_db()
  114. self.assertEqual(self.check.channel_set.count(), 0)
  115. def test_it_rejects_non_uuid_channel_code(self):
  116. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": "foo"})
  117. self.assertEqual(r.status_code, 400)
  118. self.check.refresh_from_db()
  119. self.assertEqual(self.check.channel_set.count(), 0)
  120. def test_it_rejects_non_string_channels_key(self):
  121. r = self.post(self.check.code, {"api_key": "X" * 32, "channels": None})
  122. self.assertEqual(r.status_code, 400)