2015-12-10 14:48:01 +00:00
|
|
|
import os
|
|
|
|
|
|
2015-11-25 15:29:12 +00:00
|
|
|
import pytest
|
2015-12-10 14:48:01 +00:00
|
|
|
from alembic.command import upgrade
|
|
|
|
|
from alembic.config import Config
|
2016-01-06 16:40:38 +00:00
|
|
|
from flask import url_for
|
2015-12-10 14:48:01 +00:00
|
|
|
from flask.ext.migrate import Migrate, MigrateCommand
|
|
|
|
|
from flask.ext.script import Manager
|
2016-01-06 16:40:38 +00:00
|
|
|
from flask_login import login_user
|
2015-12-10 14:48:01 +00:00
|
|
|
from sqlalchemy.schema import MetaData
|
2016-01-06 16:40:38 +00:00
|
|
|
from flask.testing import FlaskClient
|
|
|
|
|
from app.main.dao import verify_codes_dao
|
2015-11-25 15:29:12 +00:00
|
|
|
|
|
|
|
|
from app import create_app, db
|
|
|
|
|
|
2016-01-06 16:51:35 +00:00
|
|
|
|
2016-01-06 16:40:38 +00:00
|
|
|
class TestClient(FlaskClient):
|
|
|
|
|
def login(self, user):
|
|
|
|
|
# Skipping authentication here and just log them in
|
|
|
|
|
with self.session_transaction() as session:
|
2016-01-07 12:43:10 +00:00
|
|
|
session['user_email'] = user.email_address
|
2016-01-06 16:40:38 +00:00
|
|
|
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
2016-01-06 16:51:35 +00:00
|
|
|
response = self.post(
|
2016-01-06 17:17:02 +00:00
|
|
|
url_for('main.two_factor'), data={'sms_code': '12345'})
|
2016-01-06 16:40:38 +00:00
|
|
|
|
|
|
|
|
def logout(self, user):
|
|
|
|
|
self.get(url_for("main.logout"))
|
|
|
|
|
|
2015-11-25 15:29:12 +00:00
|
|
|
|
2015-12-10 14:48:01 +00:00
|
|
|
@pytest.fixture(scope='session')
|
2015-11-25 15:29:12 +00:00
|
|
|
def notifications_admin(request):
|
|
|
|
|
app = create_app('test')
|
2015-12-10 14:48:01 +00:00
|
|
|
|
2015-11-25 15:29:12 +00:00
|
|
|
ctx = app.app_context()
|
|
|
|
|
ctx.push()
|
|
|
|
|
|
|
|
|
|
def teardown():
|
|
|
|
|
ctx.pop()
|
|
|
|
|
|
|
|
|
|
request.addfinalizer(teardown)
|
2016-01-06 16:40:38 +00:00
|
|
|
app.test_client_class = TestClient
|
2015-11-25 15:29:12 +00:00
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
2015-12-10 14:48:01 +00:00
|
|
|
@pytest.fixture(scope='session')
|
2015-11-25 15:29:12 +00:00
|
|
|
def notifications_admin_db(notifications_admin, request):
|
2015-12-10 14:48:01 +00:00
|
|
|
Migrate(notifications_admin, db)
|
|
|
|
|
Manager(db, MigrateCommand)
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
|
ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations')
|
|
|
|
|
config = Config(ALEMBIC_CONFIG + '/alembic.ini')
|
|
|
|
|
config.set_main_option("script_location", ALEMBIC_CONFIG)
|
|
|
|
|
|
|
|
|
|
with notifications_admin.app_context():
|
|
|
|
|
upgrade(config, 'head')
|
|
|
|
|
|
|
|
|
|
def teardown():
|
|
|
|
|
db.session.remove()
|
|
|
|
|
db.drop_all()
|
|
|
|
|
db.engine.execute("drop table alembic_version")
|
|
|
|
|
db.get_engine(notifications_admin).dispose()
|
|
|
|
|
|
|
|
|
|
request.addfinalizer(teardown)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
def notify_db_session(request):
|
|
|
|
|
def teardown():
|
|
|
|
|
db.session.remove()
|
|
|
|
|
for tbl in reversed(meta.sorted_tables):
|
|
|
|
|
if tbl.fullname not in ['roles']:
|
|
|
|
|
db.engine.execute(tbl.delete())
|
|
|
|
|
|
|
|
|
|
meta = MetaData(bind=db.engine, reflect=True)
|
|
|
|
|
request.addfinalizer(teardown)
|