"""conftest. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ import pytest from features import config from features import server from features.constants import header from features.utils import parser from unittest.mock import MagicMock from features.connectors import split @pytest.fixture def mock_splitio_factory(monkeypatch): """Mock splitio.get_factory and return the mock factory.""" mock_factory = MagicMock() mock_get_factory = MagicMock(return_value=mock_factory) # Reset the split._factory to a new mock_factory monkeypatch.setattr(split, '_factory', mock_factory) # Make get_factory return the same mock_factory monkeypatch.setattr('splitio.get_factory', mock_get_factory) return mock_get_factory @pytest.fixture def forced_feature_data(all_feature_data): """Return data for multiple features with a user override.""" forced_data = all_feature_data forced_data['feature_2'] = 'variant_1' return forced_data @pytest.fixture def all_feature_data(): """Return data for multiple features.""" return {'feature_1': 'variant_1', 'feature_2': 'variant_2'} @pytest.fixture def single_feature_data(all_feature_data): """Return data for a single feature.""" return all_feature_data['feature_1'] @pytest.fixture def client(mock_splitio_factory): """Return flask test client. Returns: flask client: flask test client """ import application reload_features() return application.app.test_client() def reload_features(): """Reload features from config file.""" server.features.clear() server.features.update( parser.parse_features_from_yaml_file(config.FEATURES_YML_FILE) ) @pytest.fixture def forced_headers(): """Return valid Grass headers with a user with a force rule. Returns: dict: header values. """ return { header.GRASS_ACCOUNT_TYPE: header.VENDOR_GRASS_TYPE, header.GRASS_ACCOUNT_ID: '7123', header.ORCHARD_USER_ID: 'oa:123', } @pytest.fixture def valid_oa_headers(): """Return valid Grass headers for OA user.""" return {header.ORCHARD_USER_ID: 'oa:123'} @pytest.fixture def valid_vendor_headers(): """Return valid Grass headers for ALW vendor user.""" return { header.GRASS_ACCOUNT_TYPE: header.VENDOR_GRASS_TYPE, header.GRASS_ACCOUNT_ID: '7123', header.ORCHARD_USER_ID: 'alw:16091', } @pytest.fixture def valid_subaccount_headers(): """Return valid Grass headers for ALW subaccount user.""" return { header.GRASS_ACCOUNT_TYPE: header.SUBACCOUNT_GRASS_TYPE, header.GRASS_ACCOUNT_ID: '1', header.ORCHARD_USER_ID: 'alw:16092', } @pytest.fixture def invalid_headers(): """Return invalid Grass headers. Returns: dict: header values. """ return {header.GRASS_ACCOUNT_ID: '7123', header.ORCHARD_USER_ID: 'alw:16091'}