From 25cbc79e2929f01c84cc55391918213649e30298 Mon Sep 17 00:00:00 2001 From: ff137 Date: Thu, 7 Nov 2024 18:37:06 +0200 Subject: [PATCH] :white_check_mark: Modify log assertion to be more robust Signed-off-by: ff137 --- acapy_agent/core/tests/test_conductor.py | 45 +++++++++++++++++++----- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/acapy_agent/core/tests/test_conductor.py b/acapy_agent/core/tests/test_conductor.py index 2dc5fbaa73..6f7d1e6d9d 100644 --- a/acapy_agent/core/tests/test_conductor.py +++ b/acapy_agent/core/tests/test_conductor.py @@ -1,5 +1,5 @@ -import asyncio from unittest import IsolatedAsyncioTestCase +from unittest.mock import call import pytest @@ -1202,6 +1202,18 @@ async def test_print_invite_connection(self): test_profile = await create_test_profile(None, await builder.build_context()) + # Define expected invitation URLs + expected_oob_url = "http://localhost?oob=test_oob_invite" + expected_ci_url = "http://localhost?c_i=test_ci_invite" + + # Mock the InvitationRecord returned by create_invitation for OOB + mock_oob_invitation = mock.MagicMock() + mock_oob_invitation.invitation.to_url.return_value = expected_oob_url + + # Mock the InvitationRecord returned by create_invitation for Connections Protocol + mock_ci_invitation = mock.MagicMock() + mock_ci_invitation.to_url.return_value = expected_ci_url + with mock.patch.object( test_module, "wallet_config", @@ -1209,22 +1221,37 @@ async def test_print_invite_connection(self): test_profile, DIDInfo("did", "verkey", metadata={}, method=SOV, key_type=ED25519), ), - ), self.assertLogs( - "acapy_agent.core.conductor", level="INFO" - ) as captured, mock.patch.object( + ), mock.patch.object( test_module, "OutboundTransportManager", autospec=True - ) as mock_outbound_mgr: + ) as mock_outbound_mgr, mock.patch.object( + test_module, "OutOfBandManager" + ) as oob_mgr, mock.patch.object( + test_module, "ConnectionManager" + ) as conn_mgr, mock.patch.object(test_module.LOGGER, "info") as mock_logger_info: mock_outbound_mgr.return_value.registered_transports = { "test": mock.MagicMock(schemes=["http"]) } + + # Configure create_invitation to return the mocked invitations + oob_mgr.return_value.create_invitation = mock.CoroutineMock( + return_value=mock_oob_invitation + ) + conn_mgr.return_value.create_invitation = mock.CoroutineMock( + return_value=(None, mock_ci_invitation) + ) + + # Execute the conductor lifecycle await conductor.setup() await conductor.start() await conductor.stop() - await asyncio.sleep(0.1) # Allow log handlers to flush - value = captured.output - assert any("http://localhost?oob=" in msg for msg in value) - assert any("http://localhost?c_i=" in msg for msg in value) + + # Assert that LOGGER.info was called twice with the expected URLs + expected_calls = [ + call(f"Invitation URL:\n{expected_oob_url}"), + call(f"Invitation URL (Connections protocol):\n{expected_ci_url}"), + ] + mock_logger_info.assert_has_calls(expected_calls, any_order=True) async def test_clear_default_mediator(self): builder: ContextBuilder = StubContextBuilder(self.test_settings)