예제 #1
0
    async def test_service_start_introduction_target_conn_rec_not_completed(
            self):
        service = await demo_service.DemoIntroductionService.service_handler()(
            self.context)

        conn_rec_init = ConnRecord(
            connection_id=None,
            state=ConnRecord.State.COMPLETED.rfc23,
        )
        await conn_rec_init.save(self.context)
        assert conn_rec_init._id

        conn_rec_target = ConnRecord(
            connection_id=None,
            state=ConnRecord.State.ABANDONED.rfc23,
        )
        await conn_rec_target.save(self.context)
        assert conn_rec_target._id

        with self.assertRaises(base_service.IntroductionError):
            await service.start_introduction(
                init_connection_id=conn_rec_init._id,
                target_connection_id=conn_rec_target._id,
                message="Hello",
                outbound_handler=None,
            )
예제 #2
0
def conn_record():
    """Connection record fixture."""
    record = ConnRecord()
    record.metadata_get = mock.CoroutineMock(return_value={"methods": "test"})
    record.metadata_set = mock.CoroutineMock()
    record.metadata_delete = mock.CoroutineMock()
    yield record
예제 #3
0
    async def test_service_start_and_return_introduction(self):
        service = await demo_service.DemoIntroductionService.service_handler()(
            self.context)
        start_responder = MockResponder()

        conn_rec_init = ConnRecord(
            connection_id=None,
            state=ConnRecord.State.COMPLETED.rfc23,
        )
        await conn_rec_init.save(self.context)
        assert conn_rec_init._id

        conn_rec_target = ConnRecord(
            connection_id=None,
            state=ConnRecord.State.COMPLETED.rfc23,
        )
        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
예제 #4
0
async def session():
    """Fixture for session used in tests."""
    # pylint: disable=W0621
    context = RequestContext.test_context()
    context.message_receipt = MessageReceipt(sender_verkey=TEST_VERKEY)
    context.connection_record = ConnRecord(connection_id=TEST_CONN_ID)
    yield await context.session()
예제 #5
0
    async def test_service_return_invitation_not_found(self):
        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 World",
        )

        service = await demo_service.DemoIntroductionService.service_handler()(
            self.context)

        conn_rec_target = ConnRecord(
            connection_id=None,
            state=ConnRecord.State.COMPLETED.rfc23,
        )
        await conn_rec_target.save(self.context)
        assert conn_rec_target._id

        await service.return_invitation(
            target_connection_id=conn_rec_target._id,
            invitation=invite,
            outbound_handler=None,
        )
 async def setUp(self):
     self.context = RequestContext(base_context=InjectionContext(
         enforce_typing=False))
     self.context.connection_ready = True
     self.context.connection_record = ConnRecord(connection_id="conn-id")
     self.context.message_receipt = MessageReceipt(
         sender_verkey=TEST_VERKEY)
     self.context.injector.bind_instance(BaseStorage, BasicStorage())
    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)
    async def handle(self, context: RequestContext, responder: BaseResponder):
        """Handle static connection creation request."""

        session = await context.session()
        connection_mgr = ConnectionManager(session)
        wallet: BaseWallet = session.inject(BaseWallet)

        # Make our info for the connection
        my_info = await wallet.create_local_did()

        # Create connection record
        connection = ConnRecord(
            initiator=ConnRecord.INITIATOR_SELF,
            my_did=my_info.did,
            their_did=context.message.static_did,
            their_label=context.message.label,
            their_role=context.message.role if context.message.role else None,
            state=ConnRecord.STATE_ACTIVE,
            invitation_mode=ConnRecord.INVITATION_MODE_STATIC)

        # Construct their did doc from the basic components in message
        diddoc = DIDDoc(context.message.static_did)
        public_key = PublicKey(did=context.message.static_did,
                               ident="1",
                               value=context.message.static_key,
                               pk_type=PublicKeyType.ED25519_SIG_2018,
                               controller=context.message.static_did)
        service = Service(did=context.message.static_did,
                          ident="indy",
                          typ="IndyAgent",
                          recip_keys=[public_key],
                          routing_keys=[],
                          endpoint=context.message.static_endpoint)
        diddoc.set(public_key)
        diddoc.set(service)

        # Save
        await connection_mgr.store_did_document(diddoc)
        await connection.save(session, reason='Created new static connection')

        # Prepare response
        info = StaticConnectionInfo(
            did=my_info.did,
            key=my_info.verkey,
            endpoint=context.settings.get("default_endpoint"))
        info.assign_thread_from(context.message)
        await responder.send_reply(info)
        return
def conn_record_to_message_repr(conn: ConnRecord) -> Dict[str, Any]:
    """Map ConnRecord onto Connection."""
    def _state_map(state: str) -> str:
        if state in ('active', 'response'):
            return 'active'
        if state == 'error':
            return 'error'
        return 'pending'

    return {
        'label': conn.their_label,
        'my_did': conn.my_did,
        'their_did': conn.their_did,
        'state': _state_map(conn.state),
        'connection_id': conn.connection_id,
        'raw_repr': conn.serialize()
    }