agnes-the-ai-analyst/tests/test_api_scripts.py
minasarustamyan d4ac84dd46
feat(rbac): drop dataset_permissions + users.role + is_public; v19 migration (#150)
* feat(rbac): drop dataset_permissions + access_requests + users.role + is_public; v19 migration

BREAKING. Sjednocení datové RBAC vrstvy do per-group resource_grants modelu.
Před PR byla legacy data RBAC vrstva (dataset_permissions + is_public bypass)
de-facto neaktivní — is_public neměl API/UI/CLI surface, default true znamenal
že can_access_table vždycky bypassl. Dnes každý non-admin přístup vyžaduje
explicitní resource_grants(group, "table", id) řádek.

Schema v18 → v19 (src/db.py:_v18_to_v19_finalize):
- DROP TABLE dataset_permissions, access_requests
- DROP COLUMN users.role (NULL artifact since v13)
- DROP COLUMN table_registry.is_public
- Drops přes table-rebuild idiom (rename → create new → INSERT … SELECT
  → drop old) kvůli DuckDB ALTER DROP COLUMN limitacím na tabulkách
  s historic FK constraints. INSERT picks intersection sloupců, takže
  test fixtures s minimal pre-v19 schemou migrate cleanly.

Runtime:
- src/rbac.py:can_access_table → deleguje na app.auth.access.can_access
- DatasetPermissionRepository, AccessRequestRepository smazány
- AGNES_ENABLE_TABLE_GRANTS env-gate v app/resource_types.py odstraněn
  (TABLE je unconditionally enabled)

API drop:
- app/api/permissions.py, app/api/access_requests.py celé soubory
- /admin/permissions web route + admin_permissions.html
- "Request Access" modal v catalog.html + locked-row UI
- ~10 if user.get("role") != "admin" checků nahrazeno (admin shortcut
  je uvnitř can_access_table)
- /api/settings: drop permissions field z GET; PUT /api/settings/dataset
  gate přepnut na can_access(user_id, "table", dataset, conn)

Auth:
- app/auth/jwt.py:create_access_token: drop role parametr (claim zmizí
  z nově vydávaných JWT; staré tokeny zůstávají valid, claim ignored)
- app/api/users.py: drop role z CreateUserRequest / UpdateUserRequest
  (admin promotion = explicit add to Admin group via memberships API)
- src/repositories/users.py: drop role z create() / update()

CLI:
- da admin set-role smazán → hard-fail s replacement command
- da admin add-user --role flag pryč
- da auth import-token --role flag pryč
- da auth whoami: drop "Role:" výpis
- cli/config.py:save_token: role parametr now optional, no longer written
  (back-compat se starými token.json soubory zachována — pole se ignoruje)

Tests:
- DELETE: test_permissions.py, test_permissions_api.py, test_access_requests_api.py
- REWRITE: test_access_control.py (resource_grants flow), test_rbac.py
  (can_access_table over resource_grants), test_journey_rbac.py
  (drop access-request flow), test_resource_types.py (drop env-gate
  tests, drop is_public from helpers), test_v2_*.py (drop role-based
  user dicts in favor of id-based + Admin group membership),
  test_settings_api.py (no permissions field, can_access gate)
- TRIVIAL: ~30 souborů — drop role="admin" arg z UserRepository.create
  a 3rd positional role z create_access_token
- NEW: test_v18_to_v19 migration test (test_db.py),
  test_can_access_table_no_implicit_public (test_rbac.py),
  test_admin_set_role_returns_hardfail (test_cli_admin.py)
- OpenAPI snapshot regenerated

Docs:
- CHANGELOG: BREAKING entry pod [Unreleased]
- CLAUDE.md: schema v18 → v19
- docs/architecture.md: schema table + RBAC sekce přepsána
- docs/auth-google-oauth.md: admin promotion přes da admin break-glass
- cli/skills/security.md: kompletně přepsáno na group-based model
- docs/TODO-rbac-data-enforcement.md: smazáno (TODO splněn)

Test results: 2363 passed, 19 failed. Zbývající failures jsou pre-existing
Windows-specific issues (fcntl, charset) nesouvisející s tímto PR —
ověřeno git stash pop.

Plan: ~/.claude/plans/floofy-coalescing-parnas.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore(release): cut 0.27.0

---------

Co-authored-by: Minas Arustamyan <arustamyan.minas@gmail.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: ZdenekSrotyr <zdenek.srotyr@keboola.com>
2026-04-30 22:02:16 +02:00

143 lines
5.4 KiB
Python

"""Tests for scripts and settings API endpoints."""
import os
import pytest
from fastapi.testclient import TestClient
@pytest.fixture
def client(tmp_path, monkeypatch):
monkeypatch.setenv("DATA_DIR", str(tmp_path))
monkeypatch.setenv("JWT_SECRET_KEY", "test-secret-32chars-minimum!!!!!")
monkeypatch.setenv("SCRIPT_TIMEOUT", "10")
from app.main import create_app
from src.db import get_system_db
from src.repositories.users import UserRepository
from app.auth.jwt import create_access_token
from tests.helpers.auth import grant_admin
conn = get_system_db()
user_repo = UserRepository(conn)
user_repo.create(id="admin1", email="admin@acme.com", name="Admin")
user_repo.create(id="analyst1", email="analyst@acme.com", name="Analyst")
grant_admin(conn, "admin1")
# Grant analyst1 access to "sales" + "support" tables via resource_grants
# (tests below exercise enable-dataset gates that require an explicit grant).
from src.repositories.user_groups import UserGroupsRepository
from src.repositories.user_group_members import UserGroupMembersRepository
from src.repositories.resource_grants import ResourceGrantsRepository
grp = UserGroupsRepository(conn).create(
name="api-scripts-test", description="test", created_by="test",
)
UserGroupMembersRepository(conn).add_member(
"analyst1", grp["id"], source="admin", added_by="test",
)
grants = ResourceGrantsRepository(conn)
grants.create(group_id=grp["id"], resource_type="table", resource_id="sales",
assigned_by="test")
grants.create(group_id=grp["id"], resource_type="table", resource_id="support",
assigned_by="test")
conn.close()
app = create_app()
test_client = TestClient(app)
admin_token = create_access_token("admin1", "admin@acme.com")
analyst_token = create_access_token("analyst1", "analyst@acme.com")
return test_client, admin_token, analyst_token
class TestScriptsAPI:
def test_list_scripts_empty(self, client):
c, admin_token, _ = client
resp = c.get("/api/scripts", headers={"Authorization": f"Bearer {admin_token}"})
assert resp.status_code == 200
assert resp.json()["count"] == 0
def test_deploy_and_list(self, client):
c, admin_token, _ = client
headers = {"Authorization": f"Bearer {admin_token}"}
resp = c.post("/api/scripts/deploy", json={
"name": "hello", "source": "print('hello world')",
}, headers=headers)
assert resp.status_code == 201
script_id = resp.json()["id"]
resp = c.get("/api/scripts", headers=headers)
assert resp.json()["count"] == 1
def test_run_script(self, client):
c, admin_token, _ = client
headers = {"Authorization": f"Bearer {admin_token}"}
resp = c.post("/api/scripts/run", json={
"source": "print('hello from script')", "name": "test",
}, headers=headers)
assert resp.status_code == 200
data = resp.json()
assert data["exit_code"] == 0
assert "hello from script" in data["stdout"]
def test_run_blocked_import(self, client):
c, admin_token, _ = client
headers = {"Authorization": f"Bearer {admin_token}"}
resp = c.post("/api/scripts/run", json={
"source": "import subprocess; subprocess.run(['ls'])", "name": "bad",
}, headers=headers)
assert resp.status_code == 400
detail = resp.json()["detail"]
assert "disallowed" in detail or "Blocked" in detail
def test_deploy_run_undeploy(self, client):
c, admin_token, _ = client
admin_headers = {"Authorization": f"Bearer {admin_token}"}
resp = c.post("/api/scripts/deploy", json={
"name": "calc", "source": "print(2+2)", "schedule": "daily 08:00",
}, headers=admin_headers)
script_id = resp.json()["id"]
resp = c.post(f"/api/scripts/{script_id}/run", headers=admin_headers)
assert resp.status_code == 200
assert "4" in resp.json()["stdout"]
# Undeploy (requires admin)
resp = c.delete(f"/api/scripts/{script_id}", headers=admin_headers)
assert resp.status_code == 204
class TestSettingsAPI:
def test_get_settings(self, client):
c, _, analyst_token = client
resp = c.get("/api/settings", headers={"Authorization": f"Bearer {analyst_token}"})
assert resp.status_code == 200
data = resp.json()
assert data["user_id"] == "analyst1"
# v19: legacy `permissions` field dropped — use /api/me/effective-access
# if you need the per-user grant breakdown.
assert "permissions" not in data
assert "sync_settings" in data
def test_enable_dataset(self, client):
c, _, analyst_token = client
headers = {"Authorization": f"Bearer {analyst_token}"}
resp = c.put("/api/settings/dataset", json={
"dataset": "sales", "enabled": True,
}, headers=headers)
assert resp.status_code == 200
assert resp.json()["enabled"] is True
def test_enable_unauthorized_dataset(self, client):
c, _, analyst_token = client
headers = {"Authorization": f"Bearer {analyst_token}"}
resp = c.put("/api/settings/dataset", json={
"dataset": "hr_secret", "enabled": True,
}, headers=headers)
assert resp.status_code == 403