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.

155 lines
5.7 KiB

  1. from datetime import datetime, timedelta as td
  2. from unittest.mock import patch
  3. from django.utils import timezone
  4. from hc.api.models import Flip, Check, Ping
  5. from hc.test import BaseTestCase
  6. class DetailsTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.check = Check.objects.create(project=self.project)
  10. ping = Ping.objects.create(owner=self.check)
  11. # Older MySQL versions don't store microseconds. This makes sure
  12. # the ping is older than any notifications we may create later:
  13. ping.created = "2000-01-01T00:00:00+00:00"
  14. ping.save()
  15. self.url = "/checks/%s/details/" % self.check.code
  16. def test_it_works(self):
  17. self.client.login(username="[email protected]", password="password")
  18. r = self.client.get(self.url)
  19. self.assertContains(r, "How To Ping", status_code=200)
  20. # The page should contain timezone strings
  21. self.assertContains(r, "Europe/Riga")
  22. def test_it_checks_ownership(self):
  23. self.client.login(username="[email protected]", password="password")
  24. r = self.client.get(self.url)
  25. self.assertEqual(r.status_code, 404)
  26. def test_it_shows_cron_expression(self):
  27. self.check.kind = "cron"
  28. self.check.save()
  29. self.client.login(username="[email protected]", password="password")
  30. r = self.client.get(self.url)
  31. self.assertContains(r, "Cron Expression", status_code=200)
  32. def test_it_allows_cross_team_access(self):
  33. self.client.login(username="[email protected]", password="password")
  34. r = self.client.get(self.url)
  35. self.assertEqual(r.status_code, 200)
  36. def test_it_shows_new_check_notice(self):
  37. self.client.login(username="[email protected]", password="password")
  38. r = self.client.get(self.url + "?new")
  39. self.assertContains(r, "Your new check is ready!", status_code=200)
  40. def test_it_hides_actions_from_readonly_users(self):
  41. self.bobs_membership.rw = False
  42. self.bobs_membership.save()
  43. self.client.login(username="[email protected]", password="password")
  44. r = self.client.get(self.url)
  45. self.assertNotContains(r, "edit-name", status_code=200)
  46. self.assertNotContains(r, "edit-desc")
  47. self.assertNotContains(r, "Filtering Rules")
  48. self.assertNotContains(r, "pause-btn")
  49. self.assertNotContains(r, "Change Schedule")
  50. self.assertNotContains(r, "Create a Copy…")
  51. self.assertNotContains(r, "transfer-btn")
  52. self.assertNotContains(r, "details-remove-check")
  53. def test_it_hides_resume_action_from_readonly_users(self):
  54. self.bobs_membership.rw = False
  55. self.bobs_membership.save()
  56. self.check.status = "paused"
  57. self.check.manual_resume = True
  58. self.check.save()
  59. self.client.login(username="[email protected]", password="password")
  60. r = self.client.get(self.url)
  61. self.assertNotContains(r, "resume-btn", status_code=200)
  62. def test_crontab_example_guesses_schedules(self):
  63. self.client.login(username="[email protected]", password="password")
  64. pairs = [
  65. (td(minutes=1), "* * * * *"),
  66. (td(minutes=12), "*/12 * * * *"),
  67. (td(hours=1), "0 * * * *"),
  68. (td(hours=6), "0 */6 * * *"),
  69. (td(days=1), "0 0 * * *"),
  70. ]
  71. for timeout, expression in pairs:
  72. self.check.timeout = timeout
  73. self.check.save()
  74. r = self.client.get(self.url)
  75. self.assertContains(r, f"{expression} /your/command.sh")
  76. self.assertNotContains(r, 'FIXME: replace "* * * * *"')
  77. def test_crontab_example_handles_unsupported_timeout_values(self):
  78. self.client.login(username="[email protected]", password="password")
  79. self.check.timeout = td(minutes=13)
  80. self.check.save()
  81. r = self.client.get(self.url)
  82. self.assertContains(r, f"* * * * * /your/command.sh")
  83. self.assertContains(r, 'FIXME: replace "* * * * *"')
  84. @patch("hc.lib.date.timezone.now")
  85. def test_it_calculates_downtime_summary(self, mock_now):
  86. mock_now.return_value = datetime(2020, 2, 1, tzinfo=timezone.utc)
  87. self.check.created = datetime(2019, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
  88. self.check.save()
  89. # going down on Jan 15, at 12:00
  90. f1 = Flip(owner=self.check)
  91. f1.created = datetime(2020, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
  92. f1.old_status = "up"
  93. f1.new_status = "down"
  94. f1.save()
  95. # back up on Jan 15, at 13:00
  96. f2 = Flip(owner=self.check)
  97. f2.created = datetime(2020, 1, 15, 13, 0, 0, tzinfo=timezone.utc)
  98. f2.old_status = "down"
  99. f2.new_status = "up"
  100. f2.save()
  101. self.client.login(username="[email protected]", password="password")
  102. r = self.client.get(self.url)
  103. self.assertContains(r, "Feb. 2020")
  104. self.assertContains(r, "Jan. 2020")
  105. self.assertContains(r, "Dec. 2019")
  106. # The summary for Jan. 2020 should be "1 downtime, 1 hour total"
  107. self.assertContains(r, "1 downtime, 1 hour total", html=True)
  108. @patch("hc.lib.date.timezone.now")
  109. def test_it_handles_months_when_check_did_not_exist(self, mock_now):
  110. mock_now.return_value = datetime(2020, 2, 1, tzinfo=timezone.utc)
  111. self.check.created = datetime(2020, 1, 10, 0, 0, 0, tzinfo=timezone.utc)
  112. self.check.save()
  113. self.client.login(username="[email protected]", password="password")
  114. r = self.client.get(self.url)
  115. self.assertContains(r, "Feb. 2020")
  116. self.assertContains(r, "Jan. 2020")
  117. self.assertContains(r, "Dec. 2019")
  118. # The summary for Dec. 2019 should be "–"
  119. self.assertContains(r, "<td>–</td>", html=True)