Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mod_api/middleware/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions tests/api/test_middleware_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions tests/api/test_routes_runs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import json
from unittest.mock import patch

Expand Down Expand Up @@ -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('[email protected]',
'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`
Expand Down