"""Fixtures for tests.""" import uuid from unittest.mock import MagicMock import pytest from owslogger import flask_logger from owsresponse.response import Response import application from salessheets import features from salessheets.constants import field_const from salessheets.features import pythonfeatures @pytest.fixture def mock_app(): """Mock app.""" with application.app.app_context() as context: context.g.ows = flask_logger.Ows() context.g.ows.log = MagicMock() context.g.request_context = MagicMock() yield context class FeatureEngineAdaptor: """Mocking Class for Feature Engine. Patching the real feature flags client. """ def __init__(self): """Setting empty feature_flags by default.""" self.feature_flags = {} def force_flag(self, flag_name, flag_value): """Force a flag value. Args: flag_name (str): the name of the flag. flag_value (bool): the value of the flag. Set it to true for getting "enabled" value on getting the flag. Set false for getting "control". """ self.feature_flags[flag_name] = flag_value def get(self, flag_name): """Get a flag. Args: flag_name (str): the name of the flag to get. """ return self.feature_flags.get(flag_name) @pytest.fixture def feature_engine(mocker, mock_app): """Fixture to mock feature flags. Use it in the following way: def test_muy_feature(feature_engine): feature_engine.force_flag('my_flag', True) # run code using the flag you set. """ _feature_engine = FeatureEngineAdaptor() def get_single_feature(flag_name, *args, **kwargs): return Response( status=200, message=("enabled" if _feature_engine.get(flag_name) else "control") ) mocker.patch.object( pythonfeatures, "get_single_feature", side_effect=get_single_feature) yield _feature_engine @pytest.fixture def features_mock(mocker): """Fixture to mock feature flags. Use it for functions being tested outside of app and request context, those which don't guarantee having g.ows and g.request_context. It patches the whole "is_feature_enabled" function. Use it in the following way: def test_muy_feature(features_mock): features_mock.force_flag('my_flag', True) # run code using the flag you set. """ _feature_engine = FeatureEngineAdaptor() def is_feature_enabled(flag_name, *args, **kwargs): return True if _feature_engine.get(flag_name) else False mocker.patch.object( features, "is_feature_enabled", side_effect=is_feature_enabled) yield _feature_engine @pytest.fixture def valid_headers(): """Function that returns valid headers. Returns: dict: header dict """ return { field_const.CORRELATION_ID: str(uuid.uuid4()), field_const.ORCHARD_USER_ID: 'oa:1234'} @pytest.fixture def valid_alw_headers(): """Function that returns valid headers for alw user. Returns: dict: header dict """ return { field_const.CORRELATION_ID: str(uuid.uuid4()), field_const.ORCHARD_USER_ID: 'alw:1234', field_const.GRASS_ACCOUNT_ID: '1234', field_const.GRASS_ACCOUNT_TYPE: 'vendor', } @pytest.fixture def alw_headers_missing_grass_account_type(): """Function that returns headers without Grass-Account-Type for alw user. Returns: dict: header dict """ return { field_const.CORRELATION_ID: str(uuid.uuid4()), field_const.ORCHARD_USER_ID: 'alw:1234', field_const.GRASS_ACCOUNT_ID: '1234', } @pytest.fixture def alw_headers_missing_grass_account_id(): """Function that returns headers without Grass-Account-Id for alw user. Returns: dict: header dict """ return { field_const.CORRELATION_ID: str(uuid.uuid4()), field_const.ORCHARD_USER_ID: 'alw:1234', field_const.GRASS_ACCOUNT_TYPE: 'vendor', } @pytest.fixture def client(): """Function that returns flask test client. Returns: flask client: flask test client """ return application.app.test_client() @pytest.fixture def app_context(): """Create an api test app fixture.""" with application.app.app_context(): yield