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(LINGOX_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 LingoX 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') 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') 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.unpack @ddt.data( { # The site-wide language should be used instead of the request's. 'settings_lang': 'en', 'request_lang': 'eo', 'site_configs': {}, 'expected': 'Hello World', }, { # The site-wide language should be used instead of the request's. 'settings_lang': 'eo', 'request_lang': 'en', 'site_configs': {}, 'expected': u'Héllö Wörld', }, { # The "Microsite" language should be used instead. 'settings_lang': 'eo', 'request_lang': 'eo', 'site_configs': { 'LANGUAGE_CODE': 'en', }, 'expected': u'Hello World', }, { # The "Microsite" language should be used instead. 'settings_lang': 'en', 'request_lang': 'en', 'site_configs': { 'LANGUAGE_CODE': 'eo', }, 'expected': u'Héllö Wörld', }, ) @override_settings(MIDDLEWARE_CLASSES=middleware_classes) def test_enabled_middleware_in_request(self, settings_lang, request_lang, site_configs, expected): """ 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. """ with override_settings(LANGUAGE_CODE=settings_lang, MOCK_SITE_CONFIGS=site_configs): headers = { 'Accept-Language': request_lang, } res = self.client.get('/', **headers) self.assertContains(res, expected, msg_prefix='Incorrect language detected')
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.'