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)
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: # add the user and password db.execute('INSERT INTO user (username, password) VALUES (?, ?)', (username, generate_password_hash(password))) # gotta grab the id of the user we just added user_id = int( db.execute('select last_insert_rowid()').fetchone()[0]) # add empty json strings to the timeline table db.execute( "INSERT INTO timeline (settings, frames, author_id) VALUES ('{}', '[[]]', ?)", (user_id, )) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
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)
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()
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
def report(): user_id = session.get('user_id') if request.method == 'POST': return 'Report Post' db = get_db() frames = db.execute('select * from timeline where author_id=?', (user_id, )).fetchone()['frames'] res = '<h1>Report Get</h1>\n' for frame in json.loads(frames): try: res += f"<p>{frame.get('project')}: {frame.get('stop') - frame.get('start')}</p>" except: pass return res
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('timeline')) # eventually url_for('index') flash(error) return render_template('auth/login.html')