Exception handling enhancement
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# tests/test_authentication_endpoints.py
|
||||
# tests/integration/api/v1/test_authentication_endpoints.py
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
@@ -34,8 +34,11 @@ class TestAuthenticationAPI:
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "Email already registered" in response.json()["detail"]
|
||||
assert response.status_code == 409 # Changed from 400 to 409
|
||||
data = response.json()
|
||||
assert data["error_code"] == "USER_ALREADY_EXISTS"
|
||||
assert "Email already exists" in data["message"]
|
||||
assert data["field"] == "email"
|
||||
|
||||
def test_register_user_duplicate_username(self, client, test_user):
|
||||
"""Test registration with duplicate username"""
|
||||
@@ -48,8 +51,11 @@ class TestAuthenticationAPI:
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "Username already taken" in response.json()["detail"]
|
||||
assert response.status_code == 409 # Changed from 400 to 409
|
||||
data = response.json()
|
||||
assert data["error_code"] == "USER_ALREADY_EXISTS"
|
||||
assert "Username already taken" in data["message"]
|
||||
assert data["field"] == "username"
|
||||
|
||||
def test_login_success(self, client, test_user):
|
||||
"""Test successful login"""
|
||||
@@ -73,9 +79,11 @@ class TestAuthenticationAPI:
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
assert "Incorrect username or password" in response.json()["detail"]
|
||||
data = response.json()
|
||||
assert data["error_code"] == "INVALID_CREDENTIALS"
|
||||
assert "Invalid username or password" in data["message"]
|
||||
|
||||
def test_login_nonexistent_user(self, client, db): # Added db fixture
|
||||
def test_login_nonexistent_user(self, client, db):
|
||||
"""Test login with nonexistent user"""
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
@@ -83,6 +91,28 @@ class TestAuthenticationAPI:
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
data = response.json()
|
||||
assert data["error_code"] == "INVALID_CREDENTIALS"
|
||||
|
||||
def test_login_inactive_user(self, client, db, test_user):
|
||||
"""Test login with inactive user account"""
|
||||
# Manually deactivate the user for this test
|
||||
test_user.is_active = False
|
||||
db.commit()
|
||||
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={"username": test_user.username, "password": "testpass123"},
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
data = response.json()
|
||||
assert data["error_code"] == "USER_NOT_ACTIVE"
|
||||
assert "User account is not active" in data["message"]
|
||||
|
||||
# Reactivate for other tests
|
||||
test_user.is_active = True
|
||||
db.commit()
|
||||
|
||||
def test_get_current_user_info(self, client, auth_headers, test_user):
|
||||
"""Test getting current user info"""
|
||||
|
||||
Reference in New Issue
Block a user