diff --git a/app/services/dataset_manager/dataset_download.py b/app/services/dataset_manager/dataset_download.py index 0618803b..de36cb1d 100644 --- a/app/services/dataset_manager/dataset_download.py +++ b/app/services/dataset_manager/dataset_download.py @@ -43,15 +43,14 @@ def pre_dataset_version_download(self): 'Session-ID': self.session_id, } payload = {'version': self.version} - try: - response = requests.get(url, headers=headers, params=payload) - res = response.json() - code = res.get('code') - if code == 404: - SrvErrorHandler.customized_handle(ECustomizedError.VERSION_NOT_EXIST, True, self.version) - else: - return res - except Exception: + response = requests.get(url, headers=headers, params=payload) + res = response.json() + code = response.status_code + if code == 200: + return res + elif code == 404: + SrvErrorHandler.customized_handle(ECustomizedError.VERSION_NOT_EXIST, True, self.version) + else: SrvErrorHandler.default_handle(response.content, True) @require_valid_token() diff --git a/pyproject.toml b/pyproject.toml index 99a24127..4a942f44 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "app" -version = "2.2.3" +version = "2.2.4" description = "This service is designed to support pilot platform" authors = ["Indoc Research"] diff --git a/tests/app/commands/test_dataset.py b/tests/app/commands/test_dataset.py new file mode 100644 index 00000000..bf043b9c --- /dev/null +++ b/tests/app/commands/test_dataset.py @@ -0,0 +1,51 @@ +# Copyright (C) 2022-2023 Indoc Research +# +# Contact Indoc Research for any questions regarding the use of this source code. + +from app.commands.dataset import dataset_download +from app.configs.app_config import AppConfig + + +def test_download_not_exited_dataset_version(requests_mock, mocker, cli_runner, capsys): + mocker.patch('app.services.user_authentication.token_manager.SrvTokenManager.check_valid', return_value=0) + requests_mock.get( + 'http://bff_cli' + '/v1/dataset/testdataset', + json={ + 'code': 200, + 'error_msg': '', + 'result': { + 'general_info': { + 'id': 'fake-id', + 'source': '', + 'authors': ['test-admin', 'test-user'], + 'code': 'testdataset', + 'type': 'GENERAL', + 'modality': [], + 'collection_method': [], + 'license': '', + 'tags': ['cdsa'], + 'description': 'Description example.', + 'size': 25, + 'total_files': 1, + 'title': 'test dataset', + 'creator': 'test-admin', + 'project_id': 'project-id', + 'created_at': '2022-02-03T19:49:35', + 'updated_at': '2022-03-18T18:08:33', + }, + 'version_detail': [], + 'version_no': 0, + }, + }, + ) + + requests_mock.get( + AppConfig.Connections.url_dataset + '/fake-id/download/pre', + json={'error': 'version does not exist'}, + status_code=404, + ) + + result = cli_runner.invoke(dataset_download, ['testdataset', '.', '-v', '1.0']) + outputs = result.output.split('\n') + assert outputs[0] == 'Current dataset version: 1.0' + assert outputs[1] == 'Version not available: 1.0' diff --git a/tests/conftest.py b/tests/conftest.py index beeb3785..aaee5cc6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,7 @@ def reset_singletons(): def mock_settings(monkeypatch): monkeypatch.setattr(AppConfig.Connections, 'url_authn', 'http://service_auth') monkeypatch.setattr(AppConfig.Connections, 'url_bff', 'http://bff_cli') + monkeypatch.setattr(AppConfig.Connections, 'url_dataset', 'http://url_dataset') monkeypatch.setattr(AppConfig.Connections, 'url_upload_greenroom', 'http://upload_gr') monkeypatch.setattr(AppConfig.Connections, 'url_upload_core', 'http://upload_core') monkeypatch.setattr(UserConfig, 'username', 'test-user')