From bff805c9c67b4634a109065f5f314a45b1e48b97 Mon Sep 17 00:00:00 2001 From: PeterStrob Date: Tue, 26 Oct 2021 14:17:26 -0700 Subject: [PATCH 1/2] feat:added create invitation handler unit test Signed-off-by: PeterStrob --- tests/test_create_invitation_handler.py | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/test_create_invitation_handler.py diff --git a/tests/test_create_invitation_handler.py b/tests/test_create_invitation_handler.py new file mode 100644 index 00000000..c8a43f64 --- /dev/null +++ b/tests/test_create_invitation_handler.py @@ -0,0 +1,73 @@ +from unittest.mock import patch + +import pytest +from aries_cloudagent.protocols.connections.v1_0.messages.connection_invitation import ( + ConnectionInvitation, +) +from aries_cloudagent.connections.models.conn_record import ConnRecord as con +from aries_cloudagent.messaging.responder import MockResponder +from aries_cloudagent.messaging.agent_message import AgentMessage +from asynctest import mock + +import acapy_plugin_toolbox.invitations as inv +from tests.conftest import RequestContext + + +@pytest.fixture +def message(): + """Message fixture""" + yield inv.CreateInvitation( + label="test_label", + auto_accept=True, + multi_use=True, + alias="create_invitation", + mediation_id="test_med_id", + group="test_group", + ) + + +@pytest.fixture +def context(profile, mock_admin_connection, message): + """RequestContext fixture.""" + context = RequestContext(profile) + context.connection_record = mock_admin_connection + context.connection_ready = True + context.message = message + yield context + + +@pytest.mark.asyncio +async def test_createinvitationhandler(context, mock_responder): + """CreateInvitationHandler test. + + A unit test for the CreateInvitationHandler class.""" + createinvhandler = inv.CreateInvitationHandler() + connection = mock.MagicMock(spec=con) + connection.metadata_set = mock.CoroutineMock() + connection.connection_id = patch.object(con, "metadata_set", mock.CoroutineMock()) + connection.alias = "test_alias" + connection.accept = True + connection.invitation_mode = "test_mode" + connection.created_at = "test_created" + + invitation = mock.MagicMock(spec=ConnectionInvitation) + invitation.label = "test_label" + mock_conn_mgr = mock.MagicMock() + mock_conn_mgr.create_invitation = mock.CoroutineMock( + return_value=(connection, invitation) + ) + + with patch.object( + MockResponder, "send_reply", mock.CoroutineMock() + ) as mock_reply, patch.object( + inv, "ConnectionManager", mock.MagicMock(return_value=mock_conn_mgr) + ), patch.object( + connection, "metadata_set", mock.CoroutineMock() + ) as mock_set, patch.object( + AgentMessage, "assign_thread_from", mock.CoroutineMock() + ) as mock_assign: + + await createinvhandler.handle(context, mock_responder) + mock_set.assert_called_once() + mock_assign.assert_called_once() + mock_reply.assert_called_once() From bd8724869d31695f6b03b827dcf9eb7d06b1e1b2 Mon Sep 17 00:00:00 2001 From: PeterStrob Date: Wed, 27 Oct 2021 11:54:25 -0700 Subject: [PATCH 2/2] fix:removed_unnecessary_mock_instances Signed-off-by: PeterStrob --- tests/test_create_invitation_handler.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_create_invitation_handler.py b/tests/test_create_invitation_handler.py index c8a43f64..055db79c 100644 --- a/tests/test_create_invitation_handler.py +++ b/tests/test_create_invitation_handler.py @@ -58,16 +58,15 @@ async def test_createinvitationhandler(context, mock_responder): ) with patch.object( - MockResponder, "send_reply", mock.CoroutineMock() - ) as mock_reply, patch.object( inv, "ConnectionManager", mock.MagicMock(return_value=mock_conn_mgr) ), patch.object( - connection, "metadata_set", mock.CoroutineMock() - ) as mock_set, patch.object( AgentMessage, "assign_thread_from", mock.CoroutineMock() ) as mock_assign: await createinvhandler.handle(context, mock_responder) - mock_set.assert_called_once() + connection.metadata_set.assert_called_once() mock_assign.assert_called_once() - mock_reply.assert_called_once() + assert isinstance(mock_responder.messages[0][0], inv.Invitation) + assert ( + mock_responder.messages[0][0].mediation_id == context.message.mediation_id + )