def __init__( self, db: BaseUserDatabase, auth_backends: Sequence[BaseAuthentication], user_model: Type[models.BaseUser], user_create_model: Type[models.BaseUserCreate], user_update_model: Type[models.BaseUserUpdate], user_db_model: Type[models.BaseUserDB], ): self.db = db self.authenticator = Authenticator(auth_backends, db) self._user_model = user_model self._user_db_model = user_db_model self._user_create_model = user_create_model self._user_update_model = user_update_model self._user_db_model = user_db_model self.create_user = get_create_user(db, user_db_model) self.get_current_user = self.authenticator.get_current_user self.get_current_active_user = self.authenticator.get_current_active_user self.get_current_superuser = self.authenticator.get_current_superuser self.get_optional_current_user = self.authenticator.get_optional_current_user self.get_optional_current_active_user = ( self.authenticator.get_optional_current_active_user) self.get_optional_current_superuser = ( self.authenticator.get_optional_current_superuser)
async def create_users(): # # Generate random email # with open('/usr/share/dict/cracklib-small', 'r') as w: # words = w.read().splitlines() # random_word = random.choice(words) # host = random.choice(['gmail', 'yahoo', 'amazon', 'yahoo', 'microsoft', 'google']) # tld = random.choice(['org', 'com', 'net', 'io', 'com.ph', 'co.uk']) # email = f'{random_word}@{host}.{tld}' # from app.auth import userdb async with in_transaction(): # User 1 userdata = UserCreate(email=EmailStr(VERIFIED_EMAIL_DEMO), password='******') create_user = get_create_user(userdb, UserDB) created_user = await create_user(userdata, safe=True) ret = created_user groups = await Group.filter(name__in=s.USER_GROUPS) user = await UserMod.get(pk=created_user.id) user.is_verified = True user.is_superuser = True await user.save() await user.groups.add(*groups) # Perms for User 1 ll = [] userperms = await Permission.filter(code__in=enchance_only_perms ).only('id') for perm in userperms: ll.append(UserPermissions(user=user, permission=perm, author=user)) await UserPermissions.bulk_create(ll) # Group or User 1 # await user.add_group('StaffGroup') # User 2 userdata = UserCreate(email=EmailStr(UNVERIFIED_EMAIL_DEMO), password='******') create_user = get_create_user(userdb, UserDB) created_user = await create_user(userdata, safe=True) groups = await Group.filter(name__in=s.USER_GROUPS) user = await UserMod.get(pk=created_user.id) await user.groups.add(*groups) return ret
async def test_app_client( mock_user_db, after_register, get_test_client) -> AsyncGenerator[httpx.AsyncClient, None]: create_user = get_create_user(mock_user_db, UserDB) register_router = get_register_router( create_user, User, UserCreate, after_register, ) app = FastAPI() app.include_router(register_router) async for client in get_test_client(app): yield client
def __init__( self, db: BaseUserDatabase, auth_backends: Sequence[BaseAuthentication], user_model: Type[models.BaseUser], user_create_model: Type[models.BaseUserCreate], user_update_model: Type[models.BaseUserUpdate], user_db_model: Type[models.BaseUserDB], validate_password: Optional[ValidatePasswordProtocol] = None, ): self.db = db self.authenticator = Authenticator(auth_backends, db) self._user_model = user_model self._user_db_model = user_db_model self._user_create_model = user_create_model self._user_update_model = user_update_model self._user_db_model = user_db_model self.create_user = get_create_user(db, user_db_model) self.verify_user = get_verify_user(db) self.get_user = get_get_user(db) self.validate_password = validate_password self.current_user = self.authenticator.current_user self.get_current_user = self.authenticator.get_current_user self.get_current_active_user = self.authenticator.get_current_active_user self.get_current_verified_user = self.authenticator.get_current_verified_user self.get_current_superuser = self.authenticator.get_current_superuser self.get_current_verified_superuser = ( self.authenticator.get_current_verified_superuser) self.get_optional_current_user = self.authenticator.get_optional_current_user self.get_optional_current_active_user = ( self.authenticator.get_optional_current_active_user) self.get_optional_current_verified_user = ( self.authenticator.get_optional_current_verified_user) self.get_optional_current_superuser = ( self.authenticator.get_optional_current_superuser) self.get_optional_current_verified_superuser = ( self.authenticator.get_optional_current_verified_superuser)
def create_user(mock_user_db, ) -> CreateUserProtocol: return get_create_user(mock_user_db, UserDB)