Skip to content

Commit 4a0ba9f

Browse files
authored
feat: fix download authentication feeds (#867)
1 parent 5c62297 commit 4a0ba9f

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

functions-python/helpers/tests/test_helpers.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ 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"
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"
7076
expected_hash = hashlib.sha256(mock_binary_data).hexdigest()
7177
file_path = "test_file.txt"
7278
url = "https://test.com"
@@ -81,7 +87,7 @@ def test_download_and_get_hash_auth_type_1(self):
8187
"urllib3.PoolManager.request", return_value=mock_response
8288
) as mock_request:
8389
result_hash = download_and_get_hash(
84-
url, file_path, "sha256", 8192, 1, api_key_parameter_name, credentials
90+
url, file_path, "sha256", 8192, 2, api_key_parameter_name, credentials
8591
)
8692

8793
self.assertEqual(
@@ -104,8 +110,16 @@ def test_download_and_get_hash_auth_type_1(self):
104110
if os.path.exists(file_path):
105111
os.remove(file_path)
106112

107-
def test_download_and_get_hash_auth_type_2(self):
108-
mock_binary_data = b"binary data for auth type 2"
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"
109123
expected_hash = hashlib.sha256(mock_binary_data).hexdigest()
110124
file_path = "test_file.txt"
111125
base_url = "https://test.com"
@@ -126,7 +140,7 @@ def test_download_and_get_hash_auth_type_2(self):
126140
file_path,
127141
"sha256",
128142
8192,
129-
2,
143+
1,
130144
api_key_parameter_name,
131145
credentials,
132146
)

functions-python/helpers/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ def download_and_get_hash(
100100
ctx.load_default_certs()
101101
ctx.options |= 0x4 # ssl.OP_LEGACY_SERVER_CONNECT
102102

103-
# authentication_type == 1 -> the credentials are passed in the header
103+
# authentication_type == 1 -> the credentials are passed in the url
104104
headers = {
105105
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) "
106106
"AppleWebKit/537.36 (KHTML, like Gecko) "
107107
"Chrome/126.0.0.0 Mobile Safari/537.36"
108108
}
109109
if authentication_type == 1 and api_key_parameter_name and credentials:
110-
headers[api_key_parameter_name] = credentials
110+
url += f"?{api_key_parameter_name}={credentials}"
111111

112-
# authentication_type == 2 -> the credentials are passed in the url
112+
# authentication_type == 2 -> the credentials are passed in the header
113113
if authentication_type == 2 and api_key_parameter_name and credentials:
114-
url += f"?{api_key_parameter_name}={credentials}"
114+
headers[api_key_parameter_name] = credentials
115115

116116
with urllib3.PoolManager(ssl_context=ctx) as http:
117117
with http.request(

0 commit comments

Comments
 (0)