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.

135 lines
4.3 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 = Channel(user=self.alice)
  37. channel.save()
  38. self.check.assign_all_channels()
  39. r = self.post(self.check.code, {
  40. "api_key": "X" * 32,
  41. "channels": ""
  42. })
  43. self.assertEqual(r.status_code, 200)
  44. check = Check.objects.get()
  45. self.assertEqual(check.channel_set.count(), 0)
  46. def test_it_requires_post(self):
  47. url = "/api/v1/checks/%s" % self.check.code
  48. r = self.client.get(url, HTTP_X_API_KEY="X" * 32)
  49. self.assertEqual(r.status_code, 405)
  50. def test_it_handles_invalid_uuid(self):
  51. r = self.post("not-an-uuid", {"api_key": "X" * 32})
  52. self.assertEqual(r.status_code, 404)
  53. def test_it_handles_missing_check(self):
  54. made_up_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  55. r = self.post(made_up_code, {"api_key": "X" * 32})
  56. self.assertEqual(r.status_code, 404)
  57. def test_it_validates_ownership(self):
  58. check = Check(user=self.bob, status="up")
  59. check.save()
  60. r = self.post(check.code, {"api_key": "X" * 32})
  61. self.assertEqual(r.status_code, 403)
  62. def test_it_updates_cron_to_simple(self):
  63. self.check.kind = "cron"
  64. self.check.schedule = "5 * * * *"
  65. self.check.save()
  66. r = self.post(self.check.code, {"api_key": "X" * 32, "timeout": 3600})
  67. self.assertEqual(r.status_code, 200)
  68. self.check.refresh_from_db()
  69. self.assertEqual(self.check.kind, "simple")
  70. def test_it_updates_specific_channels(self):
  71. channel1 = Channel(user=self.alice)
  72. channel1.save()
  73. channel2 = Channel(user=self.alice)
  74. channel2.save()
  75. r = self.post(self.check.code, {
  76. "api_key": "X" * 32,
  77. "channels": str(channel1.code)
  78. })
  79. self.assertEqual(r.status_code, 200)
  80. check = Check.objects.get()
  81. self.assertEqual(check.channel_set.count(), 1)
  82. self.assertEqual(check.channel_set.first().code, channel1.code)
  83. # Change to the other channel
  84. r = self.post(self.check.code, {
  85. "api_key": "X" * 32,
  86. "channels": str(channel2.code)
  87. })
  88. self.assertEqual(r.status_code, 200)
  89. check = Check.objects.get()
  90. self.assertEqual(check.channel_set.count(), 1)
  91. self.assertEqual(check.channel_set.first().code, channel2.code)
  92. # Now set both channels
  93. r = self.post(self.check.code, {
  94. "api_key": "X" * 32,
  95. "channels": str(channel2.code) + "," + str(channel1.code)
  96. })
  97. self.assertEqual(r.status_code, 200)
  98. check = Check.objects.get()
  99. self.assertEqual(check.channel_set.count(), 2)
  100. # Try to use channel that does not exist
  101. r = self.post(self.check.code, {
  102. "api_key": "X" * 32,
  103. "channels": str(uuid.uuid4())
  104. })
  105. self.assertEqual(r.status_code, 400)
  106. check = Check.objects.get()
  107. self.assertEqual(check.channel_set.count(), 0)