def METHOD_NAME(grant_target_name, user_name, table_type, node=None):
    """Run scenarios to check the user's access with different privileges.
    """
    exitcode, message = errors.not_enough_privileges(name=f"{user_name}")
    with Scenario("user without privilege"):
        table_name = f"merge_tree_{getuid()}"
        with table(node, table_name, table_type):
            with When("I grant the user NONE privilege"):
                node.query(f"GRANT NONE TO {grant_target_name}")
            with And("I grant the user USAGE privilege"):
                node.query(f"GRANT USAGE ON *.* TO {grant_target_name}")
            with Then("I attempt to optimize a table without privilege"):
                node.query(f"OPTIMIZE TABLE {table_name} FINAL", settings = [("user", user_name)],
                    exitcode=exitcode, message=message)
    with Scenario("user with privilege"):
        table_name = f"merge_tree_{getuid()}"
        with table(node, table_name, table_type):
            with When("I grant the optimize privilege"):
                node.query(f"GRANT OPTIMIZE ON {table_name} TO {grant_target_name}")
            with Then("I attempt to optimize a table"):
                node.query(f"OPTIMIZE TABLE {table_name}", settings = [("user", user_name)])
    with Scenario("user with revoked privilege"):
        table_name = f"merge_tree_{getuid()}"
        with table(node, table_name, table_type):
            with When("I grant the optimize privilege"):
                node.query(f"GRANT OPTIMIZE ON {table_name} TO {grant_target_name}")
            with And("I revoke the optimize privilege"):
                node.query(f"REVOKE OPTIMIZE ON {table_name} FROM {grant_target_name}")
            with Then("I attempt to optimize a table"):
                node.query(f"OPTIMIZE TABLE {table_name}", settings = [("user", user_name)],
                    exitcode=exitcode, message=message)
    with Scenario("user with revoked ALL privilege"):
        table_name = f"merge_tree_{getuid()}"
        with table(node, table_name, table_type):
            with When("I grant the optimize privilege"):
                node.query(f"GRANT OPTIMIZE ON {table_name} TO {grant_target_name}")
            with And("I revoke ALL privilege"):
                node.query(f"REVOKE ALL ON *.* FROM {grant_target_name}")
            with Then("I attempt to optimize a table"):
                node.query(f"OPTIMIZE TABLE {table_name}", settings = [("user", user_name)],
                    exitcode=exitcode, message=message)
    with Scenario("execute on cluster"):
        table_name = f"merge_tree_{getuid()}"
        try:
            with Given("I have a table on a cluster"):
                node.query(f"CREATE TABLE {table_name} ON CLUSTER sharded_cluster (d DATE, a String, b UInt8, x String, y Int8) ENGINE = MergeTree() PARTITION BY y ORDER BY d")
            with When("I grant the optimize privilege"):
                node.query(f"GRANT OPTIMIZE ON {table_name} TO {grant_target_name}")
            with Then("I attempt to optimize a table"):
                node.query(f"OPTIMIZE TABLE {table_name} ON CLUSTER sharded_cluster", settings = [("user", user_name)])
        finally:
            with Finally("I drop the table from the cluster"):
                node.query(f"DROP TABLE IF EXISTS {table_name} ON CLUSTER sharded_cluster")
    with Scenario("user with ALL privilege"):
        table_name = f"merge_tree_{getuid()}"
        with table(node, table_name, table_type):
            with When("I revoke ALL privilege"):
                node.query(f"REVOKE ALL ON *.* FROM {grant_target_name}")
            with And("I grant ALL privilege"):
                node.query(f"GRANT ALL ON *.* TO {grant_target_name}")
            with Then("I attempt to optimize a table"):
                node.query(f"OPTIMIZE TABLE {table_name}", settings = [("user", user_name)])