Example #1
0
    def setUp(self):
        '''called before each test'''
        # admin is already in the db
        user = UserRoles.get('user')
        manager_user = UserRoles.get('manager')

        db.session.add(
            UserDetails(
                first_name='user_first_name',
                last_name='user_last_name',
                username='******',
                email='*****@*****.**',
                password=UserDetails.generate_hash('user'),
                role_id=user['id'],
                enabled=True
            )
        )

        db.session.add(
            UserDetails(
                first_name='manager_first_name',
                last_name='manager_last_name',
                username='******',
                email='*****@*****.**',
                password=UserDetails.generate_hash('manager'),
                role_id=manager_user['id'],
                enabled=True
            )
        )
        db.session.commit()
Example #2
0
def create_user(first_name, last_name, username, email, password):
    '''create a new user'''
    if not first_name or not last_name or not username or not email or not password:
        log.error(
            f'invalid input: {first_name}, {last_name}, {username}, {email}, {password}'
        )
        raise ValueError('Invalid input values!')

    _check_name_field(first_name, 3)
    _check_name_field(last_name, 3)
    _check_name_field(username)
    _check_email_address_format(email)
    _check_passwd_field(password)

    user = UserDetails.get(username)
    if user:
        raise error.StorageErrorConflict('username already exists')

    user_email = db.session.query(UserDetails).filter_by(
        email=email).one_or_none()
    if user_email:
        raise error.StorageErrorConflict('email already registered')

    #user_role = get_user_role(role)

    new_user = UserDetails(first_name=first_name,
                           last_name=last_name,
                           username=username,
                           email=email,
                           password=UserDetails.generate_hash(password))

    try:
        db.session.add(new_user)
        db.session.commit()
    except sqlalchemy.exc.SQLAlchemyError as ex:
        db.session.rollback()
        log.error(f'Could not reate user {username}: {ex}')
        raise error.StorageError(f'Could not add user {username}!')

    log.info(f'User {username} created')

    user_info = new_user.to_dict()
    return user_info
Example #3
0
    def test_get_all_users_inactive_account(self):
        from app.storage.user_details import UserDetails
        db.session.add(
            UserDetails(first_name='test',
                        last_name='test',
                        username='******',
                        email='*****@*****.**',
                        password=UserDetails.generate_hash('test'),
                        role_id=1,
                        enabled=False))
        db.session.commit()

        username = '******'
        passw = 'test'

        response = self.client.open('/api/v1/auth/login',
                                    method='POST',
                                    content_type='application/json',
                                    data=json.dumps({
                                        "username": username,
                                        "password": passw
                                    }))

        self.assertStatus(response, 401)
Example #4
0
def update_user(username, req_username, permission, **kwargs):
    '''update an existing user'''
    _validate_user_request(username, req_username, permission)

    first_name = kwargs.get('first_name')
    last_name = kwargs.get('last_name')
    email = kwargs.get('email')
    password = kwargs.get('password')
    role = kwargs.get('role')

    role_id = None
    if role:
        if not permission == UserRolesEnum.user_all.value:
            raise error.PermissionsError(
                'insufficient permissions to update role')
        role_details = UserRoles.get(role)
        if not role_details:
            raise error.RecordNotFoundError(f'role {role} not found')
        role_id = role_details['id']

    if email:
        _check_email_address_format(email)

    user = UserDetails.get(username)
    if not user:
        raise error.RecordNotFoundError(f'username {username} not found')

    changed = False
    try:
        if role_id and user.role_id != role_id:
            user.role_id = role_id
            changed = True

        if first_name and user.first_name != first_name:
            _check_name_field(first_name, 3)
            user.first_name = first_name
            changed = True

        if last_name and user.last_name != last_name:
            _check_name_field(last_name, 3)
            user.last_name = last_name
            changed = True

        if email and user.email != email:
            _check_email_address_format(email)
            user.email = email
            changed = True

        if password and not UserDetails.verify_hash(password, user.password):
            _check_passwd_field(password)
            user.password = UserDetails.generate_hash(password)
            changed = True

        if changed:

            db.session.add(user)
            db.session.commit()
    except sqlalchemy.exc.SQLAlchemyError as ex:
        db.session.rollback()
        log.error(f'Could not update user {username}: {ex}')
        raise error.StorageError(f'Could not update user {username}!')
    except Exception as ex:
        db.session.rollback()
        log.error(f'Could not update user {username}: {ex}')
        raise

    log.info(f'User {username} updated')