diff --git a/metobsapi/data_api.py b/metobsapi/data_api.py index 348e79885defd4243612734424ab6149cece370e..edc397b4d51e444831a729f8c0669d1c28200a09 100644 --- a/metobsapi/data_api.py +++ b/metobsapi/data_api.py @@ -36,11 +36,16 @@ ROUNDING = { def handle_date(date: str) -> datetime: datetime_fmt = "%Y-%m-%d" if len(date) == LENGTH_DATE_STR else "%Y-%m-%dT%H:%M:%S" try: - return datetime.strptime(date, datetime_fmt) + dt = datetime.strptime(date, datetime_fmt) except ValueError as e: msg = f"Malformed date string '{date}'" LOG.warning(msg) raise ValueError(msg) from e + if dt > (datetime.utcnow() + timedelta(minutes=1)): + msg = f"Date/time is in the future: {date}" + LOG.warning(msg) + raise ValueError(msg) + return dt def handle_time_string(date_string): @@ -392,7 +397,8 @@ def _convert_begin_and_end(begin, end) -> tuple[datetime | timedelta, datetime | begin = handle_time_string(begin) end = handle_time_string(end) except (TypeError, ValueError) as e: - raise ValueError("malformed_timestamp") from e + msg = f"malformed_timestamp: {str(e)}" + raise ValueError(msg) from e return begin, end diff --git a/metobsapi/tests/test_data_api.py b/metobsapi/tests/test_data_api.py index e891f87b6be026e145158469571249b0a110c352..6a82763a7bff17c4c456499bb2196cda8a2342fd 100644 --- a/metobsapi/tests/test_data_api.py +++ b/metobsapi/tests/test_data_api.py @@ -198,13 +198,23 @@ def test_bad_format(client): def test_bad_begin_json(client): - res = client.get("/api/data.json?symbols=air_temp&begin=blah") + res = client.get("/api/data.json?symbols=aoss.tower.air_temp&begin=blah") res = json.loads(res.data.decode()) assert res["code"] == 400 assert res["status"] == "error" assert "timestamp" in res["message"] +def test_future_begin_json(client): + now = datetime.utcnow() + future_begin = now + timedelta(days=1) + res = client.get(f"/api/data.json?symbols=aoss.tower.air_temp&begin={future_begin:%Y-%m-%dT%H:%M:%S}") + res = json.loads(res.data.decode()) + assert res["code"] == 400 + assert res["status"] == "error" + assert "is in the future" in res["message"] + + def test_bad_order(client): res = client.get("/api/data.json?order=blah&symbols=air_temp") res = json.loads(res.data.decode())