From 8aa1e27d8f42b6808342e563c5b54770d639c54e Mon Sep 17 00:00:00 2001 From: Jingsi Lu Date: Tue, 3 Dec 2024 15:27:56 -0500 Subject: [PATCH 1/5] flipped implementation of authentication type 1 and 2 --- functions-python/helpers/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/functions-python/helpers/utils.py b/functions-python/helpers/utils.py index 0bffa19a4..9a2eea72e 100644 --- a/functions-python/helpers/utils.py +++ b/functions-python/helpers/utils.py @@ -100,18 +100,18 @@ def download_and_get_hash( ctx.load_default_certs() ctx.options |= 0x4 # ssl.OP_LEGACY_SERVER_CONNECT - # authentication_type == 1 -> the credentials are passed in the header + # authentication_type == 1 -> the credentials are passed in the url headers = { "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/126.0.0.0 Mobile Safari/537.36" } if authentication_type == 1 and api_key_parameter_name and credentials: - headers[api_key_parameter_name] = credentials + url += f"?{api_key_parameter_name}={credentials}" - # authentication_type == 2 -> the credentials are passed in the url + # authentication_type == 2 -> the credentials are passed in the header if authentication_type == 2 and api_key_parameter_name and credentials: - url += f"?{api_key_parameter_name}={credentials}" + headers[api_key_parameter_name] = credentials with urllib3.PoolManager(ssl_context=ctx) as http: with http.request( From 569a793c863eb88667ca747356eddaf918f06133 Mon Sep 17 00:00:00 2001 From: Jingsi Lu Date: Tue, 7 Jan 2025 13:46:35 -0500 Subject: [PATCH 2/5] flipped auth type 1 and 2 --- functions-python/helpers/tests/test_helpers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/functions-python/helpers/tests/test_helpers.py b/functions-python/helpers/tests/test_helpers.py index 16826cca7..5028ed7f7 100644 --- a/functions-python/helpers/tests/test_helpers.py +++ b/functions-python/helpers/tests/test_helpers.py @@ -65,8 +65,8 @@ def test_download_and_get_hash(self): if os.path.exists(file_path): os.remove(file_path) - def test_download_and_get_hash_auth_type_1(self): - mock_binary_data = b"binary data for auth type 1" + def test_download_and_get_hash_auth_type_2(self): + mock_binary_data = b"binary data for auth type 2" expected_hash = hashlib.sha256(mock_binary_data).hexdigest() file_path = "test_file.txt" url = "https://test.com" @@ -104,8 +104,8 @@ def test_download_and_get_hash_auth_type_1(self): if os.path.exists(file_path): os.remove(file_path) - def test_download_and_get_hash_auth_type_2(self): - mock_binary_data = b"binary data for auth type 2" + def test_download_and_get_hash_auth_type_1(self): + mock_binary_data = b"binary data for auth type 1" expected_hash = hashlib.sha256(mock_binary_data).hexdigest() file_path = "test_file.txt" base_url = "https://test.com" From b0339e43b683116636f22a91d2d6c4c8472c06a8 Mon Sep 17 00:00:00 2001 From: Jingsi Lu Date: Tue, 7 Jan 2025 14:05:48 -0500 Subject: [PATCH 3/5] fixed broken tests --- .../helpers/tests/test_helpers.py | 234 ++++++++++++------ 1 file changed, 153 insertions(+), 81 deletions(-) diff --git a/functions-python/helpers/tests/test_helpers.py b/functions-python/helpers/tests/test_helpers.py index 5028ed7f7..665e5f383 100644 --- a/functions-python/helpers/tests/test_helpers.py +++ b/functions-python/helpers/tests/test_helpers.py @@ -65,88 +65,160 @@ def test_download_and_get_hash(self): if os.path.exists(file_path): os.remove(file_path) - def test_download_and_get_hash_auth_type_2(self): - mock_binary_data = b"binary data for auth type 2" - expected_hash = hashlib.sha256(mock_binary_data).hexdigest() - file_path = "test_file.txt" - url = "https://test.com" - api_key_parameter_name = "Authorization" - credentials = "Bearer token123" - - mock_response = MagicMock() - mock_response.read.side_effect = [mock_binary_data, b""] - mock_response.__enter__.return_value = mock_response - - with patch( - "urllib3.PoolManager.request", return_value=mock_response - ) as mock_request: - result_hash = download_and_get_hash( - url, file_path, "sha256", 8192, 1, api_key_parameter_name, credentials - ) - - self.assertEqual( - result_hash, - expected_hash, - msg=f"Hash mismatch: got {result_hash}," - f" but expected {expected_hash}", - ) - - mock_request.assert_called_with( - "GET", - url, - preload_content=False, - headers={ - "User-Agent": expected_user_agent, - api_key_parameter_name: credentials, - }, - ) - - if os.path.exists(file_path): - os.remove(file_path) - - def test_download_and_get_hash_auth_type_1(self): - mock_binary_data = b"binary data for auth type 1" - expected_hash = hashlib.sha256(mock_binary_data).hexdigest() - file_path = "test_file.txt" - base_url = "https://test.com" + # def test_download_and_get_hash_auth_type_1(self): + # mock_binary_data = b"binary data for auth type 1" + # expected_hash = hashlib.sha256(mock_binary_data).hexdigest() + # file_path = "test_file.txt" + # url = "https://test.com" + # api_key_parameter_name = "Authorization" + # credentials = "Bearer token123" + + # mock_response = MagicMock() + # mock_response.read.side_effect = [mock_binary_data, b""] + # mock_response.__enter__.return_value = mock_response + + # with patch( + # "urllib3.PoolManager.request", return_value=mock_response + # ) as mock_request: + # result_hash = download_and_get_hash( + # url, file_path, "sha256", 8192, 1, api_key_parameter_name, credentials + # ) + + # self.assertEqual( + # result_hash, + # expected_hash, + # msg=f"Hash mismatch: got {result_hash}," + # f" but expected {expected_hash}", + # ) + + # mock_request.assert_called_with( + # "GET", + # url, + # preload_content=False, + # headers={ + # "User-Agent": expected_user_agent, + # api_key_parameter_name: credentials, + # }, + # ) + + # if os.path.exists(file_path): + # os.remove(file_path) + + # def test_download_and_get_hash_auth_type_2(self): + # mock_binary_data = b"binary data for auth type 2" + # expected_hash = hashlib.sha256(mock_binary_data).hexdigest() + # file_path = "test_file.txt" + # base_url = "https://test.com" + # api_key_parameter_name = "api_key" + # credentials = "key123" + + # modified_url = f"{base_url}?{api_key_parameter_name}={credentials}" + + # mock_response = MagicMock() + # mock_response.read.side_effect = [mock_binary_data, b""] + # mock_response.__enter__.return_value = mock_response + + # with patch( + # "urllib3.PoolManager.request", return_value=mock_response + # ) as mock_request: + # result_hash = download_and_get_hash( + # base_url, + # file_path, + # "sha256", + # 8192, + # 2, + # api_key_parameter_name, + # credentials, + # ) + + # self.assertEqual( + # result_hash, + # expected_hash, + # msg=f"Hash mismatch: got {result_hash}," + # f" but expected {expected_hash}", + # ) + + # mock_request.assert_called_with( + # "GET", + # modified_url, + # preload_content=False, + # headers={"User-Agent": expected_user_agent}, + # ) + + # if os.path.exists(file_path): + # os.remove(file_path) + + def test_download_and_get_hash_auth_type_1(): + url = "http://example.com/data" api_key_parameter_name = "api_key" - credentials = "key123" - - modified_url = f"{base_url}?{api_key_parameter_name}={credentials}" - - mock_response = MagicMock() - mock_response.read.side_effect = [mock_binary_data, b""] - mock_response.__enter__.return_value = mock_response - - with patch( - "urllib3.PoolManager.request", return_value=mock_response - ) as mock_request: - result_hash = download_and_get_hash( - base_url, - file_path, - "sha256", - 8192, - 2, - api_key_parameter_name, - credentials, - ) - - self.assertEqual( - result_hash, - expected_hash, - msg=f"Hash mismatch: got {result_hash}," - f" but expected {expected_hash}", - ) - - mock_request.assert_called_with( - "GET", - modified_url, - preload_content=False, - headers={"User-Agent": expected_user_agent}, - ) - - if os.path.exists(file_path): - os.remove(file_path) + credentials = "test_api_key" + expected_url = f"{url}?{api_key_parameter_name}={credentials}" + + with patch("helpers.download.create_urllib3_context") as mock_create_context: + mock_context = mock_create_context.return_value + mock_context.load_default_certs.return_value = None + + with patch("helpers.download.urllib3.PoolManager") as mock_pool_manager: + mock_http = mock_pool_manager.return_value + mock_response = mock_http.request.return_value + mock_response.read.side_effect = [b"data_chunk_1", b"data_chunk_2", b""] + mock_response.__enter__.return_value = mock_response + + result = download_and_get_hash( + url, + file_path="test_file", + hash_algorithm="sha256", + authentication_type=1, + api_key_parameter_name=api_key_parameter_name, + credentials=credentials, + ) + + mock_http.request.assert_called_with( + "GET", + expected_url, + preload_content=False, + headers={ + "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/126.0.0.0 Mobile Safari/537.36" + }, + ) + assert result is not None + + def test_download_and_get_hash_auth_type_2(): + url = "http://example.com/data" + api_key_parameter_name = "Authorization" + credentials = "Bearer test_token" + expected_headers = { + "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/126.0.0.0 Mobile Safari/537.36", + api_key_parameter_name: credentials, + } + + with patch("helpers.download.create_urllib3_context") as mock_create_context: + mock_context = mock_create_context.return_value + mock_context.load_default_certs.return_value = None + + with patch("helpers.download.urllib3.PoolManager") as mock_pool_manager: + mock_http = mock_pool_manager.return_value + mock_response = mock_http.request.return_value + mock_response.read.side_effect = [b"data_chunk_1", b"data_chunk_2", b""] + mock_response.__enter__.return_value = mock_response + + result = download_and_get_hash( + url, + file_path="test_file", + hash_algorithm="sha256", + authentication_type=2, + api_key_parameter_name=api_key_parameter_name, + credentials=credentials, + ) + + mock_http.request.assert_called_with( + "GET", url, preload_content=False, headers=expected_headers + ) + assert result is not None def test_download_and_get_hash_exception(self): file_path = "test_file.txt" From f0dd0f4759e0032efff71a11c39660fe588ed8a1 Mon Sep 17 00:00:00 2001 From: Jingsi Lu Date: Tue, 7 Jan 2025 14:14:58 -0500 Subject: [PATCH 4/5] more fix --- functions-python/helpers/tests/test_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions-python/helpers/tests/test_helpers.py b/functions-python/helpers/tests/test_helpers.py index 665e5f383..ce2a56ed6 100644 --- a/functions-python/helpers/tests/test_helpers.py +++ b/functions-python/helpers/tests/test_helpers.py @@ -148,7 +148,7 @@ def test_download_and_get_hash(self): # if os.path.exists(file_path): # os.remove(file_path) - def test_download_and_get_hash_auth_type_1(): + def test_download_and_get_hash_auth_type_1(self): url = "http://example.com/data" api_key_parameter_name = "api_key" credentials = "test_api_key" @@ -185,7 +185,7 @@ def test_download_and_get_hash_auth_type_1(): ) assert result is not None - def test_download_and_get_hash_auth_type_2(): + def test_download_and_get_hash_auth_type_2(self): url = "http://example.com/data" api_key_parameter_name = "Authorization" credentials = "Bearer test_token" From 7bcbc3d26c2e7f000fe974a1be06bfc764b271c9 Mon Sep 17 00:00:00 2001 From: Jingsi Lu Date: Mon, 27 Jan 2025 10:19:01 -0500 Subject: [PATCH 5/5] fixed test_download_and_get_hash_auth_type tests --- .../helpers/tests/test_helpers.py | 248 +++++++----------- 1 file changed, 95 insertions(+), 153 deletions(-) diff --git a/functions-python/helpers/tests/test_helpers.py b/functions-python/helpers/tests/test_helpers.py index 04de32c78..a4a8e73d5 100644 --- a/functions-python/helpers/tests/test_helpers.py +++ b/functions-python/helpers/tests/test_helpers.py @@ -65,160 +65,102 @@ def test_download_and_get_hash(self): if os.path.exists(file_path): os.remove(file_path) - # def test_download_and_get_hash_auth_type_1(self): - # mock_binary_data = b"binary data for auth type 1" - # expected_hash = hashlib.sha256(mock_binary_data).hexdigest() - # file_path = "test_file.txt" - # url = "https://test.com" - # api_key_parameter_name = "Authorization" - # credentials = "Bearer token123" - - # mock_response = MagicMock() - # mock_response.read.side_effect = [mock_binary_data, b""] - # mock_response.__enter__.return_value = mock_response - - # with patch( - # "urllib3.PoolManager.request", return_value=mock_response - # ) as mock_request: - # result_hash = download_and_get_hash( - # url, file_path, "sha256", 8192, 1, api_key_parameter_name, credentials - # ) - - # self.assertEqual( - # result_hash, - # expected_hash, - # msg=f"Hash mismatch: got {result_hash}," - # f" but expected {expected_hash}", - # ) - - # mock_request.assert_called_with( - # "GET", - # url, - # preload_content=False, - # headers={ - # "User-Agent": expected_user_agent, - # api_key_parameter_name: credentials, - # }, - # ) - - # if os.path.exists(file_path): - # os.remove(file_path) - - # def test_download_and_get_hash_auth_type_2(self): - # mock_binary_data = b"binary data for auth type 2" - # expected_hash = hashlib.sha256(mock_binary_data).hexdigest() - # file_path = "test_file.txt" - # base_url = "https://test.com" - # api_key_parameter_name = "api_key" - # credentials = "key123" - - # modified_url = f"{base_url}?{api_key_parameter_name}={credentials}" - - # mock_response = MagicMock() - # mock_response.read.side_effect = [mock_binary_data, b""] - # mock_response.__enter__.return_value = mock_response - - # with patch( - # "urllib3.PoolManager.request", return_value=mock_response - # ) as mock_request: - # result_hash = download_and_get_hash( - # base_url, - # file_path, - # "sha256", - # 8192, - # 2, - # api_key_parameter_name, - # credentials, - # ) - - # self.assertEqual( - # result_hash, - # expected_hash, - # msg=f"Hash mismatch: got {result_hash}," - # f" but expected {expected_hash}", - # ) - - # mock_request.assert_called_with( - # "GET", - # modified_url, - # preload_content=False, - # headers={"User-Agent": expected_user_agent}, - # ) - - # if os.path.exists(file_path): - # os.remove(file_path) - - def test_download_and_get_hash_auth_type_1(self): - url = "http://example.com/data" - api_key_parameter_name = "api_key" - credentials = "test_api_key" - expected_url = f"{url}?{api_key_parameter_name}={credentials}" - - with patch("helpers.download.create_urllib3_context") as mock_create_context: - mock_context = mock_create_context.return_value - mock_context.load_default_certs.return_value = None - - with patch("helpers.download.urllib3.PoolManager") as mock_pool_manager: - mock_http = mock_pool_manager.return_value - mock_response = mock_http.request.return_value - mock_response.read.side_effect = [b"data_chunk_1", b"data_chunk_2", b""] - mock_response.__enter__.return_value = mock_response - - result = download_and_get_hash( - url, - file_path="test_file", - hash_algorithm="sha256", - authentication_type=1, - api_key_parameter_name=api_key_parameter_name, - credentials=credentials, - ) - - mock_http.request.assert_called_with( - "GET", - expected_url, - preload_content=False, - headers={ - "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) " - "AppleWebKit/537.36 (KHTML, like Gecko) " - "Chrome/126.0.0.0 Mobile Safari/537.36" - }, - ) - assert result is not None - - def test_download_and_get_hash_auth_type_2(self): - url = "http://example.com/data" + def test_download_and_get_hash_auth_type_header(self): + """ + Test the download_and_get_hash function for authentication type 2 (headers). + This test verifies that the download_and_get_hash function correctly handles authentication type 2, + where the credentials are passed in the headers. It mocks the necessary components and checks that + the request is made with the appropriate headers. + """ + mock_binary_data = b"binary data for auth type 2" + expected_hash = hashlib.sha256(mock_binary_data).hexdigest() + file_path = "test_file.txt" + url = "https://test.com" api_key_parameter_name = "Authorization" - credentials = "Bearer test_token" - expected_headers = { - "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) " - "AppleWebKit/537.36 (KHTML, like Gecko) " - "Chrome/126.0.0.0 Mobile Safari/537.36", - api_key_parameter_name: credentials, - } - - with patch("helpers.download.create_urllib3_context") as mock_create_context: - mock_context = mock_create_context.return_value - mock_context.load_default_certs.return_value = None - - with patch("helpers.download.urllib3.PoolManager") as mock_pool_manager: - mock_http = mock_pool_manager.return_value - mock_response = mock_http.request.return_value - mock_response.read.side_effect = [b"data_chunk_1", b"data_chunk_2", b""] - mock_response.__enter__.return_value = mock_response - - result = download_and_get_hash( - url, - file_path="test_file", - hash_algorithm="sha256", - authentication_type=2, - api_key_parameter_name=api_key_parameter_name, - credentials=credentials, - ) - - mock_http.request.assert_called_with( - "GET", url, preload_content=False, headers=expected_headers - ) - assert result is not None + credentials = "Bearer token123" + + mock_response = MagicMock() + mock_response.read.side_effect = [mock_binary_data, b""] + mock_response.__enter__.return_value = mock_response + + with patch( + "urllib3.PoolManager.request", return_value=mock_response + ) as mock_request: + result_hash = download_and_get_hash( + url, file_path, "sha256", 8192, 2, api_key_parameter_name, credentials + ) + + self.assertEqual( + result_hash, + expected_hash, + msg=f"Hash mismatch: got {result_hash}," + f" but expected {expected_hash}", + ) + + mock_request.assert_called_with( + "GET", + url, + preload_content=False, + headers={ + "User-Agent": expected_user_agent, + api_key_parameter_name: credentials, + }, + ) + + if os.path.exists(file_path): + os.remove(file_path) + + def test_download_and_get_hash_auth_type_api_key(self): + """ + Test the download_and_get_hash function for authentication type 1 (API key). + + This test verifies that the download_and_get_hash function correctly handles authentication type 1, + where the credentials are passed as a query parameter in the URL. It mocks the necessary components + and checks that the request is made with the appropriate URL containing the API key. + + """ + mock_binary_data = b"binary data for auth type 1" + expected_hash = hashlib.sha256(mock_binary_data).hexdigest() + file_path = "test_file.txt" + base_url = "https://test.com" + api_key_parameter_name = "api_key" + credentials = "key123" + + modified_url = f"{base_url}?{api_key_parameter_name}={credentials}" + + mock_response = MagicMock() + mock_response.read.side_effect = [mock_binary_data, b""] + mock_response.__enter__.return_value = mock_response + + with patch( + "urllib3.PoolManager.request", return_value=mock_response + ) as mock_request: + result_hash = download_and_get_hash( + base_url, + file_path, + "sha256", + 8192, + 1, + api_key_parameter_name, + credentials, + ) + + self.assertEqual( + result_hash, + expected_hash, + msg=f"Hash mismatch: got {result_hash}," + f" but expected {expected_hash}", + ) + + mock_request.assert_called_with( + "GET", + modified_url, + preload_content=False, + headers={"User-Agent": expected_user_agent}, + ) + + if os.path.exists(file_path): + os.remove(file_path) def test_download_and_get_hash_exception(self): file_path = "test_file.txt"