def create_or_update_user(username, password, avator, email, user_id=None): """Creates or updates an user. :username: a string object :password: a string object (plaintext) :avator: a string object :email: a string object :user_id: a str object. Indicates an update. :returns: a dict with the operation result """ if user_id: result = models.User(id=user_id, username=username, password=helpers.encrypt_password(password), avator=avator, email=email) _username = models.User.query.filter_by(id=user_id).first().username if result.username == _username or is_an_available_username(username): db.session.merge(result) db.session.commit() return {'updated': 'Updated the user {!r}.'.format(username)} return {'error': 'The user {!r} already exists.'.format(username)} if is_an_available_username(username) is False: return {'error': 'The user {!r} already exists.'.format(username)} result = models.User(username=username, password=helpers.encrypt_password(password), avator=avator, email=email) db.session.merge(result) db.session.commit() return {'created': 'Created the user {!r}.'.format(username)}
def create_or_update_user(username, password, user_id=None): """Creates or updates an user. :username: a string object :password: a string object (plaintext) :user_id: a str object. Indicates an update. :returns: a dict with the operation result """ if is_an_available_username(username) is False: return {'error': 'The user {!r} already exists.'.format(username)} try: query = {'id': user_id} if user_id else {'username': username} result = models.User.objects(**query).update( set__username=username, set__password=helpers.encrypt_password(password), upsert=True, full_result=True ) except Exception as e: return {'error': 'Error during the operation: {}'.format(e)} if result.get('updatedExisting') is False: return {'created': 'Created the user {!r}.'.format(username)} return {'updated': 'Updated the user {!r}.'.format(username)}
def create_or_update_user(username, password, user_id=None): """Creates or updates an user. :username: a string object :password: a string object (plaintext) :user_id: a str object. Indicates an update. :returns: a dict with the operation result """ if is_an_available_username(username) is False: return {'error': 'The user {!r} already exists.'.format(username)} try: query = {'id': user_id} if user_id else {'username': username} result = models.User.objects(**query).update( set__username=username, set__password=helpers.encrypt_password(password), upsert=True, full_result=True) except Exception as e: return {'error': 'Error during the operation: {}'.format(e)} if result.get('updatedExisting') is False: return {'created': 'Created the user {!r}.'.format(username)} return {'updated': 'Updated the user {!r}.'.format(username)}
def insert_user(user): if mongo.db.users.find_one({'username': user.get('username')}): abort(409, 'User already exists') user['password'] = encrypt_password(user.get('password', 'changeme')) if not mongo.db.users.insert_one(user).inserted_id: abort(422, 'Cannot create user') return json.loads(dumps(user))
def _seed_user(self): _user = User( username='******', password=helpers.encrypt_password('password'), email="*****@*****.**", avator="", ) db.session.add(_user) db.session.commit()
def mock_user(self): if not self._user: self._user = User( username='******', password=helpers.encrypt_password('password'), email="*****@*****.**", avator="", ) return self._user
def make_mock_user(username=None, password=None): """The real mock. Creates a object users.models.User .All parameters are optionals, by default uses the username 'mock-user', the password is the same value. :username: a string object. :password: a string object :returns: an users.models.User object """ nonlocal user user = models.User(username=username or 'mock-user', password=helpers.encrypt_password( password or 'mock-user')).save() return user
def make_mock_user(username=None, password=None): """The real mock. Creates a object users.models.User .All parameters are optionals, by default uses the username 'mock-user', the password is the same value. :username: a string object. :password: a string object :returns: an users.models.User object """ nonlocal user user = models.User( username=username or 'mock-user', password=helpers.encrypt_password(password or 'mock-user') ).save() return user
def test_if_check_password_and_encrypt_password_works_properly(sent): expected = encrypt_password(sent) assert check_password(sent, expected)
def test_verify_password(): hash = helpers.encrypt_password("password") assert helpers.verify_password("password", hash) is True assert helpers.verify_password("invalid", hash) is False
def test_encrypt_password(app): hash = helpers.encrypt_password('password') # hash 256 or 512, yep, a little bit hard code for now assert 75 <= len(hash) <= 120