From 12ab14e6aab557ac2c5e4f2585b2e3ad481e607a Mon Sep 17 00:00:00 2001 From: kaihere14 Date: Thu, 26 Nov 2026 21:45:46 +0530 Subject: [PATCH] [FIX] Convert API date filters to UTC before querying created_after and created_before accept an ISO 8601 datetime with any UTC offset, but the offset was passed through to the query. PyMySQL drops the offset when binding a datetime, while TestProgress timestamps are stored in UTC, so 2024-01-02T00:00:00+05:30 was compared as 2024-01-02 00:00 UTC instead of 2024-01-01 18:30 UTC. Convert the parsed value to UTC first. --- mod_api/middleware/validation.py | 4 +++- tests/api/test_middleware_validation.py | 11 +++++++++++ tests/api/test_routes_runs.py | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/mod_api/middleware/validation.py b/mod_api/middleware/validation.py index 7922db568..defe217a9 100644 --- a/mod_api/middleware/validation.py +++ b/mod_api/middleware/validation.py @@ -250,7 +250,9 @@ def _parse_iso8601_date(param_name, param_str): ) if dt.tzinfo is None: dt = dt.replace(tzinfo=timezone.utc) - return dt, None + # Timestamps are stored in UTC and the database driver drops the offset + # when binding, so convert before the value reaches a query. + return dt.astimezone(timezone.utc), None def validate_date_range(f): diff --git a/tests/api/test_middleware_validation.py b/tests/api/test_middleware_validation.py index 04b19f91f..46096b292 100644 --- a/tests/api/test_middleware_validation.py +++ b/tests/api/test_middleware_validation.py @@ -115,6 +115,17 @@ def dummy_handler(created_after=None, created_before=None): self.assertEqual(res.status_code, 200) self.assertIn('2023-01-01', res.json['after']) + def test_validate_date_range_converts_offset_to_utc(self): + """A datetime given with a UTC offset is passed on as the same instant in UTC.""" + @validate_date_range + def dummy_handler(created_after=None, created_before=None): + return jsonify({"after": created_after.isoformat()}) + + with self.app.test_request_context('/dummy?created_after=2024-01-02T00:00:00%2B05:30'): + res = dummy_handler() + self.assertEqual(res.status_code, 200) + self.assertEqual(res.json['after'], '2024-01-01T18:30:00+00:00') + def test_validate_date_range_invalid_format(self): @validate_date_range def dummy_handler(created_after=None, created_before=None): diff --git a/tests/api/test_routes_runs.py b/tests/api/test_routes_runs.py index 074aff6da..37081079a 100644 --- a/tests/api/test_routes_runs.py +++ b/tests/api/test_routes_runs.py @@ -1,3 +1,4 @@ +import datetime import json from unittest.mock import patch @@ -78,6 +79,23 @@ def test_list_runs_status_filter(self): self.assertEqual(res.status_code, 200) self.assertEqual(len(res.json['data']), 1) + def test_list_runs_date_filter_honours_utc_offset(self): + # 2024-01-02T00:00:00+05:30 is 2024-01-01T18:30:00Z, so a run that + # started at 20:00Z is after it, whichever way the instant is written. + self.progress.timestamp = datetime.datetime(2024, 1, 1, 20, 0, tzinfo=datetime.timezone.utc) + g.db.commit() + + token = self.get_token('runs_user@local.com', + 'userpass123', 'tz', scopes=['runs:read']) + for instant in ('2024-01-01T18:30:00Z', '2024-01-02T00:00:00%2B05:30'): + with self.subTest(instant=instant): + after = self.client.get(f'/api/v1/runs?created_after={instant}', + headers={'Authorization': f'Bearer {token}'}) + before = self.client.get(f'/api/v1/runs?created_before={instant}', + headers={'Authorization': f'Bearer {token}'}) + self.assertIn(self.test_id, [r['run_id'] for r in after.json['data']]) + self.assertNotIn(self.test_id, [r['run_id'] for r in before.json['data']]) + def test_list_runs_status_queued(self): # A run with no TestProgress rows is 'queued'. This guards the # status=queued filter, which must emit SQL `IS NULL`