Enhance test fixtures and user registration logic to ensure plan existence and correct role assignment

This commit is contained in:
JSC
2025-07-25 18:43:29 +02:00
parent 45ba28af52
commit 52ebc59293
5 changed files with 247 additions and 238 deletions

View File

@@ -130,7 +130,47 @@ async def test_pro_plan(test_session: AsyncSession) -> Plan:
@pytest_asyncio.fixture
async def test_user(test_session: AsyncSession, test_plan: Plan) -> User:
async def ensure_plans(test_session: AsyncSession) -> tuple[Plan, Plan]:
"""Ensure both free and pro plans exist."""
# Check for free plan
free_result = await test_session.exec(select(Plan).where(Plan.code == "free"))
free_plan = free_result.first()
if not free_plan:
free_plan = Plan(
code="free",
name="Free Plan",
description="Test free plan",
credits=100,
max_credits=100,
)
test_session.add(free_plan)
# Check for pro plan
pro_result = await test_session.exec(select(Plan).where(Plan.code == "pro"))
pro_plan = pro_result.first()
if not pro_plan:
pro_plan = Plan(
code="pro",
name="Pro Plan",
description="Test pro plan",
credits=300,
max_credits=300,
)
test_session.add(pro_plan)
await test_session.commit()
await test_session.refresh(free_plan)
await test_session.refresh(pro_plan)
return free_plan, pro_plan
@pytest_asyncio.fixture
async def test_user(
test_session: AsyncSession, ensure_plans: tuple[Plan, Plan]
) -> User:
"""Create a test user."""
user = User(
email="test@example.com",
@@ -138,7 +178,7 @@ async def test_user(test_session: AsyncSession, test_plan: Plan) -> User:
password_hash=PasswordUtils.hash_password("testpassword123"),
role="user",
is_active=True,
plan_id=test_plan.id,
plan_id=ensure_plans[0].id, # Use free plan
credits=100,
)
test_session.add(user)
@@ -148,7 +188,9 @@ async def test_user(test_session: AsyncSession, test_plan: Plan) -> User:
@pytest_asyncio.fixture
async def admin_user(test_session: AsyncSession, test_plan: Plan) -> User:
async def admin_user(
test_session: AsyncSession, ensure_plans: tuple[Plan, Plan]
) -> User:
"""Create a test admin user."""
user = User(
email="admin@example.com",
@@ -156,7 +198,7 @@ async def admin_user(test_session: AsyncSession, test_plan: Plan) -> User:
password_hash=PasswordUtils.hash_password("adminpassword123"),
role="admin",
is_active=True,
plan_id=test_plan.id,
plan_id=ensure_plans[1].id, # Use pro plan for admin
credits=1000,
)
test_session.add(user)