Ejemplo n.º 1
0
def test_register_handlers_skips_payload_validator_if_validation_is_disabled(
    server_info: Info,
    faker: Faker,
):
    namespace = f"/{faker.pystr()}"
    event_name = faker.word()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(publish=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={"type": "string"},
                    x_handler="tests.fixtures.handlers.ping",
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, False, True, [], None, None)

    server._register_handlers()
    _, registered_handler, _ = server.handlers[0]
    handler_with_validation = deep_unwrap(registered_handler, depth=1)
    actual_handler = deep_unwrap(handler_with_validation)

    assert handler_with_validation == actual_handler
    args = (faker.pyint(), )
    handler_with_validation(*args)  # handler does not raise validation errors
    assert True
Ejemplo n.º 2
0
def test_register_handlers_registers_channel_handlers(
    server_info: Info,
    faker: Faker,
):
    namespace = f"/{faker.pystr()}"
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(x_handlers=ChannelHandlers(
                connect="tests.fixtures.handlers.connect",
                disconnect="tests.fixtures.handlers.disconnect",
                error="tests.fixtures.handlers.some_error",
            ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    server._register_handlers()

    assert server.exception_handlers[namespace] == some_error
    for event_name, handler, handler_namespace in server.handlers:
        assert handler_namespace == namespace
        unwrapped = deep_unwrap(handler)
        if event_name == "connect":
            assert unwrapped == connect
        else:
            assert unwrapped == disconnect
Ejemplo n.º 3
0
def test_register_handlers_registers_callables_with_correct_event_name_and_namespace(
    server_info: Info,
    faker: Faker,
):
    namespace = f"/{faker.pystr()}"
    event_name = faker.word()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(publish=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={"type": "object"},
                    x_handler="tests.fixtures.handlers.ping",
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    server._register_handlers()
    assert len(server.handlers) == 2  # connection handler is also registered
    ping_handler_entry, connect_handler_entry = server.handlers

    registered_event, registered_handler, registered_namespace = ping_handler_entry
    assert registered_event == event_name
    assert deep_unwrap(registered_handler) == ping
    assert registered_namespace == namespace

    connection_event, connection_handler, registered_namespace = connect_handler_entry
    assert connection_event == "connect"
    assert deep_unwrap(connection_handler) == _noop_handler
    assert registered_namespace == namespace
Ejemplo n.º 4
0
def test_register_handlers_registers_default_error_handler(
        optional_error_handler: Optional[ErrorHandler], server_info: Info,
        faker: Faker):
    server = AsynctionSocketIO(
        AsyncApiSpec(asyncapi=faker.pystr(), info=server_info, channels={}),
        True,
        True,
        [],
        optional_error_handler,
        None,
    )

    server._register_handlers()
    assert server.default_exception_handler == optional_error_handler
Ejemplo n.º 5
0
def test_register_handlers_adds_ack_validator_if_validation_is_enabled(
        server_info: Info, faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.word()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(publish=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={"type": "string"},
                    x_handler="tests.fixtures.handlers.ping_with_ack",
                    x_ack=MessageAck(
                        args={
                            "type": "object",
                            "properties": {
                                "ack": {
                                    "type": "number"
                                }
                            },
                            "required": ["ack"],
                        }),
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    server._register_handlers()
    _, registered_handler, _ = server.handlers[0]
    handler_with_validation = deep_unwrap(registered_handler, depth=1)
    actual_handler = deep_unwrap(handler_with_validation)
    args = (faker.pystr(), )  # valid handler args

    # actual handler does not raise validation errors, although it returns invalid data
    actual_handler(*args)

    with pytest.raises(MessageAckValidationException):
        handler_with_validation(*args)