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
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)
def test_register_handlers_registers_connection_handler_with_bindings_validation( server_info: Info, faker: Faker, ): namespace = f"/{faker.pystr()}" spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={ namespace: Channel(bindings=ChannelBindings( ws=WebSocketsChannelBindings(method="GET", )), ) }, ) server = new_mock_asynction_socket_io(spec) flask_app = Flask(__name__) server._register_handlers() _, registered_handler, _ = server.handlers[0] handler_with_validation = deep_unwrap(registered_handler, depth=1) actual_handler = deep_unwrap(registered_handler) with flask_app.test_client() as c: with patch.object(server, "start_background_task"): c.post() # Inject invalid POST request actual_handler() # actual handler does not raise validation errors with pytest.raises(BindingsValidationException): handler_with_validation()
def test_register_handlers_adds_payload_validator_if_validation_is_enabled( server_info: Info, faker: Faker, ): namespace = f"/{faker.pystr()}" spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={ namespace: Channel(publish=Operation(message=OneOfMessages(one_of=[ Message( name=faker.word(), payload={"type": "string"}, x_handler=faker.pystr(), ) ]), )) }, ) server = new_mock_asynction_socket_io(spec) 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.pyint(), ) actual_handler(*args) # actual handler does not raise validation errors with pytest.raises(PayloadValidationException): handler_with_validation(*args)
def test_register_handlers_registers_noop_handler_for_message_with_no_ack( 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=faker.pystr(), ) ]), )) }, ) server = new_mock_asynction_socket_io(spec) server._register_handlers() assert len(server.handlers) == 2 # connect handler included as well registered_event, registered_handler, registered_namespace = server.handlers[ 0] assert registered_event == event_name assert registered_namespace == namespace handler = deep_unwrap(registered_handler) assert handler == _noop_handler
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
def test_make_subscription_task_with_no_message_payload_but_ack( server_info: Info, faker: Faker): namespace = f"/{faker.pystr()}" message = Message( name=faker.word(), x_ack=MessageAck(args={ "type": "string", "enum": [faker.pystr(), faker.pystr()], }), ) spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={ namespace: Channel(subscribe=Operation(message=OneOfMessages( one_of=[message]), )) }, ) server = new_mock_asynction_socket_io(spec) task = server.make_subscription_task(message=message, namespace=namespace) with patch.object(server, "emit") as emit_mock: task() emit_mock.assert_called_once_with(message.name, None, namespace=namespace, callback=_noop_handler)
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
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)
def test_make_subscription_task_with_message_payload_but_no_ack( server_info: Info, faker: Faker): namespace = f"/{faker.pystr()}" message = Message( name=faker.word(), payload={ "type": "string", "enum": [faker.pystr(), faker.pystr()], }, ) spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={ namespace: Channel(subscribe=Operation(message=OneOfMessages( one_of=[message]), )) }, ) server = new_mock_asynction_socket_io(spec) task = server.make_subscription_task(message=message, namespace=namespace) with patch.object(server, "emit") as emit_mock: task() emit_mock.assert_called_once_with(message.name, ANY, namespace=namespace, callback=None) _, data = emit_mock.call_args[0] jsonschema.validate(data, message.payload) assert True
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)
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)
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)
def test_start_background_daemon_task_with_threading_async_mode( server_info: Info, faker: Faker): spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={f"/{faker.pystr()}": Channel()}, ) server = new_mock_asynction_socket_io(spec, app=Flask(__name__), async_mode="threading") def target(): # noop pass args = tuple(faker.pylist()) kwargs = faker.pydict() with patch.object(threading, "Thread") as mock_thread_cls: t = server.start_background_task(target, *args, **kwargs) mock_thread_cls.assert_called_once_with(target=target, args=args, kwargs=kwargs, daemon=True) t.start.assert_called_once_with() # type: ignore
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)
def test_register_handlers_registers_connection_handler( server_info: Info, faker: Faker): namespace = f"/{faker.pystr()}" spec = AsyncApiSpec(asyncapi=faker.pystr(), info=server_info, channels={namespace: Channel()}) server = new_mock_asynction_socket_io(spec) server._register_handlers() assert len(server.handlers) == 1 registered_event, registered_handler, registered_namespace = server.handlers[ 0] assert registered_event == "connect" assert deep_unwrap(registered_handler) == _noop_handler assert registered_namespace == namespace
def test_register_handlers_registers_valid_handler_for_message_with_ack( server_info: Info, faker: Faker): namespace = f"/{faker.pystr()}" event_name = faker.word() ack_schema = { "type": "object", "properties": { "foo": { "type": "string", "enum": [faker.pystr(), faker.pystr()], }, "bar": { "type": "number", "minimum": 10, "maximum": 20, }, }, "required": ["foo", "bar"], } 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=faker.pystr(), x_ack=MessageAck(args=ack_schema), ) ]), )) }, ) server = new_mock_asynction_socket_io(spec) server._register_handlers() assert len(server.handlers) == 2 # connect handler included as well registered_event, registered_handler, registered_namespace = server.handlers[ 0] assert registered_event == event_name assert registered_namespace == namespace handler = deep_unwrap(registered_handler) ack = handler(faker.pydict()) jsonschema.validate(ack, ack_schema) assert True
def test_make_subscription_task_with_message_payload_and_ack( server_info: Info, faker: Faker): namespace = f"/{faker.pystr()}" message = Message( name=faker.word(), payload={ "type": "object", "properties": { "foo": { "type": "string", "enum": [faker.pystr(), faker.pystr()], }, "bar": { "type": "number", "minimum": 10, "maximum": 20, }, }, "required": ["foo", "bar"], }, x_ack=MessageAck(args={ "type": "string", "enum": [faker.pystr(), faker.pystr()], }), ) spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={ namespace: Channel(subscribe=Operation(message=OneOfMessages( one_of=[message]), )) }, ) server = new_mock_asynction_socket_io(spec) task = server.make_subscription_task(message=message, namespace=namespace) with patch.object(server, "emit") as emit_mock: task() emit_mock.assert_called_once_with(message.name, ANY, namespace=namespace, callback=_noop_handler) _, data = emit_mock.call_args[0] jsonschema.validate(data, message.payload) assert True
def test_register_namespace_handlers_includes_server_security_validation(): channel_handlers = ChannelHandlers( connect="tests.fixtures.handlers.connect") spec = AsyncApiSpec( asyncapi="2.3.0", info=Info("test", "1.0.0"), servers={ "test": Server("https://localhost/", ServerProtocol.WSS, [{ "basic": [] }]) }, channels={GLOBAL_NAMESPACE: Channel(x_handlers=channel_handlers)}, components=Components( security_schemes={ "basic": SecurityScheme( type=SecuritySchemesType.HTTP, scheme=HTTPAuthenticationScheme.BASIC, x_basic_info_func="tests.fixtures.handlers.basic_info", ) }), ) server = AsynctionSocketIO(spec, False, True, spec.servers.get("test").security, None, None) server._register_namespace_handlers( GLOBAL_NAMESPACE, channel_handlers, None, None, # No channel security requirements ) event_name, registered_handler, _ = server.handlers[0] assert event_name == "connect" handler_with_security = deep_unwrap(registered_handler, depth=1) actual_handler = deep_unwrap(handler_with_security) with Flask(__name__).test_client() as c: c.post() # Inject invalid POST request actual_handler() with pytest.raises(SecurityException): handler_with_security() # handler raises security exception assert True
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)
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)
def test_init_app_does_not_register_handlers_if_app_is_none( 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, False, False, [], None, None) server.init_app(app=None) assert not server.handlers
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)
def test_run_spawns_background_tasks_and_calls_super_run( 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.word(), payload={"type": "string"}, ) ]), )) }, ) flask_app = Flask(__name__) server = new_mock_asynction_socket_io(spec, flask_app) background_tasks: MutableSequence[MockThread] = [] def start_background_task_mock(target, *args, **kwargs): mt = MockThread(target=target, args=args, kwargs=kwargs) background_tasks.append(mt) return mt with patch.object(SocketIO, "run") as super_run_mock: with patch.object(server, "start_background_task", start_background_task_mock): server.run(flask_app) assert len(background_tasks) == 2 assert background_tasks[0].target == task_runner assert background_tasks[-1].target == task_scheduler super_run_mock.assert_called_once_with(flask_app, host=None, port=None)
def test_start_background_daemon_task_with_non_threading_async_mode( server_info: Info, faker: Faker): spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={f"/{faker.pystr()}": Channel()}, ) server = new_mock_asynction_socket_io(spec, app=Flask(__name__)) server.async_mode = "gevent" def target(): # noop pass args = tuple(faker.pylist()) kwargs = faker.pydict() with patch.object(AsynctionSocketIO, "start_background_task") as super_mock: server.start_background_task(target, *args, **kwargs) super_mock.assert_called_once_with(target, *args, **kwargs)
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)
def test_channel_raises_value_error_if_publish_messages_miss_handler( faker: Faker): with pytest.raises(ValueError): Channel( subscribe=Operation(message=OneOfMessages(one_of=[ Message( name=faker.pystr(), payload=faker.pydict(value_types=[str, int]), ) ])), publish=Operation(message=OneOfMessages(one_of=[ *[ Message( name=faker.pystr(), payload=faker.pydict(value_types=[str, int]), x_handler=faker.pystr(), ) for _ in range(faker.pyint(min_value=2, max_value=10)) ], Message( name=faker.pystr(), payload=faker.pydict(value_types=[str, int]), ), ])), )
def test_run_spawns_minimum_number_of_workers(server_info: Info, faker: Faker): max_worker_number = faker.pyint(min_value=8, max_value=15) sub_messages_number = max_worker_number - faker.pyint(min_value=3, max_value=5) namespace = f"/{faker.pystr()}" spec = AsyncApiSpec( asyncapi=faker.pystr(), info=server_info, channels={ namespace: Channel(subscribe=Operation(message=OneOfMessages(one_of=[ Message( name=faker.word(), payload={"type": "string"}, ) for _ in range(sub_messages_number) ]), )) }, ) background_tasks: MutableSequence[MockThread] = [] def start_background_task_mock(target, *args, **kwargs): mt = MockThread(target=target, args=args, kwargs=kwargs) background_tasks.append(mt) return mt flask_app = Flask(__name__) server = new_mock_asynction_socket_io(spec, flask_app) with patch.object(SocketIO, "run"): with patch.object(server, "start_background_task", start_background_task_mock): server.run(flask_app, max_worker_number=max_worker_number) assert len(background_tasks) == sub_messages_number + 1