"""conftest. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ import logging from flask.testing import FlaskClient import pytest import application from collaborator import config from collaborator.constants.header import ( COLLABORATORS_PROFILE, LABEL_RESOURCE, MONEYHUB_PROFILE, ) from collaborator.utils import features from collaborator.utils.typing import Account, Resource, User def get_test_client(profile_type): """Get a test FlaskClient class with specified profile type.""" class TestClientWithProfileAuthorization(FlaskClient): """Test FlaskClient with profile auth headers.""" headers = [ ("orchard-identity-id", "7ec441df-7b21-4a71-a9da"), ("orchard-profile-id", "90003"), ("orchard-profile-type", profile_type), ] def get(self, *args, **kw): """Get override.""" return super().get(*args, **{"headers": self.headers, **kw}) def post(self, *args, **kw): """Post override.""" return super().post(*args, **{"headers": self.headers, **kw}) def put(self, *args, **kw): """Put override.""" return super().put(*args, **{"headers": self.headers, **kw}) def delete(self, *args, **kw): """Delete override.""" return super().delete(*args, **{"headers": self.headers, **kw}) def patch(self, *args, **kw): """Patch override.""" return super().patch(*args, **{"headers": self.headers, **kw}) return TestClientWithProfileAuthorization @pytest.fixture def auth_client(): """Return flask test client with CollaboratorsProfile auth headers.""" application.app.test_client_class = get_test_client(COLLABORATORS_PROFILE) return application.app.test_client() @pytest.fixture def moneyhub_auth_client(): """Return flask test client with MoneyHubProfile auth headers.""" application.app.test_client_class = get_test_client(MONEYHUB_PROFILE) return application.app.test_client() @pytest.fixture def make_auth_client(): """Return flask test client with CollaboratorsProfile auth headers.""" def factory(profile_type=COLLABORATORS_PROFILE): application.app.test_client_class = get_test_client(profile_type) return application.app.test_client() return factory @pytest.fixture def test_client(): """Return flask test client. Returns: flask client: flask test client """ return application.app.test_client() @pytest.fixture(autouse=True) def app_context(): """Wrap test code in Flask app context.""" with application.app.app_context(): yield @pytest.fixture def mock_account(): """Create a mock account tuple.""" return Account(type="vendor", id=24601) @pytest.fixture def mock_user(): """Create a mock user tuple.""" return User(type="oa", id=24601) @pytest.fixture def mock_identity(): """Create a mock identity tuple.""" return User(type="Orchard-Identity-Id", id="7ec441df-7b21-4a71-a9da") @pytest.fixture def mock_resources(mock_account): """Create a mock workstation user tuple.""" return [Resource(LABEL_RESOURCE, str(mock_account.id))] @pytest.fixture(autouse=True) def mock_features(mocker): """Provide tests with a convenient way to mock fetures.""" features_map = {} def _side_effect(*args, **kwargs): value = features_map.get(args[0], False) return value def _mock_features(flags={}): features_map.update(flags) ident_mock = mocker.patch.object(features, "is_feature_enabled") accnt_mock = mocker.patch.object(features, "is_feature_enabled_for_account") ident_mock.side_effect = accnt_mock.side_effect = _side_effect return _mock_features @pytest.fixture(scope="session", autouse=True) def enable_sqlalchemy_logging(): """Enable SQL logging for test if enabled in env.""" if not config.SQLALCHEMY_LOGGER_LEVEL: yield return logging.basicConfig(level=config.SQLALCHEMY_LOGGER_LEVEL) logger = logging.getLogger("sqlalchemy.engine") logger.setLevel(config.SQLALCHEMY_LOGGER_LEVEL) handler = logging.StreamHandler() handler.setLevel(config.SQLALCHEMY_LOGGER_LEVEL) logger.addHandler(handler) yield logger.removeHandler(handler)