"""Tests for API factory functions.""" from unittest.mock import MagicMock, patch from owsclient import OwsClient from owsrequest.ows_client import correlation_id_getter, request_context_getter from project_manager import config from project_manager.api import ( authorization_backend, ows_client, setup_authorization_backend, setup_ows_client, ) @patch('project_manager.api.OwsClient') def test_setup_ows_client_calls_constructor_with_config(mock_ows_client_class): """Verify setup_ows_client calls OwsClient constructor with config values.""" setup_ows_client() mock_ows_client_class.assert_called_once_with( environment=config.ENVIRONMENT, service_name=config.SERVICE_NAME, correlation_id_getter=correlation_id_getter, request_context_getter=request_context_getter, ) def test_setup_ows_client_returns_ows_client_instance(): """Verify setup_ows_client returns an OwsClient instance.""" result = setup_ows_client() assert isinstance(result, OwsClient) @patch('project_manager.api.PdpAuthorizationBackend') @patch('project_manager.api.OwsPdpClient') def test_setup_authorization_backend_wraps_ows_client_in_pdp_client( mock_pdp_client, mock_pdp_backend): """Verify ows_client is wrapped in OwsPdpClient and PdpAuthorizationBackend.""" mock_ows_client = MagicMock() result = setup_authorization_backend(mock_ows_client) mock_pdp_client.assert_called_once_with(mock_ows_client) mock_pdp_backend.assert_called_once_with(mock_pdp_client.return_value) assert result is mock_pdp_backend.return_value def test_module_level_globals_satisfy_expected_interfaces(): """Verify module-level globals satisfy the expected interfaces.""" assert isinstance(ows_client, OwsClient) for method in ( 'is_authorized', 'is_authorized_many', 'is_authorized_many_resources_and_actions', 'get_authorized_tenants', ): assert callable(getattr(authorization_backend, method, None))