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.

187 lines
6.3 KiB

9 years ago
9 years ago
9 years ago
  1. from datetime import timedelta as td
  2. from django.utils import timezone
  3. from hc.api.models import Check
  4. from hc.test import BaseTestCase
  5. class UpdateTimeoutTestCase(BaseTestCase):
  6. def setUp(self):
  7. super(UpdateTimeoutTestCase, self).setUp()
  8. self.check = Check(user=self.alice, status="up")
  9. self.check.last_ping = timezone.now()
  10. self.check.save()
  11. self.url = "/checks/%s/timeout/" % self.check.code
  12. def test_it_works(self):
  13. payload = {"kind": "simple", "timeout": 3600, "grace": 60}
  14. self.client.login(username="[email protected]", password="password")
  15. r = self.client.post(self.url, data=payload)
  16. self.assertRedirects(r, "/checks/")
  17. self.check.refresh_from_db()
  18. self.assertEqual(self.check.kind, "simple")
  19. self.assertEqual(self.check.timeout.total_seconds(), 3600)
  20. self.assertEqual(self.check.grace.total_seconds(), 60)
  21. # alert_after should be updated too
  22. expected_aa = self.check.last_ping + td(seconds=3600 + 60)
  23. self.assertEqual(self.check.alert_after, expected_aa)
  24. def test_it_does_not_update_status(self):
  25. self.check.last_ping = timezone.now() - td(days=2)
  26. self.check.status = "down"
  27. self.check.save()
  28. # 1 week:
  29. payload = {"kind": "simple", "timeout": 3600 * 24 * 7, "grace": 60}
  30. self.client.login(username="[email protected]", password="password")
  31. self.client.post(self.url, data=payload)
  32. self.check.refresh_from_db()
  33. self.assertEqual(self.check.status, "down")
  34. def test_it_saves_cron_expression(self):
  35. payload = {
  36. "kind": "cron",
  37. "schedule": "5 * * * *",
  38. "tz": "UTC",
  39. "grace": 60
  40. }
  41. self.client.login(username="[email protected]", password="password")
  42. r = self.client.post(self.url, data=payload)
  43. self.assertRedirects(r, "/checks/")
  44. self.check.refresh_from_db()
  45. self.assertEqual(self.check.kind, "cron")
  46. self.assertEqual(self.check.schedule, "5 * * * *")
  47. def test_it_validates_cron_expression(self):
  48. self.client.login(username="[email protected]", password="password")
  49. samples = ["* invalid *", "1,2 3,* * * *"]
  50. for sample in samples:
  51. payload = {
  52. "kind": "cron",
  53. "schedule": sample,
  54. "tz": "UTC",
  55. "grace": 60
  56. }
  57. r = self.client.post(self.url, data=payload)
  58. self.assertEqual(r.status_code, 400)
  59. # Check should still have its original data:
  60. self.check.refresh_from_db()
  61. self.assertEqual(self.check.kind, "simple")
  62. def test_it_rejects_six_field_cron_expression(self):
  63. payload = {
  64. "kind": "cron",
  65. "schedule": "* * * * * *", # six fields instead of five
  66. "tz": "UTC",
  67. "grace": 60
  68. }
  69. self.client.login(username="[email protected]", password="password")
  70. r = self.client.post(self.url, data=payload)
  71. self.assertEqual(r.status_code, 400)
  72. # Check should still have its original data:
  73. self.check.refresh_from_db()
  74. self.assertEqual(self.check.kind, "simple")
  75. def test_it_validates_tz(self):
  76. payload = {
  77. "kind": "cron",
  78. "schedule": "* * * * *",
  79. "tz": "not-a-tz",
  80. "grace": 60
  81. }
  82. self.client.login(username="[email protected]", password="password")
  83. r = self.client.post(self.url, data=payload)
  84. self.assertEqual(r.status_code, 400)
  85. # Check should still have its original data:
  86. self.check.refresh_from_db()
  87. self.assertEqual(self.check.kind, "simple")
  88. def test_it_rejects_missing_schedule(self):
  89. # tz field is omitted so this should fail:
  90. payload = {
  91. "kind": "cron",
  92. "grace": 60,
  93. "tz": "UTC"
  94. }
  95. self.client.login(username="[email protected]", password="password")
  96. r = self.client.post(self.url, data=payload)
  97. self.assertEqual(r.status_code, 400)
  98. def test_it_rejects_missing_tz(self):
  99. # tz field is omitted so this should fail:
  100. payload = {
  101. "kind": "cron",
  102. "schedule": "* * * * *",
  103. "grace": 60
  104. }
  105. self.client.login(username="[email protected]", password="password")
  106. r = self.client.post(self.url, data=payload)
  107. self.assertEqual(r.status_code, 400)
  108. def test_team_access_works(self):
  109. payload = {"kind": "simple", "timeout": 7200, "grace": 60}
  110. # Logging in as bob, not alice. Bob has team access so this
  111. # should work.
  112. self.client.login(username="[email protected]", password="password")
  113. self.client.post(self.url, data=payload)
  114. check = Check.objects.get(code=self.check.code)
  115. assert check.timeout.total_seconds() == 7200
  116. def test_it_handles_bad_uuid(self):
  117. url = "/checks/not-uuid/timeout/"
  118. payload = {"timeout": 3600, "grace": 60}
  119. self.client.login(username="[email protected]", password="password")
  120. r = self.client.post(url, data=payload)
  121. self.assertEqual(r.status_code, 404)
  122. def test_it_handles_missing_uuid(self):
  123. # Valid UUID but there is no check for it:
  124. url = "/checks/6837d6ec-fc08-4da5-a67f-08a9ed1ccf62/timeout/"
  125. payload = {"timeout": 3600, "grace": 60}
  126. self.client.login(username="[email protected]", password="password")
  127. r = self.client.post(url, data=payload)
  128. assert r.status_code == 404
  129. def test_it_checks_ownership(self):
  130. payload = {"timeout": 3600, "grace": 60}
  131. self.client.login(username="[email protected]", password="password")
  132. r = self.client.post(self.url, data=payload)
  133. self.assertEqual(r.status_code, 404)
  134. def test_it_rejects_get(self):
  135. self.client.login(username="[email protected]", password="password")
  136. r = self.client.get(self.url)
  137. self.assertEqual(r.status_code, 405)
  138. def test_it_allows_cross_team_access(self):
  139. self.bobs_profile.current_team = None
  140. self.bobs_profile.save()
  141. payload = {"kind": "simple", "timeout": 3600, "grace": 60}
  142. self.client.login(username="[email protected]", password="password")
  143. r = self.client.post(self.url, data=payload)
  144. self.assertRedirects(r, "/checks/")