Using UTC dates on API validation for scheduled jobs.

This commit is contained in:
Martyn Inglis
2016-08-30 12:47:33 +01:00
parent 8b9319ac7a
commit b923392c49
4 changed files with 18 additions and 7 deletions

View File

@@ -101,6 +101,7 @@ def create_job(service_id):
dao_fetch_service_by_id(service_id) dao_fetch_service_by_id(service_id)
data = request.get_json() data = request.get_json()
data.update({ data.update({
"service": service_id "service": service_id
}) })

View File

@@ -39,7 +39,7 @@ def _validate_positive_number(value, msg="Not a positive integer"):
raise ValidationError(msg) raise ValidationError(msg)
def _validate_not_more_than_24_hours_in_future(dte, msg="Date cannot be more than 24hrs in the future"): def _validate_datetime_not_more_than_24_hours_in_future(dte, msg="Date cannot be more than 24hrs in the future"):
if dte > datetime.utcnow() + timedelta(hours=24): if dte > datetime.utcnow() + timedelta(hours=24):
raise ValidationError(msg) raise ValidationError(msg)
@@ -50,7 +50,17 @@ def _validate_not_in_future(dte, msg="Date cannot be in the future"):
def _validate_not_in_past(dte, msg="Date cannot be in the past"): def _validate_not_in_past(dte, msg="Date cannot be in the past"):
if dte < datetime.today(): if dte < date.today():
raise ValidationError(msg)
def _validate_datetime_not_in_future(dte, msg="Date cannot be in the future"):
if dte > datetime.utcnow():
raise ValidationError(msg)
def _validate_datetime_not_in_past(dte, msg="Date cannot be in the past"):
if dte < datetime.utcnow():
raise ValidationError(msg) raise ValidationError(msg)
@@ -223,8 +233,8 @@ class JobSchema(BaseSchema):
@validates('scheduled_for') @validates('scheduled_for')
def validate_scheduled_for(self, value): def validate_scheduled_for(self, value):
_validate_not_in_past(value) _validate_datetime_not_in_past(value)
_validate_not_more_than_24_hours_in_future(value) _validate_datetime_not_more_than_24_hours_in_future(value)
class Meta: class Meta:
model = models.Job model = models.Job

View File

@@ -52,7 +52,7 @@ class Config(object):
CELERYBEAT_SCHEDULE = { CELERYBEAT_SCHEDULE = {
'run-scheduled-jobs': { 'run-scheduled-jobs': {
'task': 'run-scheduled-jobs', 'task': 'run-scheduled-jobs',
'schedule': crontab(minute=1), 'schedule': crontab(),
'options': {'queue': 'periodic'} 'options': {'queue': 'periodic'}
}, },
'delete-verify-codes': { 'delete-verify-codes': {

View File

@@ -2,6 +2,7 @@ from datetime import date, timedelta
from flask import json from flask import json
from freezegun import freeze_time from freezegun import freeze_time
from datetime import datetime
from tests import create_authorization_header from tests import create_authorization_header
from tests.app.conftest import ( from tests.app.conftest import (
@@ -196,7 +197,6 @@ def test_get_notification_statistics_returns_both_existing_stats_and_generated_z
assert response.status_code == 200 assert response.status_code == 200
@freeze_time('1955-11-05T12:00:00')
def test_get_notification_statistics_returns_zeros_when_only_stats_for_different_date( def test_get_notification_statistics_returns_zeros_when_only_stats_for_different_date(
notify_api, notify_api,
sample_notification_statistics sample_notification_statistics
@@ -208,7 +208,7 @@ def test_get_notification_statistics_returns_zeros_when_only_stats_for_different
service_id=sample_notification_statistics.service_id service_id=sample_notification_statistics.service_id
) )
response = client.get( response = client.get(
'/notifications/statistics?day={}'.format(date.today().isoformat()), '/notifications/statistics?day={}'.format(datetime.utcnow().isoformat()),
headers=[auth_header] headers=[auth_header]
) )