This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/tests/conftest.py

100 lines
2.5 KiB
Python
Raw Normal View History

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-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
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
class TestConfig(DevicehubConfig):
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/dh_test'
2018-04-27 17:16:43 +00:00
SCHEMA = 'test'
TESTING = True
ORGANIZATION_NAME = 'FooOrg'
ORGANIZATION_TAX_ID = 'FooOrgId'
2018-04-10 15:06:39 +00:00
2018-04-27 17:16:43 +00:00
@pytest.fixture(scope='module')
2018-04-10 15:06:39 +00:00
def config():
return TestConfig()
2018-04-27 17:16:43 +00:00
@pytest.fixture(scope='module')
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:
with _app.app_context():
_app.init_db()
2018-04-27 17:16:43 +00:00
# More robust than 'yield'
request.addfinalizer(lambda *args, **kw: db.drop_all(app=_app))
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():
user = create_user()
client = UserClient(application=app,
response_wrapper=app.response_class,
email=user.email,
password='foo')
client.user, _ = client.login(client.email, client.password)
return client
def create_user(email='foo@foo.com', password='foo') -> User:
user = User(email=email, password=password)
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())
yield
2018-04-27 17:16:43 +00:00
def file(name: str) -> dict:
"""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)
@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