finish branch

This commit is contained in:
counterweight 2025-12-19 00:12:43 +01:00
parent 66bc4c5a45
commit 40ca82bb45
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
11 changed files with 139 additions and 128 deletions

View file

@ -1,6 +1,5 @@
import os
from contextlib import asynccontextmanager
from typing import List
# Set required env vars before importing app
os.environ.setdefault("SECRET_KEY", "test-secret-key-for-testing-only")
@ -12,8 +11,9 @@ from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, Asyn
from database import Base, get_db
from main import app
from models import User, Role, Permission, ROLE_DEFINITIONS
from models import User, Role, Permission, ROLE_DEFINITIONS, ROLE_REGULAR, ROLE_ADMIN
from auth import get_password_hash
from tests.helpers import unique_email
TEST_DATABASE_URL = os.getenv(
"TEST_DATABASE_URL",
@ -82,7 +82,7 @@ async def create_user_with_roles(
db: AsyncSession,
email: str,
password: str,
role_names: List[str],
role_names: list[str],
) -> User:
"""Create a user with specified roles."""
# Get roles
@ -90,8 +90,9 @@ async def create_user_with_roles(
for role_name in role_names:
result = await db.execute(select(Role).where(Role.name == role_name))
role = result.scalar_one_or_none()
if role:
roles.append(role)
if not role:
raise ValueError(f"Role '{role_name}' not found. Did you run setup_roles()?")
roles.append(role)
user = User(
email=email,
@ -144,13 +145,11 @@ async def client(client_factory):
@pytest.fixture(scope="function")
async def regular_user(client_factory):
"""Create a regular user and return their credentials and cookies."""
from tests.helpers import unique_email
email = unique_email("regular")
password = "password123"
async with client_factory.get_db_session() as db:
await create_user_with_roles(db, email, password, ["regular"])
await create_user_with_roles(db, email, password, [ROLE_REGULAR])
# Login to get cookies
response = await client_factory.post(
@ -169,13 +168,11 @@ async def regular_user(client_factory):
@pytest.fixture(scope="function")
async def admin_user(client_factory):
"""Create an admin user and return their credentials and cookies."""
from tests.helpers import unique_email
email = unique_email("admin")
password = "password123"
async with client_factory.get_db_session() as db:
await create_user_with_roles(db, email, password, ["admin"])
await create_user_with_roles(db, email, password, [ROLE_ADMIN])
# Login to get cookies
response = await client_factory.post(
@ -194,8 +191,6 @@ async def admin_user(client_factory):
@pytest.fixture(scope="function")
async def user_no_roles(client_factory):
"""Create a user with NO roles and return their credentials and cookies."""
from tests.helpers import unique_email
email = unique_email("noroles")
password = "password123"