"""Tests for Handlers.""" import json from unittest.mock import MagicMock, patch import pytest from flask.testing import FlaskClient from owsresponse.response import error from store import handlers from store.constants import authorization @patch("store.handlers.g", spec=["log"]) def test_exception_handler(mock_g: MagicMock) -> None: """Verify exception_Handler returns 500 status code and json payload.""" message = ( "The server encountered an internal error " "and was unable to complete your request." ) mock_error = MagicMock() server_response = handlers.exception_handler(mock_error) mock_g.log.exception.assert_called_with(mock_error) # assert status code is 500 assert server_response.status_code == 500 # assert json payload response_message = json.loads(server_response.data.decode()) assert response_message["message"] == message assert response_message["code"] == error.ERROR_CODE_INTERNAL_ERROR @pytest.mark.parametrize( [ "standalone_check_result", "pdp_check_result", "is_identity_header", "validate_profile_access_result", "is_jwt_identity_authorized_result", "status_code", ], [ pytest.param(True, None, True, True, False, 200, id="Standalone check pass"), pytest.param(False, False, True, True, False, 403, id="PDP check fail"), pytest.param(False, True, False, True, False, 400, id="Identity check fail"), pytest.param(False, True, True, True, False, 200, id="Profile check pass"), pytest.param(False, True, True, False, False, 401, id="Profile check fail"), pytest.param(False, True, False, False, True, 200, id="JWT check pass"), pytest.param(False, True, False, False, False, 400, id="JWT check fail"), ], ) @patch("store.handlers.g", spec=["request_context"]) @patch("store.handlers.store") @patch("store.handlers.validate_profile_access") @patch("store.handlers.authorization") @patch("store.handlers.flask_request") def test_get_stores_authorization( flask_request_mock: MagicMock, authorization_mock: MagicMock, validate_profile_access_mock: MagicMock, store_logic_mock: MagicMock, g_mock: MagicMock, standalone_check_result: bool, pdp_check_result: bool | None, is_identity_header: bool, validate_profile_access_result: bool, is_jwt_identity_authorized_result: bool, status_code: int, client: FlaskClient, ) -> None: """Test authorization for the GET /stores endpoint.""" headers = {"Orchard-Identity-ID": "test_identity_id"} if is_identity_header else {} if is_jwt_identity_authorized_result: g_mock.request_context.jwt_identity_id = ( authorization.OWS_DELIVERY_METADATA_IDENTITY_UUID ) else: g_mock.request_context.jwt_identity_id = None flask_request_mock.verify_rules_access_standalone.return_value = ( standalone_check_result ) authorization_mock.pdp_authorize_resource.return_value = pdp_check_result validate_profile_access_mock.return_value = validate_profile_access_result authorization_mock.is_jwt_identity_authorized.return_value = ( is_jwt_identity_authorized_result ) store_logic_mock.get_stores.return_value = [] res = client.get("/stores", headers=headers) assert res.status_code == status_code flask_request_mock.verify_rules_access_standalone.assert_called_once() if not standalone_check_result: authorization_mock.pdp_authorize_resource.assert_called_once_with( resource_id=0, resource_type="delivery_store", ) @pytest.mark.parametrize( ["standalone_check_result", "pdp_check_result", "status_code"], [ pytest.param(True, None, 200, id="Standalone check pass"), pytest.param(False, False, 403, id="PDP check fail"), pytest.param(False, True, 200, id="PDP check pass"), ], ) @patch("store.handlers.store") @patch("store.handlers.validate_profile_access") @patch("store.handlers.authorization") @patch("store.handlers.flask_request") def test_stores_dataloader_authorization( flask_request_mock: MagicMock, authorization_mock: MagicMock, _validate_profile_access_mock: MagicMock, store_logic_mock: MagicMock, standalone_check_result: bool, pdp_check_result: bool | None, status_code: int, client: FlaskClient, ) -> None: """Test authorization for the POST /stores/dataloader endpoint.""" headers = {"Orchard-Identity-ID": "test_identity_id"} flask_request_mock.verify_rules_access_standalone.return_value = ( standalone_check_result ) authorization_mock.pdp_authorize_resource.return_value = pdp_check_result store_logic_mock.get_stores_by_ids.return_value = [] res = client.post("/stores/dataloader", headers=headers, json=[1, 286]) assert res.status_code == status_code flask_request_mock.verify_rules_access_standalone.assert_called_once() if not standalone_check_result: authorization_mock.pdp_authorize_resource.assert_called_once_with( resource_id=0, resource_type="delivery_store", )