def setup_sqlite3_auth_tables(username, password): """Setup the SQLite3 auth database.""" auth_driver = SQLAlchemyAuthDriver('sqlite:///auth.sq3') try: auth_driver.add(username, password) except Exception as error: print('Unable to create auth tables') print(error)
def test_driver_auth_accepts_good_creds(): ''' Tests driver accepts good creds. ''' driver = SQLAlchemyAuthDriver('sqlite:///auth.sq3') with sqlite3.connect('auth.sq3') as conn: conn.execute( ''' INSERT INTO auth_record VALUES (?,?) ''', (USERNAME, DIGESTED)) driver.auth(USERNAME, PASSWORD)
def test_driver_auth_accepts_good_creds(): """ Tests driver accepts good creds. """ driver = SQLAlchemyAuthDriver("sqlite:///auth.sq3") with sqlite3.connect("auth.sq3") as conn: conn.execute( """ INSERT INTO auth_record VALUES (?,?) """, (USERNAME, DIGESTED), ) driver.auth(USERNAME, PASSWORD)
def test_driver_auth_returns_user_context(): ''' Tests driver accepts good creds. ''' driver = SQLAlchemyAuthDriver('sqlite:///auth.sq3') with sqlite3.connect('auth.sq3') as conn: conn.execute( ''' INSERT INTO auth_record VALUES (?,?) ''', (USERNAME, DIGESTED)) user = driver.auth(USERNAME, PASSWORD) assert user is not None, 'user context was None'
def test_driver_auth_returns_user_context(): """ Tests driver accepts good creds. """ driver = SQLAlchemyAuthDriver("sqlite:///auth.sq3") with sqlite3.connect("auth.sq3") as conn: conn.execute( """ INSERT INTO auth_record VALUES (?,?) """, (USERNAME, DIGESTED), ) user = driver.auth(USERNAME, PASSWORD) assert user is not None, "user context was None"
def test_driver_init_does_not_create_records(): """ Tests for creation of records after driver init. Tests driver init does not have unexpected side-effects. """ driver = SQLAlchemyAuthDriver("sqlite:///auth.sq3") # pylint: disable=unused-variable with sqlite3.connect("auth.sq3") as conn: count = conn.execute(""" SELECT COUNT(*) FROM auth_record """).fetchone()[0] assert count == 0, "driver created records upon initilization"
def test_driver_init_does_not_create_records(): ''' Tests for creation of records after driver init. Tests driver init does not have unexpected side-effects. ''' driver = SQLAlchemyAuthDriver('sqlite:///auth.sq3') with sqlite3.connect('auth.sq3') as conn: count = conn.execute(''' SELECT COUNT(*) FROM auth_record ''').fetchone()[0] assert count == 0, 'driver created records upon initilization'
def test_driver_auth_rejects_bad_creds(): ''' Test driver rejects bad creds. ''' driver = SQLAlchemyAuthDriver('sqlite:///auth.sq3') with sqlite3.connect('auth.sq3') as conn: conn.execute( ''' INSERT INTO auth_record VALUES (?, ?) ''', (USERNAME, DIGESTED)) with pytest.raises(AuthError): driver.auth(USERNAME, 'invalid_' + PASSWORD) with pytest.raises(AuthError): driver.auth('invalid_' + USERNAME, PASSWORD)
def test_driver_auth_rejects_bad_creds(): """ Test driver rejects bad creds. """ driver = SQLAlchemyAuthDriver("sqlite:///auth.sq3") with sqlite3.connect("auth.sq3") as conn: conn.execute( """ INSERT INTO auth_record VALUES (?, ?) """, (USERNAME, DIGESTED), ) with pytest.raises(AuthError): driver.auth(USERNAME, "invalid_" + PASSWORD) with pytest.raises(AuthError): driver.auth("invalid_" + USERNAME, PASSWORD)
db=db, ), index_config=index_config, ), } CONFIG["ALIAS"] = { "driver": SQLAlchemyAliasDriver( "postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db, )), } AUTH = SQLAlchemyAuthDriver( "postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db, ), arborist="http://localhost/", ) settings = {"config": CONFIG, "auth": AUTH}
CONFIG["INDEX"] = { "driver": SQLAlchemyIndexDriver( "postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db), index_config=index_config, ) } CONFIG["ALIAS"] = { "driver": SQLAlchemyAliasDriver( "postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db)) } if arborist: AUTH = SQLAlchemyAuthDriver( "postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db), arborist= "http://arborist-service/", # uncomment if you're using arborist ) else: AUTH = SQLAlchemyAuthDriver( "postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db)) settings = {"config": CONFIG, "auth": AUTH}
pgport=pgport, db=db, ), index_config=index_config), } CONFIG['ALIAS'] = { 'driver': SQLAlchemyAliasDriver( 'postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}'.format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db, )), } AUTH = SQLAlchemyAuthDriver( 'postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}'.format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db, ), arborist="http://arborist-service/", ) settings = {'config': CONFIG, 'auth': AUTH}
psw=psw, pghost=pghost, pgport=pgport, db=db, ), index_config=index_config), } CONFIG['ALIAS'] = { 'driver': SQLAlchemyAliasDriver( 'postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}'.format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db, )), } AUTH = SQLAlchemyAuthDriver( 'postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}'.format( usr=usr, psw=psw, pghost=pghost, pgport=pgport, db=db, )) settings = {'config': CONFIG, 'auth': AUTH}
import sqlite3 import hashlib import pytest import tests.util as util from indexd.errors import AuthError from indexd.auth.drivers.alchemy import SQLAlchemyAuthDriver USERNAME = "******" PASSWORD = "******" DIGESTED = SQLAlchemyAuthDriver.digest(PASSWORD) # TODO check if pytest has utilities for meta-programming of tests @util.removes("auth.sq3") def test_driver_init_does_not_create_records(): """ Tests for creation of records after driver init. Tests driver init does not have unexpected side-effects. """ driver = SQLAlchemyAuthDriver("sqlite:///auth.sq3") # pylint: disable=unused-variable with sqlite3.connect("auth.sq3") as conn: count = conn.execute(""" SELECT COUNT(*) FROM auth_record """).fetchone()[0]