From e521655fcbd7e0b30ebd8bfe8488b66f915f88f8 Mon Sep 17 00:00:00 2001 From: zhiren Date: Thu, 6 Jul 2023 15:00:41 -0400 Subject: [PATCH 1/2] add the error handler to search_item function --- app/utils/aggregated.py | 2 ++ tests/app/utils/test_aggregated.py | 32 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/app/utils/aggregated.py b/app/utils/aggregated.py index 281eee63..20be73f4 100644 --- a/app/utils/aggregated.py +++ b/app/utils/aggregated.py @@ -44,6 +44,8 @@ def search_item(project_code, zone, folder_relative_path, item_type, container_t res = requests.get(url, params=params, headers=headers) if res.status_code == 403: SrvErrorHandler.customized_handle(ECustomizedError.PERMISSION_DENIED, project_code) + elif res.status_code != 200: + SrvErrorHandler.default_handle(res.content, True) return res.json() diff --git a/tests/app/utils/test_aggregated.py b/tests/app/utils/test_aggregated.py index a6c48d5e..1e5e5e0a 100644 --- a/tests/app/utils/test_aggregated.py +++ b/tests/app/utils/test_aggregated.py @@ -2,6 +2,8 @@ # # Contact Indoc Research for any questions regarding the use of this source code. +import pytest + from app.utils.aggregated import search_item test_project_code = 'testproject' @@ -55,3 +57,33 @@ def test_search_file_should_return_200(requests_mock, mocker): } res = search_item(test_project_code, 'zone', 'folder_relative_path', 'file', 'project') assert res['result'] == expected_result + + +def test_search_file_error_handling_with_403(requests_mock, mocker, capsys): + mocker.patch('app.services.user_authentication.token_manager.SrvTokenManager.check_valid', return_value=0) + requests_mock.get( + f'http://bff_cli/v1/project/{test_project_code}/search', + json={}, + status_code=403, + ) + with pytest.raises(SystemExit): + search_item(test_project_code, 'zone', 'folder_relative_path', 'file', 'project') + out, _ = capsys.readouterr() + assert ( + out.rstrip('\n') + == 'Permission denied. Please verify your role in the Project has permission to perform this action.' + ) + + +def test_search_file_error_handling_with_401(requests_mock, mocker, capsys): + mocker.patch('app.services.user_authentication.token_manager.SrvTokenManager.check_valid', return_value=0) + requests_mock.get( + f'http://bff_cli/v1/project/{test_project_code}/search', + text='Authentication failed.', + status_code=401, + ) + with pytest.raises(SystemExit): + search_item(test_project_code, 'zone', 'folder_relative_path', 'file', 'project') + out, _ = capsys.readouterr() + out = out.rstrip('\n').replace('b\'', '').replace('\'', '') + assert out == 'Authentication failed.' From d0974a8bd3824c998375b517a1ca242b6bdda74f Mon Sep 17 00:00:00 2001 From: zhiren Date: Thu, 13 Jul 2023 09:08:47 -0400 Subject: [PATCH 2/2] update the error handling by using response.text --- app/utils/aggregated.py | 2 +- tests/app/utils/test_aggregated.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/utils/aggregated.py b/app/utils/aggregated.py index 20be73f4..db739a7a 100644 --- a/app/utils/aggregated.py +++ b/app/utils/aggregated.py @@ -45,7 +45,7 @@ def search_item(project_code, zone, folder_relative_path, item_type, container_t if res.status_code == 403: SrvErrorHandler.customized_handle(ECustomizedError.PERMISSION_DENIED, project_code) elif res.status_code != 200: - SrvErrorHandler.default_handle(res.content, True) + SrvErrorHandler.default_handle(res.text, True) return res.json() diff --git a/tests/app/utils/test_aggregated.py b/tests/app/utils/test_aggregated.py index 1e5e5e0a..ba736970 100644 --- a/tests/app/utils/test_aggregated.py +++ b/tests/app/utils/test_aggregated.py @@ -70,7 +70,7 @@ def test_search_file_error_handling_with_403(requests_mock, mocker, capsys): search_item(test_project_code, 'zone', 'folder_relative_path', 'file', 'project') out, _ = capsys.readouterr() assert ( - out.rstrip('\n') + out.rstrip() == 'Permission denied. Please verify your role in the Project has permission to perform this action.' ) @@ -85,5 +85,5 @@ def test_search_file_error_handling_with_401(requests_mock, mocker, capsys): with pytest.raises(SystemExit): search_item(test_project_code, 'zone', 'folder_relative_path', 'file', 'project') out, _ = capsys.readouterr() - out = out.rstrip('\n').replace('b\'', '').replace('\'', '') + out = out.rstrip() assert out == 'Authentication failed.'