"""Tests for the PDP authorization module.""" from unittest.mock import MagicMock, patch from store.utils import authorization @patch("store.utils.authorization.ForwardKwargsGetter") @patch("store.utils.authorization.config") def test_pdp_authorize_resource( config_mock: MagicMock, resource_getters_mock: MagicMock ) -> None: """Test the pdp_authorize_resource function.""" config_mock.pdp_authorization_backend.is_authorized.return_value = True result = authorization.pdp_authorize_resource(1, "test_resource") assert result is True config_mock.pdp_authorization_backend.is_authorized.assert_called_once_with( action="view", resource_id=1, resource_type="test_resource", resource_getter=resource_getters_mock.return_value, ) @patch("store.utils.authorization.g", spec=["log"]) @patch("store.utils.authorization.ForwardKwargsGetter") @patch("store.utils.authorization.config") def test_pdp_authorize_resource_unauthorized( config_mock: MagicMock, _resource_getters_mock: MagicMock, g_mock: MagicMock, ) -> None: """Test the pdp_authorize_resource function.""" g_mock.request_context = MagicMock(jwt_identity_id="identity-id") config_mock.pdp_authorization_backend.is_authorized.return_value = False result = authorization.pdp_authorize_resource(1, "test_resource") assert result is False g_mock.log.warn.assert_called_once_with( "User is forbidden", resources={ "identity_id": "identity-id", "resource_id": 1, "resource_type": "test_resource", "auth_response": False, }, )