Esempio n. 1
0
def test_get_close_db(app):
    with app.app_context():
        db = get_db()
        assert db is get_db()

    with pytest.raises(sqlite3.ProgrammingError) as e:
        db.execute('SELECT 1')

    assert 'closed' in str(e.value)
Esempio n. 2
0
def get_owner_id_options():
    db = get_db()
    owner_id_options = db.execute('SELECT id FROM person').fetchall()

    owner_id_options = [str(x[0]) for x in owner_id_options]

    return owner_id_options
Esempio n. 3
0
def get_number_of_vehicles_owned_by_person(owner_id):
    db = get_db()
    owned_vehicles = db.execute(
        'SELECT count(*) FROM vehicle WHERE owner_id = {}'.format(
            owner_id)).fetchone()[0]

    return owned_vehicles
Esempio n. 4
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif db.execute(
            'SELECT id FROM user WHERE username = ?', (username,)
        ).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)

        if error is None:
            db.execute(
                'INSERT INTO user (username, password) VALUES (?, ?)',
                (username, generate_password_hash(password))
            )
            db.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
Esempio n. 5
0
def app():
    db_fd, db_path = tempfile.mkstemp()

    app = create_app({
        'TESTING': True,
        'DATABASE': db_path,
    })

    with app.app_context():
        init_db()
        get_db().executescript(_data_sql)

    yield app

    os.close(db_fd)
    os.unlink(db_path)
Esempio n. 6
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute(
            'SELECT * FROM user WHERE id = ?', (user_id,)
        ).fetchone()
Esempio n. 7
0
def test_register(client, app):
    assert client.get('/auth/register').status_code == 200
    response = client.post('/auth/register',
                           data={
                               'username': '******',
                               'password': '******'
                           })
    assert 'http://localhost/auth/login' == response.headers['Location']

    with app.app_context():
        assert get_db().execute(
            "select * from user where username = '******'", ).fetchone() is not None
Esempio n. 8
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None
        user = db.execute(
            'SELECT * FROM user WHERE username = ?', (username,)
        ).fetchone()

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')
Esempio n. 9
0
def insert_new_person_in_db(first_name, last_name):
    db = get_db()
    db.execute('INSERT INTO person (first_name, last_name)'
               ' VALUES (?, ?)', (first_name, last_name))
    db.commit()
Esempio n. 10
0
def insert_new_vehicle_in_db(owner_id, color, model):
    db = get_db()
    db.execute(
        'INSERT INTO vehicle (owner_id, color, model)'
        ' VALUES (?, ?, ?)', (owner_id, color, model))
    db.commit()
Esempio n. 11
0
def set_sale_opportunity_to_false(owner_id):
    db = get_db()
    db.execute('UPDATE person'
               ' SET sale_opportunity = 0'
               ' WHERE id = ?', owner_id)
    db.commit()