Spaces:
Paused

miracle / tests /test_kb.py
dushyantsingh0
Claude Sonnet 4.6
Add comprehensive test suite targeting >90% coverage
aa13248
Raw History Blame Contribute Delete
5.25 kB
"""
Integration tests for the Knowledge Base (KB) article API.
Coverage:
- SC_ADMIN can create, retrieve, and list KB articles
- CUSTOMER_USER is forbidden from creating KB articles (403)
- KB articles are scoped to the workspace that created them
- BOLA: admin in Workspace A cannot directly retrieve a KB article that
belongs to Workspace B
"""
import pytest
from httpx import AsyncClient
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
async def _create_kb_article(
client: AsyncClient,
token: str,
title: str = "How to Reset Password",
content: str = "Step 1: Click forgot password.",
workspace_id: str | None = None,
) -> dict:
headers = {"Authorization": f"Bearer {token}", "X-Nexus-Portal": "internal"}
params = {}
if workspace_id:
params["workspace_id"] = workspace_id
res = await client.post(
"/api/v1/kb/",
json={"title": title, "content": content, "category": "FAQ"},
headers=headers,
params=params,
)
return res
# ---------------------------------------------------------------------------
# Happy-path tests
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_admin_can_create_kb_article(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN successfully creates a KB article in their workspace."""
res = await _create_kb_article(
client, admin_token_a, title="Getting Started Guide"
)
assert res.status_code == 201
data = res.json()
assert data["title"] == "Getting Started Guide"
assert "id" in data
@pytest.mark.asyncio
async def test_admin_can_list_kb_articles(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can list KB articles after creating one."""
await _create_kb_article(client, admin_token_a, title="List Test Article")
res = await client.get(
"/api/v1/kb/",
headers={"Authorization": f"Bearer {admin_token_a}", "X-Nexus-Portal": "internal"},
)
assert res.status_code == 200
articles = res.json()
assert isinstance(articles, list)
assert any(a["title"] == "List Test Article" for a in articles)
@pytest.mark.asyncio
async def test_admin_can_retrieve_kb_article_by_id(
client: AsyncClient, admin_token_a: str
):
"""SC_ADMIN can fetch a specific KB article by its ID."""
create_res = await _create_kb_article(client, admin_token_a, title="Retrieve Me")
assert create_res.status_code == 201
article_id = create_res.json()["id"]
res = await client.get(
f"/api/v1/kb/{article_id}",
headers={"Authorization": f"Bearer {admin_token_a}", "X-Nexus-Portal": "internal"},
)
assert res.status_code == 200
assert res.json()["id"] == article_id
# ---------------------------------------------------------------------------
# RBAC: customer cannot manage KB articles
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_customer_cannot_create_kb_article(client: AsyncClient, token_a: str):
"""CUSTOMER_USER must receive 403 when attempting to create a KB article."""
res = await client.post(
"/api/v1/kb/",
json={"title": "Malicious Article", "content": "...", "category": "FAQ"},
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 403
# ---------------------------------------------------------------------------
# Workspace isolation
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_kb_article_workspace_isolation(
client: AsyncClient, admin_token_a: str, admin_token_b: str
):
"""
KB articles created in Workspace B must not appear in Workspace A's listing.
Both admins list their own workspace; only their own articles are visible.
"""
# Create an article only in Workspace B
create_res = await _create_kb_article(
client, admin_token_b, title="Workspace B Exclusive Article"
)
assert create_res.status_code == 201
# Workspace A admin lists articles — should NOT see Workspace B's article
res_a = await client.get(
"/api/v1/kb/",
headers={"Authorization": f"Bearer {admin_token_a}", "X-Nexus-Portal": "internal"},
)
assert res_a.status_code == 200
titles_a = [a["title"] for a in res_a.json()]
assert "Workspace B Exclusive Article" not in titles_a
@pytest.mark.asyncio
async def test_admin_a_cannot_read_workspace_b_article_by_id(
client: AsyncClient, admin_token_a: str, admin_token_b: str
):
"""
BOLA guard: Admin A cannot fetch a specific KB article that belongs to
Workspace B. Expects 404.
"""
create_res = await _create_kb_article(
client, admin_token_b, title="Confidential B Article"
)
assert create_res.status_code == 201
b_article_id = create_res.json()["id"]
res = await client.get(
f"/api/v1/kb/{b_article_id}",
headers={"Authorization": f"Bearer {admin_token_a}", "X-Nexus-Portal": "internal"},
)
assert res.status_code == 404