From 3bfa5f4c94a252b5e49f18b6a21e861a9845152b Mon Sep 17 00:00:00 2001 From: AdamSharon Date: Mon, 3 May 2021 08:19:58 +0300 Subject: [PATCH] excpetion on auth=none and public download fails --- .../customscript/domain/script_downloader.py | 4 ++++ package/tests/helpers.py | 4 ++++ package/tests/test_script_downloader.py | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/package/cloudshell/cm/customscript/domain/script_downloader.py b/package/cloudshell/cm/customscript/domain/script_downloader.py index f90989e..c05a851 100644 --- a/package/cloudshell/cm/customscript/domain/script_downloader.py +++ b/package/cloudshell/cm/customscript/domain/script_downloader.py @@ -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...") diff --git a/package/tests/helpers.py b/package/tests/helpers.py index acc6d85..2073e65 100644 --- a/package/tests/helpers.py +++ b/package/tests/helpers.py @@ -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' } @@ -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 diff --git a/package/tests/test_script_downloader.py b/package/tests/test_script_downloader.py index c330133..ba98baf 100644 --- a/package/tests/test_script_downloader.py +++ b/package/tests/test_script_downloader.py @@ -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") \ No newline at end of file