diff --git a/src/aind_metadata_service/response_handler.py b/src/aind_metadata_service/response_handler.py index 479da047..c518a7c0 100644 --- a/src/aind_metadata_service/response_handler.py +++ b/src/aind_metadata_service/response_handler.py @@ -81,6 +81,10 @@ def _map_data_response( else: status_code = StatusCodes.VALID_DATA.value message = "Valid Model." + if self.status_code == StatusCodes.MULTI_STATUS: + status_code = self.status_code.value + message = self.message + message + else: status_code = StatusCodes.MULTIPLE_RESPONSES.value message = "Multiple Items Found." diff --git a/tests/sharepoint/test_client.py b/tests/sharepoint/test_client.py index 67b4dd2f..1ca71dfe 100644 --- a/tests/sharepoint/test_client.py +++ b/tests/sharepoint/test_client.py @@ -8,6 +8,7 @@ from typing import List, Tuple from unittest.mock import MagicMock, Mock, patch +from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse from aind_metadata_service.response_handler import ModelResponse, StatusCodes @@ -367,12 +368,70 @@ def test_merge_valid_error_procedures( self.assertEqual( StatusCodes.MULTI_STATUS, merged_responses.status_code ) - self.assertEqual(200, json_response.status_code) + self.assertEqual(207, json_response.status_code) self.assertEqual( expected_subject_procedures, actual_subject_procedures ) mock_log.assert_called_once_with("BrokenPipeError()") + @patch("aind_metadata_service.sharepoint.client.ClientContext") + def test_merge_3_responses(self, mock_sharepoint_client: MagicMock): + """Tests that multi status is returned as expected.""" + inner_mock = MagicMock() + mock_sharepoint_client.return_value.with_credentials.return_value = ( + inner_mock + ) + mock_list_views = MagicMock() + inner_mock.web.lists.get_by_title.return_value.views = mock_list_views + mock_list_items = MagicMock() + mock_list_views.get_by_title.return_value.get_items.return_value = ( + mock_list_items + ) + list_item_2019_1 = self.list_items_2019[0][0] + mock_list_item2019 = MagicMock() + mock_list_item2019.to_json.return_value = list_item_2019_1 + mock_list_items.filter.side_effect = [ + [mock_list_item2019], + ] + client = SharePointClient( + nsb_site_url="some_url", + client_id="some_client_id", + client_secret="some_client_secret", + nsb_2019_list_title="some_list_title2019", + nsb_2023_list_title="some_list_title2023", + ) + + response1 = ModelResponse.internal_server_error_response() + response2 = ModelResponse.internal_server_error_response() + response3 = client.get_procedure_info( + subject_id="12345", list_title="some_list_title2019" + ) + merged_responses = client.merge_responses( + [response1, response2, response3] + ) + actual_json = merged_responses.map_to_json_response() + expected_content = jsonable_encoder( + json.loads(response3.aind_models[0].json()) + ) + expected_json = JSONResponse( + status_code=207, + content=( + { + "message": ( + "There was an error retrieving records from one or " + "more of the databases.Valid Model." + ), + "data": expected_content, + } + ), + ) + + self.assertEqual( + StatusCodes.MULTI_STATUS, merged_responses.status_code + ) + self.assertEqual(expected_json.status_code, actual_json.status_code) + self.assertEqual(expected_json.body, actual_json.body) + def test_merge_procedures_empty_input(self): """Tests that merging nothing returns internal server error."""