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.

176 lines
5.7 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(self.check.code, {
  13. "api_key": "X" * 32,
  14. "name": "Foo",
  15. "tags": "bar,baz",
  16. "timeout": 3600,
  17. "grace": 60
  18. })
  19. self.assertEqual(r.status_code, 200)
  20. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  21. doc = r.json()
  22. assert "ping_url" in doc
  23. self.assertEqual(doc["name"], "Foo")
  24. self.assertEqual(doc["tags"], "bar,baz")
  25. self.assertEqual(doc["last_ping"], None)
  26. self.assertEqual(doc["n_pings"], 0)
  27. self.assertTrue("schedule" not in doc)
  28. self.assertTrue("tz" not in doc)
  29. self.assertEqual(Check.objects.count(), 1)
  30. self.check.refresh_from_db()
  31. self.assertEqual(self.check.name, "Foo")
  32. self.assertEqual(self.check.tags, "bar,baz")
  33. self.assertEqual(self.check.timeout.total_seconds(), 3600)
  34. self.assertEqual(self.check.grace.total_seconds(), 60)
  35. def test_it_handles_options(self):
  36. r = self.client.options("/api/v1/checks/%s" % self.check.code)
  37. self.assertEqual(r.status_code, 204)
  38. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  39. def test_it_unassigns_channels(self):
  40. Channel.objects.create(project=self.project)
  41. self.check.assign_all_channels()
  42. r = self.post(self.check.code, {
  43. "api_key": "X" * 32,
  44. "channels": ""
  45. })
  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(self.check.code, {
  78. "api_key": "X" * 32,
  79. "channels": str(channel.code)
  80. })
  81. self.assertEqual(r.status_code, 200)
  82. self.check.refresh_from_db()
  83. self.assertEqual(self.check.channel_set.count(), 1)
  84. self.assertEqual(self.check.channel_set.first().code, channel.code)
  85. def test_it_handles_comma_separated_channel_codes(self):
  86. c1 = Channel.objects.create(project=self.project)
  87. c2 = Channel.objects.create(project=self.project)
  88. r = self.post(self.check.code, {
  89. "api_key": "X" * 32,
  90. "channels": "%s,%s" % (c1.code, c2.code)
  91. })
  92. self.assertEqual(r.status_code, 200)
  93. self.check.refresh_from_db()
  94. self.assertEqual(self.check.channel_set.count(), 2)
  95. def test_it_handles_asterix(self):
  96. Channel.objects.create(project=self.project)
  97. Channel.objects.create(project=self.project)
  98. r = self.post(self.check.code, {
  99. "api_key": "X" * 32,
  100. "channels": "*"
  101. })
  102. self.assertEqual(r.status_code, 200)
  103. self.check.refresh_from_db()
  104. self.assertEqual(self.check.channel_set.count(), 2)
  105. def test_it_ignores_channels_if_channels_key_missing(self):
  106. Channel.objects.create(project=self.project)
  107. self.check.assign_all_channels()
  108. r = self.post(self.check.code, {"api_key": "X" * 32})
  109. self.assertEqual(r.status_code, 200)
  110. check = Check.objects.get()
  111. self.assertEqual(check.channel_set.count(), 1)
  112. def test_it_rejects_bad_channel_code(self):
  113. r = self.post(self.check.code, {
  114. "api_key": "X" * 32,
  115. "channels": str(uuid.uuid4())
  116. })
  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_uuid_channel_code(self):
  121. r = self.post(self.check.code, {
  122. "api_key": "X" * 32,
  123. "channels": "foo"
  124. })
  125. self.assertEqual(r.status_code, 400)
  126. self.check.refresh_from_db()
  127. self.assertEqual(self.check.channel_set.count(), 0)
  128. def test_it_rejects_non_string_channels_key(self):
  129. r = self.post(self.check.code, {
  130. "api_key": "X" * 32,
  131. "channels": None
  132. })
  133. self.assertEqual(r.status_code, 400)