Skip to content

Commit 5496dfa

Browse files
committed
Fixed #503 - Better handling of invalid API URL
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 0efe90b commit 5496dfa

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/attributecode/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ def request_license_data(api_url, api_key, license_key):
6464
msg = u"Invalid 'license': %s" % license_key
6565
errors.append(Error(ERROR, msg))
6666
except HTTPError as http_e:
67-
# some auth problem
68-
#if http_e.code == 403:
6967
msg = (u"Authorization denied. Invalid '--api_key'. "
7068
u"License generation is skipped.")
7169
errors.append(Error(ERROR, msg))
7270
except Exception as e:
73-
errors.append(Error(ERROR, str(e)))
71+
# Already checked the authorization and accessible of the URL.
72+
# The only exception left is URL is accessible, but it's not a valid API URL
73+
msg = (u"Invalid '--api_url'. "
74+
u"License generation is skipped.")
75+
errors.append(Error(ERROR, msg))
7476

7577
finally:
7678
if license_data.get('count') == 1:

src/attributecode/model.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,12 @@ def pre_process_and_fetch_license_dict(abouts, api_url=None, api_key=None, scanc
17041704
captured_license.append(lic_key)
17051705
if api_key:
17061706
license_data, errs = api.get_license_details_from_api(url, api_key, lic_key)
1707+
# Catch incorrect API URL
1708+
if errs:
1709+
_, msg = errs[0]
1710+
if msg == "Invalid '--api_url'. License generation is skipped.":
1711+
errors.extend(errs)
1712+
return key_text_dict, errors
17071713
for severity, message in errs:
17081714
msg = (about.about_file_path + ": " + message)
17091715
errors.append(Error(severity, msg))

tests/test_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,13 @@ def test_api_request_license_data_without_result(self, mock_data):
7171
api_url='http://fake.url/', api_key='api_key', license_key='apache-2.0')
7272
expected = ({}, [Error(ERROR, "Invalid 'license': apache-2.0")])
7373
assert expected == license_data
74+
75+
@mock.patch.object(api, 'urlopen')
76+
def test_api_request_license_data_with_incorrect_url(self, mock_data):
77+
# Some URL that is accessible but not a correct API URL
78+
response_content = b'<html></html>'
79+
mock_data.return_value = FakeResponse(response_content)
80+
license_data = api.request_license_data(
81+
api_url='http://fake.url/', api_key='api_key', license_key='apache-2.0')
82+
expected = ({}, [Error(ERROR, "Invalid '--api_url'. License generation is skipped.")])
83+
assert expected == license_data

0 commit comments

Comments
 (0)