From d9256c335447f5f0e8d4e90d87a6155582d6bf78 Mon Sep 17 00:00:00 2001 From: Rudolf Braun Date: Tue, 25 Jun 2024 11:14:30 +0200 Subject: [PATCH] Update get_user_by_email add para make_new_user_if_none:boolean defaults to True is added to if user is None add test for make_new_user_if_none=False in tests.read.test_get_user_by_email Signed-off-by: Rudolf Braun --- pvsite_datamodel/read/user.py | 5 +++-- tests/read/test_get_user_by_email.py | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pvsite_datamodel/read/user.py b/pvsite_datamodel/read/user.py index b626cc3..aaac86e 100644 --- a/pvsite_datamodel/read/user.py +++ b/pvsite_datamodel/read/user.py @@ -11,18 +11,19 @@ logger = logging.getLogger(__name__) -def get_user_by_email(session: Session, email: str): +def get_user_by_email(session: Session, email: str, make_new_user_if_none: bool = True): """ Get user by email. If user does not exist, make one. :param session: database session :param email: email of user + :param make_new_user_if_none: make user with email if doesn't exist :return: user object """ user = session.query(UserSQL).filter(UserSQL.email == email).first() - if user is None: + if user is None and make_new_user_if_none == True: logger.info(f"User with email {email} not found, so making one") # making a new site group diff --git a/tests/read/test_get_user_by_email.py b/tests/read/test_get_user_by_email.py index c239d7a..ec743ed 100644 --- a/tests/read/test_get_user_by_email.py +++ b/tests/read/test_get_user_by_email.py @@ -23,3 +23,10 @@ def test_get_user_by_email_with_users(self, db_session): user = get_user_by_email(session=db_session, email="test_1@test.com") assert user.email == "test_1@test.com" assert len(db_session.query(UserSQL).all()) == 2 + + def test_get_user_by_email_no_user_maker_user_false(self, db_session): + user = get_user_by_email( + session=db_session, email="test@test.com", make_new_user_if_none=False + ) + assert user == None + assert len(db_session.query(UserSQL).all()) == 0