96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
"""Test auth endpoints."""
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from app.main import app
|
|
|
|
|
|
class TestAuth:
|
|
"""Test authentication endpoints."""
|
|
|
|
def test_register_user(self, client: TestClient):
|
|
"""Test user registration."""
|
|
response = client.post(
|
|
"/api/v1/auth/register",
|
|
json={
|
|
"email": "newuser@example.com",
|
|
"password": "password123",
|
|
"first_name": "John",
|
|
"last_name": "Doe"
|
|
}
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["email"] == "newuser@example.com"
|
|
assert data["first_name"] == "John"
|
|
|
|
def test_register_duplicate_user(self, client: TestClient, sample_user):
|
|
"""Test registering with existing email."""
|
|
response = client.post(
|
|
"/api/v1/auth/register",
|
|
json={
|
|
"email": sample_user.email,
|
|
"password": "password123",
|
|
"first_name": "Jane",
|
|
"last_name": "Smith"
|
|
}
|
|
)
|
|
|
|
assert response.status_code == 409
|
|
|
|
def test_login_success(self, client: TestClient, sample_user):
|
|
"""Test successful login."""
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={
|
|
"email": sample_user.email,
|
|
"password": "password123"
|
|
}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
assert data["token_type"] == "bearer"
|
|
assert data["user"]["email"] == sample_user.email
|
|
|
|
def test_login_invalid_credentials(self, client: TestClient):
|
|
"""Test login with invalid credentials."""
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={
|
|
"email": "nonexistent@example.com",
|
|
"password": "wrongpassword"
|
|
}
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
|
|
def test_login_wrong_password(self, client: TestClient, sample_user):
|
|
"""Test login with wrong password."""
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={
|
|
"email": sample_user.email,
|
|
"password": "wrongpassword"
|
|
}
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
|
|
def test_get_current_user(self, client: TestClient, sample_user, auth_headers):
|
|
"""Test getting current user."""
|
|
response = client.get("/api/v1/auth/me", headers=auth_headers)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["id"] == sample_user.id
|
|
assert data["email"] == sample_user.email
|
|
|
|
def test_get_current_user_unauthorized(self, client: TestClient):
|
|
"""Test getting current user without auth."""
|
|
response = client.get("/api/v1/auth/me")
|
|
|
|
assert response.status_code == 403
|