"""Test pytest plugin mock functionality.""" from unittest.mock import MagicMock import httpx import pytest from owsclient import ImpersonationM2MTokenManager, ImpersonationOwsClient from owsclient.base import AsyncOwsClient, OwsClient from owsclient.m2m.base import AsyncM2MTokenManager, M2MTokenManager from owsclient.test.mock import OwsClientMock # Mimic the `requests.py` pattern used in Python lambdas # to instantiate `ows_client` as a global variable. # # Ex: # # secrets_manager = LambdaSecretsManager( # environment=ENVIRONMENT, # service_name=APPLICATION_NAME, # ) # m2m_token_manager = M2MTokenManager( # secrets_manager=secrets_manager, # environment=ENVIRONMENT, # service_name=APPLICATION_NAME, # ) # ows_client = OwsClient( # environment=ENVIRONMENT, # service_name=APPLICATION_NAME, # m2m_token_manager=m2m_token_manager # ) global_m2m_token_manager = M2MTokenManager( secrets_manager=MagicMock(), environment="test", service_name="test-app", ) # The `ows_client_mock` fixture will find this global `OwsClient` instance # and patch the `m2m_token_manager` to use a mock. global_ows_client_to_be_patched = OwsClient( environment="test", service_name="test-app", m2m_token_manager=global_m2m_token_manager, ) def test_ows_client_mock_patches_global_token_manager( ows_client_mock: OwsClientMock, ) -> None: """Test that ows_client_mock patches global OwsClient instances.""" # The `ows_client_mock` fixture should have patched the token manager assert ( global_ows_client_to_be_patched.m2m_token_manager is ows_client_mock.mock_m2m_token_manager ) # Verify original `global_m2m_token_manager` has not been patched assert global_m2m_token_manager is not ows_client_mock.mock_m2m_token_manager # Mock a route response_json = {"status": "ok"} ows_client_mock.get("ows-permissions", path="/test/").mock( return_value=httpx.Response(status_code=200, json=response_json) ) # Make a request using the global client response = global_ows_client_to_be_patched.get("ows-permissions", path="/test/") # Verify the response assert response.status_code == 200 assert response.json() == response_json # Verify the mock token manager was called ows_client_mock.mock_m2m_token_manager.get_token_string.assert_called_once() # type: ignore[attr-defined] @pytest.mark.ows_client_mock_disable_m2m_patch def test_ows_client_mock_disables_patching_with_marker( ows_client_mock: OwsClientMock, ) -> None: """Test that the marker disables global OwsClient patching.""" # The `ows_client_mock` fixture should NOT have patched the token manager assert global_ows_client_to_be_patched.m2m_token_manager is global_m2m_token_manager # Verify it's not using the mock token manager assert ( global_ows_client_to_be_patched.m2m_token_manager is not ows_client_mock.mock_m2m_token_manager ) # Create a global async client similar to the sync version global_async_m2m_token_manager = AsyncM2MTokenManager( secrets_manager=MagicMock(), environment="test", service_name="test-app", ) global_async_ows_client = AsyncOwsClient( environment="test", service_name="test-app", m2m_token_manager=global_async_m2m_token_manager, ) @pytest.mark.anyio async def test_async_ows_client_mock_patches_global_token_manager( ows_client_mock: OwsClientMock, ) -> None: """Test that ows_client_mock patches global AsyncOwsClient instances.""" # The `ows_client_mock` fixture should have patched the token manager assert ( global_async_ows_client.m2m_token_manager is ows_client_mock.mock_async_m2m_token_manager ) # Verify original manager has not been patched assert ( global_async_m2m_token_manager is not ows_client_mock.mock_async_m2m_token_manager ) # Mock a route response_json = {"status": "ok"} ows_client_mock.get("ows-permissions", path="/test/").mock( return_value=httpx.Response(status_code=200, json=response_json) ) # Make a request using the global async client response = await global_async_ows_client.get("ows-permissions", path="/test/") # Verify the response assert response.status_code == 200 assert response.json() == response_json # Verify the mock token manager was called ows_client_mock.mock_async_m2m_token_manager.get_token_string.assert_called_once() # type: ignore[attr-defined] @pytest.mark.anyio @pytest.mark.ows_client_mock_disable_m2m_patch async def test_async_ows_client_mock_disables_patching_with_marker( ows_client_mock: OwsClientMock, ) -> None: """Test that the marker disables global AsyncOwsClient patching.""" # The `ows_client_mock` fixture should NOT have patched the token manager assert global_async_ows_client.m2m_token_manager is global_async_m2m_token_manager # Verify it's not using the mock token manager assert ( global_async_ows_client.m2m_token_manager is not ows_client_mock.mock_async_m2m_token_manager ) # Impersonation Clients global_impersonation_m2m_token_manager = ImpersonationM2MTokenManager( secrets_manager=MagicMock(), environment="test", service_name="test-app", ) global_impersonation_ows_client_to_be_patched = ImpersonationOwsClient( environment="test", service_name="test-app", m2m_token_manager=global_impersonation_m2m_token_manager, ) def test_impersonation_ows_client_mock_patches_global_token_manager( ows_client_mock: OwsClientMock, ) -> None: """Test that ows_client_mock patches global ImpersonationOwsClient instances.""" # The `ows_client_mock` fixture should have patched the token manager assert ( global_impersonation_ows_client_to_be_patched.m2m_token_manager is ows_client_mock.mock_impersonation_m2m_token_manager ) # Verify original manager has not been patched assert ( global_impersonation_m2m_token_manager is not ows_client_mock.mock_impersonation_m2m_token_manager ) # Mock a route response_json = {"status": "ok"} ows_client_mock.get("ows-permissions", path="/test/").mock( return_value=httpx.Response(status_code=200, json=response_json) ) # Make a request using the global impersonation client impersonated_identity_uuid = "f94b0c5a-b520-486b-ac17-e59e9888b8bd" response = global_impersonation_ows_client_to_be_patched.get( "ows-permissions", path="/test/", impersonated_identity_uuid=impersonated_identity_uuid, ) # Verify the response assert response.status_code == 200 assert response.json() == response_json # Verify the mock token manager was called ows_client_mock.mock_impersonation_m2m_token_manager.get_token_string.assert_called_once_with( # type: ignore[attr-defined] impersonated_identity_uuid=impersonated_identity_uuid ) @pytest.mark.ows_client_mock_disable_m2m_patch def test_impersonation_ows_client_mock_disables_patching_with_marker( ows_client_mock: OwsClientMock, ) -> None: """Test that the marker disables global ImpersonationOwsClient patching.""" # The `ows_client_mock` fixture should NOT have patched the token manager assert ( global_impersonation_ows_client_to_be_patched.m2m_token_manager is global_impersonation_m2m_token_manager ) # Verify it's not using the mock token manager assert ( global_impersonation_ows_client_to_be_patched.m2m_token_manager is not ows_client_mock.mock_impersonation_m2m_token_manager )