"""Tests for authorization utilities.""" from unittest.mock import call import pytest from flask import g from product_review.util.authorization import pdp_authorize_resource_without_attributes @pytest.mark.parametrize( ( "test_description", "expected_result", "expected_log_calls" ), [ ( "Authorization succeeds", True, [] ), ( "Authorization fails", False, [call('Unauthorized access to resource', resources={ 'identity_id': "test-identity-id", 'resource_id': 123, 'resource_type': "test_resource", 'auth_response': False })], ) ]) def test_pdp_authorize_resource_without_attributes( mocker, test_description, expected_result, expected_log_calls ): """Test the pdp_authorize_resource_without_attributes function.""" # Test data resource_id = 123 resource_type = "test_resource" action = "test" # Setup mocks # Mock pdp_authorization_backend mock_pdp_backend = mocker.patch( "product_review.util.authorization.pdp_authorization_backend" ) mock_pdp_backend.is_authorized.return_value = expected_result # Mock ForwardKwargsGetter mock_getter = mocker.patch( "product_review.util.authorization.ForwardKwargsGetter" ) mock_getter_instance = mocker.MagicMock() mock_getter.return_value = mock_getter_instance # Mock request context mock_request_context = mocker.MagicMock() mock_request_context.jwt_identity_id = "test-identity-id" mocker.patch.object(g, "request_context", mock_request_context, create=True) # Mock logger mock_logger = mocker.MagicMock() mocker.patch.object(g, "log", mock_logger, create=True) # Call the function result = pdp_authorize_resource_without_attributes( resource_id=resource_id, resource_type=resource_type, action=action ) # Assertions assert result == expected_result # Check mock backend was called with correct parameters mock_pdp_backend.is_authorized.assert_called_once_with( action=action, resource_id=resource_id, resource_type=resource_type, resource_getter=mock_getter.return_value ) # Check warning logs were made as expected assert mock_logger.warn.mock_calls == expected_log_calls