Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def download(self, url, auth, verify_certificate):
if response_valid:
file_name = self._get_filename(response)

# if fails on public and no auth - no point carry on, user need to fix his URL or add credentials
if not response_valid and auth is None:
raise Exception('Please make sure the URL is valid, and the credentials are correct and necessary.')

# repo is private and token provided
if not response_valid and auth.token is not None:
self.logger.info("Token provided. Starting download script with Token...")
Expand Down
4 changes: 4 additions & 0 deletions package/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def mocked_requests_get(*args, **kwargs):
"private_token_auth_pattern": 'https://gitlab.mock.com/api/v4/SomeUser/SomePrivateTokenRepo/master/bashScript.sh',
"private_cred": 'https://raw.repocontentservice.com/SomeUser/SomePrivateCredRepo/master/bashScript.sh',
"private_cred_gitlab_struct": 'https://gitlab.mock.com/api/v4/SomeUser/SomePrivateTokenRepo/master/bashScript%2Esh/raw?ref=master',
"bad_url": 'https://badurl.mock.com/SomePublicRepo/master/bashScript.sh',
"content": 'SomeBashScriptContent'
}

Expand All @@ -31,6 +32,9 @@ def iter_content(self, chunk):
yield bytes(self.json_data, 'utf-8')
# return self.json_data

if args[0] == repo_dict['bad_url']:
response = MockResponse("error", 404, {"Content-Type": "text/plain"}, "error content")

if args[0] == repo_dict['public']:
response = MockResponse(repo_dict['content'], 200, {"Content-Type": "text/plain"}, repo_dict['public'])
return response
Expand Down
20 changes: 20 additions & 0 deletions package/tests/test_script_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ def test_download_as_private_with_credentials_and_failed_token(self, mocked_requ
# assert name and content
self.assertEqual(script_file.name, "bashScript.sh")
self.assertEqual(script_file.text, "SomeBashScriptContent")

@mock.patch('cloudshell.cm.customscript.domain.script_downloader.requests.get', side_effect=mocked_requests_get)
def test_download_fails_public_with_no_credentials_throws_exception(self, mocked_requests_get):
# private - url, with token that fails and user\password. note - this is will not work on GitHub repo, they require token
private_repo_url = 'https://badurl.mock.com/SomePublicRepo/master/bashScript.sh'
self.auth = None

# set downloaded and downaload
self.logger.info = print_logs
script_downloader = ScriptDownloader(self.logger, self.cancel_sampler)
# script_file = script_downloader.download(private_repo_url, self.auth, True)

with self.assertRaises(Exception) as context:
script_downloader.download(private_repo_url, self.auth, True)

self.assertIn('Please make sure the URL is valid, and the credentials are correct and necessary.', str(context.exception))

# assert name and content
#self.assertEqual(script_file.name, "bashScript.sh")
#self.assertEqual(script_file.text, "SomeBashScriptContent")