"""GET /auth/tenants — the sign-in / registration tenant dropdown."""

from sqlalchemy import update

from app.core.config import settings
from tests.conftest import AsyncSessionLocal
from app.models.identity import Tenant


async def test_lists_active_tenants_only_and_can_be_turned_off(client, test_tenant, monkeypatch):
    tid = str(test_tenant["tenant_id"])
    resp = await client.get("/auth/tenants")  # no token needed
    assert resp.status_code == 200
    mine = [t for t in resp.json() if t["id"] == tid]
    assert len(mine) == 1 and set(mine[0]) == {"id", "name"}

    async with AsyncSessionLocal() as db:
        async with db.begin():
            await db.execute(update(Tenant).where(Tenant.id == test_tenant["tenant_id"]).values(status="suspended"))
    assert tid not in {t["id"] for t in (await client.get("/auth/tenants")).json()}

    monkeypatch.setattr(settings, "public_tenant_list", False)
    assert (await client.get("/auth/tenants")).json() == []
