Beispiel #1
0
def test_emit_validiation_is_ignored_if_validation_flag_is_false(
        super_method_mock: mock.Mock, server_info: Info, faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={"type": "number"},
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, False, True, [], None, None)

    event_args = [faker.pystr()]  # invalid args
    server.emit(event_name, *event_args, namespace=namespace)

    # super method called because validation was skipped
    super_method_mock.assert_called_once_with(event_name,
                                              *event_args,
                                              namespace=namespace)
Beispiel #2
0
def test_emit_event_with_tuple_payload_is_treated_as_multiple_args(
        super_method_mock: mock.Mock, server_info: Info, faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={
                        "type": "array",
                        "prefixItems": [
                            {
                                "type": "number"
                            },
                            {
                                "type": "string"
                            },
                        ],
                    },
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)
    payload = (faker.pyint(), faker.pystr())
    server.emit(event_name, payload, namespace=namespace)
    super_method_mock.assert_called_once_with(event_name,
                                              payload,
                                              namespace=namespace)
Beispiel #3
0
def test_emit_event_with_tuple_payload_fails_array_schema_validation(
        server_info: Info, faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    },
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    payload = ("foo", "bar")  # valid element types, but invalid container type
    with pytest.raises(PayloadValidationException):
        server.emit(event_name, payload, namespace=namespace)
Beispiel #4
0
def test_emit_event_with_array_payload_is_treated_as_single_arg(
        super_method_mock: mock.Mock, server_info: Info, faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={
                        "type": "array",
                        "items": {
                            "type": "number"
                        }
                    },
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)
    payload = faker.pylist(value_types=[int])
    server.emit(event_name, payload, namespace=namespace)
    super_method_mock.assert_called_once_with(event_name,
                                              payload,
                                              namespace=namespace)
Beispiel #5
0
def test_emit_valid_event_invokes_super_method(super_method_mock: mock.Mock,
                                               server_info: Info,
                                               faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={"type": "string"},
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    event_args = [faker.pystr()]
    server.emit(event_name, *event_args, namespace=namespace)
    super_method_mock.assert_called_once_with(event_name,
                                              *event_args,
                                              namespace=namespace)
Beispiel #6
0
def test_emit_event_not_defined_under_given_valid_namespace_raises_validation_exc(
    server_info: Info,
    faker: Faker,
):
    namespace = f"/{faker.pystr()}"
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=faker.pystr(),
                    payload={"type": "object"},
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    with pytest.raises(ValidationException):
        # Correct namespace but undefined event:
        server.emit(faker.pystr(),
                    faker.pydict(value_types=[str, int]),
                    namespace=namespace)
Beispiel #7
0
def test_emit_event_wraps_callback_with_validator(super_method_mock: mock.Mock,
                                                  server_info: Info,
                                                  faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={"type": "number"},
                    x_ack=MessageAck(args={"type": "boolean"}),
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    def actual_callback(*args):
        # dummy callback

        pass

    server.emit(event_name,
                faker.pyint(),
                namespace=namespace,
                callback=actual_callback)
    super_method_mock.assert_called_once()
    *_, kwargs = super_method_mock.call_args
    callback_with_validation = kwargs["callback"]

    callback_args = [faker.pystr()
                     ]  # invalid callback args (should have been a boolean)

    # actual callback has no validation -- hence it does not fail
    actual_callback(*callback_args)

    with pytest.raises(MessageAckValidationException):
        callback_with_validation(*callback_args)
Beispiel #8
0
def test_emit_event_with_invalid_args_fails_validation(server_info: Info,
                                                       faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    spec = AsyncApiSpec(
        asyncapi=faker.pystr(),
        info=server_info,
        channels={
            namespace:
            Channel(subscribe=Operation(message=OneOfMessages(one_of=[
                Message(
                    name=event_name,
                    payload={"type": "number"},
                )
            ]), ))
        },
    )
    server = AsynctionSocketIO(spec, True, True, [], None, None)

    with pytest.raises(PayloadValidationException):
        # Event args do not adhere to the schema
        server.emit(event_name, faker.pystr(), namespace=namespace)
Beispiel #9
0
def test_emit_event_that_has_no_subscribe_operation_raises_validation_exc(
        server_info: Info, faker: Faker):
    namespace = f"/{faker.pystr()}"
    event_name = faker.pystr()
    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)

    with pytest.raises(ValidationException):
        server.emit(event_name,
                    faker.pydict(value_types=[str, int]),
                    namespace=namespace)