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.

161 lines
5.1 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. 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_unassigns_channels(self):
  36. Channel.objects.create(user=self.alice)
  37. self.check.assign_all_channels()
  38. r = self.post(self.check.code, {
  39. "api_key": "X" * 32,
  40. "channels": ""
  41. })
  42. self.assertEqual(r.status_code, 200)
  43. check = Check.objects.get()
  44. self.assertEqual(check.channel_set.count(), 0)
  45. def test_it_requires_post(self):
  46. url = "/api/v1/checks/%s" % self.check.code
  47. r = self.client.get(url, HTTP_X_API_KEY="X" * 32)
  48. self.assertEqual(r.status_code, 405)
  49. def test_it_handles_invalid_uuid(self):
  50. r = self.post("not-an-uuid", {"api_key": "X" * 32})
  51. self.assertEqual(r.status_code, 404)
  52. def test_it_handles_missing_check(self):
  53. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  54. r = self.post(made_up_code, {"api_key": "X" * 32})
  55. self.assertEqual(r.status_code, 404)
  56. def test_it_validates_ownership(self):
  57. check = Check(user=self.bob, status="up")
  58. check.save()
  59. r = self.post(check.code, {"api_key": "X" * 32})
  60. self.assertEqual(r.status_code, 403)
  61. def test_it_updates_cron_to_simple(self):
  62. self.check.kind = "cron"
  63. self.check.schedule = "5 * * * *"
  64. self.check.save()
  65. r = self.post(self.check.code, {"api_key": "X" * 32, "timeout": 3600})
  66. self.assertEqual(r.status_code, 200)
  67. self.check.refresh_from_db()
  68. self.assertEqual(self.check.kind, "simple")
  69. def test_it_sets_single_channel(self):
  70. channel = Channel.objects.create(user=self.alice)
  71. # Create another channel so we can test that only the first one
  72. # gets assigned:
  73. Channel.objects.create(user=self.alice)
  74. r = self.post(self.check.code, {
  75. "api_key": "X" * 32,
  76. "channels": str(channel.code)
  77. })
  78. self.assertEqual(r.status_code, 200)
  79. self.check.refresh_from_db()
  80. self.assertEqual(self.check.channel_set.count(), 1)
  81. self.assertEqual(self.check.channel_set.first().code, channel.code)
  82. def test_it_handles_comma_separated_channel_codes(self):
  83. c1 = Channel.objects.create(user=self.alice)
  84. c2 = Channel.objects.create(user=self.alice)
  85. r = self.post(self.check.code, {
  86. "api_key": "X" * 32,
  87. "channels": "%s,%s" % (c1.code, c2.code)
  88. })
  89. self.assertEqual(r.status_code, 200)
  90. self.check.refresh_from_db()
  91. self.assertEqual(self.check.channel_set.count(), 2)
  92. def test_it_handles_asterix(self):
  93. Channel.objects.create(user=self.alice)
  94. Channel.objects.create(user=self.alice)
  95. r = self.post(self.check.code, {
  96. "api_key": "X" * 32,
  97. "channels": "*"
  98. })
  99. self.assertEqual(r.status_code, 200)
  100. self.check.refresh_from_db()
  101. self.assertEqual(self.check.channel_set.count(), 2)
  102. def test_it_ignores_channels_if_channels_key_missing(self):
  103. Channel.objects.create(user=self.alice)
  104. self.check.assign_all_channels()
  105. r = self.post(self.check.code, {"api_key": "X" * 32})
  106. self.assertEqual(r.status_code, 200)
  107. check = Check.objects.get()
  108. self.assertEqual(check.channel_set.count(), 1)
  109. def test_it_rejects_bad_channel_code(self):
  110. r = self.post(self.check.code, {
  111. "api_key": "X" * 32,
  112. "channels": str(uuid.uuid4())
  113. })
  114. self.assertEqual(r.status_code, 400)
  115. self.check.refresh_from_db()
  116. self.assertEqual(self.check.channel_set.count(), 0)
  117. def test_it_rejects_non_string_channels_key(self):
  118. r = self.post(self.check.code, {
  119. "api_key": "X" * 32,
  120. "channels": None
  121. })
  122. self.assertEqual(r.status_code, 400)