Example #1
0
def _db_users(session):
    user1 = models.User(
        username='******', hashed_password=make_password_hash('password1')
    )
    user2 = models.User(
        username='******', hashed_password=make_password_hash('password2')
    )

    session.add(user1)
    session.add(user2)
    session.commit()

    return user1, user2
Example #2
0
def create_user(session: Session, user: schemas.UserCreate) -> models.User:
    hashed_password = make_password_hash(user.password)
    db_user = models.User(username=user.username,
                          hashed_password=hashed_password)
    session.add(db_user)
    session.flush()
    return db_user
Example #3
0
def _db_new_user(session, db_users):
    username = '******'
    password = '******'

    new_user = models.User(
        username=username, hashed_password=make_password_hash(password)
    )
    session.add(new_user)
    session.commit()

    return new_user
def test_registration(unauth_client, session, db_users):
    response = unauth_client.post('/users',
                                  json={
                                      'username': '******',
                                      'password': '******'
                                  })
    users = session.query(models.User).all()
    new_user = session.query(models.User).filter_by(username='******').one()

    assert response.status_code == 201
    assert response.json()['username'] == 'new_user'
    assert new_user.hashed_password == make_password_hash('pass')
    assert len(users) == len(db_users) + 1
Example #5
0
def get_current_user(
        credentials: HTTPBasicCredentials = Depends(security),
        session: Session = Depends(get_session),
) -> schemas.User:
    db_user = get_user_by_username(username=credentials.username,
                                   session=session)

    if not db_user:
        raise UnauthorizedException

    correct_password = secrets.compare_digest(
        make_password_hash(credentials.password), db_user.hashed_password)

    if not correct_password:
        raise UnauthorizedException

    return schemas.User.from_orm(db_user)