"""Tests for authorizing many accounts.""" from unittest.mock import ANY, MagicMock, patch import pytest from python_pdp_sdk.resource_getters.base import ForwardKwargsGetter from abacus_schedule.utils.authorization import pdp_authorize_resource @pytest.mark.parametrize( 'auth_response,expected_result', [ pytest.param(True, True, id='authorized, expect True'), pytest.param(False, False, id='unauthorized, expect False'), ], ) @patch('abacus_schedule.utils.authorization.authorization_backend') @patch('abacus_schedule.utils.authorization.g') def test_pdp_authorize_resource( mock_g: MagicMock, mock_authorization_backend: MagicMock, auth_response: bool, expected_result: bool, ) -> None: """Test pdp_authorize_resource.""" resource_id = 1234 resource_type = 'schedule' mock_authorization_backend.is_authorized.return_value = auth_response result = pdp_authorize_resource( resource_id=resource_id, resource_type=resource_type ) assert result == expected_result mock_authorization_backend.is_authorized.assert_called_once_with( action='view', resource_id=resource_id, resource_type=resource_type, resource_getter=ANY, ) call_args = mock_authorization_backend.is_authorized.call_args assert call_args is not None kwargs = call_args[1] assert isinstance(kwargs['resource_getter'], ForwardKwargsGetter) if not result: mock_g.log.warn.assert_called_once_with( 'Unauthorized to access resource', resources={ 'identity_id': mock_g.request_context.jwt_identity_id, 'resource_id': resource_id, 'resource_type': resource_type, 'auth_response': auth_response, }, ) else: mock_g.log.warn.assert_not_called()