from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from httpx import AsyncClient

from tests.test_me import make_token


async def test_org_dep_exposes_org_id(
    client: AsyncClient, test_private_key: Ed25519PrivateKey
) -> None:
    resp = await client.get(
        "/_test/org", headers={"Authorization": f"Bearer {make_token(test_private_key)}"}
    )
    assert resp.status_code == 200
    assert resp.json() == {"org_id": "org_1"}


async def test_org_dep_without_org_is_problem_404(
    client: AsyncClient, test_private_key: Ed25519PrivateKey
) -> None:
    token = make_token(test_private_key, org_id=None)
    resp = await client.get("/_test/org", headers={"Authorization": f"Bearer {token}"})
    assert resp.status_code == 404
    assert resp.headers["content-type"].startswith("application/problem+json")
    body = resp.json()
    assert body["title"] == "Not Found"
    assert body["detail"] == "Aucune organisation active"


async def test_org_dep_without_token_is_401(client: AsyncClient) -> None:
    resp = await client.get("/_test/org")
    assert resp.status_code == 401
    assert resp.headers["www-authenticate"] == "Bearer"
