Files
FabledSteward/tests/integration/test_api_tokens.py
T
bvandeusenandClaude Opus 5.5 3474cdff46
CI / lint (push) Successful in 1s
CI / integration (push) Successful in 44s
CI / unit (push) Successful in 1m5s
CI / publish (push) Successful in 1m15s
feat(mcp): connect card, MCP docs, and an end-to-end roles/audit test over HTTP
MCP-8 #4972 "Connect card, docs, and end-to-end integration test".

- AI access page: a "Connect a client" card with the /mcp URL (from
  Settings → General → Public base URL, else the request host) and recipes
  for Claude Code, Claude Desktop (mcp-remote) and other Streamable HTTP
  clients, plus the reverse-proxy note. The token-created page shows the
  same recipes with the new token filled in, ready to paste.
- docs/core/mcp.md: connecting, the reverse-proxy requirements, the
  security model (token ceiling, default deny, check-mode default, secrets,
  audit), tools by role, response conventions, adding tools.
  docs/architecture.md describes the /mcp dispatch outside the blueprint
  pipeline; docs/reference/code-map.md gains an MCP section.
- tests/integration/test_mcp.py: over HTTP, a viewer token lists no write
  tools and is refused one by name; an operator token reads get_overview
  and its maintenance-window create/end land in the audit log as
  "<user> via MCP" with the token id. (Revoked → 401 was already covered.)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 08:25:41 -04:00

176 lines
7.2 KiB
Python

"""Integration: personal API tokens against real Postgres.
Resolution reads the token and its user's CURRENT role in one query, so a
demoted or disabled user's tokens weaken or die with nothing revoked by hand.
The routes are driven through the test client so their transactions run for
real: a mocked session cannot catch a double-begin (#884).
"""
from __future__ import annotations
import asyncio
import os
import re
import uuid
from datetime import datetime, timedelta, timezone
import pytest
pytestmark = pytest.mark.integration
_NEEDS_DB = pytest.mark.skipif(
not os.environ.get("STEWARD_DATABASE_URL"),
reason="integration test needs a live Postgres (STEWARD_DATABASE_URL)",
)
@pytest.fixture
def app():
if not os.environ.get("STEWARD_DATABASE_URL"):
pytest.skip("needs Postgres")
from steward.app import create_app
return create_app(testing=False)
async def _make_user(app, role):
from steward.models.users import User
uid = str(uuid.uuid4())
async with app.db_sessionmaker() as s:
async with s.begin():
s.add(User(id=uid, username=f"tok-{uid[:8]}", email=f"{uid}@test",
password_hash="x", role=role, is_active=True))
return uid
async def _mint(app, uid, user_role, role, **kw):
from steward.core import api_tokens
async with app.db_sessionmaker() as s:
raw, token = await api_tokens.create_token(
s, user_id=uid, user_role=user_role, name="it", role=role, **kw)
await s.commit()
return raw, token.id
@_NEEDS_DB
def test_resolve_lifecycle(app):
from sqlalchemy import select, update
from steward.core import api_tokens
from steward.models.api_tokens import ApiToken
from steward.models.users import User, UserRole
async def _go():
uid = await _make_user(app, UserRole.admin)
raw, tid = await _mint(app, uid, UserRole.admin, UserRole.operator)
caller = await api_tokens.resolve_token(app, raw)
assert caller is not None
assert (caller.user_id, caller.role, caller.token_id) == (uid, UserRole.operator, tid)
async with app.db_sessionmaker() as s:
used = (await s.execute(select(ApiToken.last_used_at).where(ApiToken.id == tid))).scalar_one()
assert used is not None
# A wrong token, and the right one with a character changed, resolve to nobody.
assert await api_tokens.resolve_token(app, api_tokens.generate_token()) is None
assert await api_tokens.resolve_token(app, raw[:-1] + ("A" if raw[-1] != "A" else "B")) is None
# Demoting the user caps the token at the user's new role.
async with app.db_sessionmaker() as s:
async with s.begin():
await s.execute(update(User).where(User.id == uid).values(role=UserRole.viewer))
assert (await api_tokens.resolve_token(app, raw)).role == UserRole.viewer
# Disabling the user kills the token.
async with app.db_sessionmaker() as s:
async with s.begin():
await s.execute(update(User).where(User.id == uid).values(is_active=False))
assert await api_tokens.resolve_token(app, raw) is None
async with app.db_sessionmaker() as s:
async with s.begin():
await s.execute(update(User).where(User.id == uid).values(is_active=True))
assert await api_tokens.resolve_token(app, raw) is not None
# Revoking kills it, and only the owner can revoke.
other = await _make_user(app, UserRole.admin)
async with app.db_sessionmaker() as s:
assert await api_tokens.revoke_token(s, user_id=other, token_id=tid) is None
await s.commit()
assert await api_tokens.resolve_token(app, raw) is not None
async with app.db_sessionmaker() as s:
assert await api_tokens.revoke_token(s, user_id=uid, token_id=tid) is not None
await s.commit()
assert await api_tokens.resolve_token(app, raw) is None
# An expired token resolves to nobody.
raw2, tid2 = await _mint(app, uid, UserRole.viewer, UserRole.viewer)
async with app.db_sessionmaker() as s:
async with s.begin():
await s.execute(update(ApiToken).where(ApiToken.id == tid2).values(
expires_at=datetime.now(timezone.utc) - timedelta(seconds=1)))
assert await api_tokens.resolve_token(app, raw2) is None
# Deleting the user takes their tokens with them.
async with app.db_sessionmaker() as s:
async with s.begin():
await s.execute(User.__table__.delete().where(User.id == uid))
left = (await s.execute(select(ApiToken).where(ApiToken.user_id == uid))).all()
assert left == []
asyncio.run(_go())
@_NEEDS_DB
def test_routes_create_list_and_revoke(app):
from sqlalchemy import select
from steward.core import api_tokens
from steward.models.api_tokens import ApiToken
from steward.models.audit import AuditEvent
from steward.models.users import UserRole
async def _go():
uid = await _make_user(app, UserRole.operator)
client = app.test_client()
async with client.session_transaction() as sess:
sess["user_id"] = uid
sess["user_role"] = "operator"
sess["username"] = "route-test"
resp = await client.get("/ai-access/")
assert resp.status_code == 200
page = await resp.get_data(as_text=True)
assert "You have no tokens yet" in page
# An operator can't offer admin.
assert 'value="admin"' not in page
# The connect card names the endpoint, with a placeholder for the token.
assert 'value="http://localhost/mcp"' in page and "claude mcp add" in page
resp = await client.post("/ai-access/tokens", form={
"name": "laptop", "role": "admin", "expires_in_days": ""})
assert resp.status_code == 400
assert "higher role" in await resp.get_data(as_text=True)
resp = await client.post("/ai-access/tokens", form={
"name": "laptop", "role": "operator", "expires_in_days": "30"})
assert resp.status_code == 200
created = await resp.get_data(as_text=True)
raw = re.search(r'value="(stw_[^"]+)"', created).group(1)
# The recipes on the created page carry the new token, ready to paste.
assert f"Bearer {raw}" in created and "mcp-remote" in created
caller = await api_tokens.resolve_token(app, raw)
assert caller is not None and caller.role == UserRole.operator
async with app.db_sessionmaker() as s:
token = (await s.execute(select(ApiToken).where(ApiToken.user_id == uid))).scalar_one()
page = await (await client.get("/ai-access/")).get_data(as_text=True)
assert "laptop" in page and token.prefix in page
assert raw not in page
resp = await client.post(f"/ai-access/tokens/{token.id}/revoke")
assert resp.status_code == 302
assert await api_tokens.resolve_token(app, raw) is None
async with app.db_sessionmaker() as s:
actions = (await s.execute(
select(AuditEvent.action).where(AuditEvent.user_id == uid))).scalars().all()
assert "api_token.create" in actions and "api_token.revoke" in actions
asyncio.run(_go())