async def test_handle_update_query(self): self.context.message = RouteUpdateRequest( updates=[ RouteUpdate( recipient_key=TEST_VERKEY, action=RouteUpdate.ACTION_CREATE, ) ] ) update_handler = RouteUpdateRequestHandler() update_responder = MockResponder() await update_handler.handle(self.context, update_responder) messages = update_responder.messages assert len(messages) == 1 result, target = messages[0] assert isinstance(result, RouteUpdateResponse) assert len(result.updated) == 1 assert result.updated[0].recipient_key == TEST_VERKEY assert result.updated[0].action == RouteUpdate.ACTION_CREATE assert result.updated[0].result == RouteUpdated.RESULT_SUCCESS assert not target self.context.message = RouteQueryRequest() query_handler = RouteQueryRequestHandler() query_responder = MockResponder() await query_handler.handle(self.context, query_responder) messages = query_responder.messages assert len(messages) == 1 result, target = messages[0] assert isinstance(result, RouteQueryResponse) assert result.routes[0].recipient_key == TEST_VERKEY assert not target
async def test_no_connection(self): self.context.connection_ready = False self.context.message = RouteQueryRequest() handler = RouteQueryRequestHandler() responder = MockResponder() with self.assertRaises(HandlerException): await handler.handle(self.context, responder) self.context.message = RouteUpdateRequest() handler = RouteUpdateRequestHandler() responder = MockResponder() with self.assertRaises(HandlerException): await handler.handle(self.context, responder) self.context.message = RouteQueryResponse() handler = RouteQueryResponseHandler() responder = MockResponder() with self.assertRaises(HandlerException): await handler.handle(self.context, responder) self.context.message = RouteUpdateResponse() handler = RouteUpdateResponseHandler() responder = MockResponder() with self.assertRaises(HandlerException): await handler.handle(self.context, responder)
async def test_service_start_and_return_introduction(self): service = await demo_service.DemoIntroductionService.service_handler()( self.context) start_responder = MockResponder() conn_rec_init = ConnectionRecord( connection_id=None, state=ConnectionRecord.STATE_ACTIVE, ) await conn_rec_init.save(self.context) assert conn_rec_init._id conn_rec_target = ConnectionRecord( connection_id=None, state=ConnectionRecord.STATE_ACTIVE, ) await conn_rec_target.save(self.context) assert conn_rec_target._id await service.start_introduction( init_connection_id=conn_rec_init._id, target_connection_id=conn_rec_target._id, message="Hello Start", outbound_handler=start_responder.send, ) messages = start_responder.messages assert len(messages) == 1 (result, target) = messages[0] assert isinstance(result, demo_service.InvitationRequest) assert result.message == "Hello Start" assert target["connection_id"] == conn_rec_target._id invite = demo_service.Invitation( invitation=ConnectionInvitation( label=TEST_LABEL, did=TEST_DID, recipient_keys=[TEST_VERKEY], endpoint=TEST_ENDPOINT, routing_keys=[TEST_ROUTE_VERKEY], image_url=TEST_IMAGE_URL, ), message="Hello Invite", _id=result._id, ) return_responder = MockResponder() await service.return_invitation( target_connection_id=conn_rec_target._id, invitation=invite, outbound_handler=return_responder.send, ) messages = return_responder.messages assert len(messages) == 1 (result, target) = messages[0] assert isinstance(result, demo_service.ForwardInvitation) assert result.message == "Hello Invite" assert target["connection_id"] == conn_rec_init._id
async def test_called(self): self.context = RequestContext(base_context=InjectionContext( enforce_typing=False)) MenuService = async_mock.MagicMock(handler.BaseMenuService, autospec=True) self.menu_service = MenuService() self.context.injector.bind_instance(handler.BaseMenuService, self.menu_service) self.context.inject = async_mock.CoroutineMock( return_value=self.menu_service) self.context.connection_record = async_mock.MagicMock() self.context.connection_record.connection_id = "dummy" responder = MockResponder() self.context.message = handler.Perform() self.menu_service.perform_menu_action = async_mock.CoroutineMock( return_value="perform") handler_inst = handler.PerformHandler() await handler_inst.handle(self.context, responder) messages = responder.messages assert len(messages) == 1 (result, target) = messages[0] assert result == "perform" assert target == {}
async def test_get_active_menu(self): self.context = RequestContext(base_context=InjectionContext( enforce_typing=False)) self.responder = MockResponder() self.context.injector.bind_instance(test_module.BaseResponder, self.responder) self.menu_service = await ( test_module.DriverMenuService.service_handler()(self.context)) connection = async_mock.MagicMock() connection.connection_id = "connid" thread_id = "thid" await self.menu_service.get_active_menu(connection, thread_id) webhooks = self.responder.webhooks assert len(webhooks) == 1 (result, target) = webhooks[0] assert result == "get-active-menu" assert target == { "connection_id": connection.connection_id, "thread_id": thread_id, }
async def test_handle_auto_accept(self): handler = test_module.InvitationRequestHandler() self.context.update_settings({"accept_requests": True}) conn_invitation = ConnectionInvitation( label=TEST_LABEL, did=TEST_DID, recipient_keys=[TEST_VERKEY], endpoint=TEST_ENDPOINT, routing_keys=[TEST_ROUTE_VERKEY], image_url=TEST_IMAGE_URL, ) mock_conn_rec = async_mock.MagicMock(connection_id="dummy") responder = MockResponder() with async_mock.patch.object( test_module, "ConnectionManager", autospec=True ) as mock_mgr: mock_mgr.return_value.create_invitation = async_mock.CoroutineMock( return_value=(mock_conn_rec, conn_invitation) ) await handler.handle(self.context, responder) assert mock_mgr.return_value.create_invitation.called_once_with() messages = responder.messages assert len(messages) == 1 (result, _) = messages[0] assert type(result) == Invitation assert result._thread._thid == self.context.message._message_id
async def test_perform_menu_action(self): self.context = RequestContext(base_context=InjectionContext( enforce_typing=False)) self.responder = MockResponder() self.context.injector.bind_instance(test_module.BaseResponder, self.responder) self.menu_service = await ( test_module.DriverMenuService.service_handler()(self.context)) action_name = "action" action_params = {"a": 1, "b": 2} connection = async_mock.MagicMock() connection.connection_id = "connid" thread_id = "thid" await self.menu_service.perform_menu_action(action_name, action_params, connection, thread_id) webhooks = self.responder.webhooks assert len(webhooks) == 1 (result, target) = webhooks[0] assert result == "perform-menu-action" assert target == { "connection_id": connection.connection_id, "thread_id": thread_id, "action_name": action_name, "action_params": action_params, }
def setUp(self): self.storage = BasicStorage() self.cache = BasicCache() self.wallet = BasicWallet() self.responder = MockResponder() self.responder.send = async_mock.CoroutineMock() self.context = InjectionContext(enforce_typing=False) self.context.injector.bind_instance(BaseStorage, self.storage) self.context.injector.bind_instance(BaseWallet, self.wallet) self.context.injector.bind_instance(BaseResponder, self.responder) self.context.injector.bind_instance(BaseCache, self.cache) self.ledger = async_mock.create_autospec(BaseLedger) self.ledger.__aenter__ = async_mock.CoroutineMock( return_value=self.ledger) self.context.injector.bind_instance(BaseLedger, self.ledger) self.context.update_settings({ "default_endpoint": TestConfig.test_endpoint, "default_label": "This guy", "additional_endpoints": ["http://aries.ca/another-endpoint"], "debug.auto_accept_invites": True, "debug.auto_accept_requests": True, }) self.manager = OutOfBandManager(self.context) self.test_conn_rec = ConnectionRecord( my_did=TestConfig.test_did, their_did=TestConfig.test_target_did, their_role=None, state=ConnectionRecord.STATE_ACTIVE, )
async def test_handle_auto_accept(self): handler = test_module.ForwardInvitationHandler() self.context.update_settings({"accept_invites": True}) mock_conn_rec = async_mock.MagicMock(connection_id="dummy") mock_conn_req = async_mock.MagicMock(label="test") responder = MockResponder() with async_mock.patch.object(test_module, "ConnectionManager", autospec=True) as mock_mgr: mock_mgr.return_value.receive_invitation = async_mock.CoroutineMock( return_value=mock_conn_rec) mock_mgr.return_value.create_request = async_mock.CoroutineMock( return_value=mock_conn_req) await handler.handle(self.context, responder) assert mock_mgr.return_value.create_request.called_once_with( mock_conn_rec) messages = responder.messages assert len(messages) == 1 (result, target) = messages[0] assert result == mock_conn_req assert target["connection_id"] == "dummy"
async def test_ping_not_ready(self, request_context): request_context.message_receipt = MessageReceipt() request_context.message = Ping(response_requested=False) request_context.connection_ready = False handler = PingHandler() responder = MockResponder() assert not await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 0
async def test_query_none(self): self.context.message = RouteQueryRequest() handler = RouteQueryRequestHandler() responder = MockResponder() await handler.handle(self.context, responder) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert isinstance(result, RouteQueryResponse) and result.routes == [] assert not target
async def test_ping_response(self, request_context): request_context.message_receipt = MessageReceipt() request_context.message = PingResponse() request_context.settings = {"debug.monitor_ping": True} request_context.connection_ready = True handler = PingResponseHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 0
async def test_handle(self): handler = test_module.InvitationRequestHandler() responder = MockResponder() inv_req = InvitationRequest(responder=responder, message="Hello") with async_mock.patch.object( test_module, "ConnectionManager", autospec=True ) as mock_mgr: await handler.handle(self.context, responder)
async def test_called(self, mock_conn_mgr, request_context): mock_conn_mgr.return_value.receive_request = async_mock.CoroutineMock() request_context.message = ConnectionRequest() handler_inst = handler.ConnectionRequestHandler() responder = MockResponder() await handler_inst.handle(request_context, responder) mock_conn_mgr.assert_called_once_with(request_context) mock_conn_mgr.return_value.receive_request.assert_called_once_with( request_context.message, request_context.message_receipt) assert not responder.messages
async def test_problem_report(self, request_context): request_context.message = ConnectionInvitation() handler = ConnectionInvitationHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert (isinstance(result, ProblemReport) and result.problem_code == ProblemReportReason.INVITATION_NOT_ACCEPTED) assert not target
async def test_query_all(self, request_context): request_context.message = Query(query="*") handler = QueryHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert isinstance(result, Disclose) and result.protocols assert result.protocols[0]["pid"] == TEST_MESSAGE_FAMILY assert not target
async def test_handle(self): handler = test_module.ForwardInvitationHandler() responder = MockResponder() with async_mock.patch.object(test_module, "ConnectionManager", autospec=True) as mock_mgr: mock_mgr.return_value.receive_invitation = async_mock.CoroutineMock( return_value=ConnectionRecord(connection_id="dummy")) await handler.handle(self.context, responder) assert not (responder.messages)
async def test_problem_report(self, request_context): request_context.message_receipt = MessageReceipt() request_context.message = ProblemReport() request_context.connection_ready = True handler = ProblemReportHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 0 hooks = responder.webhooks assert len(hooks) == 1 assert hooks[0] == ("problem_report", request_context.message.serialize())
async def test_ping_response(self, request_context): request_context.message_receipt = MessageReceipt() request_context.message = Ping(response_requested=True) request_context.connection_ready = True handler = PingHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert isinstance(result, PingResponse) assert result._thread_id == request_context.message._thread_id assert not target
async def test_disclose(self): ctx = RequestContext() registry = ProtocolRegistry() registry.register_message_types({TEST_MESSAGE_TYPE: object()}) ctx.injector.bind_instance(ProtocolRegistry, registry) ctx.message = Disclose(protocols=[{ "pid": "did:sov:BzCbsNYhMrjHiqZDTUASHg;test_proto/test_message", "roles": [], }]) handler = DiscloseHandler() mock_responder = MockResponder() await handler.handle(ctx, mock_responder) assert not mock_responder.messages
async def test_problem_report(self, mock_conn_mgr, request_context): mock_conn_mgr.return_value.receive_request = async_mock.CoroutineMock() mock_conn_mgr.return_value.receive_request.side_effect = ConnectionManagerError( error_code=ProblemReportReason.REQUEST_NOT_ACCEPTED) request_context.message = ConnectionRequest() handler_inst = handler.ConnectionRequestHandler() responder = MockResponder() await handler_inst.handle(request_context, responder) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert (isinstance(result, ProblemReport) and result.problem_code == ProblemReportReason.REQUEST_NOT_ACCEPTED) assert target == {"target_list": None}
async def test_called_auto_ping(self, mock_conn_mgr, request_context): request_context.update_settings({"auto_ping_connection": True}) mock_conn_mgr.return_value.accept_response = async_mock.CoroutineMock() request_context.message = ConnectionResponse() handler_inst = handler.ConnectionResponseHandler() responder = MockResponder() await handler_inst.handle(request_context, responder) mock_conn_mgr.assert_called_once_with(request_context) mock_conn_mgr.return_value.accept_response.assert_called_once_with( request_context.message, request_context.message_receipt) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert isinstance(result, Ping)
async def test_basic_message_response(self, request_context): request_context.update_settings({"debug.auto_respond_messages": True}) request_context.connection_record = mock.MagicMock() request_context.default_label = "agent" test_message_content = "hello" request_context.message = BasicMessage(content=test_message_content) request_context.connection_ready = True handler = BasicMessageHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 1 reply, target = messages[0] assert isinstance(reply, BasicMessage) assert reply._thread_id == request_context.message._thread_id assert not target
async def test_handle_cannot_resolve_recipient(self): self.context.message_receipt = MessageReceipt( recipient_verkey=TEST_VERKEY) handler = test_module.ForwardHandler() responder = MockResponder() with async_mock.patch.object(test_module, "RoutingManager", autospec=True) as mock_mgr: mock_mgr.return_value.get_recipient = async_mock.CoroutineMock( side_effect=test_module.RoutingManagerError()) await handler.handle(self.context, responder) messages = responder.messages assert not messages
async def test_called(self): request_context = RequestContext() request_context.connection_record = async_mock.MagicMock() request_context.connection_record.connection_id = "dummy" handler.save_connection_menu = async_mock.CoroutineMock() responder = MockResponder() request_context.message = handler.Menu() handler_inst = handler.MenuHandler() await handler_inst.handle(request_context, responder) handler.save_connection_menu.assert_called_once_with( request_context.message, request_context.connection_record.connection_id, request_context, )
async def test_problem_report_did_doc_no_conn_target( self, mock_conn_target, mock_conn_mgr, request_context, did_doc): mock_conn_mgr.return_value.accept_response = async_mock.CoroutineMock() mock_conn_mgr.return_value.accept_response.side_effect = ConnectionManagerError( error_code=ProblemReportReason.REQUEST_NOT_ACCEPTED) mock_conn_mgr.return_value.diddoc_connection_targets = async_mock.MagicMock( side_effect=ConnectionManagerError("no target")) request_context.message = ConnectionResponse( connection=ConnectionDetail(did=TEST_DID, did_doc=did_doc)) handler_inst = handler.ConnectionResponseHandler() responder = MockResponder() await handler_inst.handle(request_context, responder) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert (isinstance(result, ProblemReport) and result.problem_code == ProblemReportReason.REQUEST_NOT_ACCEPTED) assert target == {"target_list": None}
async def test_handle(self): self.context.message_receipt = MessageReceipt( recipient_verkey=TEST_VERKEY) handler = test_module.ForwardHandler() responder = MockResponder() with async_mock.patch.object(test_module, "RoutingManager", autospec=True) as mock_mgr: mock_mgr.return_value.get_recipient = async_mock.CoroutineMock( return_value=RouteRecord(connection_id="dummy")) await handler.handle(self.context, responder) messages = responder.messages assert len(messages) == 1 (result, target) = messages[0] assert json.loads(result) == self.context.message.msg assert target["connection_id"] == "dummy"
async def test_handle(self): handler = test_module.InvitationHandler() mock_conn_rec = async_mock.MagicMock(connection_id="dummy") responder = MockResponder() with async_mock.patch.object( self.context, "inject", async_mock.CoroutineMock() ) as mock_ctx_inject: mock_ctx_inject.return_value = async_mock.MagicMock( return_invitation=async_mock.CoroutineMock() ) await handler.handle(self.context, responder) assert mock_ctx_inject.return_value.return_invitation.called_once_with( self.context.connection_record.connection_id, self.context.message, responder.send, )
async def test_basic_message(self, request_context): request_context.connection_record = mock.MagicMock() test_message_content = "http://aries.ca/hello" request_context.message = BasicMessage(content=test_message_content) request_context.connection_ready = True handler = BasicMessageHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 0 hooks = responder.webhooks assert len(hooks) == 1 assert hooks[0] == ( "basicmessages", { "connection_id": request_context.connection_record.connection_id, "message_id": request_context.message._id, "content": test_message_content, "state": "received", }, )
async def test_problem_report_did_doc(self, mock_conn_target, mock_conn_mgr, request_context, did_doc): mock_conn_mgr.return_value.receive_request = async_mock.CoroutineMock() mock_conn_mgr.return_value.receive_request.side_effect = ConnectionManagerError( error_code=ProblemReportReason.REQUEST_NOT_ACCEPTED) mock_conn_mgr.return_value.diddoc_connection_targets = async_mock.MagicMock( return_value=[mock_conn_target]) request_context.message = ConnectionRequest( connection=ConnectionDetail(did=TEST_DID, did_doc=did_doc), label=TEST_LABEL, image_url=TEST_IMAGE_URL, ) handler_inst = handler.ConnectionRequestHandler() responder = MockResponder() await handler_inst.handle(request_context, responder) messages = responder.messages assert len(messages) == 1 result, target = messages[0] assert (isinstance(result, ProblemReport) and result.problem_code == ProblemReportReason.REQUEST_NOT_ACCEPTED) assert target == {"target_list": [mock_conn_target]}