Spaces:
Sleeping
Sleeping
File size: 3,744 Bytes
507973e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | """Integration tests for authentication endpoints."""
import pytest
from fastapi.testclient import TestClient
def test_register_success(client: TestClient):
"""Test successful user registration."""
response = client.post(
"/auth/register",
json={
"email": "newuser@test.com",
"password": "password123",
"name": "New User",
},
)
assert response.status_code == 201
data = response.json()
assert data["success"] is True
assert "token" in data["data"]
assert data["data"]["token"]["token_type"] == "bearer"
assert "user" in data["data"]
assert data["data"]["user"]["email"] == "newuser@test.com"
def test_register_duplicate_email(client: TestClient):
"""Test registration with duplicate email fails."""
# Register first user
client.post(
"/auth/register",
json={
"email": "duplicate@test.com",
"password": "password123",
"name": "First User",
},
)
# Try to register again with same email
response = client.post(
"/auth/register",
json={
"email": "duplicate@test.com",
"password": "password456",
"name": "Second User",
},
)
assert response.status_code == 400
def test_register_short_password(client: TestClient):
"""Test registration with password < 8 characters fails."""
response = client.post(
"/auth/register",
json={
"email": "short@test.com",
"password": "short",
"name": "Short Password",
},
)
assert response.status_code == 422 # Validation error
def test_login_success(client: TestClient):
"""Test successful login."""
# Register user first
client.post(
"/auth/register",
json={
"email": "login@test.com",
"password": "password123",
"name": "Login User",
},
)
# Login
response = client.post(
"/auth/login",
json={"email": "login@test.com", "password": "password123"},
)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert "token" in data["data"]
assert data["message"] == "Login successful"
def test_login_wrong_password(client: TestClient):
"""Test login with wrong password fails."""
# Register user
client.post(
"/auth/register",
json={
"email": "wrongpass@test.com",
"password": "correctpass",
"name": "User",
},
)
# Login with wrong password
response = client.post(
"/auth/login",
json={"email": "wrongpass@test.com", "password": "wrongpass"},
)
assert response.status_code == 401
def test_login_nonexistent_user(client: TestClient):
"""Test login with non-existent user fails."""
response = client.post(
"/auth/login",
json={"email": "noexist@test.com", "password": "password123"},
)
assert response.status_code == 401
def test_logout_success(client: TestClient, auth_token_factory):
"""Test logout with valid token."""
# Create a mock user_id and token
token = auth_token_factory(1)
response = client.post(
"/auth/logout",
headers={"Authorization": f"Bearer {token}"},
)
# Logout returns 200 even though JWT is stateless
assert response.status_code == 200
data = response.json()
assert data["success"] is True
def test_logout_without_token(client: TestClient):
"""Test logout without token fails."""
response = client.post("/auth/logout")
assert response.status_code == 403 # Forbidden (no auth header)
|