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)
class TestInvitationRequestHandler(AsyncTestCase): 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 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_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_handle_not_ready(self): handler = test_module.InvitationRequestHandler() self.context.connection_ready = False with self.assertRaises(HandlerException): await handler.handle(self.context, None)
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 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_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(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_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_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_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_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_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_connections_list(self): context = RequestContext(base_context=InjectionContext(enforce_typing=False)) 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 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 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_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 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.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 test_connections_create_invitation(self): context = RequestContext(base_context=InjectionContext(enforce_typing=False)) mock_req = async_mock.MagicMock() context.update_settings({"public_invites": True}) mock_req.app = { "request_context": context, } mock_req.query = { "auto_accept": "true", "alias": "alias", "public": "true", "multi_use": "true", } 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_invitation = async_mock.CoroutineMock( return_value=( async_mock.MagicMock( # connection record connection_id="dummy", alias="conn-alias" ), async_mock.MagicMock( # invitation serialize=async_mock.MagicMock(return_value={"a": "value"}), to_url=async_mock.MagicMock(return_value="http://endpoint.ca"), ), ) ) await test_module.connections_create_invitation(mock_req) mock_response.assert_called_once_with( { "connection_id": "dummy", "invitation": {"a": "value"}, "invitation_url": "http://endpoint.ca", "alias": "conn-alias", } )
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 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 test_connections_create_invitation_x(self): context = RequestContext(base_context=InjectionContext(enforce_typing=False)) mock_req = async_mock.MagicMock() context.update_settings({"public_invites": True}) mock_req.app = { "request_context": context, } mock_req.query = { "auto_accept": "true", "alias": "alias", "public": "true", "multi_use": "true", } with async_mock.patch.object( test_module, "ConnectionManager", autospec=True ) as mock_conn_mgr: mock_conn_mgr.return_value.create_invitation = async_mock.CoroutineMock( side_effect=test_module.ConnectionManagerError() ) with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_create_invitation(mock_req)
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 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_retrieve_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() mock_conn_rec.serialize = async_mock.MagicMock( side_effect=test_module.BaseModelError() ) 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_retrieve(mock_req)
async def test_connections_retrieve(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() mock_conn_rec.serialize = async_mock.MagicMock(return_value={"hello": "world"}) 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.web, "json_response" ) as mock_response: mock_conn_rec_retrieve_by_id.return_value = mock_conn_rec await test_module.connections_retrieve(mock_req) mock_response.assert_called_once_with({"hello": "world"})
async def test_connections_create_static_x(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: mock_conn_mgr.return_value.create_static_connection = async_mock.CoroutineMock( side_effect=test_module.WalletError() ) with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_create_static(mock_req)
async def test_connections_accept_invitation_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"} 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_mgr.return_value.create_request = async_mock.CoroutineMock( side_effect=test_module.ConnectionManagerError() ) with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_accept_invitation(mock_req)
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 = 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", ) self.context.connection_record = async_mock.MagicMock(connection_id="dummy")
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 = { "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( side_effect=test_module.StorageError() ) with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.connections_list(mock_req)
async def test_introduction_start_x(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: mock_ctx_inject.return_value = async_mock.MagicMock( start_introduction=async_mock.CoroutineMock( side_effect=test_module.IntroductionError())) with self.assertRaises(test_module.web.HTTPBadRequest): await test_module.introduction_start(mock_req)