"""Tests for authorization utilities.""" from unittest.mock import call, MagicMock, patch import pytest from flask import g from blacklist_manager import api from blacklist_manager.utils.authorization import pdp_authorize_resource_without_attributes @pytest.mark.parametrize( ("is_authorized", "expected_result", "expected_warn_calls"), [ pytest.param(True, True, [], id="authorization succeeds"), pytest.param( False, False, [call( 'Unauthorized access to resource', resources={ 'identity_id': 'test-identity-id', 'resource_id': 1, 'resource_type': 'example_type', 'auth_response': False, } )], id="authorization fails", ), ] ) @patch('blacklist_manager.utils.authorization.ForwardKwargsGetter') @patch('blacklist_manager.utils.authorization.pdp_authorization_backend') def test_pdp_authorize_resource_without_attributes( mock_pdp_backend, mock_getter, is_authorized, expected_result, expected_warn_calls, ): """Test pdp_authorize_resource_without_attributes for success and failure.""" mock_pdp_backend.is_authorized.return_value = is_authorized mock_getter_instance = MagicMock() mock_getter.return_value = mock_getter_instance mock_request_context = MagicMock() mock_request_context.jwt_identity_id = 'test-identity-id' mock_log = MagicMock() with api.app.test_request_context(): with patch.object(g, 'request_context', mock_request_context, create=True), \ patch.object(g, 'log', mock_log, create=True): result = pdp_authorize_resource_without_attributes( resource_id=1, resource_type='example_type', action='xyz', ) assert result == expected_result mock_pdp_backend.is_authorized.assert_called_once_with( action='xyz', resource_id=1, resource_type='example_type', resource_getter=mock_getter_instance, ) assert mock_log.warn.mock_calls == expected_warn_calls