51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
import uuid
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from models import User, Invite, InviteStatus, ROLE_ADMIN
|
|
from invite_utils import generate_invite_identifier
|
|
|
|
|
|
def unique_email(prefix: str = "test") -> str:
|
|
"""Generate a unique email for tests sharing the same database."""
|
|
return f"{prefix}-{uuid.uuid4().hex[:8]}@example.com"
|
|
|
|
|
|
async def create_invite_for_registration(db: AsyncSession, godfather_email: str) -> str:
|
|
"""
|
|
Create an invite that can be used for registration.
|
|
Returns the invite identifier.
|
|
"""
|
|
# Find godfather
|
|
result = await db.execute(select(User).where(User.email == godfather_email))
|
|
godfather = result.scalar_one_or_none()
|
|
|
|
if not godfather:
|
|
# Create a godfather user (admin can create invites)
|
|
from auth import get_password_hash
|
|
from models import Role
|
|
|
|
result = await db.execute(select(Role).where(Role.name == ROLE_ADMIN))
|
|
admin_role = result.scalar_one_or_none()
|
|
|
|
godfather = User(
|
|
email=godfather_email,
|
|
hashed_password=get_password_hash("password123"),
|
|
roles=[admin_role] if admin_role else [],
|
|
)
|
|
db.add(godfather)
|
|
await db.flush()
|
|
|
|
# Create invite
|
|
identifier = generate_invite_identifier()
|
|
invite = Invite(
|
|
identifier=identifier,
|
|
godfather_id=godfather.id,
|
|
status=InviteStatus.READY,
|
|
)
|
|
db.add(invite)
|
|
await db.commit()
|
|
|
|
return identifier
|
|
|