2015-07-22 19:51:55 +02:00
|
|
|
import os
|
2015-06-25 01:48:40 +02:00
|
|
|
import pytest
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
from flask import current_app
|
|
|
|
|
|
|
|
from flask.ext.principal import identity_changed, Identity
|
|
|
|
|
2015-06-25 01:48:40 +02:00
|
|
|
from lemur import create_app
|
2015-06-26 03:05:52 +02:00
|
|
|
from lemur.database import db as _db
|
2015-06-25 01:48:40 +02:00
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
from .factories import AuthorityFactory, NotificationFactory, DestinationFactory, \
|
|
|
|
CertificateFactory, UserFactory, RoleFactory
|
2015-06-25 01:48:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
if 'slow' in item.keywords and not item.config.getoption("--runslow"):
|
|
|
|
pytest.skip("need --runslow option to run")
|
|
|
|
|
|
|
|
if "incremental" in item.keywords:
|
|
|
|
previousfailed = getattr(item.parent, "_previousfailed", None)
|
|
|
|
if previousfailed is not None:
|
|
|
|
pytest.xfail("previous test failed ({0})".format(previousfailed.name))
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
if "incremental" in item.keywords:
|
|
|
|
if call.excinfo is not None:
|
|
|
|
parent = item.parent
|
|
|
|
parent._previousfailed = item
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture(scope="session")
|
2015-09-02 23:37:07 +02:00
|
|
|
def app(request):
|
2015-06-25 01:48:40 +02:00
|
|
|
"""
|
|
|
|
Creates a new Flask application for a test duration.
|
|
|
|
Uses application factory `create_app`.
|
|
|
|
"""
|
2016-05-05 21:52:08 +02:00
|
|
|
_app = create_app(os.path.dirname(os.path.realpath(__file__)) + '/conf.py')
|
2015-07-22 19:51:55 +02:00
|
|
|
ctx = _app.app_context()
|
2015-06-25 01:48:40 +02:00
|
|
|
ctx.push()
|
|
|
|
|
2015-07-22 19:51:55 +02:00
|
|
|
yield _app
|
2015-06-25 01:48:40 +02:00
|
|
|
|
|
|
|
ctx.pop()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture(scope="session")
|
2015-06-26 03:05:52 +02:00
|
|
|
def db(app, request):
|
|
|
|
_db.drop_all()
|
|
|
|
_db.create_all()
|
2015-06-25 01:48:40 +02:00
|
|
|
|
2015-06-26 03:05:52 +02:00
|
|
|
_db.app = app
|
2015-06-25 01:48:40 +02:00
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
UserFactory()
|
|
|
|
r = RoleFactory(name='admin')
|
|
|
|
UserFactory(roles=[r])
|
|
|
|
|
2015-06-26 19:31:55 +02:00
|
|
|
_db.session.commit()
|
2015-06-26 03:05:52 +02:00
|
|
|
yield _db
|
|
|
|
|
2015-06-25 01:48:40 +02:00
|
|
|
|
|
|
|
@pytest.yield_fixture(scope="function")
|
2015-06-26 03:05:52 +02:00
|
|
|
def session(db, request):
|
2015-06-25 01:48:40 +02:00
|
|
|
"""
|
|
|
|
Creates a new database session with (with working transaction)
|
|
|
|
for test duration.
|
|
|
|
"""
|
2015-06-26 03:05:52 +02:00
|
|
|
db.session.begin_nested()
|
2016-05-05 21:52:08 +02:00
|
|
|
yield db.session
|
2015-06-26 03:05:52 +02:00
|
|
|
db.session.rollback()
|
2015-06-25 01:48:40 +02:00
|
|
|
|
|
|
|
|
2015-06-26 03:05:52 +02:00
|
|
|
@pytest.yield_fixture(scope="function")
|
2015-07-06 19:53:12 +02:00
|
|
|
def client(app, session, client):
|
|
|
|
yield client
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def authority(session):
|
|
|
|
a = AuthorityFactory()
|
|
|
|
session.commit()
|
|
|
|
return a
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def destination(session):
|
|
|
|
d = DestinationFactory()
|
|
|
|
session.commit()
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def notification(session):
|
|
|
|
n = NotificationFactory()
|
|
|
|
session.commit()
|
|
|
|
return n
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def certificate(session):
|
|
|
|
c = CertificateFactory()
|
|
|
|
session.commit()
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def role(session):
|
|
|
|
r = RoleFactory()
|
|
|
|
session.commit()
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
@pytest.yield_fixture(scope="function")
|
|
|
|
def logged_in_user(app, user):
|
|
|
|
with app.test_request_context():
|
|
|
|
identity_changed.send(current_app._get_current_object(), identity=Identity(user.id))
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture(scope="function")
|
|
|
|
def logged_in_admin(app, admin_user):
|
|
|
|
with app.test_request_context():
|
|
|
|
identity_changed.send(current_app._get_current_object(), identity=Identity(admin_user.id))
|
|
|
|
yield
|