async def update_current_user( user_update: schemas.UserUpdate = Body(..., embed=True, alias="user"), current_user: schemas.UserDB = Depends(deps.get_current_user()), ) -> schemas.UserResponse: if user_update.username and user_update.username != current_user.username: user_db = await crud_user.get_user_by_username( username=user_update.username) if user_db: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="user with this username already exists", ) if user_update.email and user_update.email != current_user.email: user_db = await crud_user.get_user_by_email(email=user_update.email) if user_db: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="user with this email already exists", ) user_id = await crud_user.update(user_id=current_user.id, payload=user_update) user_db = await crud_user.get(user_id) token = security.create_access_token(current_user.id) return schemas.UserResponse(user=schemas.UserWithToken( username=user_db.username, # type: ignore email=user_db.email, # type: ignore bio=user_db.bio, # type: ignore image=user_db.image, # type: ignore token=token, ))
async def update_current_user( user_update: schemas.UserUpdate = Body(..., embed=True, alias="user"), current_user: models.User = Depends(deps.get_current_user), db: Session = Depends(deps.get_db), ) -> schemas.UserResponse: if user_update.username and user_update.username != current_user.username: user = crud.user.get_user_by_username(db, username=user_update.username) if user: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="user with this username already exists", ) if user_update.email and user_update.email != current_user.email: user = crud.user.get_user_by_email(db, email=user_update.email) if user: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="user with this email already exists", ) user = crud.user.update(db, db_obj=current_user, obj_in=user_update) token = security.create_access_token(user.id) return schemas.UserResponse(user=schemas.UserWithToken( username=user.username, email=user.email, bio=user.bio, image=user.image, token=token, ))
async def test_user_login_success( async_client: AsyncClient, test_user: schemas.UserDB ) -> None: login_data = {"user": {"email": test_user.email, "password": TEST_USER_PASSWORD}} r = await async_client.post(f"{API_AUTHENTICATION}/login", json=login_data) assert r.status_code == status.HTTP_200_OK user_response = schemas.UserResponse(**r.json()) assert_user_response(expected=test_user, actual=user_response)
async def test_retrieve_current_user_success( async_client: AsyncClient, test_user: schemas.UserDB, token: str ) -> None: headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {token}"} r = await async_client.get(API_USERS, headers=headers) assert r.status_code == status.HTTP_200_OK user_response = schemas.UserResponse(**r.json()) assert_user_response(expected=test_user, actual=user_response)
async def retrieve_current_user( current_user: schemas.UserDB = Depends(deps.get_current_user()), ) -> schemas.UserResponse: token = security.create_access_token(current_user.id) return schemas.UserResponse(user=schemas.UserWithToken( username=current_user.username, email=current_user.email, bio=current_user.bio, image=current_user.image, token=token, ))
async def login( user_login: schemas.LoginUser = Body(..., embed=True, alias="user", name="Credentials to use"), ) -> schemas.UserResponse: user = await crud_user.authenticate(email=user_login.email, password=user_login.password) if not user: raise HTTPException(status_code=400, detail="Incorrect email or password") token = security.create_access_token(user.id) return schemas.UserResponse(user=schemas.UserWithToken( username=user.username, email=user.email, bio=user.bio, image=user.image, token=token, ))
async def register( user_in: schemas.UserCreate = Body(..., embed=True, alias="user"), ) -> schemas.UserResponse: user_db = await crud_user.get_user_by_email(email=user_in.email) if user_db: raise HTTPException( status_code=400, detail="The user with this username already exists in the system.", ) user_id = await crud_user.create(user_in) token = security.create_access_token(user_id) return schemas.UserResponse(user=schemas.UserWithToken( username=user_in.username, email=user_in.email, bio=user_in.bio, image=user_in.image, token=token, ))