Skip to content

Commit

Permalink
updating sign-up route to handle user groups (#95)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MadelaineJ authored Feb 9, 2024
1 parent 7183439 commit 2fa26a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
20 changes: 15 additions & 5 deletions app/api/routes/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,40 @@
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()


@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)
29 changes: 29 additions & 0 deletions app/api/tests/test_register.py
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"

0 comments on commit 2fa26a2

Please sign in to comment.