99from services .dicom .dicom_uploader import DICOMUploader
1010
1111
12+ @patch ("services.dicom.dicom_uploader.requests.put" )
1213class TestDICOMUploader :
1314 @pytest .fixture
1415 def dicom_file (self ):
@@ -17,7 +18,6 @@ def dicom_file(self):
1718 tf .close ()
1819 yield tf .name
1920
20- @patch ("services.dicom.dicom_uploader.requests.put" )
2121 def test_upload_success (self , mock_put , dicom_file ):
2222 mock_response = Mock ()
2323 mock_response .status_code = 201
@@ -39,7 +39,7 @@ def test_upload_success(self, mock_put, dicom_file):
3939 files = mock_put .call_args [1 ]["files" ],
4040 timeout = 30 ,
4141 verify = True ,
42- headers = uploader .headers () ,
42+ headers = uploader .headers ,
4343 )
4444
4545 call_kwargs = mock_put .call_args [1 ]
@@ -49,14 +49,13 @@ def test_upload_success(self, mock_put, dicom_file):
4949 assert isinstance (file_tuple [1 ], io .BufferedReader )
5050 assert file_tuple [1 ].read () == open (dicom_file , "rb" ).read ()
5151
52- def test_upload_without_action_id (self , dicom_file ):
52+ def test_upload_without_action_id (self , _ , dicom_file ):
5353 """Upload without action_id does not make request."""
5454 uploader = DICOMUploader ()
5555 result = uploader .upload_dicom (sop_instance_uid = "1.2.3" , dicom_stream = open (dicom_file , "rb" ), action_id = None )
5656
5757 assert result is False
5858
59- @patch ("services.dicom.dicom_uploader.requests.put" )
6059 def test_upload_failure_status_code (self , mock_put , dicom_file ):
6160 mock_response = Mock ()
6261 mock_response .status_code = 500
@@ -68,7 +67,6 @@ def test_upload_failure_status_code(self, mock_put, dicom_file):
6867
6968 assert result is False
7069
71- @patch ("services.dicom.dicom_uploader.requests.put" )
7270 def test_upload_timeout (self , mock_put , dicom_file ):
7371 mock_put .side_effect = requests .exceptions .Timeout ()
7472
@@ -77,11 +75,43 @@ def test_upload_timeout(self, mock_put, dicom_file):
7775
7876 assert result is False
7977
80- @patch ("services.dicom.dicom_uploader.requests.put" )
8178 def test_upload_network_error (self , mock_put , dicom_file ):
8279 mock_put .side_effect = requests .exceptions .ConnectionError ()
8380
8481 uploader = DICOMUploader ()
8582 result = uploader .upload_dicom ("1.2.3" , open (dicom_file , "rb" ), None )
8683
8784 assert result is False
85+
86+ def test_upload_headers_with_managed_identity_access_token (self , _ , monkeypatch ):
87+ """Test that headers include access token from ManagedIdentityCredential."""
88+ monkeypatch .setenv ("CLOUD_API_RESOURCE" , "https://example.com/.default" )
89+ with patch ("services.dicom.dicom_uploader.ManagedIdentityCredential" ) as mock_credential :
90+ mock_credential_instance = Mock ()
91+ mock_credential_instance .get_token .return_value .token = "fake_access_token"
92+ mock_credential .return_value = mock_credential_instance
93+
94+ assert DICOMUploader ().headers == {"Authorization" : "Bearer fake_access_token" }
95+
96+ def test_upload_headers_without_managed_identity_resource (self , _ , monkeypatch ):
97+ """Test that headers include CLOUD_API_TOKEN if CLOUD_API_RESOURCE is not set."""
98+ monkeypatch .setenv ("CLOUD_API_TOKEN" , "env_access_token" )
99+
100+ assert DICOMUploader ().headers == {"Authorization" : "Bearer env_access_token" }
101+
102+ def test_upload_headers_in_production_with_no_cloud_api_resource (self , _ , monkeypatch ):
103+ """Test that headers include access token from ManagedIdentityCredential in production even if CLOUD_API_RESOURCE is not set."""
104+ monkeypatch .setenv ("ENVIRONMENT" , "prod" )
105+ with patch ("services.dicom.dicom_uploader.ManagedIdentityCredential" ) as mock_credential :
106+ mock_credential_instance = Mock ()
107+ mock_credential_instance .get_token .return_value .token = "prod_access_token"
108+ mock_credential .return_value = mock_credential_instance
109+
110+ assert DICOMUploader ().headers == {"Authorization" : "Bearer prod_access_token" }
111+ assert mock_credential_instance .get_token .call_args [0 ][0 ] == ""
112+
113+ def test_upload_headers_without_any_token (self , _ , monkeypatch ):
114+ """Test that headers include empty token if neither CLOUD_API_RESOURCE nor CLOUD_API_TOKEN is set."""
115+ monkeypatch .delenv ("CLOUD_API_RESOURCE" , raising = False )
116+ monkeypatch .delenv ("CLOUD_API_TOKEN" , raising = False )
117+ assert DICOMUploader ().headers == {"Authorization" : "Bearer " }
0 commit comments