1- import pytest
21from datetime import datetime , timedelta
32from unittest .mock import patch
3+
4+ import pytest
45from rest_framework .exceptions import NotFound
56
67from upload .tokenless .azure import TokenlessAzureHandler
78
9+
810@pytest .fixture
911def upload_params ():
1012 return {
1113 "job" : "899861" ,
1214 "project" : "public" ,
1315 "server_uri" : "https://dev.azure.com/dnceng-public/" ,
1416 "build" : "20241219.14" ,
15- "commit" : "0f6e31fec5876be932f9e52f739ce1a2e04f11e3"
17+ "commit" : "0f6e31fec5876be932f9e52f739ce1a2e04f11e3" ,
1618 }
1719
20+
1821def test_verify_handles_nanosecond_timestamp (upload_params ):
1922 """
2023 Test that the handler correctly processes timestamps with nanosecond precision
2124 from the Azure DevOps API.
2225 """
2326 handler = TokenlessAzureHandler (upload_params )
24-
27+
2528 # Mock a response with nanosecond precision timestamp (7 digits after decimal)
2629 current_time = datetime .now ()
2730 timestamp = current_time .strftime ("%Y-%m-%dT%H:%M:%S.1234567Z" )
28-
31+
2932 mock_build_response = {
3033 "status" : "completed" ,
3134 "finishTime" : timestamp ,
3235 "buildNumber" : "20241219.14" ,
3336 "sourceVersion" : "0f6e31fec5876be932f9e52f739ce1a2e04f11e3" ,
34- "repository" : {"type" : "GitHub" }
37+ "repository" : {"type" : "GitHub" },
3538 }
36-
37- with patch .object (handler , ' get_build' , return_value = mock_build_response ):
39+
40+ with patch .object (handler , " get_build" , return_value = mock_build_response ):
3841 service = handler .verify ()
3942 assert service == "github"
4043
44+
4145def test_verify_handles_microsecond_timestamp (upload_params ):
4246 """
4347 Test that the handler still works correctly with regular microsecond precision
4448 timestamps.
4549 """
4650 handler = TokenlessAzureHandler (upload_params )
47-
51+
4852 # Mock a response with microsecond precision (6 digits after decimal)
4953 current_time = datetime .now ()
5054 timestamp = current_time .strftime ("%Y-%m-%dT%H:%M:%S.123456Z" )
51-
55+
5256 mock_build_response = {
5357 "status" : "completed" ,
5458 "finishTime" : timestamp ,
5559 "buildNumber" : "20241219.14" ,
5660 "sourceVersion" : "0f6e31fec5876be932f9e52f739ce1a2e04f11e3" ,
57- "repository" : {"type" : "GitHub" }
61+ "repository" : {"type" : "GitHub" },
5862 }
59-
60- with patch .object (handler , ' get_build' , return_value = mock_build_response ):
63+
64+ with patch .object (handler , " get_build" , return_value = mock_build_response ):
6165 service = handler .verify ()
6266 assert service == "github"
6367
68+
6469def test_verify_rejects_old_timestamp (upload_params ):
6570 """
6671 Test that the handler correctly rejects timestamps older than 4 minutes,
6772 even with nanosecond precision.
6873 """
6974 handler = TokenlessAzureHandler (upload_params )
70-
75+
7176 # Create a timestamp that's more than 4 minutes old
7277 old_time = datetime .now () - timedelta (minutes = 5 )
7378 timestamp = old_time .strftime ("%Y-%m-%dT%H:%M:%S.1234567Z" )
74-
79+
7580 mock_build_response = {
7681 "status" : "completed" ,
7782 "finishTime" : timestamp ,
7883 "buildNumber" : "20241219.14" ,
7984 "sourceVersion" : "0f6e31fec5876be932f9e52f739ce1a2e04f11e3" ,
80- "repository" : {"type" : "GitHub" }
85+ "repository" : {"type" : "GitHub" },
8186 }
82-
83- with patch .object (handler , ' get_build' , return_value = mock_build_response ):
87+
88+ with patch .object (handler , " get_build" , return_value = mock_build_response ):
8489 with pytest .raises (NotFound , match = "Azure build has already finished" ):
85- handler .verify ()
90+ handler .verify ()
0 commit comments