forked from GithubBackups/healthchecks
Validate cron expression before saving check.
This commit is contained in:
parent
0b6d484bd5
commit
b22b0a44e2
@ -1,5 +1,5 @@
|
||||
from django import forms
|
||||
from hc.front.validators import WebhookValidator
|
||||
from hc.front.validators import CronExpressionValidator, WebhookValidator
|
||||
from hc.api.models import CHECK_KINDS
|
||||
|
||||
|
||||
@ -21,7 +21,8 @@ class NameTagsForm(forms.Form):
|
||||
class TimeoutForm(forms.Form):
|
||||
kind = forms.ChoiceField(choices=CHECK_KINDS)
|
||||
timeout = forms.IntegerField(min_value=60, max_value=2592000)
|
||||
schedule = forms.CharField(required=False, max_length=100)
|
||||
schedule = forms.CharField(required=False, max_length=100,
|
||||
validators=[CronExpressionValidator()])
|
||||
tz = forms.CharField(required=False, max_length=36)
|
||||
grace = forms.IntegerField(min_value=60, max_value=2592000)
|
||||
|
||||
|
@ -31,7 +31,7 @@ class UpdateTimeoutTestCase(BaseTestCase):
|
||||
url = "/checks/%s/timeout/" % self.check.code
|
||||
payload = {
|
||||
"kind": "cron",
|
||||
"schedule": "* * * * *",
|
||||
"schedule": "5 * * * *",
|
||||
"tz": "UTC",
|
||||
"timeout": 60,
|
||||
"grace": 60
|
||||
@ -43,7 +43,28 @@ class UpdateTimeoutTestCase(BaseTestCase):
|
||||
|
||||
self.check.refresh_from_db()
|
||||
self.assertEqual(self.check.kind, "cron")
|
||||
self.assertEqual(self.check.schedule, "* * * * *")
|
||||
self.assertEqual(self.check.schedule, "5 * * * *")
|
||||
|
||||
def test_it_validates_cron_expression(self):
|
||||
self.check.last_ping = None
|
||||
self.check.save()
|
||||
|
||||
url = "/checks/%s/timeout/" % self.check.code
|
||||
payload = {
|
||||
"kind": "cron",
|
||||
"schedule": "* invalid *",
|
||||
"tz": "UTC",
|
||||
"timeout": 60,
|
||||
"grace": 60
|
||||
}
|
||||
|
||||
self.client.login(username="alice@example.org", password="password")
|
||||
r = self.client.post(url, data=payload)
|
||||
self.assertRedirects(r, "/checks/")
|
||||
|
||||
# Check should still have its original data:
|
||||
self.check.refresh_from_db()
|
||||
self.assertEqual(self.check.kind, "simple")
|
||||
|
||||
def test_team_access_works(self):
|
||||
url = "/checks/%s/timeout/" % self.check.code
|
||||
|
@ -1,3 +1,4 @@
|
||||
from croniter import croniter
|
||||
from django.core.exceptions import ValidationError
|
||||
from six.moves.urllib_parse import urlparse
|
||||
|
||||
@ -12,3 +13,13 @@ class WebhookValidator(object):
|
||||
|
||||
if parsed.hostname in ("127.0.0.1", "localhost"):
|
||||
raise ValidationError(message=self.message)
|
||||
|
||||
|
||||
class CronExpressionValidator(object):
|
||||
message = "Not a valid cron expression."
|
||||
|
||||
def __call__(self, value):
|
||||
try:
|
||||
croniter(value)
|
||||
except:
|
||||
raise ValidationError(message=self.message)
|
||||
|
@ -28,12 +28,10 @@
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
|
||||
|
||||
{% if checks %}
|
||||
{% include "front/my_checks_mobile.html" %}
|
||||
{% include "front/my_checks_desktop.html" %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user