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.

154 lines
5.3 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_it_rejects_missing_schedule(self):
  69. url = "/checks/%s/timeout/" % self.check.code
  70. # tz field is omitted so this should fail:
  71. payload = {
  72. "kind": "cron",
  73. "grace": 60,
  74. "tz": "UTC"
  75. }
  76. self.client.login(username="[email protected]", password="password")
  77. r = self.client.post(url, data=payload)
  78. self.assertEqual(r.status_code, 400)
  79. def test_it_rejects_missing_tz(self):
  80. url = "/checks/%s/timeout/" % self.check.code
  81. # tz field is omitted so this should fail:
  82. payload = {
  83. "kind": "cron",
  84. "schedule": "* * * * *",
  85. "grace": 60
  86. }
  87. self.client.login(username="[email protected]", password="password")
  88. r = self.client.post(url, data=payload)
  89. self.assertEqual(r.status_code, 400)
  90. def test_team_access_works(self):
  91. url = "/checks/%s/timeout/" % self.check.code
  92. payload = {"kind": "simple", "timeout": 7200, "grace": 60}
  93. # Logging in as bob, not alice. Bob has team access so this
  94. # should work.
  95. self.client.login(username="[email protected]", password="password")
  96. self.client.post(url, data=payload)
  97. check = Check.objects.get(code=self.check.code)
  98. assert check.timeout.total_seconds() == 7200
  99. def test_it_handles_bad_uuid(self):
  100. url = "/checks/not-uuid/timeout/"
  101. payload = {"timeout": 3600, "grace": 60}
  102. self.client.login(username="[email protected]", password="password")
  103. r = self.client.post(url, data=payload)
  104. self.assertEqual(r.status_code, 404)
  105. def test_it_handles_missing_uuid(self):
  106. # Valid UUID but there is no check for it:
  107. url = "/checks/6837d6ec-fc08-4da5-a67f-08a9ed1ccf62/timeout/"
  108. payload = {"timeout": 3600, "grace": 60}
  109. self.client.login(username="[email protected]", password="password")
  110. r = self.client.post(url, data=payload)
  111. assert r.status_code == 404
  112. def test_it_checks_ownership(self):
  113. url = "/checks/%s/timeout/" % self.check.code
  114. payload = {"timeout": 3600, "grace": 60}
  115. self.client.login(username="[email protected]", password="password")
  116. r = self.client.post(url, data=payload)
  117. assert r.status_code == 403
  118. def test_it_rejects_get(self):
  119. url = "/checks/%s/timeout/" % self.check.code
  120. self.client.login(username="[email protected]", password="password")
  121. r = self.client.get(url)
  122. self.assertEqual(r.status_code, 405)