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.

188 lines
6.4 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(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_saves_cron_expression(self):
  36. payload = {
  37. "kind": "cron",
  38. "schedule": "5 * * * *",
  39. "tz": "UTC",
  40. "grace": 60
  41. }
  42. self.client.login(username="[email protected]", password="password")
  43. r = self.client.post(self.url, data=payload)
  44. self.assertRedirects(r, self.redirect_url)
  45. self.check.refresh_from_db()
  46. self.assertEqual(self.check.kind, "cron")
  47. self.assertEqual(self.check.schedule, "5 * * * *")
  48. def test_it_validates_cron_expression(self):
  49. self.client.login(username="[email protected]", password="password")
  50. samples = ["* invalid *", "1,2 3,* * * *"]
  51. for sample in samples:
  52. payload = {
  53. "kind": "cron",
  54. "schedule": sample,
  55. "tz": "UTC",
  56. "grace": 60
  57. }
  58. r = self.client.post(self.url, data=payload)
  59. self.assertEqual(r.status_code, 400)
  60. # Check should still have its original data:
  61. self.check.refresh_from_db()
  62. self.assertEqual(self.check.kind, "simple")
  63. def test_it_rejects_six_field_cron_expression(self):
  64. payload = {
  65. "kind": "cron",
  66. "schedule": "* * * * * *", # six fields instead of five
  67. "tz": "UTC",
  68. "grace": 60
  69. }
  70. self.client.login(username="[email protected]", password="password")
  71. r = self.client.post(self.url, data=payload)
  72. self.assertEqual(r.status_code, 400)
  73. # Check should still have its original data:
  74. self.check.refresh_from_db()
  75. self.assertEqual(self.check.kind, "simple")
  76. def test_it_validates_tz(self):
  77. payload = {
  78. "kind": "cron",
  79. "schedule": "* * * * *",
  80. "tz": "not-a-tz",
  81. "grace": 60
  82. }
  83. self.client.login(username="[email protected]", password="password")
  84. r = self.client.post(self.url, data=payload)
  85. self.assertEqual(r.status_code, 400)
  86. # Check should still have its original data:
  87. self.check.refresh_from_db()
  88. self.assertEqual(self.check.kind, "simple")
  89. def test_it_rejects_missing_schedule(self):
  90. # tz field is omitted so this should fail:
  91. payload = {
  92. "kind": "cron",
  93. "grace": 60,
  94. "tz": "UTC"
  95. }
  96. self.client.login(username="[email protected]", password="password")
  97. r = self.client.post(self.url, data=payload)
  98. self.assertEqual(r.status_code, 400)
  99. def test_it_rejects_missing_tz(self):
  100. # tz field is omitted so this should fail:
  101. payload = {
  102. "kind": "cron",
  103. "schedule": "* * * * *",
  104. "grace": 60
  105. }
  106. self.client.login(username="[email protected]", password="password")
  107. r = self.client.post(self.url, data=payload)
  108. self.assertEqual(r.status_code, 400)
  109. def test_team_access_works(self):
  110. payload = {"kind": "simple", "timeout": 7200, "grace": 60}
  111. # Logging in as bob, not alice. Bob has team access so this
  112. # should work.
  113. self.client.login(username="[email protected]", password="password")
  114. self.client.post(self.url, data=payload)
  115. check = Check.objects.get(code=self.check.code)
  116. assert check.timeout.total_seconds() == 7200
  117. def test_it_handles_bad_uuid(self):
  118. url = "/checks/not-uuid/timeout/"
  119. payload = {"timeout": 3600, "grace": 60}
  120. self.client.login(username="[email protected]", password="password")
  121. r = self.client.post(url, data=payload)
  122. self.assertEqual(r.status_code, 404)
  123. def test_it_handles_missing_uuid(self):
  124. # Valid UUID but there is no check for it:
  125. url = "/checks/6837d6ec-fc08-4da5-a67f-08a9ed1ccf62/timeout/"
  126. payload = {"timeout": 3600, "grace": 60}
  127. self.client.login(username="[email protected]", password="password")
  128. r = self.client.post(url, data=payload)
  129. assert r.status_code == 404
  130. def test_it_checks_ownership(self):
  131. payload = {"timeout": 3600, "grace": 60}
  132. self.client.login(username="[email protected]", password="password")
  133. r = self.client.post(self.url, data=payload)
  134. self.assertEqual(r.status_code, 404)
  135. def test_it_rejects_get(self):
  136. self.client.login(username="[email protected]", password="password")
  137. r = self.client.get(self.url)
  138. self.assertEqual(r.status_code, 405)
  139. def test_it_allows_cross_team_access(self):
  140. self.bobs_profile.current_project = None
  141. self.bobs_profile.save()
  142. payload = {"kind": "simple", "timeout": 3600, "grace": 60}
  143. self.client.login(username="[email protected]", password="password")
  144. r = self.client.post(self.url, data=payload)
  145. self.assertRedirects(r, self.redirect_url)