コード例 #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)
コード例 #2
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)
コード例 #3
0
ファイル: qna.py プロジェクト: computest-mark/cicd-lab-code
def index():
    db = get_db()
    questions = db.execute(
        'SELECT q.id, question, answer, created, author_id, username'
        ' FROM qna q JOIN user u ON q.author_id = u.id'
        ' ORDER BY created DESC'
    ).fetchall()
    return render_template('qna/index.html', questions=questions)
コード例 #4
0
ファイル: auth.py プロジェクト: computest-mark/cicd-lab-code
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()
コード例 #5
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
コード例 #6
0
ファイル: qna.py プロジェクト: computest-mark/cicd-lab-code
def get_question(q):
    question = get_db().execute(
        'SELECT q.id, question, answer, created, author_id, username'
        ' FROM qna q JOIN user u ON q.author_id = u.id'
        ' WHERE q.question = ?',
        (q,)
    ).fetchone()

    if question is None:
        abort(404, "question '{0}' doesn't exist.".format(q))

    return question
コード例 #7
0
ファイル: qna.py プロジェクト: computest-mark/cicd-lab-code
def get_question_by_id(id, check_author=True):
    question = get_db().execute(
        'SELECT q.id, question, answer, created, author_id, username'
        ' FROM qna q JOIN user u ON q.author_id = u.id'
        ' WHERE q.id = ?',
        (id,)
    ).fetchone()

    if question is None:
        abort(404, "question id {0} doesn't exist.".format(id))

    if check_author and question['author_id'] != g.user['id']:
        abort(403)

    return question
コード例 #8
0
ファイル: auth.py プロジェクト: computest-mark/cicd-lab-code
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 or not check_password_hash(user['password'], password):
            error = 'Incorrect username/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')
コード例 #9
0
ファイル: qna.py プロジェクト: computest-mark/cicd-lab-code
def create():
    if request.method == 'POST':
        question = request.form['question']
        answer = request.form['answer']
        error = None

        if not question:
            error = 'Question is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute(
                'INSERT INTO qna (question, answer, author_id)'
                ' VALUES (?, ?, ?)',
                (question, answer, g.user['id'])
            )
            db.commit()
            return redirect(url_for('qna.index'))

    return render_template('qna/create.html')
コード例 #10
0
ファイル: auth.py プロジェクト: computest-mark/cicd-lab-code
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')
コード例 #11
0
ファイル: qna.py プロジェクト: computest-mark/cicd-lab-code
def update(id):
    question = get_question_by_id(id)

    if request.method == 'POST':
        question = request.form['question']
        answer = request.form['answer']
        error = None

        if not question:
            error = 'question is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute(
                'UPDATE qna SET question = ?, answer = ?'
                ' WHERE id = ?',
                (question, answer, id)
            )
            db.commit()
            return redirect(url_for('qna.index'))

    return render_template('qna/update.html', question=question)
コード例 #12
0
ファイル: qna.py プロジェクト: computest-mark/cicd-lab-code
def delete(id):
    get_question_by_id(id)
    db = get_db()
    db.execute('DELETE FROM question WHERE id = ?', (id,))
    db.commit()
    return redirect(url_for('qna.index'))