Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

10.7 KB
Raw Blame
"""E2E tests for KB collection-level access control (403/404 contracts).

These tests complement ``test_multitenancy_isolation_e2e.py`` by asserting
single, explicit HTTP outcomes on routes that use
:func:`xagent.web.api.kb._ensure_collection_access` (and related rename rules).

Ingest uses a **stub embedding pipeline** so CI does not depend on external
embedding keys; assertions are HTTP semantics (403/404), not provider quality.
Real embedding coverage lives in ``real_rag`` suites (e.g. multitenancy E2E).
"""

from __future__ import annotations

from pathlib import Path

import pytest
from fastapi.testclient import TestClient

from tests.web_integration.http_helpers import http_detail

pytestmark = [
    pytest.mark.e2e,
    pytest.mark.contract_stub,
    pytest.mark.usefixtures("kb_engine"),
]


def _register_and_login(
    client: TestClient, username: str, password: str, email: str
) -> str:
    """Register (idempotent) and return JWT access token."""
    reg = client.post(
        "/api/auth/register",
        json={"username": username, "password": password, "email": email},
    )
    assert reg.status_code in (200, 400)
    login = client.post(
        "/api/auth/login",
        json={"username": username, "password": password},
    )
    assert login.status_code == 200
    return str(login.json()["access_token"])


def _write_sample_txt(path: Path) -> Path:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text("access-control e2e sample content", encoding="utf-8")
    return path


def _ingest_txt(
    client: TestClient,
    token: str,
    collection: str,
    file_path: Path,
) -> None:
    headers = {"Authorization": f"Bearer {token}"}
    with open(file_path, "rb") as handle:
        resp = client.post(
            "/api/kb/ingest",
            files={"file": (file_path.name, handle, "text/plain")},
            data={"collection": collection},
            headers=headers,
        )
    assert resp.status_code == 200, http_detail(resp)


class TestKbAccessControlContract:
    """Strict status-code checks for KB access boundaries."""

    def test_parse_result_cross_tenant_returns_403(
        self, client: TestClient, tmp_path: Path
    ) -> None:
        """Another tenant cannot read parse results for a foreign collection name."""
        t1 = _register_and_login(
            client, "ac_parse_t1", "pw-t1-123", "[email protected]"
        )
        t2 = _register_and_login(
            client, "ac_parse_t2", "pw-t2-123", "[email protected]"
        )

        coll = "ac_parse_coll_t1"
        sample = _write_sample_txt(tmp_path / "ac_parse_doc.txt")
        _ingest_txt(client, t1, coll, sample)

        doc_id = "legit-doc-id-01"
        url = f"/api/kb/collections/{coll}/parses/{doc_id}/parse_result"
        resp = client.get(
            url,
            headers={"Authorization": f"Bearer {t2}"},
        )
        assert resp.status_code == 403
        assert "Access denied" in resp.json()["detail"]

    def test_parse_result_unknown_collection_returns_404(
        self, client: TestClient
    ) -> None:
        """A collection name that does not exist anywhere yields 404 (not 403)."""
        token = _register_and_login(
            client, "ac_parse_404", "pw-404-123", "[email protected]"
        )
        missing = "ac_no_such_collection_xyz"
        doc_id = "legit-doc-id-02"
        url = f"/api/kb/collections/{missing}/parses/{doc_id}/parse_result"
        resp = client.get(
            url,
            headers={"Authorization": f"Bearer {token}"},
        )
        assert resp.status_code == 404
        assert "not found" in resp.json()["detail"].lower()

    def test_rename_target_name_taken_by_other_tenant_returns_409(
        self, client: TestClient, tmp_path: Path
    ) -> None:
        """Renaming into a taken name is a naming conflict, not an access violation."""
        t1 = _register_and_login(
            client, "ac_rn_t1", "pw-r1-123", "[email protected]"
        )
        t2 = _register_and_login(
            client, "ac_rn_t2", "pw-r2-123", "[email protected]"
        )

        coll_a = "ac_rename_source_coll"
        coll_b = "ac_rename_target_coll"
        sample1 = _write_sample_txt(tmp_path / "ac_rn_a.txt")
        sample2 = _write_sample_txt(tmp_path / "ac_rn_b.txt")
        _ingest_txt(client, t1, coll_a, sample1)
        _ingest_txt(client, t2, coll_b, sample2)

        resp = client.put(
            f"/api/kb/collections/{coll_a}",
            data={"new_name": coll_b},
            headers={"Authorization": f"Bearer {t1}"},
        )
        assert resp.status_code == 409, resp.text
        detail = resp.json()["detail"]
        assert "name unavailable" in detail
        assert "Access denied" not in detail

    def test_rename_onto_own_collection_names_it(
        self, client: TestClient, tmp_path: Path
    ) -> None:
        """A caller's own collection is named outright: they can act on it.

        Only the cross-tenant branch above has to stay vague about ownership.
        """
        token = _register_and_login(
            client, "ac_rn_own", "pw-ro-123", "[email protected]"
        )

        coll_a = "ac_rename_own_source"
        coll_b = "ac_rename_own_target"
        _ingest_txt(client, token, coll_a, _write_sample_txt(tmp_path / "own_a.txt"))
        _ingest_txt(client, token, coll_b, _write_sample_txt(tmp_path / "own_b.txt"))

        resp = client.put(
            f"/api/kb/collections/{coll_a}",
            data={"new_name": coll_b},
            headers={"Authorization": f"Bearer {token}"},
        )
        assert resp.status_code == 409, resp.text
        assert resp.json()["detail"] == f"Target collection already exists: {coll_b}"

    def test_documents_check_cross_tenant_returns_403(
        self, client: TestClient, tmp_path: Path
    ) -> None:
        """Duplicate-check is read-only, so a foreign collection is a denial.

        It creates nothing, so it must not answer with a naming-conflict status.
        """
        t1 = _register_and_login(
            client, "ac_chk_t1", "pw-c1-123", "[email protected]"
        )
        t2 = _register_and_login(
            client, "ac_chk_t2", "pw-c2-123", "[email protected]"
        )

        coll = "ac_check_foreign_coll"
        sample = _write_sample_txt(tmp_path / "ac_chk.txt")
        _ingest_txt(client, t1, coll, sample)

        resp = client.post(
            f"/api/kb/collections/{coll}/documents/check",
            json={"filenames": ["any.txt"]},
            headers={"Authorization": f"Bearer {t2}"},
        )
        assert resp.status_code == 403
        assert "Access denied" in resp.json()["detail"]

    def test_save_collection_config_cross_tenant_returns_403(
        self, client: TestClient, tmp_path: Path
    ) -> None:
        """Config saves stay a denial: the body is settings, with no name field.

        The other ``allow_create=True`` callers answer 409 because the user just
        typed the name and can type another. This one is reached from the settings
        panel of a knowledge base the caller already sees, so a foreign name is an
        access attempt -- and "pick a different name" would point at an input the
        form does not have. Hence ``taken_name_is_conflict=False``.
        """
        t1 = _register_and_login(
            client, "ac_cfg_t1", "pw-g1-123", "[email protected]"
        )
        t2 = _register_and_login(
            client, "ac_cfg_t2", "pw-g2-123", "[email protected]"
        )

        coll = "ac_config_foreign_coll"
        sample = _write_sample_txt(tmp_path / "ac_cfg.txt")
        _ingest_txt(client, t1, coll, sample)

        resp = client.post(
            f"/api/kb/collections/{coll}/config",
            json={"embedding_model_id": "text-embedding-v4"},
            headers={"Authorization": f"Bearer {t2}"},
        )
        assert resp.status_code == 403, resp.text
        detail = resp.json()["detail"]
        assert "Access denied" in detail
        # Still no name-picking advice, and still no owner id.
        assert "different name" not in detail
        assert "user_" not in detail

    @pytest.mark.parametrize(
        "slug, path, style",
        [
            ("f", "/api/kb/ingest", "file"),
            ("fj", "/api/kb/ingest/jobs", "file"),
            ("w", "/api/kb/ingest-web", "web"),
            ("wj", "/api/kb/ingest-web/jobs", "web"),
            ("c", "/api/kb/ingest-cloud", "cloud"),
        ],
    )
    def test_ingest_into_name_taken_by_other_tenant_returns_409(
        self, client: TestClient, tmp_path: Path, slug: str, path: str, style: str
    ) -> None:
        """The path from #1139: creating a knowledge base by importing content.

        Every ingest entry point takes ``allow_create=True``, and the creation
        dialog drives all of them, so each must report a taken name as a conflict
        rather than accuse the caller of an access violation.
        """
        t1 = _register_and_login(
            client, f"ac_ing_{slug}_t1", "pw-i1-123", f"ac_ing_{slug}[email protected]"
        )
        t2 = _register_and_login(
            client, f"ac_ing_{slug}_t2", "pw-i2-123", f"ac_ing_{slug}[email protected]"
        )

        coll = f"ac_ingest_foreign_coll_{slug}"
        sample = _write_sample_txt(tmp_path / "ac_ing.txt")
        _ingest_txt(client, t1, coll, sample)

        headers = {"Authorization": f"Bearer {t2}"}
        if style == "file":
            with open(sample, "rb") as handle:
                resp = client.post(
                    path,
                    files={"file": (sample.name, handle, "text/plain")},
                    data={"collection": coll},
                    headers=headers,
                )
        elif style == "web":
            resp = client.post(
                path,
                data={"collection": coll, "start_url": "https://example.com/docs"},
                headers=headers,
            )
        else:
            resp = client.post(
                path,
                json={
                    "collection": coll,
                    "files": [
                        {
                            "provider": "google_drive",
                            "fileId": "file-1",
                            "fileName": "alpha.pdf",
                        }
                    ],
                },
                headers=headers,
            )

        # Status first: a success body here is a bare list on some endpoints, so
        # indexing "detail" would raise TypeError instead of naming the mismatch.
        assert resp.status_code == 409, resp.text
        detail = resp.json()["detail"]
        assert "name unavailable" in detail
        assert "Access denied" not in detail
        # Wording only: it does not name an owner. See the note above -- the
        # status code itself is what discloses that the name exists.
        assert "already exists" not in detail