forked from GithubBackups/healthchecks
Cleanup and tests for cron preview.
This commit is contained in:
parent
b29400710f
commit
aabfd55f7c
@ -1,5 +1,6 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from hc.front.validators import CronExpressionValidator, WebhookValidator
|
from hc.front.validators import (CronExpressionValidator, TimezoneValidator,
|
||||||
|
WebhookValidator)
|
||||||
|
|
||||||
|
|
||||||
class NameTagsForm(forms.Form):
|
class NameTagsForm(forms.Form):
|
||||||
@ -25,7 +26,8 @@ class TimeoutForm(forms.Form):
|
|||||||
class CronForm(forms.Form):
|
class CronForm(forms.Form):
|
||||||
schedule = forms.CharField(required=False, max_length=100,
|
schedule = forms.CharField(required=False, max_length=100,
|
||||||
validators=[CronExpressionValidator()])
|
validators=[CronExpressionValidator()])
|
||||||
tz = forms.CharField(required=False, max_length=36)
|
tz = forms.CharField(required=False, max_length=36,
|
||||||
|
validators=[TimezoneValidator()])
|
||||||
grace = forms.IntegerField(min_value=1, max_value=43200)
|
grace = forms.IntegerField(min_value=1, max_value=43200)
|
||||||
|
|
||||||
|
|
||||||
|
32
hc/front/tests/test_cron_preview.py
Normal file
32
hc/front/tests/test_cron_preview.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from hc.test import BaseTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class CronPreviewTestCase(BaseTestCase):
|
||||||
|
|
||||||
|
def test_it_works(self):
|
||||||
|
payload = {
|
||||||
|
"schedule": "* * * * *",
|
||||||
|
"tz": "UTC"
|
||||||
|
}
|
||||||
|
r = self.client.post("/checks/cron_preview/", payload)
|
||||||
|
self.assertContains(r, "cron-preview-title", status_code=200)
|
||||||
|
|
||||||
|
def test_it_handles_invalid_cron_expression(self):
|
||||||
|
for schedule in [None, "", "*", "100 100 100 100 100"]:
|
||||||
|
payload = {"schedule": schedule, "tz": "UTC"}
|
||||||
|
r = self.client.post("/checks/cron_preview/", payload)
|
||||||
|
self.assertContains(r, "Invalid cron expression", status_code=200)
|
||||||
|
|
||||||
|
def test_it_handles_invalid_timezone(self):
|
||||||
|
for tz in [None, "", "not-a-timezone"]:
|
||||||
|
payload = {"schedule": "* * * * *", "tz": tz}
|
||||||
|
r = self.client.post("/checks/cron_preview/", payload)
|
||||||
|
self.assertContains(r, "Invalid timezone", status_code=200)
|
||||||
|
|
||||||
|
def test_it_handles_missing_arguments(self):
|
||||||
|
r = self.client.post("/checks/cron_preview/", {})
|
||||||
|
self.assertContains(r, "Invalid cron expression", status_code=200)
|
||||||
|
|
||||||
|
def test_it_rejects_get(self):
|
||||||
|
r = self.client.get("/checks/cron_preview/", {})
|
||||||
|
self.assertEqual(r.status_code, 400)
|
@ -33,7 +33,6 @@ class UpdateTimeoutTestCase(BaseTestCase):
|
|||||||
"kind": "cron",
|
"kind": "cron",
|
||||||
"schedule": "5 * * * *",
|
"schedule": "5 * * * *",
|
||||||
"tz": "UTC",
|
"tz": "UTC",
|
||||||
"timeout": 60,
|
|
||||||
"grace": 60
|
"grace": 60
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,13 +53,32 @@ class UpdateTimeoutTestCase(BaseTestCase):
|
|||||||
"kind": "cron",
|
"kind": "cron",
|
||||||
"schedule": "* invalid *",
|
"schedule": "* invalid *",
|
||||||
"tz": "UTC",
|
"tz": "UTC",
|
||||||
"timeout": 60,
|
|
||||||
"grace": 60
|
"grace": 60
|
||||||
}
|
}
|
||||||
|
|
||||||
self.client.login(username="alice@example.org", password="password")
|
self.client.login(username="alice@example.org", password="password")
|
||||||
r = self.client.post(url, data=payload)
|
r = self.client.post(url, data=payload)
|
||||||
self.assertRedirects(r, "/checks/")
|
self.assertEqual(r.status_code, 400)
|
||||||
|
|
||||||
|
# Check should still have its original data:
|
||||||
|
self.check.refresh_from_db()
|
||||||
|
self.assertEqual(self.check.kind, "simple")
|
||||||
|
|
||||||
|
def test_it_validates_tz(self):
|
||||||
|
self.check.last_ping = None
|
||||||
|
self.check.save()
|
||||||
|
|
||||||
|
url = "/checks/%s/timeout/" % self.check.code
|
||||||
|
payload = {
|
||||||
|
"kind": "cron",
|
||||||
|
"schedule": "* * * * *",
|
||||||
|
"tz": "not-a-tz",
|
||||||
|
"grace": 60
|
||||||
|
}
|
||||||
|
|
||||||
|
self.client.login(username="alice@example.org", password="password")
|
||||||
|
r = self.client.post(url, data=payload)
|
||||||
|
self.assertEqual(r.status_code, 400)
|
||||||
|
|
||||||
# Check should still have its original data:
|
# Check should still have its original data:
|
||||||
self.check.refresh_from_db()
|
self.check.refresh_from_db()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from croniter import croniter
|
from croniter import croniter
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from six.moves.urllib_parse import urlparse
|
from six.moves.urllib_parse import urlparse
|
||||||
|
from pytz import all_timezones
|
||||||
|
|
||||||
|
|
||||||
class WebhookValidator(object):
|
class WebhookValidator(object):
|
||||||
@ -23,3 +24,11 @@ class CronExpressionValidator(object):
|
|||||||
croniter(value)
|
croniter(value)
|
||||||
except:
|
except:
|
||||||
raise ValidationError(message=self.message)
|
raise ValidationError(message=self.message)
|
||||||
|
|
||||||
|
|
||||||
|
class TimezoneValidator(object):
|
||||||
|
message = "Not a valid time zone."
|
||||||
|
|
||||||
|
def __call__(self, value):
|
||||||
|
if value not in all_timezones:
|
||||||
|
raise ValidationError(message=self.message)
|
||||||
|
@ -22,6 +22,7 @@ from hc.front.forms import (AddWebhookForm, NameTagsForm,
|
|||||||
TimeoutForm, AddUrlForm, AddPdForm, AddEmailForm,
|
TimeoutForm, AddUrlForm, AddPdForm, AddEmailForm,
|
||||||
AddOpsGenieForm, CronForm)
|
AddOpsGenieForm, CronForm)
|
||||||
from pytz import all_timezones
|
from pytz import all_timezones
|
||||||
|
from pytz.exceptions import UnknownTimeZoneError
|
||||||
|
|
||||||
|
|
||||||
# from itertools recipes:
|
# from itertools recipes:
|
||||||
@ -173,7 +174,7 @@ def update_timeout(request, code):
|
|||||||
if kind == "simple":
|
if kind == "simple":
|
||||||
form = TimeoutForm(request.POST)
|
form = TimeoutForm(request.POST)
|
||||||
if not form.is_valid():
|
if not form.is_valid():
|
||||||
return redirect("hc-checks")
|
return HttpResponseBadRequest()
|
||||||
|
|
||||||
check.kind = "simple"
|
check.kind = "simple"
|
||||||
check.timeout = td(seconds=form.cleaned_data["timeout"])
|
check.timeout = td(seconds=form.cleaned_data["timeout"])
|
||||||
@ -181,7 +182,7 @@ def update_timeout(request, code):
|
|||||||
elif kind == "cron":
|
elif kind == "cron":
|
||||||
form = CronForm(request.POST)
|
form = CronForm(request.POST)
|
||||||
if not form.is_valid():
|
if not form.is_valid():
|
||||||
return redirect("hc-checks")
|
return HttpResponseBadRequest()
|
||||||
|
|
||||||
check.kind = "cron"
|
check.kind = "cron"
|
||||||
check.schedule = form.cleaned_data["schedule"]
|
check.schedule = form.cleaned_data["schedule"]
|
||||||
@ -197,23 +198,24 @@ def update_timeout(request, code):
|
|||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def cron_preview(request):
|
def cron_preview(request):
|
||||||
|
if request.method != "POST":
|
||||||
|
return HttpResponseBadRequest()
|
||||||
|
|
||||||
schedule = request.POST.get("schedule")
|
schedule = request.POST.get("schedule")
|
||||||
tz = request.POST.get("tz")
|
tz = request.POST.get("tz")
|
||||||
|
ctx = {"tz": tz, "dates": []}
|
||||||
ctx = {
|
|
||||||
"tz": tz,
|
|
||||||
"dates": []
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with timezone.override(tz):
|
with timezone.override(tz):
|
||||||
now_naive = timezone.make_naive(timezone.now())
|
now_naive = timezone.make_naive(timezone.now())
|
||||||
it = croniter(schedule, now_naive)
|
it = croniter(schedule, now_naive)
|
||||||
for i in range(0, 6):
|
for i in range(0, 6):
|
||||||
date_naive = it.get_next(datetime)
|
naive = it.get_next(datetime)
|
||||||
ctx["dates"].append(timezone.make_aware(date_naive))
|
aware = timezone.make_aware(naive)
|
||||||
|
ctx["dates"].append((naive, aware))
|
||||||
|
except UnknownTimeZoneError:
|
||||||
|
ctx["bad_tz"] = True
|
||||||
except:
|
except:
|
||||||
ctx["error"] = True
|
ctx["bad_schedule"] = True
|
||||||
|
|
||||||
return render(request, "front/cron_preview.html", ctx)
|
return render(request, "front/cron_preview.html", ctx)
|
||||||
|
|
||||||
|
@ -30,10 +30,6 @@
|
|||||||
width: 70px;
|
width: 70px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#update-timeout-simple {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
#update-cron-form .modal-body {
|
#update-cron-form .modal-body {
|
||||||
padding: 40px;
|
padding: 40px;
|
||||||
}
|
}
|
||||||
@ -62,15 +58,15 @@
|
|||||||
font-size: small;
|
font-size: small;
|
||||||
}
|
}
|
||||||
|
|
||||||
.cron-preview-date {
|
#cron-preview-table tr td:nth-child(1) {
|
||||||
width: 120px;
|
width: 120px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.cron-preview-rel {
|
#cron-preview-table tr td:nth-child(2) {
|
||||||
font-size: small;
|
font-size: small;
|
||||||
}
|
}
|
||||||
|
|
||||||
.cron-preview-timestamp {
|
#cron-preview-table tr td:nth-child(3) {
|
||||||
font-size: small;
|
font-size: small;
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
text-align: right;
|
text-align: right;
|
||||||
@ -106,7 +102,6 @@
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.update-timeout-terms span {
|
.update-timeout-terms span {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ $(function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$("#cron-preview" ).html(data);
|
$("#cron-preview" ).html(data);
|
||||||
var haveError = $("#invalid-cron-expression").size() > 0;
|
var haveError = $("#invalid-arguments").size() > 0;
|
||||||
$("#update-cron-submit").prop("disabled", haveError);
|
$("#update-cron-submit").prop("disabled", haveError);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
// ===================
|
// ===================
|
||||||
var Tab = function( element,options ) {
|
var Tab = function( element,options ) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
this.tab = typeof element === 'object' ? element : document.querySelector(element);
|
this.tab = typeof element === 'object' ? element : document.querySelector(element);
|
||||||
this.tabs = this.tab.parentNode.parentNode;
|
this.tabs = this.tab.parentNode.parentNode;
|
||||||
this.dropdown = this.tabs.querySelector('.dropdown');
|
this.dropdown = this.tabs.querySelector('.dropdown');
|
||||||
@ -94,8 +94,6 @@
|
|||||||
} else if ( activeTabs.length > 1 ) {
|
} else if ( activeTabs.length > 1 ) {
|
||||||
return activeTabs[activeTabs.length-1]
|
return activeTabs[activeTabs.length-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(activeTabs.length)
|
|
||||||
},
|
},
|
||||||
this.getActiveContent = function() {
|
this.getActiveContent = function() {
|
||||||
var a = self.getActiveTab().getElementsByTagName('A')[0].getAttribute('href').replace('#','');
|
var a = self.getActiveTab().getElementsByTagName('A')[0].getAttribute('href').replace('#','');
|
||||||
|
@ -1,20 +1,18 @@
|
|||||||
{% load humanize tz %}
|
{% load humanize tz %}
|
||||||
|
|
||||||
{% if error %}
|
{% if bad_schedule %}
|
||||||
<p id="invalid-cron-expression">Invalid cron expression</p>
|
<p id="invalid-arguments">Invalid cron expression</p>
|
||||||
|
{% elif bad_tz %}
|
||||||
|
<p id="invalid-arguments">Invalid timezone</p>
|
||||||
{% else %}
|
{% else %}
|
||||||
<table class="table">
|
<table id="cron-preview-table" class="table">
|
||||||
|
<tr><th id="cron-preview-title" colspan="3">Expected Ping Dates</th></tr>
|
||||||
|
{% for naive, aware in dates %}
|
||||||
<tr>
|
<tr>
|
||||||
<th id="cron-preview-title" colspan="3">Expected Ping Dates</th>
|
<td>{{ naive|date:"M j, H:i" }}</td>
|
||||||
</tr>
|
<td>{{ aware|naturaltime }}</td>
|
||||||
{% for date in dates %}
|
<td>{{ aware|date:"c" }}</td>
|
||||||
<tr>
|
|
||||||
{% timezone tz %}
|
|
||||||
<td class="cron-preview-date">{{ date|date:"M j, H:i" }}</td>
|
|
||||||
{% endtimezone %}
|
|
||||||
<td class="cron-preview-rel">{{ date|naturaltime }}</td>
|
|
||||||
<td class="cron-preview-timestamp">{{ date|date:"c" }}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user