async def test_queries(sqlalchemy_user_db): user = BaseUserDB( id="111", email="*****@*****.**", hashed_password=get_password_hash("guinevere"), ) # Create user_db = await sqlalchemy_user_db.create(user) assert user_db.id is not None assert user_db.is_active is True assert user_db.is_superuser is False assert user_db.email == user.email # Update user_db.is_superuser = True await sqlalchemy_user_db.update(user_db) # Get by id id_user = await sqlalchemy_user_db.get(user.id) assert id_user.id == user_db.id assert id_user.is_superuser is True # Get by email email_user = await sqlalchemy_user_db.get_by_email(user.email) assert email_user.id == user_db.id # List users = await sqlalchemy_user_db.list() assert len(users) == 1 first_user = users[0] assert first_user.id == user_db.id # Exception when inserting existing email with pytest.raises(sqlite3.IntegrityError): await sqlalchemy_user_db.create(user) # Exception when inserting non-nullable fields with pytest.raises(sqlite3.IntegrityError): wrong_user = BaseUserDB(id="222", hashed_password="******") await sqlalchemy_user_db.create(wrong_user) # Unknown user unknown_user = await sqlalchemy_user_db.get_by_email("*****@*****.**") assert unknown_user is None # Delete user await sqlalchemy_user_db.delete(user) deleted_user = await sqlalchemy_user_db.get(user.id) assert deleted_user is None
def superuser() -> BaseUserDB: return BaseUserDB( id="ccc", email="*****@*****.**", hashed_password=viviane_password_hash, is_superuser=True, )
def inactive_user() -> BaseUserDB: return BaseUserDB( id="bbb", email="*****@*****.**", hashed_password=angharad_password_hash, is_active=False, )
async def _update_user(user: models.BaseUserDB, update_dict: Dict[str, Any]): for field in update_dict: if field == "password": hashed_password = get_password_hash(update_dict[field]) user.hashed_password = hashed_password else: setattr(user, field, update_dict[field]) return await user_db.update(user)
async def _update_user(user: models.BaseUserDB, update_dict: Dict[str, Any], request: Request): for field in update_dict: if field == "password": hashed_password = get_password_hash(update_dict[field]) user.hashed_password = hashed_password else: setattr(user, field, update_dict[field]) updated_user = await user_db.update(user) if after_update: await run_handler(after_update, updated_user, update_dict, request) return updated_user
async def on_after_register(user: BaseUserDB, request: Request): personal_info_dao: IPersonalInfoDAO = instance(IPersonalInfoDAO) street_id = get_street_id_by_name('sntzhd', user.street) payer_id = '{}-{}-{}'.format( aliases.get('sntzhd').get('payee_inn')[4:8], street_id, user.numsite) await personal_info_dao.create( PersonalInfoDB(**user.dict(), user_id=user.id, street_name=user.street, snt_alias='sntzhd', payer_id=payer_id)) print('on_after_register')
async def test_queries(mongodb_user_db): user = BaseUserDB( id="111", email="*****@*****.**", hashed_password=get_password_hash("guinevere"), ) # Create user_db = await mongodb_user_db.create(user) assert user_db.id is not None assert user_db.is_active is True assert user_db.is_superuser is False assert user_db.email == user.email # Update user_db.is_superuser = True await mongodb_user_db.update(user_db) # Get by id id_user = await mongodb_user_db.get(user.id) assert id_user.id == user_db.id assert id_user.is_superuser is True # Get by email email_user = await mongodb_user_db.get_by_email(user.email) assert email_user.id == user_db.id # List users = await mongodb_user_db.list() assert len(users) == 1 first_user = users[0] assert first_user.id == user_db.id # Exception when inserting existing email with pytest.raises(pymongo.errors.DuplicateKeyError): await mongodb_user_db.create(user) # Unknown user unknown_user = await mongodb_user_db.get_by_email("*****@*****.**") assert unknown_user is None # Delete user await mongodb_user_db.delete(user) deleted_user = await mongodb_user_db.get(user.id) assert deleted_user is None
async def update(self, user: BaseUserDB) -> BaseUserDB: await self.collection.replace_one({"id": user.id}, user.dict()) return user
async def create(self, user: BaseUserDB) -> BaseUserDB: await self.collection.insert_one(user.dict()) return user
async def get_by_email(self, email: str) -> Optional[BaseUserDB]: user = await self.collection.find_one({"email": email}) return BaseUserDB(**user) if user else None
async def get(self, id: str) -> Optional[BaseUserDB]: user = await self.collection.find_one({"id": id}) return BaseUserDB(**user) if user else None
async def list(self) -> List[BaseUserDB]: return [BaseUserDB(**user) async for user in self.collection.find()]
async def verify_user(user: models.BaseUserDB) -> models.BaseUserDB: if user.is_verified: raise UserAlreadyVerified() user.is_verified = True return await user_db.update(user)
async def update(self, user: BaseUserDB) -> BaseUserDB: query = (self.users.update().where(self.users.c.id == user.id).values( **user.dict())) await self.database.execute(query) return user
async def create(self, user: BaseUserDB) -> BaseUserDB: query = self.users.insert().values(**user.dict()) await self.database.execute(query) return user
def user() -> BaseUserDB: return BaseUserDB( id="aaa", email="*****@*****.**", hashed_password=guinevere_password_hash, )