def test_check_if_user_is_superuser_normal_user(db: Session) -> None: username = random_email() password = random_lower_string() user_in = UserCreate(email=username, password=password) user = user_repo.create(db, obj_in=user_in) is_superuser = user_repo.is_superuser(user) assert is_superuser is False
def test_check_if_user_is_active_inactive(db: Session) -> None: email = random_email() password = random_lower_string() user_in = UserCreate(email=email, password=password, disabled=True) user = user_repo.create(db, obj_in=user_in) is_active = user_repo.is_active(user) assert is_active
def test_check_if_user_is_superuser(db: Session) -> None: email = random_email() password = random_lower_string() user_in = UserCreate(email=email, password=password, is_superuser=True) user = user_repo.create(db, obj_in=user_in) is_superuser = user_repo.is_superuser(user) assert is_superuser is True
def test_create_user(db: Session) -> None: email = random_email() password = random_lower_string() user_in = UserCreate(email=email, password=password) user = user_repo.create(db, obj_in=user_in) assert user.email == email assert hasattr(user, "hashed_password")
def test_get_user(db: Session) -> None: password = random_lower_string() username = random_email() user_in = UserCreate(email=username, password=password, is_superuser=True) user = user_repo.create(db, obj_in=user_in) user_2 = user_repo.get(db, id=user.id) assert user_2 assert user.email == user_2.email assert jsonable_encoder(user) == jsonable_encoder(user_2)
def test_authenticate_user(db: Session) -> None: email = random_email() password = random_lower_string() user_in = UserCreate(email=email, password=password) user = user_repo.create(db, obj_in=user_in) authenticated_user = user_repo.authenticate(db, email=email, password=password) assert authenticated_user assert user.email == authenticated_user.email
def test_update_user(db: Session) -> None: password = random_lower_string() email = random_email() user_in = UserCreate(email=email, password=password, is_superuser=True) user = user_repo.create(db, obj_in=user_in) new_password = random_lower_string() user_in_update = UserUpdate(password=new_password, is_superuser=True) user_repo.update(db, db_obj=user, obj_in=user_in_update) user_2 = user_repo.get(db, id=user.id) assert user_2 assert user.email == user_2.email assert verify_password(new_password, user_2.hashed_password)
def init_db() -> None: # Tables should be created with Alembic migrations # But if you don't want to use migrations, create # the tables un-commenting the next line base.Base.metadata.create_all(bind=engine) with SessionLocal() as db: user = user_repo.get_by_email(db, email=settings.FIRST_SUPERUSER) if not user: user_in = UserCreate( email=settings.FIRST_SUPERUSER, password=settings.FIRST_SUPERUSER_PASSWORD, is_superuser=True, ) user = user_repo.create(db, obj_in=user_in) # noqa: F841
def authentication_token_from_email(*, client: TestClient, email: str, db: Session) -> Dict[str, str]: """ Return a valid token for the user with given email. If the user doesn't exist it is created first. """ password = random_lower_string() user = user_repo.get_by_email(db, email=email) if not user: user_in_create = UserCreate(email=email, password=password) user = user_repo.create(db, obj_in=user_in_create) else: user_in_update = UserUpdate(password=password) user = user_repo.update(db, db_obj=user, obj_in=user_in_update) return user_authentication_headers(client=client, email=email, password=password)
def register_user( *, image: Image = Depends(upload_image), db: Session = Depends(database.get_db), password: str = Body(...), email: EmailStr = Body(...), full_name: str = Body(None), ) -> Any: """ Create new user. """ user = user_repo.get_by_email(db, email=email) if user: raise HTTPException( status_code=400, detail="The user with this username already exists in the system", ) user_in = UserCreate(password=password, email=email, full_name=full_name) user = user_repo.create(db, obj_in=user_in) user_repo.add_profile_pic(db, db_obj=user, image=image) return user
def create_random_user(db: Session) -> User: email = random_email() password = random_lower_string() user_in = UserCreate(username=email, email=email, password=password) user = user_repo.create(db=db, obj_in=user_in) return user