Beispiel #1
0
def test_exception_on_creating_user_with_not_unique_username(tables):
    with pytest.raises(IntegrityError):
        duplicated_username = '******'
        password = '******'

        existing_user = User(username=duplicated_username, password=password)
        duplicated_user = User(username=duplicated_username, password=password)

        existing_user.save()
        duplicated_user.save()
Beispiel #2
0
def do_create(user: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]:
    try:
        new_user = User(username=user['username'],
                        email=user['email'],
                        password=user['password'],
                        roles=[Role(name='user')])
        new_user.save()

        try:
            default_groups = Group.get_default_groups()
            for group in default_groups:
                group.add_user(new_user)
        except Exception:
            log.warning(
                "User has been created, but not added to default group.")
    except AssertionError as e:
        content = {
            'msg': USER['create']['failure']['invalid'].format(reason=e)
        }
        status = 422
    except IntegrityError:
        content = {'msg': USER['create']['failure']['duplicate']}
        status = 409
    except Exception as e:
        content = {'msg': GENERAL['internal_error'] + str(e)}
        status = 500
    else:
        content = {
            'msg': USER['create']['success'],
            'user': new_user.as_dict(include_private=True)
        }
        status = 201
    finally:
        return content, status
Beispiel #3
0
def do_create(user):
    try:
        new_user = User(username=user['username'],
                        email=user['email'],
                        password=user['password'],
                        roles=[Role(name='user')])
        new_user.save()
    except AssertionError as e:
        content = {'msg': R['create']['failure']['invalid'].format(reason=e)}
        status = 422
    except IntegrityError:
        content = {'msg': R['create']['failure']['duplicate']}
        status = 409
    except Exception as e:
        content = {'msg': G['internal_error'] + str(e)}
        status = 500
    else:
        content = {'msg': R['create']['success'], 'user': new_user.as_dict}
        status = 201
    finally:
        return content, status
Beispiel #4
0
                                           spawnAt=timenow,
                                           terminateAt=timenow))
     print(content, status)
 elif action == '7':
     task_id = input('ID > ')
     content, status = business_destroy(int(task_id))
     print(content, status)
 elif action == '8':
     import string
     import random
     rand_str = lambda: ''.join(
         random.choice(string.ascii_uppercase) for x in range(8))
     random_username, random_email = rand_str(
     ), rand_str() + '@test.com'
     user = User(password='******',
                 email=random_email,
                 username=random_username)
     user.save()
     for _ in range(3):
         # TODO add spawnAt, terminateAt
         content, status = business_create(
             dict(userId=user.id, hostname=host, command=cmd))
         print(content, status)
     print(user)
 elif action == '9':
     user_id = input('User ID > ')
     user = User.get(user_id)
     print('[BEFORE] User has {} tasks.'.format(len(user.tasks)))
     user.destroy()
     tasks_after = Task.query.filter(Task.user_id == user_id).all()
     print('[AFTER] User has now {} tasks.'.format(len(tasks_after)))
Beispiel #5
0
def new_admin():
    return User(username='******',
                password='******',
                roles=[Role(name='user'),
                       Role(name='admin')])
Beispiel #6
0
def new_user_2():
    return User(username='******',
                password='******',
                roles=[Role(name='user')])
Beispiel #7
0
def new_user():
    return User(username='******',
                password='******',
                roles=[Role(name='user')])
Beispiel #8
0
def test_exception_on_creating_user_with_no_password(tables):
    with pytest.raises(IntegrityError):
        User(username='******').save()
Beispiel #9
0
def test_exception_on_creating_user_with_invalid_username(tables, test_name, test_username):
    with pytest.raises(AssertionError):
        User(username=test_username, password='******').save()
Beispiel #10
0
 def __init__(self):
     init_db()
     # Prepare empty ORM object
     self.new_user = User()
Beispiel #11
0
 def __init__(self):
     ensure_db_with_current_schema()
     self._check_restrictions()
     # Prepare empty ORM object
     self.new_user = User()