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 test_save_token(app): with app.app_context(): save_token('test_token') result = get_db().execute( "SELECT * FROM blacklist_tokens WHERE token LIKE '{}'".format( 'test_token')).fetchone() assert result is not None
def test_save_existing_token(app): with app.app_context(): save_token('test_token') save_token('test_token') result = get_db().execute( "SELECT count(*) FROM blacklist_tokens").fetchone() assert result[0] == 1
def check_blacklist(token): db = get_db() db_token = db.execute('SELECT * FROM blacklist_tokens WHERE token = ?', (token, )).fetchone() if db_token is None: return False return True
def test_logout(app, client): auth_token = 'test_token' response = client.post('/logout', headers={'Authorization': auth_token}) assert response.status_code == 200 with app.app_context(): result = get_db().execute( "SELECT * FROM blacklist_tokens WHERE token LIKE '{}'".format( auth_token)).fetchone() assert result is not None
def save_token(token): blacklist_token = BlacklistToken(token) db = get_db() if not check_blacklist(token): db.execute( 'INSERT INTO blacklist_tokens (token, blacklisted_on) \ VALUES (?, ?)', (blacklist_token.token, blacklist_token.blacklisted_on)) db.commit()
def create_user(username, password): db = get_db() user = User(username, password) try: db.execute('INSERT INTO user (username, password) VALUES (?, ?)', (user.username, user.password_hash)) db.commit() current_app.logger.info('Created user {}'.format(username)) except IntegrityError: current_app.logger.error('Error: Username already exists')
def find_user(username): db = get_db() db_user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if db_user is None: current_app.logger.info('User %s not found', username) return None user = User(username=db_user['username']) user.password_hash = db_user['password'] current_app.logger.info('User %s found', username) return user