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.

49 lines
2.0 KiB

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