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.

61 lines
2.6 KiB

  1. from datetime import datetime
  2. from unittest.mock import patch
  3. from hc.test import BaseTestCase
  4. import pytz
  5. class CronPreviewTestCase(BaseTestCase):
  6. def test_it_works(self):
  7. payload = {"schedule": "* * * * *", "tz": "UTC"}
  8. r = self.client.post("/checks/cron_preview/", payload)
  9. self.assertContains(r, "cron-preview-title", status_code=200)
  10. self.assertContains(r, "“Every minute”")
  11. def test_it_accepts_sunday_7(self):
  12. payload = {"schedule": "* * * * 7", "tz": "UTC"}
  13. r = self.client.post("/checks/cron_preview/", payload)
  14. self.assertContains(r, "Expected Ping Dates", status_code=200)
  15. self.assertNotContains(r, "Invalid cron expression", status_code=200)
  16. def test_it_rejects_invalid_cron_expression(self):
  17. samples = ["", "*", "100 100 100 100 100", "* * * * * *"]
  18. for schedule in samples:
  19. payload = {"schedule": schedule, "tz": "UTC"}
  20. r = self.client.post("/checks/cron_preview/", payload)
  21. self.assertContains(r, "Invalid cron expression", status_code=200)
  22. def test_it_handles_invalid_timezone(self):
  23. for tz in ["", "not-a-timezone"]:
  24. payload = {"schedule": "* * * * *", "tz": tz}
  25. r = self.client.post("/checks/cron_preview/", payload)
  26. self.assertContains(r, "Invalid timezone", status_code=200)
  27. def test_it_handles_missing_arguments(self):
  28. r = self.client.post("/checks/cron_preview/", {})
  29. self.assertContains(r, "Invalid timezone", status_code=200)
  30. def test_it_rejects_get(self):
  31. r = self.client.get("/checks/cron_preview/", {})
  32. self.assertEqual(r.status_code, 405)
  33. @patch("hc.front.views.timezone.now")
  34. def test_it_handles_dst_transition(self, mock_now):
  35. # Consider year 2018, Riga, Latvia:
  36. # The daylight-saving-time ends at 4AM on October 28.
  37. # At that time, the clock is turned back one hour.
  38. # So, on that date, 3AM happens *twice* and saying
  39. # "3AM on October 28" is ambiguous.
  40. mock_now.return_value = datetime(2018, 10, 26, tzinfo=pytz.UTC)
  41. # This schedule will hit the ambiguous date. Cron preview must
  42. # be able to handle this:
  43. payload = {"schedule": "0 3 * * *", "tz": "Europe/Riga"}
  44. r = self.client.post("/checks/cron_preview/", payload)
  45. self.assertNotContains(r, "Invalid cron expression", status_code=200)
  46. def test_it_handles_feb_29(self):
  47. payload = {"schedule": "0 0 29 2 *", "tz": "UTC"}
  48. r = self.client.post("/checks/cron_preview/", payload)
  49. self.assertContains(r, "Feb 29")