Revert "Merge get_user into authenticate_user, it was only doing one db call"
ups, das war ja doch von mehr verwendet...
This reverts commit 0337a90f15.
This commit is contained in:
@@ -26,8 +26,14 @@ def verify_password(plain_password, hashed_password):
|
|||||||
def get_password_hash(password):
|
def get_password_hash(password):
|
||||||
return password_hash.hash(password)
|
return password_hash.hash(password)
|
||||||
|
|
||||||
def authenticate_user(db, username: str, password: str):
|
def get_user(db, username: str):
|
||||||
user = db.exec(select(UserDB).where(UserDB.name == username)).first()
|
user = db.exec(select(UserDB).where(UserDB.name == username)).first()
|
||||||
|
if user is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Username not found in get_user, this shouldn't happen")
|
||||||
|
return user
|
||||||
|
|
||||||
|
def authenticate_user(db, username: str, password: str):
|
||||||
|
user = get_user(db, username)
|
||||||
if not user:
|
if not user:
|
||||||
return False
|
return False
|
||||||
if not verify_password(password, user.passwordhash):
|
if not verify_password(password, user.passwordhash):
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import pytest
|
|||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from app.services.auth import (
|
from app.services.auth import (
|
||||||
verify_password, get_password_hash, authenticate_user,
|
verify_password, get_password_hash, get_user, authenticate_user,
|
||||||
create_access_token, get_current_user, auth_is_admin, create_first_user
|
create_access_token, get_current_user, auth_is_admin, create_first_user
|
||||||
)
|
)
|
||||||
from app.model.models import UserDB
|
from app.model.models import UserDB
|
||||||
@@ -24,6 +24,27 @@ def test_password_hashing():
|
|||||||
# Verify incorrect password
|
# Verify incorrect password
|
||||||
assert verify_password("wrong_password", hashed) is False
|
assert verify_password("wrong_password", hashed) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_user(db_session):
|
||||||
|
"""Test get_user function."""
|
||||||
|
from app.services.auth import get_password_hash
|
||||||
|
|
||||||
|
# Create a user
|
||||||
|
user = UserDB(name="testuser", passwordhash=get_password_hash("password"))
|
||||||
|
db_session.add(user)
|
||||||
|
db_session.commit()
|
||||||
|
|
||||||
|
# Get existing user
|
||||||
|
retrieved_user = get_user(db_session, "testuser")
|
||||||
|
assert retrieved_user is not None
|
||||||
|
assert retrieved_user.name == "testuser"
|
||||||
|
|
||||||
|
# Try to get non-existent user
|
||||||
|
with pytest.raises(HTTPException) as exc_info:
|
||||||
|
get_user(db_session, "nonexistent")
|
||||||
|
assert exc_info.value.status_code == status.HTTP_404_NOT_FOUND
|
||||||
|
|
||||||
|
|
||||||
def test_authenticate_user(db_session):
|
def test_authenticate_user(db_session):
|
||||||
"""Test user authentication."""
|
"""Test user authentication."""
|
||||||
from app.services.auth import get_password_hash
|
from app.services.auth import get_password_hash
|
||||||
|
|||||||
Reference in New Issue
Block a user