def test_api_generation(self): """ Makes sure api keys are generated on registration """ resp = self.register_user("apiuser", "this_/is_OUR_secret") assert resp.status_code == 302 user = Account.objects(username="******").first() assert len(user.apikey) == 32 and len(user.secret) == 40
def get_account(value, field='username'): """ Retrieve an Account object. :Parameters: - `value`: Value to filter by. - `field`: Field to filter on. Default field is username. """ return Account.objects(**{field: value}).first()
def create_user(self, username, password, roles=[]): """ Shortcut for creating users. """ if username not in self._created_users: self._created_users.append(username) self.account = Account.objects(username=username).first() if self.account: return account = Account() account.username = username account.password = generate_password_hash( password, application.app.config['BCRYPT_LOG_ROUNDS'] ) account.active = True account.roles = roles account.save() self.account = account
def __call__(self, form, field): if Account.objects(username=field.data).first(): raise ValidationError('Username is not available.')
def delete_user(username): for account in Account.objects(username=username): account.delete()
def create_user(username, password, email=None, roles=[]): """ Create a new user :Parameters: - `username`: User Name - `password`: Plain-text password - `roles`: A list of roles to assign the user - `email`: The user's email address """ new_user = Account() new_user.username = username new_user.set_password(password) if email is not None: new_user.email = email.strip() new_user.roles = roles new_user.active = True new_user.validate() new_user.save() return User(username)