feat(auth): implement auth stack
This commit is contained in:
65
tests/api/helpers.py
Normal file
65
tests/api/helpers.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""Shared helpers for API integration tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import asyncpg
|
||||
from httpx import AsyncClient
|
||||
|
||||
API_PREFIX = "/v1"
|
||||
|
||||
|
||||
async def register_user(
|
||||
client: AsyncClient,
|
||||
*,
|
||||
email: str,
|
||||
password: str,
|
||||
org_name: str = "Test Org",
|
||||
) -> dict[str, Any]:
|
||||
"""Call the register endpoint and return JSON body (raises on failure)."""
|
||||
|
||||
response = await client.post(
|
||||
f"{API_PREFIX}/auth/register",
|
||||
json={"email": email, "password": password, "org_name": org_name},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
|
||||
async def create_org(
|
||||
conn: asyncpg.Connection,
|
||||
*,
|
||||
name: str,
|
||||
slug: str | None = None,
|
||||
) -> UUID:
|
||||
"""Insert an organization row and return its ID."""
|
||||
|
||||
org_id = uuid4()
|
||||
slug_value = slug or name.lower().replace(" ", "-")
|
||||
await conn.execute(
|
||||
"INSERT INTO orgs (id, name, slug) VALUES ($1, $2, $3)",
|
||||
org_id,
|
||||
name,
|
||||
slug_value,
|
||||
)
|
||||
return org_id
|
||||
|
||||
|
||||
async def add_membership(
|
||||
conn: asyncpg.Connection,
|
||||
*,
|
||||
user_id: UUID,
|
||||
org_id: UUID,
|
||||
role: str,
|
||||
) -> None:
|
||||
"""Insert a membership record for the user/org pair."""
|
||||
|
||||
await conn.execute(
|
||||
"INSERT INTO org_members (id, user_id, org_id, role) VALUES ($1, $2, $3, $4)",
|
||||
uuid4(),
|
||||
user_id,
|
||||
org_id,
|
||||
role,
|
||||
)
|
||||
Reference in New Issue
Block a user