105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
"""
|
|
Integration tests for POST /api/register.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
os.environ.setdefault("BOT_TOKEN", "test-bot-token")
|
|
os.environ.setdefault("CHAT_ID", "-1001234567890")
|
|
os.environ.setdefault("WEBHOOK_SECRET", "test-webhook-secret")
|
|
os.environ.setdefault("WEBHOOK_URL", "https://example.com/api/webhook/telegram")
|
|
os.environ.setdefault("FRONTEND_ORIGIN", "http://localhost:3000")
|
|
|
|
import pytest
|
|
from tests.conftest import make_app_client
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_new_user_success():
|
|
"""POST /api/register returns 200 with user_id > 0."""
|
|
async with make_app_client() as client:
|
|
resp = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-001", "name": "Alice"},
|
|
)
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["user_id"] > 0
|
|
assert data["uuid"] == "reg-uuid-001"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_idempotent():
|
|
"""Registering the same uuid twice returns the same user_id."""
|
|
async with make_app_client() as client:
|
|
r1 = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-002", "name": "Bob"},
|
|
)
|
|
r2 = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-002", "name": "Bob"},
|
|
)
|
|
assert r1.status_code == 200
|
|
assert r2.status_code == 200
|
|
assert r1.json()["user_id"] == r2.json()["user_id"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_empty_name_returns_422():
|
|
"""Empty name must fail validation with 422."""
|
|
async with make_app_client() as client:
|
|
resp = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-003", "name": ""},
|
|
)
|
|
assert resp.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_missing_uuid_returns_422():
|
|
"""Missing uuid field must return 422."""
|
|
async with make_app_client() as client:
|
|
resp = await client.post(
|
|
"/api/register",
|
|
json={"name": "Charlie"},
|
|
)
|
|
assert resp.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_missing_name_returns_422():
|
|
"""Missing name field must return 422."""
|
|
async with make_app_client() as client:
|
|
resp = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-004"},
|
|
)
|
|
assert resp.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_user_stored_in_db():
|
|
"""After register, the user is persisted (second call returns same id)."""
|
|
async with make_app_client() as client:
|
|
r1 = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-005", "name": "Dana"},
|
|
)
|
|
r2 = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-005", "name": "Dana"},
|
|
)
|
|
assert r1.json()["user_id"] == r2.json()["user_id"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_response_contains_uuid():
|
|
"""Response body includes the submitted uuid."""
|
|
async with make_app_client() as client:
|
|
resp = await client.post(
|
|
"/api/register",
|
|
json={"uuid": "reg-uuid-006", "name": "Eve"},
|
|
)
|
|
assert resp.json()["uuid"] == "reg-uuid-006"
|