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.

128 lines
4.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. from django.utils import timezone
  2. from hc.api.models import Check
  3. from hc.test import BaseTestCase
  4. class UpdateTimeoutTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(UpdateTimeoutTestCase, self).setUp()
  7. self.check = Check(user=self.alice)
  8. self.check.last_ping = timezone.now()
  9. self.check.save()
  10. def test_it_works(self):
  11. url = "/checks/%s/timeout/" % self.check.code
  12. payload = {"kind": "simple", "timeout": 3600, "grace": 60}
  13. self.client.login(username="[email protected]", password="password")
  14. r = self.client.post(url, data=payload)
  15. self.assertRedirects(r, "/checks/")
  16. self.check.refresh_from_db()
  17. self.assertEqual(self.check.kind, "simple")
  18. self.assertEqual(self.check.timeout.total_seconds(), 3600)
  19. self.assertEqual(self.check.grace.total_seconds(), 60)
  20. # alert_after should be updated too
  21. self.assertEqual(self.check.alert_after, self.check.get_alert_after())
  22. def test_it_saves_cron_expression(self):
  23. url = "/checks/%s/timeout/" % self.check.code
  24. payload = {
  25. "kind": "cron",
  26. "schedule": "5 * * * *",
  27. "tz": "UTC",
  28. "grace": 60
  29. }
  30. self.client.login(username="[email protected]", password="password")
  31. r = self.client.post(url, data=payload)
  32. self.assertRedirects(r, "/checks/")
  33. self.check.refresh_from_db()
  34. self.assertEqual(self.check.kind, "cron")
  35. self.assertEqual(self.check.schedule, "5 * * * *")
  36. def test_it_validates_cron_expression(self):
  37. self.check.last_ping = None
  38. self.check.save()
  39. url = "/checks/%s/timeout/" % self.check.code
  40. payload = {
  41. "kind": "cron",
  42. "schedule": "* invalid *",
  43. "tz": "UTC",
  44. "grace": 60
  45. }
  46. self.client.login(username="[email protected]", password="password")
  47. r = self.client.post(url, data=payload)
  48. self.assertEqual(r.status_code, 400)
  49. # Check should still have its original data:
  50. self.check.refresh_from_db()
  51. self.assertEqual(self.check.kind, "simple")
  52. def test_it_validates_tz(self):
  53. self.check.last_ping = None
  54. self.check.save()
  55. url = "/checks/%s/timeout/" % self.check.code
  56. payload = {
  57. "kind": "cron",
  58. "schedule": "* * * * *",
  59. "tz": "not-a-tz",
  60. "grace": 60
  61. }
  62. self.client.login(username="[email protected]", password="password")
  63. r = self.client.post(url, data=payload)
  64. self.assertEqual(r.status_code, 400)
  65. # Check should still have its original data:
  66. self.check.refresh_from_db()
  67. self.assertEqual(self.check.kind, "simple")
  68. def test_team_access_works(self):
  69. url = "/checks/%s/timeout/" % self.check.code
  70. payload = {"kind": "simple", "timeout": 7200, "grace": 60}
  71. # Logging in as bob, not alice. Bob has team access so this
  72. # should work.
  73. self.client.login(username="[email protected]", password="password")
  74. self.client.post(url, data=payload)
  75. check = Check.objects.get(code=self.check.code)
  76. assert check.timeout.total_seconds() == 7200
  77. def test_it_handles_bad_uuid(self):
  78. url = "/checks/not-uuid/timeout/"
  79. payload = {"timeout": 3600, "grace": 60}
  80. self.client.login(username="[email protected]", password="password")
  81. r = self.client.post(url, data=payload)
  82. assert r.status_code == 400
  83. def test_it_handles_missing_uuid(self):
  84. # Valid UUID but there is no check for it:
  85. url = "/checks/6837d6ec-fc08-4da5-a67f-08a9ed1ccf62/timeout/"
  86. payload = {"timeout": 3600, "grace": 60}
  87. self.client.login(username="[email protected]", password="password")
  88. r = self.client.post(url, data=payload)
  89. assert r.status_code == 404
  90. def test_it_checks_ownership(self):
  91. url = "/checks/%s/timeout/" % self.check.code
  92. payload = {"timeout": 3600, "grace": 60}
  93. self.client.login(username="[email protected]", password="password")
  94. r = self.client.post(url, data=payload)
  95. assert r.status_code == 403
  96. def test_it_rejects_get(self):
  97. url = "/checks/%s/timeout/" % self.check.code
  98. self.client.login(username="[email protected]", password="password")
  99. r = self.client.get(url)
  100. self.assertEqual(r.status_code, 405)