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.

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