def test_call_update_middleware_twice(self): """ Ensure that the method works well if it was called twice. """ once = add_locale_middleware(UNMODIFIED_MIDDLEWARE_CLASSES) twice = add_locale_middleware( add_locale_middleware(UNMODIFIED_MIDDLEWARE_CLASSES)) assert once == twice
def test_missing_site_middleware(self): """ The helper should require the CurrentSiteMiddleware to be available in the middleware classes list. """ middleware_classes = tuple( class_name for class_name in UNMODIFIED_MIDDLEWARE_CLASSES if class_name != SITE_MIDDLEWARE) with self.assertRaises(ValueError): add_locale_middleware(middleware_classes)
def test_missing_middleware_on_update(self, middleware_to_remove): """ Ensure that the helper fails explicitly when an expected middleware is missing. """ middleware_classes = tuple( class_name for class_name in UNMODIFIED_MIDDLEWARE_CLASSES if class_name != middleware_to_remove) with self.assertRaises(ValueError): add_locale_middleware(middleware_classes)
def test_middleware_order(self, other_middleware): """ Ensures that the middleware comes before any other locale-related middleware. """ updated_middlewares = add_locale_middleware( UNMODIFIED_MIDDLEWARE_CLASSES) lingx_index = updated_middlewares.index(LOCALIZERX_MIDDLEWARE) other_index = updated_middlewares.index(other_middleware) assert lingx_index < other_index, \ 'DefaultLocaleMiddleware should come before any other locale-related middleware'
def test_incorrect_site_middleware_location(self): """ Ensure the helper complains about bizarre middleware configs. DefaultLocaleMiddleware can only work _after_ the CurrentSiteMiddleware and _before_ every locale-aware middleware. """ middleware_classes = [ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'openedx.core.djangoapps.lang_pref.middleware.LanguagePreferenceMiddleware', # After a locale-middleware, to confuse the helper 'django.contrib.sites.middleware.CurrentSiteMiddleware', 'openedx.core.djangoapps.dark_lang.middleware.DarkLangMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', ] with self.assertRaises(ImproperlyConfigured): add_locale_middleware(middleware_classes)
def ready(self): """ Monkeypatch MIDDLEWARE_CLASSES to the LocalizerX middleware. """ settings.MIDDLEWARE_CLASSES = add_locale_middleware( settings.MIDDLEWARE_CLASSES)
class DefaultLocaleMiddlewareTest(TestCase): """ Unit and integration tests for the DefaultLocaleMiddleware. """ middleware_classes = add_locale_middleware(UNMODIFIED_MIDDLEWARE_CLASSES) def setUp(self): """ Set up the environment for the test case. """ super(DefaultLocaleMiddlewareTest, self).setUp() self.middleware = DefaultLocaleMiddleware() self.request_factory = RequestFactory() @override_settings(LANGUAGE_CODE='eo', FEATURES={'ENABLE_LOCALIZERX': True}) def test_non_api_views(self): """ Test the middleware on non-API pages. """ req = self.request_factory.get('/dummy/') req.META['HTTP_ACCEPT_LANGUAGE'] = 'en' self.middleware.process_request(req) assert req.META['HTTP_ACCEPT_LANGUAGE'] == 'eo', \ 'The middleware is installed so it should change the language for non-API views.' assert req.META['_HTTP_ACCEPT_LANGUAGE'] == 'en', \ 'Should preserve the original language in another META variable.' @ddt.data('/api/', '/user_api/') @override_settings(LANGUAGE_CODE='ar', FEATURES={'ENABLE_LOCALIZERX': True}) def test_api_views(self, api_url): """ Ensure that the middleware doesn't change the non-API pages. """ req = self.request_factory.get(api_url) client_language = 'en' req.META['HTTP_ACCEPT_LANGUAGE'] = client_language self.middleware.process_request(req) assert req.META['HTTP_ACCEPT_LANGUAGE'] == client_language, \ 'The middleware is being used but it should NOT change the language for API views.' @ddt.data( { 'settings_lang': 'en', 'request_lang': 'eo', 'site_configs': {}, 'expected': 'Hello World', 'unexpected': 'Héllö Wörld', 'message': 'The site-wide language should be used instead of the request\'s.', }, { 'settings_lang': 'eo', 'request_lang': 'en', 'site_configs': {}, 'expected': 'Héllö Wörld', 'unexpected': 'Hello World', 'message': 'The site-wide language should be used instead of the request\'s.', }, { 'settings_lang': 'eo', 'request_lang': 'eo', 'site_configs': { 'LANGUAGE_CODE': 'en', }, 'expected': 'Hello World', 'unexpected': 'Héllö Wörld', 'message': 'The "Microsite" language should be used instead.', }, { 'settings_lang': 'en', 'request_lang': 'en', 'site_configs': { 'LANGUAGE_CODE': 'eo', }, 'expected': 'Héllö Wörld', 'unexpected': 'Hello World', 'message': 'The "Microsite" language should be used instead.', }, ) def test_enabled_middleware_in_request(self, data): """ Test different combinations of LANGUAGE_CODE and Accept-Language. The response language should always respect the `settings_lang` and ignore the `request_lang`. If `openedx.core.djangoapps.site_configuration.helpers.get_value('LANGUAGE_CODE') is available`, that should override the settings.LANGUAGE_CODE. """ overrides = { 'LANGUAGE_CODE': data['settings_lang'], 'MOCK_SITE_CONFIGS': data['site_configs'], 'FEATURES': { 'ENABLE_LOCALIZERX': True }, 'MIDDLEWARE_CLASSES': self.middleware_classes, } with override_settings(**overrides): res = self.client.get('/', HTTP_ACCEPT_LANGUAGE=data['request_lang']) msg_prefix = 'Incorrect language detected - {message}'.format( message=data['message']) self.assertNotContains(res, data['unexpected'], msg_prefix=msg_prefix) self.assertContains(res, data['expected'], msg_prefix=msg_prefix)
def test_middleware_type(self, middlewares): """ Ensure that the method works regardless whether the MIDDLEWARE_CLASSES was a list or a tuple. """ assert isinstance(add_locale_middleware(middlewares), tuple), \ 'Should convert to list, regardless of the input.'
class IsFeatureEnabledHelperTest(TestCase): """ Tests for the `is_feature_enabled` helper function. """ middleware_classes = add_locale_middleware(UNMODIFIED_MIDDLEWARE_CLASSES) @ddt.data( { 'features': { 'ENABLE_LOCALIZERX': True, }, 'site_configs': {}, 'expected': True, 'message': 'Enabled via platform flag', }, { 'features': {}, 'site_configs': { 'ENABLE_LOCALIZERX': True, }, 'expected': True, 'message': 'Enabled via site configs', }, { 'features': { 'ENABLE_LOCALIZERX': True, }, 'site_configs': { 'ENABLE_LOCALIZERX': False, }, 'expected': False, 'message': 'Disabled via site configs', }, { 'features': {}, 'site_configs': {}, 'expected': False, 'message': 'Disabled by default', }) def test_is_enabled_method(self, data): """ Tests for the `is_feature_enabled` method. """ overrides = { 'MOCK_SITE_CONFIGS': data['site_configs'], 'FEATURES': data['features'], } with override_settings(**overrides): assert is_feature_enabled() == data['expected'], data['message'] @ddt.data( { 'is_feature_enabled': True, 'site_configs': {}, 'expected': 'Héllö Wörld', 'unexpected': 'Hello World', 'message': 'Enabled, so site lang (eo) should be USED and req lang (en) should be IGNORED', }, { 'is_feature_enabled': False, 'unexpected': 'Héllö Wörld', 'expected': 'Hello World', 'message': 'Disabled, so site lang (eo) should be IGNORED and req lang (en) should be USED', }) @override_settings(MIDDLEWARE_CLASSES=middleware_classes, LANGUAGE_CODE='eo') def test_feature_flags(self, data): """ Test different combinations of feature flags. """ with patch( target='localizerx.middleware.is_feature_enabled', return_value=data['is_feature_enabled'], ): from localizerx.middleware import is_feature_enabled as patched_is_feature_enabled assert patched_is_feature_enabled() == data['is_feature_enabled'] res = self.client.get('/', HTTP_ACCEPT_LANGUAGE='en') msg_prefix = 'Incorrect language detected - {message}'.format( message=data['message']) self.assertNotContains(res, data['unexpected'], msg_prefix=msg_prefix) self.assertContains(res, data['expected'], msg_prefix=msg_prefix)