2018-08-26 17:04:42 +00:00
|
|
|
import io
|
|
|
|
from contextlib import redirect_stdout
|
|
|
|
from datetime import datetime
|
2018-04-27 17:16:43 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
import pytest
|
2018-04-27 17:16:43 +00:00
|
|
|
import yaml
|
2018-10-05 12:35:51 +00:00
|
|
|
from psycopg2 import IntegrityError
|
2018-07-14 14:41:22 +00:00
|
|
|
from sqlalchemy.exc import ProgrammingError
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.client import Client, UserClient
|
2018-04-10 15:06:39 +00:00
|
|
|
from ereuse_devicehub.config import DevicehubConfig
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
2018-08-03 16:15:08 +00:00
|
|
|
from ereuse_devicehub.resources.agent.models import Person
|
2018-05-30 10:49:40 +00:00
|
|
|
from ereuse_devicehub.resources.tag import Tag
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-08-26 17:04:42 +00:00
|
|
|
STARTT = datetime(year=2000, month=1, day=1, hour=1)
|
|
|
|
"""A dummy starting time to use in tests."""
|
|
|
|
ENDT = datetime(year=2000, month=1, day=1, hour=2)
|
|
|
|
"""A dummy ending time to use in tests."""
|
|
|
|
T = {'start_time': STARTT, 'end_time': ENDT}
|
|
|
|
"""A dummy start_time/end_time to use as function keywords."""
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
class TestConfig(DevicehubConfig):
|
2018-07-08 13:00:28 +00:00
|
|
|
SQLALCHEMY_DATABASE_URI = 'postgresql://dhub:ereuse@localhost/dh_test'
|
2018-04-27 17:16:43 +00:00
|
|
|
SCHEMA = 'test'
|
|
|
|
TESTING = True
|
2018-05-30 10:49:40 +00:00
|
|
|
ORGANIZATION_NAME = 'FooOrg'
|
2018-09-30 10:29:33 +00:00
|
|
|
ORGANIZATION_TAX_ID = 'foo-org-id'
|
2018-10-05 15:13:23 +00:00
|
|
|
SERVER_NAME = 'localhost'
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
2018-10-05 12:35:51 +00:00
|
|
|
@pytest.fixture(scope='session')
|
2018-04-10 15:06:39 +00:00
|
|
|
def config():
|
|
|
|
return TestConfig()
|
|
|
|
|
|
|
|
|
2018-10-05 12:35:51 +00:00
|
|
|
@pytest.fixture(scope='session')
|
2018-04-27 17:16:43 +00:00
|
|
|
def _app(config: TestConfig) -> Devicehub:
|
|
|
|
return Devicehub(config=config, db=db)
|
|
|
|
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
@pytest.fixture()
|
2018-04-27 17:16:43 +00:00
|
|
|
def app(request, _app: Devicehub) -> Devicehub:
|
|
|
|
# More robust than 'yield'
|
2018-07-14 14:41:22 +00:00
|
|
|
def _drop(*args, **kwargs):
|
|
|
|
with _app.app_context():
|
|
|
|
db.drop_all()
|
|
|
|
|
|
|
|
with _app.app_context():
|
|
|
|
try:
|
2018-08-26 17:04:42 +00:00
|
|
|
with redirect_stdout(io.StringIO()):
|
|
|
|
_app.init_db()
|
2018-10-05 12:35:51 +00:00
|
|
|
except (ProgrammingError, IntegrityError):
|
2018-07-14 14:41:22 +00:00
|
|
|
print('Database was not correctly emptied. Re-empty and re-installing...')
|
|
|
|
_drop()
|
|
|
|
_app.init_db()
|
|
|
|
|
|
|
|
request.addfinalizer(_drop)
|
2018-04-27 17:16:43 +00:00
|
|
|
return _app
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def client(app: Devicehub) -> Client:
|
|
|
|
return app.test_client()
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
2018-04-30 17:58:19 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def app_context(app: Devicehub):
|
|
|
|
with app.app_context():
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def user(app: Devicehub) -> UserClient:
|
|
|
|
"""Gets a client with a logged-in dummy user."""
|
|
|
|
with app.app_context():
|
2018-06-20 21:18:15 +00:00
|
|
|
password = 'foo'
|
|
|
|
user = create_user(password=password)
|
2018-08-03 16:15:08 +00:00
|
|
|
client = UserClient(app, user.email, password, response_wrapper=app.response_class)
|
|
|
|
client.login()
|
2018-04-27 17:16:43 +00:00
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
def create_user(email='foo@foo.com', password='foo') -> User:
|
|
|
|
user = User(email=email, password=password)
|
2018-08-03 16:15:08 +00:00
|
|
|
user.individuals.add(Person(name='Timmy'))
|
2018-04-27 17:16:43 +00:00
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
2018-04-30 17:58:19 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def auth_app_context(app: Devicehub):
|
|
|
|
"""Creates an app context with a set user."""
|
|
|
|
with app.app_context():
|
|
|
|
user = create_user()
|
|
|
|
|
|
|
|
class Auth: # Mock
|
|
|
|
username = user.token
|
|
|
|
password = ''
|
|
|
|
|
|
|
|
app.auth.perform_auth(Auth())
|
2018-10-11 09:22:59 +00:00
|
|
|
yield app
|
2018-04-30 17:58:19 +00:00
|
|
|
|
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
def file(name: str) -> dict:
|
2018-05-30 10:49:40 +00:00
|
|
|
"""Opens and parses a YAML file from the ``files`` subdir."""
|
2018-04-27 17:16:43 +00:00
|
|
|
with Path(__file__).parent.joinpath('files').joinpath(name + '.yaml').open() as f:
|
|
|
|
return yaml.load(f)
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def tag_id(app: Devicehub) -> str:
|
|
|
|
"""Creates a tag and returns its id."""
|
|
|
|
with app.app_context():
|
|
|
|
t = Tag(id='foo')
|
|
|
|
db.session.add(t)
|
|
|
|
db.session.commit()
|
|
|
|
return t.id
|