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, )
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 setup(context: InjectionContext): """Setup requester capabilities.""" protocol_registry = context.inject(ProtocolRegistry) protocol_registry.register_message_types({ ResolveDIDResult.Meta.message_type: ResolveDIDResult, ResolveDIDProblemReport.Meta.message_type: ResolveDIDProblemReport, }) registry = context.inject(DIDResolverRegistry) resolver = DIDCommResolver() registry.register(resolver) await resolver.setup(context)
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 setUp(self): self.context: InjectionContext = InjectionContext() storage = BasicStorage() self.wallet = BasicWallet() await self.wallet.create_public_did() public_did = await self.wallet.get_public_did() assert public_did != None self.holder = PDSHolder(self.wallet, storage, self.context) issuer = PDSIssuer(self.wallet) self.context.injector.bind_instance(BaseWallet, self.wallet) self.context.injector.bind_instance(BaseStorage, storage) self.context.settings.set_value( "personal_storage_registered_types", {"local": "aries_cloudagent.pdstorage_thcf.local.LocalPDS"}, ) pds = LocalPDS() self.context.injector.bind_instance(BasePDS, pds) new_storage = SavedPDS(state=SavedPDS.ACTIVE) await new_storage.save(self.context) self.credential = await create_test_credential(issuer) self.cred_id = await self.holder.store_credential({}, self.credential, {}) requested_credentials["credential_id"] = self.cred_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_connections_receive_invitation(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, } mock_req.json = async_mock.CoroutineMock() mock_req.query = { "auto_accept": "true", "alias": "alias", } mock_conn_rec = async_mock.MagicMock() mock_conn_rec.serialize = async_mock.MagicMock() with async_mock.patch.object( test_module.ConnectionInvitation, "deserialize", autospec=True) as mock_inv_deser, async_mock.patch.object( test_module, "ConnectionManager", autospec=True) as mock_conn_mgr, async_mock.patch.object( test_module.web, "json_response") as mock_response: mock_conn_mgr.return_value.receive_invitation = async_mock.CoroutineMock( return_value=mock_conn_rec) await test_module.connections_receive_invitation(mock_req) mock_response.assert_called_once_with( mock_conn_rec.serialize.return_value)
async def test_connections_establish_inbound(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, "outbound_message_router": async_mock.CoroutineMock(), } mock_req.match_info = {"conn_id": "dummy", "ref_id": "ref"} mock_req.query = { "my_endpoint": "http://endpoint.ca", } mock_conn_rec = async_mock.MagicMock() with async_mock.patch.object( test_module.ConnectionRecord, "retrieve_by_id", async_mock.CoroutineMock( )) as mock_conn_rec_retrieve_by_id, async_mock.patch.object( test_module, "ConnectionManager", autospec=True) as mock_conn_mgr, async_mock.patch.object( test_module.web, "json_response") as mock_response: mock_conn_rec_retrieve_by_id.return_value = mock_conn_rec mock_conn_mgr.return_value.establish_inbound = async_mock.CoroutineMock( ) await test_module.connections_establish_inbound(mock_req) mock_response.assert_called_once_with({})
async def test_connections_list_x(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, } mock_req.query = { "their_role": ConnRecord.Role.REQUESTER.rfc160, "alias": "my connection", "state": ConnRecord.State.COMPLETED.rfc23, } STATE_COMPLETED = ConnRecord.State.COMPLETED ROLE_REQUESTER = ConnRecord.Role.REQUESTER with async_mock.patch.object(test_module, "ConnRecord", autospec=True) as mock_conn_rec: mock_conn_rec.Role = async_mock.MagicMock( return_value=ROLE_REQUESTER) mock_conn_rec.State = async_mock.MagicMock( COMPLETED=STATE_COMPLETED, get=async_mock.MagicMock( return_value=ConnRecord.State.COMPLETED), ) mock_conn_rec.query = async_mock.CoroutineMock( side_effect=test_module.StorageError()) with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_list(mock_req)
async def test_introduction_start_no_service(self): context = RequestContext(base_context=InjectionContext(enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = {"request_context": context, "outbound_message_router": None} mock_req.json = async_mock.CoroutineMock( return_value={ "my_seed": "my_seed", "my_did": "my_did", "their_seed": "their_seed", "their_did": "their_did", "their_verkey": "their_verkey", "their_endpoint": "their_endpoint", "their_role": "their_role", "alias": "alias", } ) mock_req.match_info = {"conn_id": "dummy"} mock_req.query = { "target_connection_id": "dummy", "message": "Hello", } with self.assertRaises(test_module.web.HTTPForbidden): await test_module.introduction_start(mock_req)
async def test_connections_establish_inbound_x(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, "outbound_message_router": async_mock.CoroutineMock(), } mock_req.match_info = {"conn_id": "dummy", "ref_id": "ref"} mock_req.query = { "my_endpoint": "http://endpoint.ca", } mock_conn_rec = async_mock.MagicMock() with async_mock.patch.object( test_module.ConnectionRecord, "retrieve_by_id", async_mock.CoroutineMock( )) as mock_conn_rec_retrieve_by_id, async_mock.patch.object( test_module, "ConnectionManager", autospec=True) as mock_conn_mgr: mock_conn_rec_retrieve_by_id.return_value = mock_conn_rec mock_conn_mgr.return_value.establish_inbound = async_mock.CoroutineMock( side_effect=test_module.ConnectionManagerError()) with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_establish_inbound(mock_req)
async def test_connections_receive_invitation_bad(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, } mock_req.json = async_mock.CoroutineMock() mock_req.query = { "auto_accept": "true", "alias": "alias", } mock_conn_rec = async_mock.MagicMock() mock_conn_rec.serialize = async_mock.MagicMock() with async_mock.patch.object( test_module.ConnectionInvitation, "deserialize", autospec=True) as mock_inv_deser, async_mock.patch.object( test_module, "ConnectionManager", autospec=True) as mock_conn_mgr: mock_inv_deser.side_effect = test_module.BaseModelError() with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_receive_invitation(mock_req)
async def setUp(self): self.context = RequestContext( base_context=InjectionContext(enforce_typing=False) ) self.context.connection_ready = True self.context.connection_record = ConnectionRecord(connection_id="conn-id") self.context.message_receipt = MessageReceipt(sender_verkey=TEST_VERKEY) self.context.injector.bind_instance(BaseStorage, BasicStorage())
async def test_connections_receive_invitation_forbidden(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) context.update_settings({"admin.no_receive_invites": True}) mock_req = async_mock.MagicMock() with self.assertRaises(test_module.web.HTTPForbidden): await test_module.connections_receive_invitation(mock_req)
async def test_connections_create_static(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, } mock_req.json = async_mock.CoroutineMock( return_value={ "my_seed": "my_seed", "my_did": "my_did", "their_seed": "their_seed", "their_did": "their_did", "their_verkey": "their_verkey", "their_endpoint": "their_endpoint", "their_role": "their_role", "alias": "alias", }) mock_req.query = { "auto_accept": "true", "alias": "alias", } mock_req.match_info = {"conn_id": "dummy"} mock_conn_rec = async_mock.MagicMock() mock_conn_rec.serialize = async_mock.MagicMock() mock_my_info = async_mock.MagicMock() mock_my_info.did = "my_did" mock_my_info.verkey = "my_verkey" mock_their_info = async_mock.MagicMock() mock_their_info.did = "their_did" mock_their_info.verkey = "their_verkey" with async_mock.patch.object( test_module, "ConnectionManager", autospec=True) as mock_conn_mgr, async_mock.patch.object( test_module.web, "json_response") as mock_response: mock_conn_mgr.return_value.create_static_connection = ( async_mock.CoroutineMock(return_value=(mock_my_info, mock_their_info, mock_conn_rec))) await test_module.connections_create_static(mock_req) mock_response.assert_called_once_with({ "my_did": mock_my_info.did, "my_verkey": mock_my_info.verkey, "their_did": mock_their_info.did, "their_verkey": mock_their_info.verkey, "my_endpoint": context.settings.get("default_endpoint"), "record": mock_conn_rec.serialize.return_value, })
async def setUp(self): self.context = RequestContext(base_context=InjectionContext( enforce_typing=False)) self.context.message_receipt = MessageReceipt( sender_verkey=TEST_VERKEY) self.storage = BasicStorage() self.context.injector.bind_instance(BaseStorage, self.storage) self.manager = RoutingManager(self.context) assert self.manager.context
async def setUp(self): self.context = RequestContext(base_context=InjectionContext( enforce_typing=False)) self.context.connection_ready = True self.context.message = InvitationRequest( responder="test-agent", message="Hello World", ) self.context.update_settings({"accept_requests": False})
async def setUp(self): self.context = InjectionContext(enforce_typing=False) self.storage = async_mock.create_autospec(BaseStorage) self.context.injector.bind_instance(BaseStorage, self.storage) self.wallet = self.wallet = BasicWallet() self.did_info = await self.wallet.create_local_did() self.context.injector.bind_instance(BaseWallet, self.wallet) self.app = { "request_context": self.context, }
async def setUp(self): self.storage = BasicStorage() self.context = RequestContext( base_context=InjectionContext(enforce_typing=False) ) self.context.injector.bind_instance(BaseStorage, self.storage) self.context.connection_ready = True self.context.message = Forward(to="sample-did", msg={"msg": "sample-message"})
async def testSend(self): context = RequestContext(base_context=InjectionContext()) requestMock = async_mock.MagicMock() requestMock.app = { "request_context": context, "outbound_message_router": async_mock.CoroutineMock(), } params = {"connection_id": "1234", "payload": "test, test"} hash_id = hashlib.sha256(params["payload"].encode("UTF-8")).hexdigest() requestMock.json = async_mock.CoroutineMock(return_value=params)
async def test_controller(self): self.context = RequestContext(base_context=InjectionContext( enforce_typing=False)) MenuService = async_mock.MagicMock(test_module.BaseMenuService, autospec=True) self.menu_service = MenuService() self.context.injector.bind_instance(test_module.BaseMenuService, self.menu_service) self.context.inject = async_mock.CoroutineMock( return_value=self.menu_service) controller = test_module.Controller("protocol") assert await controller.determine_roles(self.context) == ["provider"]
async def setup(context: InjectionContext): """Setup plugin.""" protocol_registry = context.inject(ProtocolRegistry) assert protocol_registry protocol_registry.register_message_types( { Status.message_type: Status, StatusRequest.message_type: StatusRequest, Delivery.message_type: Delivery, DeliveryRequest.message_type: DeliveryRequest, MessagesReceived.message_type: MessagesReceived, LiveDeliveryChange.message_type: LiveDeliveryChange, } )
async def test_connections_list(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) context.default_endpoint = "http://1.2.3.4:8081" # for coverage assert context.default_endpoint == "http://1.2.3.4:8081" # for coverage mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, } mock_req.query = { "invitation_id": "dummy", "initiator": ConnectionRecord.INITIATOR_SELF, } with async_mock.patch.object(test_module, "ConnectionRecord", autospec=True) as mock_conn_rec: mock_conn_rec.STATE_INVITATION = ConnectionRecord.STATE_INVITATION mock_conn_rec.STATE_INACTIVE = ConnectionRecord.STATE_INACTIVE mock_conn_rec.query = async_mock.CoroutineMock() conns = [ # in order here async_mock.MagicMock(serialize=async_mock.MagicMock( return_value={ "state": ConnectionRecord.STATE_ACTIVE, "created_at": "1234567890", })), async_mock.MagicMock(serialize=async_mock.MagicMock( return_value={ "state": ConnectionRecord.STATE_INVITATION, "created_at": "1234567890", })), async_mock.MagicMock(serialize=async_mock.MagicMock( return_value={ "state": ConnectionRecord.STATE_INACTIVE, "created_at": "1234567890", })), ] mock_conn_rec.query.return_value = [conns[2], conns[0], conns[1]] # jumbled with async_mock.patch.object(test_module.web, "json_response") as mock_response: await test_module.connections_list(mock_req) mock_response.assert_called_once_with({ "results": [{ k: c.serialize.return_value[k] for k in ["state", "created_at"] } for c in conns] } # sorted )
async def test_connections_retrieve_not_found(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, } mock_req.match_info = {"conn_id": "dummy"} with async_mock.patch.object( test_module.ConnectionRecord, "retrieve_by_id", async_mock.CoroutineMock()) as mock_conn_rec_retrieve_by_id: mock_conn_rec_retrieve_by_id.side_effect = StorageNotFoundError() with self.assertRaises(test_module.web.HTTPNotFound): await test_module.connections_retrieve(mock_req)
async def setUp(self): self.context = RequestContext(base_context=InjectionContext( enforce_typing=False)) self.context.connection_ready = True self.context.message = ForwardInvitation( 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", ) self.context.update_settings({"accept_invites": False})
async def testSaveAndRetrieve(self): context = InjectionContext() storage = BasicStorage() context.injector.bind_instance(BaseStorage, storage) record = SchemaExchangeRecord( payload=self.payload, author=self.author, ) record_id = await record.save(context) assert record_id == self.hash_id self.assert_record(record) query = await SchemaExchangeRecord.retrieve_by_id( context, self.hash_id) self.assert_record(query) assert query == record
async def test_connections_create_invitation_public_forbidden(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() context.update_settings({"public_invites": False}) mock_req.app = { "request_context": context, } mock_req.query = { "auto_accept": "true", "alias": "alias", "public": "true", "multi_use": "true", } with self.assertRaises(test_module.web.HTTPForbidden): await test_module.connections_create_invitation(mock_req)
async def test_introduction_start(self): context = RequestContext(base_context=InjectionContext(enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = {"request_context": context, "outbound_message_router": None} mock_req.json = async_mock.CoroutineMock( return_value={ "my_seed": "my_seed", "my_did": "my_did", "their_seed": "their_seed", "their_did": "their_did", "their_verkey": "their_verkey", "their_endpoint": "their_endpoint", "their_role": "their_role", "alias": "alias", } ) mock_req.match_info = {"conn_id": "dummy"} mock_req.query = { "target_connection_id": "dummy", "message": "Hello", } mock_conn_rec = async_mock.MagicMock() mock_conn_rec.serialize = async_mock.MagicMock() with async_mock.patch.object( context, "inject", async_mock.CoroutineMock() ) as mock_ctx_inject, async_mock.patch.object( test_module.web, "json_response" ) as mock_response: mock_ctx_inject.return_value = async_mock.MagicMock( start_introduction=async_mock.CoroutineMock() ) await test_module.introduction_start(mock_req) mock_ctx_inject.return_value.start_introduction.assert_called_once_with( mock_req.match_info["conn_id"], mock_req.query["target_connection_id"], mock_req.query["message"], mock_req.app["outbound_message_router"], ) mock_response.assert_called_once_with({})
async def test_connections_remove_x(self): context = RequestContext(base_context=InjectionContext( enforce_typing=False)) mock_req = async_mock.MagicMock() mock_req.app = { "request_context": context, } mock_req.match_info = {"conn_id": "dummy"} mock_conn_rec = async_mock.MagicMock( delete_record=async_mock.CoroutineMock( side_effect=test_module.StorageError())) with async_mock.patch.object( test_module.ConnectionRecord, "retrieve_by_id", async_mock.CoroutineMock()) as mock_conn_rec_retrieve_by_id: mock_conn_rec_retrieve_by_id.return_value = mock_conn_rec with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_remove(mock_req)