def authenticate_user(db: Session, username: str, password: str): user = get_user_by_email(db, username) if not user: return False if not verify_password(password, user.password): return False return create_access_token(user.id)
def test_user_creation_success(db_session, test_user): user_create_schema = test_user response = client.post("/users/", json=user_create_schema.dict()) user = get_user_by_email(db_session, user_create_schema.email) json_response = response.json() assert response.status_code == 200 assert json_response["email"] == user.email assert json_response["public_id"] == user.public_id
def get_current_user(database: Session = Depends(get_db), token: str = Depends(oauth2_scheme)): """ :return: schemas.UserInDB """ credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) try: payload = jwt.decode(token, SECRET_KEY, algorithms=[JWT_HASH_ALGORITHM]) email: str = payload.get("sub") if email is None: raise credentials_exception token_data = TokenData(email=email) except JWTError as jwt_exception: raise credentials_exception from jwt_exception user = get_user_by_email(database=database, email=token_data.email) if user is None: raise credentials_exception return UserInDB(**user.__dict__)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): db_user = crud.get_user_by_email(db, email=user.email) if db_user: raise HTTPException(status_code=400, detail="Email already registered") return crud.create_user(db=db, user=user)
async def create_user(user: schema.UserCreate, db: Session = Depends(get_db)): if crud.get_user_by_email(db, user.email): raise DuplicateResourceError(resource="user", value="email") return crud.create_user(db=db, user=user)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): db_user = crud.get_user_by_email(db=db, email=user.email) if db_user: return {"data": "", "resCode": 1, "msg": "已存在此用户"} return crud.create_user(db=db, user=user)
def test_update_application(test_application_instance): """ TEST PATCH /application/{application.id} """ application = test_application_instance.applications[0] # Unauthenticated response = test_application_instance.client.patch( "/application/%d/" % application.id ) assert response.status_code == 401 # Not Acceptable response = test_application_instance.client.patch( "/application/%d/" % application.id, json={"approved": "ACCEPTED"}, headers=test_application_instance. set_auth_from_user(test_application_instance.users[Roles.ADMIN]) ) assert response.status_code == 422 # Admin approved with test_application_instance.mail_instance.record_messages() as outbox: application = test_application_instance.applications[1] response = test_application_instance.client.patch( "/application/%d/" % application.id, json={"approved": True}, headers=test_application_instance. set_auth_from_user(test_application_instance.users[Roles.ADMIN]) ) assert response.status_code == 200 # Test User Creation assert get_user_by_email( database=test_application_instance.database_conn, email=application.email) is not None # Check mail assert len(outbox) == 1 outbox.pop() # Admin reject application = test_application_instance.applications[2] response = test_application_instance.client.patch( "/application/%d/" % application.id, json={"approved": False}, headers=test_application_instance. set_auth_from_user(test_application_instance.users[Roles.ADMIN]) ) assert response.status_code == 200 # Check mail assert len(outbox) == 1 # Admin approves rejected application response = test_application_instance.client.patch( "/application/%d/" % application.id, json={"approved": True}, headers=test_application_instance. set_auth_from_user(test_application_instance.users[Roles.ADMIN]) ) assert response.status_code == 400 # Other users try updating users = { Roles.MODERATOR: 403, Roles.USER: 403 } for application in test_application_instance.applications[3:]: test_application_instance.assert_user_permissions( users, method="PATCH", url="/application/%d/" % application.id, json_={"approved": randint(0, 9) > 4} )