From 2fa26a24bd34239670cc9dc23fee8806588e4886 Mon Sep 17 00:00:00 2001 From: MadelaineJ <68253853+MadelaineJ@users.noreply.github.com> Date: Fri, 9 Feb 2024 14:13:09 -0800 Subject: [PATCH] updating sign-up route to handle user groups (#95) Problem sign up route doesn't handle or expect a user group Solution - sign-up now expects user group - sign-up puts user in appropriate db given user_group - created 1 unit test for valid sign in - TODO: write tests for invalid sign in Ticket URL https://mediform.atlassian.net/browse/MEDI-58 Documentation NA Tests Run - make all && make api && make testapi --- app/api/routes/register.py | 20 +++++++++++++++----- app/api/tests/test_register.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 app/api/tests/test_register.py diff --git a/app/api/routes/register.py b/app/api/routes/register.py index 7f0a3b6..a6ba88b 100644 --- a/app/api/routes/register.py +++ b/app/api/routes/register.py @@ -6,7 +6,7 @@ from api.schemas.users import UserLogin, UserLoginResponse from api.models.user import create_user, get_user_by_email from api.main.auth import generate_auth_token -from api.main.database import get_db +from api.main.database import db_functions router = APIRouter() @@ -14,22 +14,32 @@ @router.post( "/sign-up", status_code=200, response_model=UserLoginResponse, name="sign-up" ) -def sign_up(user: UserLogin, db: Session = Depends(get_db)) -> Any: +def sign_up(user: UserLogin) -> Any: """ Create a new user if a user with that email doesn't exist. Otherwise raise exceptions. """ + # get the correct db given the user group + if user.user_group and user.user_group in db_functions: + db_generator = db_functions[user.user_group]() + db = next(db_generator) + else: + raise HTTPException(status_code=400, detail="Invalid user group.") + found_user = get_user_by_email(db, str(user.email)) if found_user: raise HTTPException( status_code=409, detail="User with that email already exists." ) - user = create_user(db, email=str(user.email), password=user.password) - if not user: + # otherwise if user doesn't exist + newUser = create_user(db, email=str(user.email), password=user.password) + if not newUser: raise HTTPException( status_code=500, detail="Unable to create user at this time." ) - token = generate_auth_token(data={"sub": user.email}) + token = generate_auth_token( + data={"sub": newUser.email}, user_group=user.user_group + ) return UserLoginResponse(access_token=token) diff --git a/app/api/tests/test_register.py b/app/api/tests/test_register.py new file mode 100644 index 0000000..c3fdac9 --- /dev/null +++ b/app/api/tests/test_register.py @@ -0,0 +1,29 @@ +import pytest +from fastapi.testclient import TestClient + +from api.constants import MEDICAL +from api.models.user import create_user +from api.main.database import SessionLocal + + +@pytest.mark.needs(postgres=True) +def test_valid_register(client: TestClient) -> None: + """ + Test that a new user is able to sign up successfully + """ + + resp = client.post( + "/api/sign-up", + json={ + "email": "testsignup@gmail.com", + "password": "Testing-123", + "user_group": MEDICAL, + }, + ) + resp_data = resp.json() + assert ( + resp.status_code == 200 + ), "Error while signing up test user" + str(resp_data) + assert ( + "access_token" in resp_data + ), "Access token was not provided after signing up a valid user"