def test_save_existent_user(postgresql_db):
    session = postgresql_db.session
    create_all(session)
    saved_user = User('*****@*****.**', 'bbb', 'Jane Doe', '111-1111', None,
                      False, False, False)
    sut = UserPersistence(postgresql_db)
    sut.save(saved_user)
    with pytest.raises(UserAlreadyRegisteredException):
        user_to_save = User('*****@*****.**', 'aaa', 'John Doe', '555-5555',
                            None, False, False, False)
        sut.save(user_to_save)
def test_save_password_user(postgresql_db):
    session = postgresql_db.session
    create_all(session)
    assert query_first_user(session) is None
    user_to_save = User('*****@*****.**', 'aaa', 'John Doe', '555-5555', None,
                        False, False, False)
    sut = UserPersistence(postgresql_db)
    sut.save(user_to_save)
    row = query_first_user(session)
    assert row is not None
    assert row[0] == '*****@*****.**'
    assert row[1] == 'John Doe'
    assert row[2] == '555-5555'
def _register_user():
    try:
        data = request.json
        user = body_parser.parse_regular_user(data)
        user_persistence = UserPersistence(current_app.db)
        user_persistence.save(user)
        result = {
            'Registration':
            'Successfully registered new user with email {0}'.format(
                user.email)
        }
        logger.debug('User was inserted')
        return result, HTTPStatus.CREATED
    except UserAlreadyRegisteredException:
        logger.error('This user already exists!')
        result = {'Registration': 'This user already exists!'}
        return result, HTTPStatus.CONFLICT
def _login_user_using_firebase():
    try:
        id_token = request.headers.get('authorization', None)
        claims = firebase_admin.auth.verify_id_token(id_token)
        if not claims or not claims.get('email'):
            logger.debug('Response from auth server login is 401')
            result = {'Login': '******'}
            status_code = 401
            return result, status_code

        user_persistence = UserPersistence(current_app.db)
        user = None
        try:
            user = user_persistence.get_user_by_email(claims.get('email'))
        except UserNotFoundException:
            user = User(claims.get('email'), None, claims.get('name'), 'NULL',
                        claims.get('picture'), True, False, False)
            user_persistence.save(user)

        if user.is_blocked_user():
            logger.debug('This user is BLOCKED')
            result = {'Login': '******'}
            status_code = 401
        elif user.is_firebase_user():
            logger.debug('Usuario logueado con exito')
            token = generate_auth_token(user)
            logger.debug('This is the token {0}'.format(token))
            result = {'Token': token, 'claims': claims}
            status_code = HTTPStatus.OK
        else:
            logger.debug('User not registered with Firebase')
            result = {'Login': '******'}
            status_code = 401
        return result, status_code
    except ValueError as exc:
        result = {'Login': '******'}
        status_code = 401
        logger.error(exc)
        return result, status_code
    except firebase_admin._auth_utils.InvalidIdTokenError as invalid_token_error:
        result = {'Register': str(invalid_token_error)}
        status_code = 401
        logger.error(invalid_token_error)
        return result, status_code