@@ -65,160 +65,102 @@ def test_download_and_get_hash(self):
6565 if os .path .exists (file_path ):
6666 os .remove (file_path )
6767
68- # def test_download_and_get_hash_auth_type_1(self):
69- # mock_binary_data = b"binary data for auth type 1"
70- # expected_hash = hashlib.sha256(mock_binary_data).hexdigest()
71- # file_path = "test_file.txt"
72- # url = "https://test.com"
73- # api_key_parameter_name = "Authorization"
74- # credentials = "Bearer token123"
75-
76- # mock_response = MagicMock()
77- # mock_response.read.side_effect = [mock_binary_data, b""]
78- # mock_response.__enter__.return_value = mock_response
79-
80- # with patch(
81- # "urllib3.PoolManager.request", return_value=mock_response
82- # ) as mock_request:
83- # result_hash = download_and_get_hash(
84- # url, file_path, "sha256", 8192, 1, api_key_parameter_name, credentials
85- # )
86-
87- # self.assertEqual(
88- # result_hash,
89- # expected_hash,
90- # msg=f"Hash mismatch: got {result_hash},"
91- # f" but expected {expected_hash}",
92- # )
93-
94- # mock_request.assert_called_with(
95- # "GET",
96- # url,
97- # preload_content=False,
98- # headers={
99- # "User-Agent": expected_user_agent,
100- # api_key_parameter_name: credentials,
101- # },
102- # )
103-
104- # if os.path.exists(file_path):
105- # os.remove(file_path)
106-
107- # def test_download_and_get_hash_auth_type_2(self):
108- # mock_binary_data = b"binary data for auth type 2"
109- # expected_hash = hashlib.sha256(mock_binary_data).hexdigest()
110- # file_path = "test_file.txt"
111- # base_url = "https://test.com"
112- # api_key_parameter_name = "api_key"
113- # credentials = "key123"
114-
115- # modified_url = f"{base_url}?{api_key_parameter_name}={credentials}"
116-
117- # mock_response = MagicMock()
118- # mock_response.read.side_effect = [mock_binary_data, b""]
119- # mock_response.__enter__.return_value = mock_response
120-
121- # with patch(
122- # "urllib3.PoolManager.request", return_value=mock_response
123- # ) as mock_request:
124- # result_hash = download_and_get_hash(
125- # base_url,
126- # file_path,
127- # "sha256",
128- # 8192,
129- # 2,
130- # api_key_parameter_name,
131- # credentials,
132- # )
133-
134- # self.assertEqual(
135- # result_hash,
136- # expected_hash,
137- # msg=f"Hash mismatch: got {result_hash},"
138- # f" but expected {expected_hash}",
139- # )
140-
141- # mock_request.assert_called_with(
142- # "GET",
143- # modified_url,
144- # preload_content=False,
145- # headers={"User-Agent": expected_user_agent},
146- # )
147-
148- # if os.path.exists(file_path):
149- # os.remove(file_path)
150-
151- def test_download_and_get_hash_auth_type_1 (self ):
152- url = "http://example.com/data"
153- api_key_parameter_name = "api_key"
154- credentials = "test_api_key"
155- expected_url = f"{ url } ?{ api_key_parameter_name } ={ credentials } "
156-
157- with patch ("helpers.download.create_urllib3_context" ) as mock_create_context :
158- mock_context = mock_create_context .return_value
159- mock_context .load_default_certs .return_value = None
160-
161- with patch ("helpers.download.urllib3.PoolManager" ) as mock_pool_manager :
162- mock_http = mock_pool_manager .return_value
163- mock_response = mock_http .request .return_value
164- mock_response .read .side_effect = [b"data_chunk_1" , b"data_chunk_2" , b"" ]
165- mock_response .__enter__ .return_value = mock_response
166-
167- result = download_and_get_hash (
168- url ,
169- file_path = "test_file" ,
170- hash_algorithm = "sha256" ,
171- authentication_type = 1 ,
172- api_key_parameter_name = api_key_parameter_name ,
173- credentials = credentials ,
174- )
175-
176- mock_http .request .assert_called_with (
177- "GET" ,
178- expected_url ,
179- preload_content = False ,
180- headers = {
181- "User-Agent" : "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) "
182- "AppleWebKit/537.36 (KHTML, like Gecko) "
183- "Chrome/126.0.0.0 Mobile Safari/537.36"
184- },
185- )
186- assert result is not None
187-
188- def test_download_and_get_hash_auth_type_2 (self ):
189- url = "http://example.com/data"
68+ def test_download_and_get_hash_auth_type_header (self ):
69+ """
70+ Test the download_and_get_hash function for authentication type 2 (headers).
71+ This test verifies that the download_and_get_hash function correctly handles authentication type 2,
72+ where the credentials are passed in the headers. It mocks the necessary components and checks that
73+ the request is made with the appropriate headers.
74+ """
75+ mock_binary_data = b"binary data for auth type 2"
76+ expected_hash = hashlib .sha256 (mock_binary_data ).hexdigest ()
77+ file_path = "test_file.txt"
78+ url = "https://test.com"
19079 api_key_parameter_name = "Authorization"
191- credentials = "Bearer test_token"
192- expected_headers = {
193- "User-Agent" : "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) "
194- "AppleWebKit/537.36 (KHTML, like Gecko) "
195- "Chrome/126.0.0.0 Mobile Safari/537.36" ,
196- api_key_parameter_name : credentials ,
197- }
198-
199- with patch ("helpers.download.create_urllib3_context" ) as mock_create_context :
200- mock_context = mock_create_context .return_value
201- mock_context .load_default_certs .return_value = None
202-
203- with patch ("helpers.download.urllib3.PoolManager" ) as mock_pool_manager :
204- mock_http = mock_pool_manager .return_value
205- mock_response = mock_http .request .return_value
206- mock_response .read .side_effect = [b"data_chunk_1" , b"data_chunk_2" , b"" ]
207- mock_response .__enter__ .return_value = mock_response
208-
209- result = download_and_get_hash (
210- url ,
211- file_path = "test_file" ,
212- hash_algorithm = "sha256" ,
213- authentication_type = 2 ,
214- api_key_parameter_name = api_key_parameter_name ,
215- credentials = credentials ,
216- )
217-
218- mock_http .request .assert_called_with (
219- "GET" , url , preload_content = False , headers = expected_headers
220- )
221- assert result is not None
80+ credentials = "Bearer token123"
81+
82+ mock_response = MagicMock ()
83+ mock_response .read .side_effect = [mock_binary_data , b"" ]
84+ mock_response .__enter__ .return_value = mock_response
85+
86+ with patch (
87+ "urllib3.PoolManager.request" , return_value = mock_response
88+ ) as mock_request :
89+ result_hash = download_and_get_hash (
90+ url , file_path , "sha256" , 8192 , 2 , api_key_parameter_name , credentials
91+ )
92+
93+ self .assertEqual (
94+ result_hash ,
95+ expected_hash ,
96+ msg = f"Hash mismatch: got { result_hash } ,"
97+ f" but expected { expected_hash } " ,
98+ )
99+
100+ mock_request .assert_called_with (
101+ "GET" ,
102+ url ,
103+ preload_content = False ,
104+ headers = {
105+ "User-Agent" : expected_user_agent ,
106+ api_key_parameter_name : credentials ,
107+ },
108+ )
109+
110+ if os .path .exists (file_path ):
111+ os .remove (file_path )
112+
113+ def test_download_and_get_hash_auth_type_api_key (self ):
114+ """
115+ 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+
121+ """
122+ mock_binary_data = b"binary data for auth type 1"
123+ expected_hash = hashlib .sha256 (mock_binary_data ).hexdigest ()
124+ file_path = "test_file.txt"
125+ base_url = "https://test.com"
126+ api_key_parameter_name = "api_key"
127+ credentials = "key123"
128+
129+ modified_url = f"{ base_url } ?{ api_key_parameter_name } ={ credentials } "
130+
131+ mock_response = MagicMock ()
132+ mock_response .read .side_effect = [mock_binary_data , b"" ]
133+ mock_response .__enter__ .return_value = mock_response
134+
135+ with patch (
136+ "urllib3.PoolManager.request" , return_value = mock_response
137+ ) as mock_request :
138+ result_hash = download_and_get_hash (
139+ base_url ,
140+ file_path ,
141+ "sha256" ,
142+ 8192 ,
143+ 1 ,
144+ api_key_parameter_name ,
145+ credentials ,
146+ )
147+
148+ self .assertEqual (
149+ result_hash ,
150+ expected_hash ,
151+ msg = f"Hash mismatch: got { result_hash } ,"
152+ f" but expected { expected_hash } " ,
153+ )
154+
155+ mock_request .assert_called_with (
156+ "GET" ,
157+ modified_url ,
158+ preload_content = False ,
159+ headers = {"User-Agent" : expected_user_agent },
160+ )
161+
162+ if os .path .exists (file_path ):
163+ os .remove (file_path )
222164
223165 def test_download_and_get_hash_exception (self ):
224166 file_path = "test_file.txt"
0 commit comments