Browse Source

Validate cron expression before saving check.

pull/109/head
Pēteris Caune 8 years ago
parent
commit
b22b0a44e2
4 changed files with 38 additions and 7 deletions
  1. +3
    -2
      hc/front/forms.py
  2. +23
    -2
      hc/front/tests/test_update_timeout.py
  3. +11
    -0
      hc/front/validators.py
  4. +1
    -3
      templates/front/my_checks.html

+ 3
- 2
hc/front/forms.py View File

@ -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)


+ 23
- 2
hc/front/tests/test_update_timeout.py View File

@ -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="[email protected]", 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


+ 11
- 0
hc/front/validators.py View File

@ -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)

+ 1
- 3
templates/front/my_checks.html View File

@ -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…
Cancel
Save