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 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_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_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_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)
예제 #6
0
class TestForwardInvitationHandler(AsyncTestCase):
    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 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_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_handle_not_ready(self):
        handler = test_module.ForwardInvitationHandler()
        self.context.connection_ready = False

        with self.assertRaises(HandlerException):
            await handler.handle(self.context, None)