Пример #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()
Пример #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
Пример #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)