async def test_routing_many(event, dispatcher, conn):
    """Test that routing to a module works."""
    dispatcher.called_module = None

    class TestModule1(Module):
        """Simple module for testing"""

        protocol = "test_protocol/1.0"
        route = ModuleRouter(protocol)

        @route
        async def testing_type(self, _msg, **kwargs):
            """Test that this method is called"""
            kwargs["dispatcher"].called_module = 1
            kwargs["event"].set()

    class TestModule2(Module):
        """Simple module for testing"""

        protocol = "test_protocol/2.0"
        route = ModuleRouter(protocol)

        @route
        async def testing_type(self, _msg, **kwargs):
            """Test that this methodis called"""
            kwargs["dispatcher"].called_module = 2
            kwargs["event"].set()

    conn.route_module(TestModule1())
    conn.route_module(TestModule2())

    test_msg = Message.parse_obj({
        "@type": "test_protocol/1.0/testing_type",
        "test": "test"
    })
    await dispatcher.dispatch(test_msg, event=event, dispatcher=dispatcher)
    await event.wait()

    assert event.is_set()
    assert dispatcher.called_module == 1

    event.clear()

    test_msg = Message.parse_obj({
        "@type": "test_protocol/2.0/testing_type",
        "test": "test"
    })
    await dispatcher.dispatch(test_msg, event=event, dispatcher=dispatcher)
    await event.wait()

    assert event.is_set()
    assert dispatcher.called_module == 2

    assert dispatcher.handlers
    conn.clear_routes()
    assert not dispatcher.handlers
Exemple #2
0
def test_pack_unpack_auth(alice, bob):
    """Test the pack-unpack loop with authcrypt."""
    msg = Message.parse_obj({"@type": "doc;protocol/1.0/name"})
    packed_msg = alice.pack(msg)
    assert isinstance(packed_msg, bytes)

    unpacked_msg = bob.unpack(packed_msg)
    assert isinstance(unpacked_msg, Message)
    assert hasattr(unpacked_msg, "mtc")
    assert unpacked_msg.mtc.is_authcrypted()
    assert unpacked_msg.mtc.sender == alice.verkey_b58
    assert unpacked_msg.mtc.recipient == bob.verkey_b58
async def test_simple_route(event, dispatcher, conn):
    """Test using route decorator on a method."""
    @conn.route("test_protocol/1.0/testing_type")
    async def test(msg, **kwargs):  # pylint: disable=unused-variable
        assert msg["test"] == "test"
        kwargs["event"].set()

    await dispatcher.dispatch(
        Message.parse_obj({
            "@type": "test_protocol/1.0/testing_type",
            "test": "test"
        }),
        event=event,
    )

    assert event.is_set()
async def test_routing_module_explicit_def(event, dispatcher, conn, route_args,
                                           route_kwargs, send_type):
    """Test that routing to a module works."""
    class TestModule(Module):
        """Simple module for testing"""

        protocol = "test_protocol/1.0"
        route = ModuleRouter(protocol)

        @route(*route_args, **route_kwargs)
        async def route_gets_called(self, _msg, **kwargs):
            """Test that this method is called"""
            kwargs["event"].set()

    mod = TestModule()
    conn.route_module(mod)

    test_msg = Message.parse_obj({"@type": send_type, "test": "test"})
    await dispatcher.dispatch(test_msg, event=event)

    assert event.is_set()
async def test_routing_no_matching_version(event, dispatcher, conn):
    """Test error raised on no matching handlers."""
    class TestModule(Module):
        """Simple module for testing"""

        protocol = "test_protocol/1.0"
        route = ModuleRouter(protocol)

        @route
        async def testing_type(self, _msg, **kwargs):
            """Test that this method is called"""
            kwargs["event"].set()

    mod = TestModule()
    conn.route_module(mod)

    test_msg = Message.parse_obj({
        "@type": "test_protocol/3.0/testing_type",
        "test": "test"
    })
    with pytest.raises(NoRegisteredHandlerException):
        await dispatcher.dispatch(test_msg, event=event)
async def test_routing_minor_version_different(event, dispatcher, conn):
    """Test routing when minor version is different."""
    class TestModule(Module):
        """Simple module for testing"""

        protocol = "test_protocol/1.0"
        route = ModuleRouter(protocol)

        @route
        async def testing_type(self, _msg, **kwargs):
            """Test that this method is called"""
            kwargs["event"].set()

    mod = TestModule()
    conn.route_module(mod)

    test_msg = Message.parse_obj({
        "@type": "test_protocol/1.0/testing_type",
        "test": "test"
    })
    await dispatcher.dispatch(test_msg, event=event)

    assert event.is_set()
def response():
    yield Message.parse_obj({"@type": "doc;protocol/1.0/response"})
def message():
    yield Message.parse_obj({"@type": "doc;protocol/1.0/name"})
def message():
    yield Message.parse_obj(
        {"@type": "doc_uri/protocol/0.1/test", "@id": "12345", "content": "test"}
    )