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.

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