Skip to content

Commit

Permalink
Add datetime validation
Browse files Browse the repository at this point in the history
STONEBLD-627

Signed-off-by: Felipe de Almeida <[email protected]>
  • Loading branch information
fepas authored and taylormadore committed Mar 9, 2023
1 parent 0213518 commit ee7cc50
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cachito/web/static/api_v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ paths:
schema:
type: string
example: "2019-09-17T18:52:57.577851"
format: datetime
- name: created_to
in: query
description: >
Expand All @@ -168,6 +169,7 @@ paths:
schema:
type: string
example: "2019-09-17T18:52:57.577851"
format: datetime
- name: error_origin
in: query
description: Filter requests by the given request error origin
Expand Down Expand Up @@ -715,6 +717,7 @@ paths:
required: false
schema:
type: string
format: datetime
examples:
date:
value: "2021-12-01"
Expand All @@ -728,6 +731,7 @@ paths:
required: false
schema:
type: string
format: datetime
examples:
date:
value: "2021-12-31"
Expand Down Expand Up @@ -787,13 +791,15 @@ paths:
schema:
type: string
example: "2021-12-01"
format: datetime
- name: finished_to
description: The latest possible date/time of request end (date, datetime or timestamp format)
in: query
required: true
schema:
type: string
example: "2021-12-31"
format: datetime
responses:
"200":
description: Aggregated data about all requests of the given period
Expand Down
17 changes: 17 additions & 0 deletions cachito/web/validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later

import datetime
from typing import Any, Dict, List

import jsonschema
Expand Down Expand Up @@ -94,3 +95,19 @@ def handle_wrapper(request):
raise ValidationError(exception.detail)

return handle_wrapper


@jsonschema.draft4_format_checker.checks("datetime")
def datetime_validator(val: Any) -> bool:
"""Validate that datetime fields have the correct format."""
if not isinstance(val, str):
raise ValidationError(
f"'{val}' is not string type to be evaluated as datetime(ISO 8601 format)."
)

try:
datetime.datetime.fromisoformat(val)
except ValueError:
raise ValidationError(f"'{val}' is not a valid datetime(ISO 8601 format).")

return True
29 changes: 29 additions & 0 deletions tests/test_api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,35 @@ def test_extra_query_parameter(client):
assert rv.json["error"] == "Extra query parameter(s) tom_hanks not in spec"


@pytest.mark.parametrize(
"date, is_valid, expected_status",
[
("tom_hanks", False, 400),
("2011-11-04", True, 200),
("2011-20-04", False, 400),
("2011-11-04T00:05:23", True, 200),
("2011-01-01T24:00:00", False, 400),
("2011-11-04 00:05:23.283", True, 200),
("2011-01-01T22:00:00.0", True, 200),
("1676468605", False, 400),
],
)
def test_datetime_validator(client, date, is_valid, expected_status):
urls = [
f"requests?created_from={date}",
f"requests?created_to={date}",
f"request-metrics?finished_from={date}",
f"request-metrics?finished_to={date}",
]

for url in urls:
rv = client.get(f"api/v1/{url}")
if not is_valid:
expected = f"'{date}' is not a valid datetime(ISO 8601 format)."
assert expected in rv.json["error"]
assert rv.status_code == expected_status


def test_fetch_paginated_requests(
app, auth_env, client, db, sample_deps_replace, sample_package, worker_auth_env, tmpdir
):
Expand Down

0 comments on commit ee7cc50

Please sign in to comment.