Skip to content

Commit

Permalink
conflict; user already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
quirrelHK committed Mar 6, 2024
1 parent 1498292 commit 90d9970
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/routers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=schemas.UserOut) # Default status code changed, first validate the pydantic model and only show that to the user (prevent sharing unneccesary data)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):


if not db.query(models.User).filter(models.User.email == user.email).first() is None:
raise HTTPException(status_code=status.HTTP_409_CONFLICT,
detail=f"Username already exists.")

hashed_password = utils.hash(user.password)
user.password = hashed_password
new_user = models.User(**user.model_dump())

db.add(new_user)
db.commit()
db.refresh(new_user)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ def test_incorrect_login(client, test_user, email, password, status_code):
res = client.post("/login",
data={"username": email, "password": password})

assert res.status_code == status_code
assert res.status_code == status_code

def test_user_conflict_user_already_exists(client, test_user):
res = client.post("/users/",
json={"email": test_user["email"], "password": "somePassword"})

assert res.status_code == 409

0 comments on commit 90d9970

Please sign in to comment.