def setup_mysql_db(request): if os.environ.get("TRAVIS", False): # Using Travis-CI - https://travis-ci.org/ password = "" db_name = "myapp_test" else: password = "" db_name = "cork_functional_test" uri = "mysql://*****:*****@localhost/%s" % (password, db_name) mb = SqlAlchemyBackend(uri, initialize=True) ## Purge DB mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 # Create roles mb.roles.insert({"role": "special", "level": 200}) mb.roles.insert({"role": "admin", "level": 100}) mb.roles.insert({"role": "editor", "level": 60}) mb.roles.insert({"role": "user", "level": 50}) # Create admin mb.users.insert( { "username": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723", "last_login": "******", } ) assert len(mb.roles) == 4 assert len(mb.users) == 1 def fin(): return # TODO: fix mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 request.addfinalizer(fin) return mb
def setup_mysql_db(request): if os.environ.get('TRAVIS', False): # Using Travis-CI - https://travis-ci.org/ password = '' db_name = 'myapp_test' else: password = '' db_name = 'cork_functional_test' uri = "mysql://*****:*****@localhost/%s" % (password, db_name) mb = SqlAlchemyBackend(uri, initialize=True) ## Purge DB mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 # Create roles mb.roles.insert({'role': 'special', 'level': 200}) mb.roles.insert({'role': 'admin', 'level': 100}) mb.roles.insert({'role': 'editor', 'level': 60}) mb.roles.insert({'role': 'user', 'level': 50}) # Create admin mb.users.insert({ "username": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723", "last_login": "******" }) assert len(mb.roles) == 4 assert len(mb.users) == 1 def fin(): return # TODO: fix mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 request.addfinalizer(fin) return mb
def setup(engine): Role.metadata.create_all(engine) User.metadata.create_all(engine) # setup cork auth backend = SqlAlchemyBackend(engine.url) aaa = Cork(backend=backend) setattr(__import__(__name__), 'aaa', aaa)
def setup_postgresql_db(request): if os.environ.get('TRAVIS', False): # Using Travis-CI - https://travis-ci.org/ db_name = 'myapp_test' else: db_name = 'cork_functional_test' uri = "postgresql+psycopg2://postgres:@/%s" % db_name mb = SqlAlchemyBackend(uri, initialize=True) # Purge DB mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 # Create roles mb.roles.insert({'role': 'special', 'level': 200}) mb.roles.insert({'role': 'admin', 'level': 100}) mb.roles.insert({'role': 'editor', 'level': 60}) mb.roles.insert({'role': 'user', 'level': 50}) # Create admin mb.users.insert({ "username": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723", "last_login": "******" }) assert len(mb.roles) == 4 assert len(mb.users) == 1 def fin(): mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 request.addfinalizer(fin) return mb
def change_pass(pwd): sqlalchemy_backend = Cork(backend=SqlAlchemyBackend(config.db_url)) metadata = MetaData() engine = create_engine(config.db_url) metadata.bind = engine users = Table("users", metadata, extend_existing=True, autoload=True, autoload_with=engine) users.update().where(users.c.username == "transito").values( hash=sqlalchemy_backend._hash("transito", pwd)).execute()
def connect_to_test_db(): if os.environ.get('TRAVIS', False): # Using Travis-CI - https://travis-ci.org/ password = '' db_name = 'myapp_test' else: password = '' db_name = 'cork_functional_test' uri = "mysql://*****:*****@localhost/%s" % (password, db_name) return SqlAlchemyBackend(uri, initialize=True)
def add_user(Base, session): Users = Base.classes.users sqlalchemy_backend = Cork(backend=SqlAlchemyBackend(config.db_url)) session.add( Users(username="******", role="admin", hash=sqlalchemy_backend._hash("transito", "transito"), email_addr="*****@*****.**", desc="operario", creation_date=str(datetime.now()), last_login=str(datetime.now()))) session.commit() session.close()
def setup_sqlalchemy_with_sqlite_in_memory_db(request): # Setup an SqlAlchemyBackend backend using an in-memory SQLite DB mb = SqlAlchemyBackend("sqlite:///:memory:", initialize=True) ## Purge DB mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 # Create roles mb.roles.insert({"role": "special", "level": 200}) mb.roles.insert({"role": "admin", "level": 100}) mb.roles.insert({"role": "editor", "level": 60}) mb.roles.insert({"role": "user", "level": 50}) # Create admin mb.users.insert( { "username": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723", "last_login": "******", } ) assert len(mb.roles) == 4 assert len(mb.users) == 1 def fin(): mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 request.addfinalizer(fin) return mb
def setup_sqlalchemy_with_sqlite_in_memory_db(request): # Setup an SqlAlchemyBackend backend using an in-memory SQLite DB mb = SqlAlchemyBackend('sqlite:///:memory:', initialize=True) ## Purge DB mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 # Create roles mb.roles.insert({'role': 'special', 'level': 200}) mb.roles.insert({'role': 'admin', 'level': 100}) mb.roles.insert({'role': 'editor', 'level': 60}) mb.roles.insert({'role': 'user', 'level': 50}) # Create admin mb.users.insert({ "username": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723", "last_login": "******" }) assert len(mb.roles) == 4 assert len(mb.users) == 1 def fin(): mb._drop_all_tables() assert len(mb.roles) == 0 assert len(mb.users) == 0 request.addfinalizer(fin) return mb
def auth_sqlalchemy(): sqlalchemy_backend = SqlAlchemyBackend(config.db_url) return sqlalchemy_backend
import logging import smtplib from operator import itemgetter import os MODULEPATH = os.path.dirname(__file__) TEMPLATE_PATH.insert(0, os.path.join(MODULEPATH, "views")) logging.basicConfig(format='localhost - - [%(asctime)s] %(message)s', level=logging.DEBUG) log = logging.getLogger(__name__) postgresConn = os.environ.get("POSTGRES_CONN", "") sessionEncryptKey = os.environ.get("SESSION_KEY", "asdfghjkl") devMode = os.environ.get("DEV_MODE", "disabled") corkBackend = SqlAlchemyBackend(postgresConn, initialize=False) aaa = Cork(backend=corkBackend, email_sender='', smtp_url='') # Section: Utilities def postd(): return bottle.request.forms def post_get(name, default=''): return bottle.request.POST.get(name, default).strip() def get_get(name, default=''):
def purge_test_db(): # Purge DB mb = SqlAlchemyBackend('mysql://*****:*****@localhost/cork_functional_test', initialize=True) mb._drop_all_tables()
def getMysqlBackend(): connectionString = config.generateConnectioString() sqlBackend = SqlAlchemyBackend(db_full_url=connectionString) return sqlBackend
COOKIE_SECRET = os.environ['COOKIE_SECRET'] # SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify' # RECAPTCHA_SECRET = '6LfeHx4UAAAAAFWXGh_xcL0B8vVcXnhn9q_SnQ1b' logging.basicConfig(format='heroku - - [%(asctime)s] %(message)s', level=logging.DEBUG) from cork.backends import SqlAlchemyBackend if not DATABASE_URL.startswith('postgresql'): DATABASE_URL = 'postgresql:' + DATABASE_URL.split(':', 1)[1] sa = SqlAlchemyBackend(DATABASE_URL, initialize=True, connect_args={'dbname': DBNAME}) SMTP_URL = ('starttls://' + MAILID + ':' + PASSWORD + '@smtp-mail.outlook.com:587') aaa = Cork(smtp_url=SMTP_URL, email_sender=MAILID, backend=sa) authorize = aaa.make_auth_decorator(fail_redirect="/login", role="user") app = bottle.Bottle() app.install(FlashPlugin(secret=COOKIE_SECRET)) def postd(): return bottle.request.forms