Refactor AuthService for better error handling and CRUD usage
Replaced raw database queries with CRUD operations for consistency and modularity. Enhanced error handling by adding detailed exception messages and logging for failed actions, such as authentication and registration. Updated tests to reflect new exception-based error handling approach.
This commit is contained in:
@@ -32,13 +32,14 @@ class TestAuthServiceAuthentication:
|
||||
|
||||
def test_authenticate_nonexistent_user(self, db_session):
|
||||
"""Test authenticating with an email that doesn't exist"""
|
||||
user = AuthService.authenticate_user(
|
||||
db=db_session,
|
||||
email="nonexistent@example.com",
|
||||
password="password"
|
||||
)
|
||||
with pytest.raises(AuthenticationError):
|
||||
user = AuthService.authenticate_user(
|
||||
db=db_session,
|
||||
email="nonexistent@example.com",
|
||||
password="password"
|
||||
)
|
||||
|
||||
assert user is None
|
||||
assert user is None
|
||||
|
||||
def test_authenticate_with_wrong_password(self, db_session, mock_user):
|
||||
"""Test authenticating with the wrong password"""
|
||||
@@ -48,13 +49,14 @@ class TestAuthServiceAuthentication:
|
||||
db_session.commit()
|
||||
|
||||
# Authenticate with wrong password
|
||||
user = AuthService.authenticate_user(
|
||||
db=db_session,
|
||||
email=mock_user.email,
|
||||
password="WrongPassword123"
|
||||
)
|
||||
with pytest.raises(AuthenticationError):
|
||||
user = AuthService.authenticate_user(
|
||||
db=db_session,
|
||||
email=mock_user.email,
|
||||
password="WrongPassword123"
|
||||
)
|
||||
|
||||
assert user is None
|
||||
assert user is None
|
||||
|
||||
def test_authenticate_inactive_user(self, db_session, mock_user):
|
||||
"""Test authenticating an inactive user"""
|
||||
|
||||
Reference in New Issue
Block a user