Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

16.0 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Account boundaries exercised through the real media object routes and stores."""

from __future__ import annotations


import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from PIL import Image

from auth import policy, storage as auth_storage
from auth.authentication import authenticated_via_api_key, get_current_subject
from core.inference import audio_gallery, image_gallery, search_images, video_gallery
from routes import inference, video
from utils.account_context import OWNER, AccountContext, bind_account, reset_account, run_as

ALICE = AccountContext("a" * 32, "alice")
BOB = AccountContext("b" * 32, "bob")


@pytest.fixture(autouse = True)
def isolated(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
    monkeypatch.setattr(policy, "installation_is_multi_user", lambda: True)
    monkeypatch.setattr(search_images, "_account_states", {})
    monkeypatch.setattr(video, "_managed_jobs", {})
    monkeypatch.setattr(auth_storage, "DB_PATH", tmp_path / "auth.db")
    monkeypatch.setattr(auth_storage, "_BOOTSTRAP_PW_PATH", tmp_path / ".bootstrap_password")
    monkeypatch.setattr(auth_storage, "_bootstrap_password", None)
    policy.invalidate_account_cache()
    connection = auth_storage.get_connection()
    with connection:
        for account in (ALICE, BOB):
            connection.execute(
                "INSERT INTO auth_user (username, password_salt, password_hash, jwt_secret,"
                " account_id, role, is_active) VALUES (?, 'salt', 'hash', 'secret', ?, 'user', 1)",
                (account.username, account.account_id),
            )
    connection.close()
    yield
    policy.invalidate_account_cache()


def _save(kind):
    meta = {
        "prompt": "private prompt",
        "model": "private/model",
        "created_at": 100.0,
        "width": 512,
        "height": 512,
        "steps": 5,
        "guidance": 1.0,
        "seed": 1,
        "num_frames": 49,
        "fps": 24,
        "duration_s": 2.0,
    }
    if kind == "images":
        return image_gallery.save(Image.new("RGB", (8, 8)), meta)
    if kind == "audio":
        meta.update(audio_type = "snac", sample_rate = 24000, created_at = "2026-08-06T00:00:00Z")
        return audio_gallery.save(b"RIFF\x24\x00\x00\x00WAVEfmt ", meta)
    meta["created_at"] = "2026-08-06T00:00:00Z"
    return video_gallery.save(b"\x00\x00\x00\x18ftypmp42", meta)


def _client(account):
    app = FastAPI()

    async def subject():
        token = bind_account(account)
        try:
            yield account.username
        finally:
            reset_account(token)

    app.dependency_overrides[get_current_subject] = subject
    app.dependency_overrides[authenticated_via_api_key] = lambda: False
    # Mounted like main.py: the audio file, input and voice routes are on both.
    app.include_router(inference.router, prefix = "/api/inference")
    app.include_router(inference.studio_router, prefix = "/api/inference")
    app.include_router(video.router, prefix = "/api/inference")
    app.include_router(video.openai_router, prefix = "/v1")
    return TestClient(app)


@pytest.mark.parametrize("kind", ["images", "audio", "video"])
@pytest.mark.parametrize("account,other", [(ALICE, BOB), (BOB, ALICE)])
def test_gallery_object_routes_do_not_resolve_another_accounts_ids(kind, account, other):
    record = run_as(account, _save, kind)
    root = f"/api/inference/{kind}/gallery"
    with _client(other) as client:
        assert client.get(f"{root}/{record['id']}/file").status_code == 404
        assert client.patch(f"{root}/{record['id']}", json = {"starred": True}).status_code == 404
        assert client.delete(f"{root}/{record['id']}").status_code == 404
        assert record["id"] not in client.get(root).text
        assert client.get(f"{root}/{record['id']}/signed-url").status_code == 404
        if kind == "video":
            assert client.get(f"{root}/{record['id']}/export").status_code == 404
    with _client(account) as client:
        assert client.get(f"{root}/{record['id']}/file").status_code == 200
        assert record["id"] in client.get(root).text


@pytest.mark.parametrize(
    "kind,module", [("images", image_gallery), ("audio", audio_gallery), ("video", video_gallery)]
)
def test_clear_and_paths_are_account_scoped(kind, module, tmp_path):
    a = run_as(ALICE, _save, kind)
    b = run_as(BOB, _save, kind)
    assert run_as(ALICE, module.clear) == 1
    path_fn = {
        "images": image_gallery.owned_image_path,
        "audio": audio_gallery.owned_audio_path,
        "video": video_gallery.owned_video_path,
    }[kind]
    assert run_as(ALICE, path_fn, a["id"]) is None
    assert run_as(BOB, path_fn, b["id"]).is_file()
    folder = "videos" if kind == "video" else kind
    assert run_as(OWNER, module.gallery_dir) == tmp_path / folder
    assert run_as(BOB, module.gallery_dir) == tmp_path / "accounts" / BOB.account_id / folder


@pytest.mark.parametrize(
    "kind,module,sign,verify",
    [
        ("images", inference, "_sign_image_id", "_verify_image_link_token"),
        ("audio", inference, "_sign_audio_id", "_verify_audio_link_token"),
        ("video", video, "_sign_video_id", "_verify_video_link_token"),
    ],
)
def test_signed_links_authorize_only_the_original_account_and_media(kind, module, sign, verify):
    record = run_as(ALICE, _save, kind)
    sign, verify = getattr(module, sign), getattr(module, verify)
    token = run_as(ALICE, sign, record["id"])
    root = f"/api/inference/{kind}/gallery"
    with _client(BOB) as client:
        assert (
            client.get(f"{root}/{record['id']}/file-signed", params = {"token": token}).status_code
            == 200
        )
        assert client.get(f"{root}/guessed/file-signed", params = {"token": token}).status_code == 401
        tampered = token.replace(ALICE.account_id, BOB.account_id)
        assert (
            client.get(f"{root}/{record['id']}/file-signed", params = {"token": tampered}).status_code
            == 401
        )
    owner_token = run_as(OWNER, sign, record["id"])
    assert owner_token.split(".")[0] == record["id"]
    assert verify(owner_token) == record["id"]


def test_search_ids_and_clear_fences_are_private(monkeypatch):
    raw = [{"thumbnail": "https://example.com/image.jpg", "url": "https://example.com/page"}]
    monkeypatch.setattr(search_images, "_fetch_thumbnail_bytes", lambda *args: b"thumbnail")
    image_id = run_as(ALICE, search_images.register_images, raw)[0]["id"]
    assert run_as(BOB, search_images.lookup_image, image_id) is None
    assert run_as(BOB, search_images.thumbnail_bytes, image_id) is None
    before = run_as(ALICE, search_images.cache_generation)
    run_as(BOB, search_images.snapshot_and_fence_registrations)
    run_as(BOB, search_images.clear_cache)
    assert run_as(ALICE, search_images.cache_generation) == before
    assert run_as(ALICE, search_images.thumbnail_bytes, image_id) == b"thumbnail"
    with _client(BOB) as client:
        assert client.get(f"/api/inference/search-images/{image_id}").status_code == 404
    run_as(ALICE, search_images.clear_cache)
    assert run_as(ALICE, search_images.thumbnail_bytes, image_id) is None


def test_openai_video_jobs_are_private_in_memory_and_after_rehydration():
    job = video._VideoJob(
        id = "private-job",
        created_at = 100,
        prompt = "private",
        model = "private/model",
        size = "512x512",
        seconds = "2",
        status = "failed",
    )
    run_as(ALICE, video._remember_job, job)
    assert run_as(BOB, video._lookup_video, job.id) is None
    assert run_as(BOB, video._all_videos) == []
    assert run_as(ALICE, video._lookup_video, job.id).id == job.id
    video._managed_jobs.clear()
    assert run_as(BOB, video._lookup_video, job.id) is None
    assert run_as(ALICE, video._lookup_video, job.id).id == job.id
    with _client(BOB) as client:
        assert client.get(f"/v1/videos/{job.id}").status_code == 404
        assert client.get(f"/v1/videos/{job.id}/content").status_code == 404
        assert client.delete(f"/v1/videos/{job.id}").status_code == 404


def test_clearing_a_managed_chat_history_reaps_only_its_own_thumbnails(monkeypatch):
    """The clear reaps every account's private thumbnails, not just the owner's."""
    from routes import chat_history

    monkeypatch.setattr(search_images, "_fetch_thumbnail_bytes", lambda *args: b"thumbnail")
    raw = [{"thumbnail": "https://example.com/i.jpg", "url": "https://example.com/p"}]
    ids = {
        account: run_as(account, search_images.register_images, raw)[0]["id"]
        for account in (ALICE, BOB)
    }
    snapshot = run_as(ALICE, chat_history._snapshot_chat_images)
    assert snapshot == {ids[ALICE]}
    run_as(ALICE, search_images.clear_cache, snapshot)
    assert run_as(ALICE, search_images.thumbnail_bytes, ids[ALICE]) is None
    assert run_as(BOB, search_images.thumbnail_bytes, ids[BOB]) == b"thumbnail"


def test_a_failed_replacement_load_leaves_residency_with_the_resident_model(monkeypatch):
    import threading

    from core.inference import gpu_arbiter, video as video_module

    backend = video_module.get_video_backend()
    monkeypatch.setattr(gpu_arbiter, "_owner", None)
    monkeypatch.setattr(gpu_arbiter, "_owner_account", None)
    monkeypatch.setattr(gpu_arbiter, "_prior_account", None, raising = False)
    resident = {**backend.status(), "loaded": True, "repo_id": "alice/private-video"}
    monkeypatch.setattr(backend, "status", lambda: resident)
    monkeypatch.setattr(backend, "_state", object())
    unloaded: list[str] = []
    monkeypatch.setattr(backend, "unload", lambda: unloaded.append("unload") or resident)
    run_as(ALICE, gpu_arbiter.acquire_for, gpu_arbiter.VIDEO, lambda: None)
    run_as(BOB, gpu_arbiter.acquire_for, gpu_arbiter.VIDEO, lambda: None)
    monkeypatch.setattr(
        video_module,
        "_detect_load_family",
        lambda *args, **kwargs: (_ for _ in ()).throw(RuntimeError("boom")),
    )
    run_as(
        BOB,
        backend._run_load,
        repo_id = "bob/model",
        _load_token = backend._load_token,
        _cancel_event = threading.Event(),
    )
    assert gpu_arbiter.owner_account() == ALICE.account_id
    with _client(BOB) as client:
        assert client.get("/api/inference/video/status").json() == {"loaded": True, "yours": False}
        assert client.post("/api/inference/video/unload").status_code == 404
    assert unloaded == []
    with _client(ALICE) as client:
        assert client.get("/api/inference/video/status").json()["repo_id"] == "alice/private-video"


def _save_input(account):
    import asyncio
    import io
    import wave

    from core.inference import audio_inputs

    buf = io.BytesIO()
    with wave.open(buf, "wb") as w:
        w.setparams((1, 2, 16000, 0, "NONE", "not compressed"))
        w.writeframes(b"\x01\x00" * 16000)

    async def chunks():
        yield buf.getvalue()

    return run_as(account, lambda: asyncio.run(audio_inputs.save_stream(chunks(), "me.wav"))[0])


def _save_voice(account):
    from core.inference import audio_inputs, audio_voices
    record = _save_input(account)
    return run_as(
        account,
        lambda: audio_voices.create(audio_inputs.input_path(record["id"]), {"name": "private"}),
    )


@pytest.mark.parametrize("account,other", [(ALICE, BOB), (BOB, ALICE)])
def test_audio_inputs_voices_and_clips_do_not_resolve_another_accounts_ids(
    account, other, monkeypatch
):
    from core.inference import audio_inputs, audio_voices

    async def fake_transcribe(*_args, **_kwargs):
        return {"text": "private words", "language": None}

    monkeypatch.setattr(inference, "_transcribe_audio_result", fake_transcribe)
    record = _save_input(account)
    path = run_as(account, audio_inputs.input_path, record["id"])
    voice = run_as(account, audio_voices.create, path, {"name": "p"})
    clip = run_as(account, _save, "audio")
    inputs = f"/api/inference/audio/inputs/{record['id']}"
    voices = "/api/inference/audio/voices"
    source = "/api/inference/audio/inputs/source/transcribe"
    model = {"model": "m"}
    with _client(other) as client:
        assert client.get(f"{inputs}/file").status_code == 404
        assert client.post(f"{inputs}/transcribe", json = model).status_code == 404
        assert client.delete(inputs).status_code == 404
        assert voice["id"] not in client.get(voices).text
        assert client.get(f"{voices}/{voice['id']}/file").status_code == 404
        assert client.patch(f"{voices}/{voice['id']}", json = {"name": "x"}).status_code == 404
        assert client.delete(f"{voices}/{voice['id']}").status_code == 404
        for params in ({"voice_id": voice["id"]}, {"clip_id": clip["id"]}):
            assert client.post(source, params = params, json = model).status_code == 404
        stolen = {"source": {"clip_id": clip["id"]}, "name": "stolen"}
        assert client.post(voices, json = stolen).status_code == 404
    with _client(account) as client:
        assert [v["id"] for v in client.get(voices).json()["voices"]] == [voice["id"]]
        assert client.get(f"{voices}/{voice['id']}/file").status_code == 200
        assert client.get(f"{inputs}/file").status_code == 200
        assert client.post(f"{inputs}/transcribe", json = model).json()["text"] == "private words"
        assert client.delete(inputs).json() == {"removed": True}


def test_inputs_and_voices_live_in_the_accounts_audio_folder(tmp_path):
    from core.inference import audio_inputs, audio_voices

    bob = tmp_path / "accounts" / BOB.account_id / "audio"
    assert run_as(BOB, audio_inputs.inputs_dir) == bob / "inputs"
    assert run_as(BOB, audio_voices.voices_dir) == bob / "voices"
    assert run_as(OWNER, audio_inputs.inputs_dir) == tmp_path / "audio" / "inputs"


@pytest.mark.parametrize("role", ["source", "target"])
@pytest.mark.parametrize("kind", ["input", "clip", "voice"])
def test_a_conversion_does_not_resolve_another_accounts_audio(role, kind, monkeypatch):
    async def fail(*_args, **_kwargs):
        raise AssertionError("a foreign id must stop before generation")

    monkeypatch.setattr(inference, "_generate_tts_wav", fail)
    if kind == "input":
        foreign = {"input_id": _save_input(ALICE)["id"]}
    elif kind == "clip":
        foreign = {"clip_id": run_as(ALICE, _save, "audio")["id"]}
    else:
        foreign = {"voice_id": _save_voice(ALICE)["id"]}
    own = {"input_id": _save_input(BOB)["id"]}
    inputs = {"source": own, "target": own, role: foreign}
    app = FastAPI()

    async def subject():
        token = bind_account(BOB)
        try:
            yield BOB.username
        finally:
            reset_account(token)

    app.dependency_overrides[get_current_subject] = subject
    app.dependency_overrides[authenticated_via_api_key] = lambda: False
    app.include_router(inference.router, prefix = "/api/inference")
    with TestClient(app) as client:
        response = client.post(
            "/api/inference/audio/run", json = {"workflow": "convert", "inputs": inputs}
        )
    assert response.status_code == 404, response.text


@pytest.mark.parametrize("account,other", [(ALICE, BOB), (BOB, ALICE)])
def test_transcribe_sources_and_transcripts_do_not_resolve_another_accounts_ids(account, other):
    from core.inference import transcript_gallery

    clip_id = run_as(account, _save, "audio")["id"]
    transcript = run_as(account, transcript_gallery.save, {"text": "t", "model": "m"}, "t")
    with _client(other) as client:
        for source in ({"input_id": _save_input(account)["id"]}, {"clip_id": clip_id}):
            body = {"source": source, "model": "small"}
            response = client.post("/api/inference/audio/transcribe/source", json = body)
            assert response.status_code == 404, response.text
        url = f"/api/inference/audio/transcripts/{transcript['id']}"
        assert client.get(url).status_code == 404
        assert client.patch(url, json = {"speaker_names": {"S01": "Mallory"}}).status_code == 404
        assert client.patch(url, json = {"archived": True}).status_code == 404
        assert transcript["id"] not in client.get("/api/inference/audio/transcripts").text