Skip to content

Commit

Permalink
Chane get_user_by_email to be case insensetive.
Browse files Browse the repository at this point in the history
  • Loading branch information
grabov committed Dec 14, 2024
1 parent 88c83cc commit e90143f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/app/crud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from typing import Any

from sqlmodel import Session, select
from sqlmodel import Session, func, select

from app.core.security import get_password_hash, verify_password
from app.models import Item, ItemCreate, User, UserCreate, UserUpdate
Expand Down Expand Up @@ -32,7 +32,7 @@ def update_user(*, session: Session, db_user: User, user_in: UserUpdate) -> Any:


def get_user_by_email(*, session: Session, email: str) -> User | None:
statement = select(User).where(User.email == email)
statement = select(User).where(func.lower(User.email) == func.lower(email))
session_user = session.exec(statement).first()
return session_user

Expand Down
24 changes: 24 additions & 0 deletions backend/app/tests/crud/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,27 @@ def test_update_user(db: Session) -> None:
assert user_2
assert user.email == user_2.email
assert verify_password(new_password, user_2.hashed_password)


def test_get_user_by_email(db: Session) -> None:
password = random_lower_string()
email = random_email()
user_in = UserCreate(email=email, password=password, is_superuser=True)
crud.create_user(session=db, user_create=user_in)
user = crud.get_user_by_email(session=db, email=email)
assert user
user_2 = db.get(User, user.id)
assert user_2
assert user.email == user_2.email


def test_get_user_by_email_case_insesetive(db: Session) -> None:
password = random_lower_string()
email = random_email().lower()
user_in = UserCreate(email=email, password=password, is_superuser=True)
crud.create_user(session=db, user_create=user_in)
user = crud.get_user_by_email(session=db, email=email.upper())
assert user
user_2 = db.get(User, user.id)
assert user_2
assert user.email == user_2.email

0 comments on commit e90143f

Please sign in to comment.