From 4e35660080ccb8fc6351bc7945b3a19ba4bccbfc Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 14 Mar 2022 13:32:39 +0100 Subject: [PATCH] change conftest --- tests/conftest.py | 61 +++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 991374ba..b85b34fd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,15 +1,15 @@ import io import uuid -import jwt -import ereuse_utils from contextlib import redirect_stdout from datetime import datetime from pathlib import Path -from decouple import config import boltons.urlutils +import ereuse_utils +import jwt import pytest import yaml +from decouple import config from psycopg2 import IntegrityError from sqlalchemy.exc import ProgrammingError @@ -17,11 +17,12 @@ from ereuse_devicehub.client import Client, UserClient from ereuse_devicehub.config import DevicehubConfig from ereuse_devicehub.db import db from ereuse_devicehub.devicehub import Devicehub +from ereuse_devicehub.inventory.views import devices from ereuse_devicehub.resources.agent.models import Person -from ereuse_devicehub.resources.tag import Tag -from ereuse_devicehub.resources.user.models import User -from ereuse_devicehub.resources.user.models import Session from ereuse_devicehub.resources.enums import SessionType +from ereuse_devicehub.resources.tag import Tag +from ereuse_devicehub.resources.user.models import Session, User +from ereuse_devicehub.views import core STARTT = datetime(year=2000, month=1, day=1, hour=1) """A dummy starting time to use in tests.""" @@ -50,6 +51,19 @@ def config(): @pytest.fixture(scope='session') def _app(config: TestConfig) -> Devicehub: + # dh_config = DevicehubConfig() + # config = TestConfig(dh_config) + app = Devicehub(inventory='test', config=config, db=db) + app.register_blueprint(core) + app.register_blueprint(devices) + app.config["SQLALCHEMY_RECORD_QUERIES"] = True + app.config['PROFILE'] = True + # app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) + return app + + +@pytest.fixture(scope='session') +def _app2(config: TestConfig) -> Devicehub: return Devicehub(inventory='test', config=config, db=db) @@ -61,13 +75,15 @@ def app(request, _app: Devicehub) -> Devicehub: db.drop_all() def _init(): - _app.init_db(name='Test Inventory', - org_name='FooOrg', - org_id='foo-org-id', - tag_url=boltons.urlutils.URL('https://example.com'), - tag_token=uuid.UUID('52dacef0-6bcb-4919-bfed-f10d2c96ecee'), - erase=False, - common=True) + _app.init_db( + name='Test Inventory', + org_name='FooOrg', + org_id='foo-org-id', + tag_url=boltons.urlutils.URL('https://example.com'), + tag_token=uuid.UUID('52dacef0-6bcb-4919-bfed-f10d2c96ecee'), + erase=False, + common=True, + ) with _app.app_context(): try: @@ -99,7 +115,9 @@ def user(app: Devicehub) -> UserClient: with app.app_context(): password = 'foo' user = create_user(password=password) - client = UserClient(app, user.email, password, response_wrapper=app.response_class) + client = UserClient( + app, user.email, password, response_wrapper=app.response_class + ) client.login() return client @@ -111,7 +129,9 @@ def user2(app: Devicehub) -> UserClient: password = 'foo' email = 'foo2@foo.com' user = create_user(email=email, password=password) - client = UserClient(app, user.email, password, response_wrapper=app.response_class) + client = UserClient( + app, user.email, password, response_wrapper=app.response_class + ) client.login() return client @@ -145,16 +165,13 @@ def auth_app_context(app: Devicehub): def json_encode(dev: str) -> dict: """Encode json.""" data = {"type": "Snapshot"} - data['data'] = jwt.encode(dev, - P, - algorithm="HS256", - json_encoder=ereuse_utils.JSONEncoder + data['data'] = jwt.encode( + dev, P, algorithm="HS256", json_encoder=ereuse_utils.JSONEncoder ) return data - def yaml2json(name: str) -> dict: """Opens and parses a YAML file from the ``files`` subdir.""" with Path(__file__).parent.joinpath('files').joinpath(name + '.yaml').open() as f: @@ -168,7 +185,9 @@ def file(name: str) -> dict: def file_workbench(name: str) -> dict: """Opens and parses a YAML file from the ``files`` subdir.""" - with Path(__file__).parent.joinpath('workbench_files').joinpath(name + '.json').open() as f: + with Path(__file__).parent.joinpath('workbench_files').joinpath( + name + '.json' + ).open() as f: return yaml.load(f)