fix: persist account ownership for script templates and generations
This commit is contained in:
@@ -6,14 +6,18 @@ from datetime import datetime, timezone
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
|
||||
from app.models.script_template import ScriptGeneration
|
||||
from app.models.user import User
|
||||
|
||||
# ── Fixtures ──────────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.fixture
|
||||
async def seed_script_data(test_db):
|
||||
async def seed_script_data(test_db, test_user):
|
||||
"""Seed script categories and templates into the test database."""
|
||||
now = datetime.now(timezone.utc)
|
||||
cat_id = uuid.UUID("00000000-0000-0000-0000-000000000001")
|
||||
user_result = await test_db.execute(sa.select(User).where(User.email == test_user["email"]))
|
||||
user = user_result.scalar_one()
|
||||
|
||||
# Insert category
|
||||
await test_db.execute(
|
||||
@@ -142,20 +146,20 @@ async def seed_script_data(test_db):
|
||||
await test_db.execute(
|
||||
sa.text("""
|
||||
INSERT INTO script_templates (
|
||||
id, category_id, name, slug, description,
|
||||
id, category_id, account_id, name, slug, description,
|
||||
script_body, parameters_schema, default_values, validation_rules,
|
||||
tags, complexity, estimated_runtime, requires_elevation,
|
||||
requires_modules, version, is_verified, is_active, usage_count,
|
||||
created_at, updated_at
|
||||
) VALUES (
|
||||
:id, :category_id, :name, :slug, :description,
|
||||
:id, :category_id, :account_id, :name, :slug, :description,
|
||||
:script_body, CAST(:parameters_schema AS jsonb), '{}'::jsonb, '{}'::jsonb,
|
||||
CAST(:tags AS jsonb), :complexity, :estimated_runtime, :requires_elevation,
|
||||
'[]'::jsonb, 1, true, true, 0,
|
||||
:now, :now
|
||||
)
|
||||
"""),
|
||||
{**tmpl, "category_id": cat_id, "now": now},
|
||||
{**tmpl, "category_id": cat_id, "account_id": user.account_id, "now": now},
|
||||
)
|
||||
|
||||
await test_db.commit()
|
||||
@@ -245,7 +249,7 @@ async def test_get_template_detail_not_found(client, auth_headers):
|
||||
# ── Generate ──────────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_script_success(client, auth_headers, seed_script_data):
|
||||
async def test_generate_script_success(client, auth_headers, seed_script_data, test_db, test_user):
|
||||
list_resp = await client.get(
|
||||
"/api/v1/scripts/templates?search=unlock",
|
||||
headers=auth_headers,
|
||||
@@ -265,6 +269,13 @@ async def test_generate_script_success(client, auth_headers, seed_script_data):
|
||||
assert "script" in data
|
||||
assert "jsmith" in data["script"]
|
||||
assert "id" in data
|
||||
generation_result = await test_db.execute(
|
||||
sa.select(ScriptGeneration).where(ScriptGeneration.id == uuid.UUID(data["id"]))
|
||||
)
|
||||
generation = generation_result.scalar_one()
|
||||
user_result = await test_db.execute(sa.select(User).where(User.email == test_user["email"]))
|
||||
user = user_result.scalar_one()
|
||||
assert generation.account_id == user.account_id
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in New Issue
Block a user