51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import pytest
|
|
import httpx
|
|
from app.core.security import create_access_token
|
|
|
|
BASE_URL = "http://localhost:8000"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_registration():
|
|
import uuid
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
username = f"testuser_{unique_id}"
|
|
email = f"{unique_id}@example.com"
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
# Test successful registration
|
|
response = await client.post(f"{BASE_URL}/auth/register", json={
|
|
"username": username,
|
|
"email": email,
|
|
"password": "password123",
|
|
"role": "listener"
|
|
})
|
|
assert response.status_code == 200
|
|
assert response.json()["username"] == username
|
|
|
|
# Test duplicate registration
|
|
response = await client.post(f"{BASE_URL}/auth/register", json={
|
|
"username": username,
|
|
"email": email,
|
|
"password": "password123",
|
|
"role": "listener"
|
|
})
|
|
assert response.status_code == 400
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login():
|
|
async with httpx.AsyncClient() as client:
|
|
# Test successful login (seeded users)
|
|
response = await client.post(f"{BASE_URL}/auth/login", json={
|
|
"username": "admin",
|
|
"password": "password123"
|
|
})
|
|
assert response.status_code == 200
|
|
assert "access_token" in response.json()
|
|
|
|
# Test failed login (wrong password)
|
|
response = await client.post(f"{BASE_URL}/auth/login", json={
|
|
"username": "admin",
|
|
"password": "wrongpassword"
|
|
})
|
|
assert response.status_code == 401
|