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.

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