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.

192 lines
6.5 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 Flip, 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)
  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. self.assertEqual(self.check.alert_after, self.check.get_alert_after())
  23. def test_it_updates_status(self):
  24. self.check.last_ping = timezone.now() - td(days=2)
  25. self.check.status = "down"
  26. self.check.save()
  27. # 1 week:
  28. payload = {"kind": "simple", "timeout": 3600 * 24 * 7, "grace": 60}
  29. self.client.login(username="[email protected]", password="password")
  30. r = self.client.post(self.url, data=payload)
  31. self.assertRedirects(r, "/checks/")
  32. self.check.refresh_from_db()
  33. self.assertEqual(self.check.status, "up")
  34. flip = Flip.objects.get()
  35. self.assertEqual(flip.owner_id, self.check.id)
  36. self.assertEqual(flip.old_status, "down")
  37. self.assertEqual(flip.new_status, "up")
  38. def test_it_saves_cron_expression(self):
  39. payload = {
  40. "kind": "cron",
  41. "schedule": "5 * * * *",
  42. "tz": "UTC",
  43. "grace": 60
  44. }
  45. self.client.login(username="[email protected]", password="password")
  46. r = self.client.post(self.url, data=payload)
  47. self.assertRedirects(r, "/checks/")
  48. self.check.refresh_from_db()
  49. self.assertEqual(self.check.kind, "cron")
  50. self.assertEqual(self.check.schedule, "5 * * * *")
  51. def test_it_validates_cron_expression(self):
  52. self.client.login(username="[email protected]", password="password")
  53. samples = ["* invalid *", "1,2 3,* * * *"]
  54. for sample in samples:
  55. payload = {
  56. "kind": "cron",
  57. "schedule": sample,
  58. "tz": "UTC",
  59. "grace": 60
  60. }
  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_rejects_six_field_cron_expression(self):
  67. payload = {
  68. "kind": "cron",
  69. "schedule": "* * * * * *", # six fields instead of five
  70. "tz": "UTC",
  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_validates_tz(self):
  80. payload = {
  81. "kind": "cron",
  82. "schedule": "* * * * *",
  83. "tz": "not-a-tz",
  84. "grace": 60
  85. }
  86. self.client.login(username="[email protected]", password="password")
  87. r = self.client.post(self.url, data=payload)
  88. self.assertEqual(r.status_code, 400)
  89. # Check should still have its original data:
  90. self.check.refresh_from_db()
  91. self.assertEqual(self.check.kind, "simple")
  92. def test_it_rejects_missing_schedule(self):
  93. # tz field is omitted so this should fail:
  94. payload = {
  95. "kind": "cron",
  96. "grace": 60,
  97. "tz": "UTC"
  98. }
  99. self.client.login(username="[email protected]", password="password")
  100. r = self.client.post(self.url, data=payload)
  101. self.assertEqual(r.status_code, 400)
  102. def test_it_rejects_missing_tz(self):
  103. # tz field is omitted so this should fail:
  104. payload = {
  105. "kind": "cron",
  106. "schedule": "* * * * *",
  107. "grace": 60
  108. }
  109. self.client.login(username="[email protected]", password="password")
  110. r = self.client.post(self.url, data=payload)
  111. self.assertEqual(r.status_code, 400)
  112. def test_team_access_works(self):
  113. payload = {"kind": "simple", "timeout": 7200, "grace": 60}
  114. # Logging in as bob, not alice. Bob has team access so this
  115. # should work.
  116. self.client.login(username="[email protected]", password="password")
  117. self.client.post(self.url, data=payload)
  118. check = Check.objects.get(code=self.check.code)
  119. assert check.timeout.total_seconds() == 7200
  120. def test_it_handles_bad_uuid(self):
  121. url = "/checks/not-uuid/timeout/"
  122. payload = {"timeout": 3600, "grace": 60}
  123. self.client.login(username="[email protected]", password="password")
  124. r = self.client.post(url, data=payload)
  125. self.assertEqual(r.status_code, 404)
  126. def test_it_handles_missing_uuid(self):
  127. # Valid UUID but there is no check for it:
  128. url = "/checks/6837d6ec-fc08-4da5-a67f-08a9ed1ccf62/timeout/"
  129. payload = {"timeout": 3600, "grace": 60}
  130. self.client.login(username="[email protected]", password="password")
  131. r = self.client.post(url, data=payload)
  132. assert r.status_code == 404
  133. def test_it_checks_ownership(self):
  134. payload = {"timeout": 3600, "grace": 60}
  135. self.client.login(username="[email protected]", password="password")
  136. r = self.client.post(self.url, data=payload)
  137. self.assertEqual(r.status_code, 404)
  138. def test_it_rejects_get(self):
  139. self.client.login(username="[email protected]", password="password")
  140. r = self.client.get(self.url)
  141. self.assertEqual(r.status_code, 405)
  142. def test_it_allows_cross_team_access(self):
  143. self.bobs_profile.current_team = None
  144. self.bobs_profile.save()
  145. payload = {"kind": "simple", "timeout": 3600, "grace": 60}
  146. self.client.login(username="[email protected]", password="password")
  147. r = self.client.post(self.url, data=payload)
  148. self.assertRedirects(r, "/checks/")