@@ -43,7 +43,7 @@ def test_file_download_event_builder(app, mock_user_ctx, sequential_ids, objects
4343 file_download_event_builder (event , app , file_obj )
4444 assert event == {
4545 # When:
46- "timestamp" : NewDate .now (tzinfo = timezone .utc ).isoformat (),
46+ "timestamp" : NewDate .now (tzinfo = timezone .utc ).replace ( tzinfo = None ). isoformat (),
4747 # What:
4848 "bucket_id" : str (file_obj .bucket_id ),
4949 "file_id" : str (file_obj .file_id ),
@@ -63,7 +63,7 @@ def test_record_view_event_builder(app, mock_user_ctx, record, pid):
6363 record_view_event_builder (event , app , pid , record )
6464 assert event == {
6565 # When:
66- "timestamp" : NewDate .now (tzinfo = timezone .utc ).isoformat (),
66+ "timestamp" : NewDate .now (tzinfo = timezone .utc ).replace ( tzinfo = None ). isoformat (),
6767 # What:
6868 "record_id" : str (record .id ),
6969 "pid_type" : pid .pid_type ,
@@ -72,3 +72,38 @@ def test_record_view_event_builder(app, mock_user_ctx, record, pid):
7272 # Who:
7373 ** get_user (),
7474 }
75+
76+
77+ def test_file_download_event_builder_aware_datetime (
78+ app , mock_user_ctx , sequential_ids , objects
79+ ):
80+ """Test file-download event builder produces aware UTC datetime when opted in."""
81+ file_obj = objects [0 ]
82+ file_obj .bucket_id = sequential_ids [0 ]
83+
84+ app .config ["STATS_EVENTS_USE_NAIVE_UTC_DATETIME" ] = False
85+ try :
86+ with app .test_request_context (headers = headers ):
87+ event = {}
88+ with patch ("datetime.datetime" , NewDate ):
89+ file_download_event_builder (event , app , file_obj )
90+ assert event ["timestamp" ] == NewDate .now (tzinfo = timezone .utc ).isoformat ()
91+ # Aware ISO format must contain timezone offset
92+ assert "+00:00" in event ["timestamp" ]
93+ finally :
94+ app .config ["STATS_EVENTS_USE_NAIVE_UTC_DATETIME" ] = True
95+
96+
97+ def test_record_view_event_builder_aware_datetime (app , mock_user_ctx , record , pid ):
98+ """Test record-view event builder produces aware UTC datetime when opted in."""
99+ app .config ["STATS_EVENTS_USE_NAIVE_UTC_DATETIME" ] = False
100+ try :
101+ with app .test_request_context (headers = headers ):
102+ event = {}
103+ with patch ("datetime.datetime" , NewDate ):
104+ record_view_event_builder (event , app , pid , record )
105+ assert event ["timestamp" ] == NewDate .now (tzinfo = timezone .utc ).isoformat ()
106+ # Aware ISO format must contain timezone offset
107+ assert "+00:00" in event ["timestamp" ]
108+ finally :
109+ app .config ["STATS_EVENTS_USE_NAIVE_UTC_DATETIME" ] = True
0 commit comments