84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
"""Authentication tests."""
|
|
|
|
import pytest
|
|
|
|
|
|
def test_login_success(client, admin_user):
|
|
"""Test successful login."""
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "admin@test.com",
|
|
"password": "admin123"
|
|
})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
assert data["user"]["email"] == "admin@test.com"
|
|
|
|
|
|
def test_login_invalid_password(client, admin_user):
|
|
"""Test login with invalid password."""
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "admin@test.com",
|
|
"password": "wrongpassword"
|
|
})
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_login_nonexistent_user(client):
|
|
"""Test login with nonexistent user."""
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "nonexistent@test.com",
|
|
"password": "password"
|
|
})
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_auth_me_authenticated(client, admin_token):
|
|
"""Test /auth/me with valid token."""
|
|
response = client.get("/api/v1/auth/me", headers={
|
|
"Authorization": f"Bearer {admin_token}"
|
|
})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["email"] == "admin@test.com"
|
|
assert "permissions" in data
|
|
assert isinstance(data["permissions"], list)
|
|
|
|
|
|
def test_auth_me_unauthenticated(client):
|
|
"""Test /auth/me without token."""
|
|
response = client.get("/api/v1/auth/me")
|
|
assert response.status_code == 403 # HTTPBearer returns 403
|
|
|
|
|
|
def test_auth_me_invalid_token(client):
|
|
"""Test /auth/me with invalid token."""
|
|
response = client.get("/api/v1/auth/me", headers={
|
|
"Authorization": "Bearer invalid_token_here"
|
|
})
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_register_success(client):
|
|
"""Test successful registration."""
|
|
response = client.post("/api/v1/auth/register", json={
|
|
"email": "new@test.com",
|
|
"password": "newpass123",
|
|
"first_name": "New",
|
|
"last_name": "User"
|
|
})
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["email"] == "new@test.com"
|
|
|
|
|
|
def test_register_duplicate_email(client, admin_user):
|
|
"""Test registration with existing email."""
|
|
response = client.post("/api/v1/auth/register", json={
|
|
"email": "admin@test.com",
|
|
"password": "password",
|
|
"first_name": "Dup",
|
|
"last_name": "User"
|
|
})
|
|
assert response.status_code == 409
|