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.

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