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.

169 lines
6.2 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 = {"kind": "cron", "schedule": "5 * * * *", "tz": "UTC", "grace": 60}
  37. self.client.login(username="[email protected]", password="password")
  38. r = self.client.post(self.url, data=payload)
  39. self.assertRedirects(r, self.redirect_url)
  40. self.check.refresh_from_db()
  41. self.assertEqual(self.check.kind, "cron")
  42. self.assertEqual(self.check.schedule, "5 * * * *")
  43. def test_it_validates_cron_expression(self):
  44. self.client.login(username="[email protected]", password="password")
  45. samples = ["* invalid *", "1,2 3,* * * *"]
  46. for sample in samples:
  47. payload = {"kind": "cron", "schedule": sample, "tz": "UTC", "grace": 60}
  48. r = self.client.post(self.url, data=payload)
  49. self.assertEqual(r.status_code, 400)
  50. # Check should still have its original data:
  51. self.check.refresh_from_db()
  52. self.assertEqual(self.check.kind, "simple")
  53. def test_it_rejects_six_field_cron_expression(self):
  54. payload = {
  55. "kind": "cron",
  56. "schedule": "* * * * * *", # six fields instead of five
  57. "tz": "UTC",
  58. "grace": 60,
  59. }
  60. self.client.login(username="[email protected]", password="password")
  61. r = self.client.post(self.url, data=payload)
  62. self.assertEqual(r.status_code, 400)
  63. # Check should still have its original data:
  64. self.check.refresh_from_db()
  65. self.assertEqual(self.check.kind, "simple")
  66. def test_it_validates_tz(self):
  67. payload = {
  68. "kind": "cron",
  69. "schedule": "* * * * *",
  70. "tz": "not-a-tz",
  71. "grace": 60,
  72. }
  73. self.client.login(username="[email protected]", password="password")
  74. r = self.client.post(self.url, data=payload)
  75. self.assertEqual(r.status_code, 400)
  76. # Check should still have its original data:
  77. self.check.refresh_from_db()
  78. self.assertEqual(self.check.kind, "simple")
  79. def test_it_rejects_missing_schedule(self):
  80. # tz field is omitted so this should fail:
  81. payload = {"kind": "cron", "grace": 60, "tz": "UTC"}
  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. def test_it_rejects_missing_tz(self):
  86. # tz field is omitted so this should fail:
  87. payload = {"kind": "cron", "schedule": "* * * * *", "grace": 60}
  88. self.client.login(username="[email protected]", password="password")
  89. r = self.client.post(self.url, data=payload)
  90. self.assertEqual(r.status_code, 400)
  91. def test_team_access_works(self):
  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(self.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. payload = {"timeout": 3600, "grace": 60}
  114. self.client.login(username="[email protected]", password="password")
  115. r = self.client.post(self.url, data=payload)
  116. self.assertEqual(r.status_code, 404)
  117. def test_it_rejects_get(self):
  118. self.client.login(username="[email protected]", password="password")
  119. r = self.client.get(self.url)
  120. self.assertEqual(r.status_code, 405)
  121. def test_it_allows_cross_team_access(self):
  122. self.bobs_profile.current_project = None
  123. self.bobs_profile.save()
  124. payload = {"kind": "simple", "timeout": 3600, "grace": 60}
  125. self.client.login(username="[email protected]", password="password")
  126. r = self.client.post(self.url, data=payload)
  127. self.assertRedirects(r, self.redirect_url)