Browse Source

Relax cron expression validation

Accept all expressions that croniter accepts.
If cron-descriptor throws an exception, don't show the
description to the user.
pull/483/head
Pēteris Caune 4 years ago
parent
commit
1e84cac37d
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 19 additions and 2 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +7
    -1
      hc/front/tests/test_cron_preview.py
  3. +11
    -1
      hc/front/views.py

+ 1
- 0
CHANGELOG.md View File

@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
## Bug Fixes
- Fix downtime summary to handle months when the check didn't exist yet (#472)
- Relax cron expression validation: accept all expressions that croniter accepts
## v1.19.0 - 2021-02-03


+ 7
- 1
hc/front/tests/test_cron_preview.py View File

@ -12,8 +12,14 @@ class CronPreviewTestCase(BaseTestCase):
self.assertContains(r, "cron-preview-title", status_code=200)
self.assertContains(r, "“Every minute”")
def test_it_accepts_sunday_7(self):
payload = {"schedule": "* * * * 7", "tz": "UTC"}
r = self.client.post("/checks/cron_preview/", payload)
self.assertContains(r, "Expected Ping Dates", status_code=200)
self.assertNotContains(r, "Invalid cron expression", status_code=200)
def test_it_rejects_invalid_cron_expression(self):
samples = ["", "*", "100 100 100 100 100", "* * * * * *", "1,2 3,* * * *"]
samples = ["", "*", "100 100 100 100 100", "* * * * * *"]
for schedule in samples:
payload = {"schedule": schedule, "tz": "UTC"}


+ 11
- 1
hc/front/views.py View File

@ -477,12 +477,22 @@ def cron_preview(request):
for i in range(0, 6):
ctx["dates"].append(it.get_next(datetime))
ctx["desc"] = str(ExpressionDescriptor(schedule, use_24hour_time_format=True))
pass
except UnknownTimeZoneError:
ctx["bad_tz"] = True
except:
ctx["bad_schedule"] = True
if ctx["dates"]:
try:
descriptor = ExpressionDescriptor(schedule, use_24hour_time_format=True)
ctx["desc"] = descriptor.get_description()
except:
# We assume the schedule is valid if croniter accepts it.
# If cron-descriptor throws an exception, don't show the description
# to the user.
pass
return render(request, "front/cron_preview.html", ctx)


Loading…
Cancel
Save