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_is_saving_record(self): context = RequestContext() storage = BasicStorage() context.injector.bind_instance(BaseStorage, storage) context.connection_ready = True context.message = CredentialRequest( credential={ "credential_type": "TEST", "credential_values": { "test": "one", "value": "two" }, }) responder = MockResponder() responder.connection_id = "1234" handler_inst = CredentialRequestHandler() await handler_inst.handle(context, responder) assert len(responder.messages) == 0 assert 1 == len(responder.webhooks) id = responder.webhooks[0][1]["credential_exchange_id"] exchange = await CredentialExchangeRecord.retrieve_by_id(context, id) assert exchange != None assert exchange.connection_id == responder.connection_id assert exchange.state == CredentialExchangeRecord.STATE_REQUEST_RECEIVED assert exchange.credential_request == context.message.credential
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_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_is_handler_saving_record(self): context = RequestContext() # wallet is required for issuer to sign stuff cause it holds keys wallet = BasicWallet() # storage is required to save exchange record and save credential storage = BasicStorage(wallet) # issuer required to create credential issuer = PDSIssuer(wallet) # holder requiered to save credential holder = PDSHolder(context) context.injector.bind_instance(BaseWallet, wallet) context.injector.bind_instance(BaseStorage, storage) context.injector.bind_instance(BaseIssuer, issuer) context.injector.bind_instance(BaseHolder, holder) record = CredentialExchangeRecord( connection_id=connection_id, initiator=CredentialExchangeRecord.INITIATOR_SELF, role=CredentialExchangeRecord.ROLE_HOLDER, state=CredentialExchangeRecord.STATE_REQUEST_SENT, thread_id=thread_id, credential_request=credential_request, ) await record.save(context) credential = await create_credential(context, credential_request, their_public_did="1234-theirdid") context.message: CredentialIssue = CredentialIssue( credential=credential) context.message.assign_thread_id(thread_id) context.connection_ready = True handler_inst = CredentialIssueHandler() responder = MockResponder() responder.connection_id = connection_id await handler_inst.handle(context, responder) credential_id = responder.webhooks[0][1]["credential_id"] assert credential_id credential = await holder.get_credential(credential_id) credential = json.loads(credential) assert credential["credentialSubject"] for key in credential_request["credential_values"]: if (credential["credentialSubject"][key] != credential_request["credential_values"][key]): raise Exception( f"""Requested Credential VALUES differ from Issued Credential, RequestedCredential: {credential_request}, IssuedCredential: {credential}""")
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, }
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(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, async_mock.patch.object( test_module, "ConnectionManager", autospec=True) as mock_connection_mgr: mock_mgr.return_value.get_recipient = async_mock.CoroutineMock( return_value=RouteRecord(connection_id="dummy")) mock_connection_mgr.return_value.get_connection_targets = ( async_mock.CoroutineMock(return_value=[ ConnectionTarget(recipient_keys=["recip_key"], ) ])) 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"
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.context.injector.bind_instance(BaseLedger, async_mock.MagicMock()) self.context.update_settings({ "default_endpoint": "http://aries.ca/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=self.test_did, their_did=self.test_target_did, their_role=None, state=ConnectionRecord.STATE_ACTIVE, )
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_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.MenuRequest() self.menu_service.get_active_menu = async_mock.CoroutineMock( return_value="menu" ) handler_inst = handler.MenuRequestHandler() await handler_inst.handle(self.context, responder) messages = responder.messages assert len(messages) == 1 (result, target) = messages[0] assert result == "menu" assert target == {}
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_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_provide_data_handler(context): """Test provide data handler.""" handler, responder = ProvideDataHandler(), MockResponder() await handler.handle(context, responder) assert len(responder.webhooks) == 1 topic, payload = responder.webhooks[0] assert topic == "data-transfer/test_goal" assert payload == { "connection_id": TEST_CONN_ID, "data": [TEST_DATA.serialize()] }
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(self, request_context): request_context.message_receipt = MessageReceipt() request_context.message = Ping(response_requested=False) request_context.settings["debug.monitor_ping"] = True request_context.connection_ready = True handler = PingHandler() responder = MockResponder() await handler.handle(request_context, responder) messages = responder.messages assert len(messages) == 0
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_called(self, mock_conn_mgr, request_context): 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) 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_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=ConnRecord(connection_id="dummy")) await handler.handle(self.context, responder) assert not (responder.messages)
def create_default_context(self): context = InjectionContext() storage = BasicStorage() responder = MockResponder() context.injector.bind_instance(BaseStorage, storage) context.connection_ready = True context.connection_record = ConnectionRecord( connection_id=self.connection_id) return [context, storage, responder]
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_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_discovery_response_handler(self): context = RequestContext() storage = BasicStorage() responder = MockResponder() context.injector.bind_instance(BaseStorage, storage) context.connection_ready = True context.connection_record = ConnectionRecord(connection_id=self.connection_id) context.message = DiscoveryResponse(services=[self.service]) handler = DiscoveryResponseHandler() await handler.handle(context, responder)
async def test_problem_report(self, mock_conn_mgr, request_context): mock_conn_mgr.return_value.accept_response = async_mock.CoroutineMock() mock_conn_mgr.return_value.accept_response.side_effect = ConnectionManagerError( error_code=ProblemReportReason.RESPONSE_NOT_ACCEPTED) request_context.message = ConnectionResponse() 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.RESPONSE_NOT_ACCEPTED) assert target == {"target_list": None}
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_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