@@ -53,6 +53,7 @@ def test_download_and_get_hash(self):
5353
5454 mock_response = MagicMock ()
5555 mock_response .read .side_effect = [mock_binary_data , b"" ]
56+ mock_response .status = 200
5657 mock_response .__enter__ .return_value = mock_response
5758
5859 with patch ("urllib3.PoolManager.request" , return_value = mock_response ):
@@ -80,6 +81,7 @@ def test_download_and_get_hash_auth_type_header(self):
8081 credentials = "Bearer token123"
8182
8283 mock_response = MagicMock ()
84+ mock_response .status = 200
8385 mock_response .read .side_effect = [mock_binary_data , b"" ]
8486 mock_response .__enter__ .return_value = mock_response
8587
@@ -105,6 +107,7 @@ def test_download_and_get_hash_auth_type_header(self):
105107 "User-Agent" : expected_user_agent ,
106108 api_key_parameter_name : credentials ,
107109 },
110+ redirect = True ,
108111 )
109112
110113 if os .path .exists (file_path ):
@@ -113,28 +116,26 @@ def test_download_and_get_hash_auth_type_header(self):
113116 def test_download_and_get_hash_auth_type_api_key (self ):
114117 """
115118 Test the download_and_get_hash function for authentication type 1 (API key).
116-
117- This test verifies that the download_and_get_hash function correctly handles authentication type 1,
118- where the credentials are passed as a query parameter in the URL. It mocks the necessary components
119- and checks that the request is made with the appropriate URL containing the API key.
120-
121119 """
122120 mock_binary_data = b"binary data for auth type 1"
123121 expected_hash = hashlib .sha256 (mock_binary_data ).hexdigest ()
124122 file_path = "test_file.txt"
125123 base_url = "https://test.com"
126124 api_key_parameter_name = "api_key"
127125 credentials = "key123"
128-
129126 modified_url = f"{ base_url } ?{ api_key_parameter_name } ={ credentials } "
130127
131128 mock_response = MagicMock ()
132129 mock_response .read .side_effect = [mock_binary_data , b"" ]
130+ mock_response .status = 200
131+ mock_response .release_conn = MagicMock ()
133132 mock_response .__enter__ .return_value = mock_response
134133
135- with patch (
136- "urllib3.PoolManager.request" , return_value = mock_response
137- ) as mock_request :
134+ mock_http = MagicMock ()
135+ mock_http .request .return_value = mock_response
136+ mock_http .__enter__ .return_value = mock_http
137+
138+ with patch ("urllib3.PoolManager" , return_value = mock_http ):
138139 result_hash = download_and_get_hash (
139140 base_url ,
140141 file_path ,
@@ -148,15 +149,15 @@ def test_download_and_get_hash_auth_type_api_key(self):
148149 self .assertEqual (
149150 result_hash ,
150151 expected_hash ,
151- msg = f"Hash mismatch: got { result_hash } ,"
152- f" but expected { expected_hash } " ,
152+ msg = f"Hash mismatch: got { result_hash } , but expected { expected_hash } " ,
153153 )
154154
155- mock_request .assert_called_with (
155+ mock_http . request .assert_called_with (
156156 "GET" ,
157157 modified_url ,
158158 preload_content = False ,
159159 headers = {"User-Agent" : expected_user_agent },
160+ redirect = True ,
160161 )
161162
162163 if os .path .exists (file_path ):
0 commit comments