def test_get_best_language_exact_match(self): """ Should return exact match if it is in settings.LANGUAGE_URL_MAP """ request = self.factory.get("/") prefixer = Prefixer(request) eq_(prefixer.get_best_language("de, es"), "de")
def test_get_best_language_exact_match(self): """ Should return exact match if it is in settings.LANGUAGE_URL_MAP """ request = self.factory.get('/') prefixer = Prefixer(request) eq_(prefixer.get_best_language('de, es'), 'de')
def test_get_best_language_handles_parse_accept_lang_header_error(self): """ Should return None despite error raised by bug described in https://code.djangoproject.com/ticket/21078 """ request = self.factory.get("/") prefixer = Prefixer(request) eq_(prefixer.get_best_language("en; q=1,"), None)
def test_get_best_language_prefix_match(self): """ Should return a language with a matching prefix from settings.LANGUAGE_URL_MAP if it exists but no exact match does """ request = self.factory.get('/') prefixer = Prefixer(request) eq_(prefixer.get_best_language('es-CL'), 'es-AR')
def test_get_best_language_no_match(self): """ Should return None if there is no exact match or matching prefix """ request = self.factory.get('/') prefixer = Prefixer(request) eq_(prefixer.get_best_language('de'), None)
def test_get_best_language_no_match(self): """ Should return None if there is no exact match or matching prefix """ request = self.factory.get("/") prefixer = Prefixer(request) eq_(prefixer.get_best_language("de"), None)
def test_get_best_language_handles_parse_accept_lang_header_error(self): """ Should return None despite error raised by bug described in https://code.djangoproject.com/ticket/21078 """ request = self.factory.get('/') prefixer = Prefixer(request) eq_(prefixer.get_best_language('en; q=1,'), None)
def test_get_language_valid_lang_param(self): """ Should return lang param value if it is in settings.LANGUAGE_URL_MAP """ request = self.factory.get("/?lang=de") eq_(request.GET.get("lang"), "de") ok_("de" in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), "de")
def test_get_language_valid_lang_param(self): """ Should return lang param value if it is in settings.LANGUAGE_URL_MAP """ request = self.factory.get('/?lang=de') eq_(request.GET.get('lang'), 'de') ok_('de' in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), 'de')
def test_get_language_invalid_lang_param(self): """ Should return default set by settings.LANGUAGE_CODE if lang param value is not in settings.LANGUAGE_URL_MAP """ request = self.factory.get("/?lang=de") ok_("lang" in request.GET) self.assertFalse("de" in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), "en-US")
def test_get_language_default_language_code(self): """ Should return default set by settings.LANGUAGE_CODE if no 'lang' url parameter and no Accept-Language header """ request = self.factory.get('/') self.assertFalse('lang' in request.GET) self.assertFalse(request.META.get('HTTP_ACCEPT_LANGUAGE')) prefixer = Prefixer(request) eq_(prefixer.get_language(), 'en-US')
def test_get_language_default_language_code(self): """ Should return default set by settings.LANGUAGE_CODE if no 'lang' url parameter and no Accept-Language header """ request = self.factory.get("/") self.assertFalse("lang" in request.GET) self.assertFalse(request.META.get("HTTP_ACCEPT_LANGUAGE")) prefixer = Prefixer(request) eq_(prefixer.get_language(), "en-US")
def test_get_language_invalid_lang_param(self): """ Should return default set by settings.LANGUAGE_CODE if lang param value is not in settings.LANGUAGE_URL_MAP """ request = self.factory.get('/?lang=de') ok_('lang' in request.GET) self.assertFalse('de' in settings.LANGUAGE_URL_MAP) prefixer = Prefixer(request) eq_(prefixer.get_language(), 'en-US')
def test_get_language_returns_best(self): """ Should pass Accept-Language header value to get_best_language and return result """ request = self.factory.get('/') request.META['HTTP_ACCEPT_LANGUAGE'] = 'de, es' prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value='de') eq_(prefixer.get_language(), 'de') prefixer.get_best_language.assert_called_once_with('de, es')
def test_get_language_no_best(self): """ Should return default set by settings.LANGUAGE_CODE if get_best_language return value is None """ request = self.factory.get('/') request.META['HTTP_ACCEPT_LANGUAGE'] = 'de, es' prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value=None) eq_(prefixer.get_language(), 'en-US') prefixer.get_best_language.assert_called_once_with('de, es')
def test_get_language_no_best(self): """ Should return default set by settings.LANGUAGE_CODE if get_best_language return value is None """ request = self.factory.get("/") request.META["HTTP_ACCEPT_LANGUAGE"] = "de, es" prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value=None) eq_(prefixer.get_language(), "en-US") prefixer.get_best_language.assert_called_once_with("de, es")
def test_get_language_returns_best(self): """ Should pass Accept-Language header value to get_best_language and return result """ request = self.factory.get("/") request.META["HTTP_ACCEPT_LANGUAGE"] = "de, es" prefixer = Prefixer(request) prefixer.get_best_language = Mock(return_value="de") eq_(prefixer.get_language(), "de") prefixer.get_best_language.assert_called_once_with("de, es")
def _pre_setup(self): # Add the models to the db. self._original_installed_apps = list(settings.INSTALLED_APPS) for app in self.apps: settings.INSTALLED_APPS.append(app) loading.cache.loaded = False call_command('syncdb', interactive=False, verbosity=0) call_command('update_badges', verbosity=0) badger.autodiscover() if get_url_prefix: # If we're in funfactoryland, make sure a locale prefix is # set for urlresolvers locale = 'en-US' self.old_prefix = get_url_prefix() self.old_locale = get_language() rf = RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale, )))) activate(locale) # Create a default user for tests self.user_1 = self._get_user(username="******", email="*****@*****.**", password="******") # Call the original method that does the fixtures etc. super(test.TestCase, self)._pre_setup()
def activate(self, locale): """Context manager that temporarily activates a locale.""" old_prefix = get_url_prefix() old_locale = get_language() rf = test_utils.RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale, )))) activate(locale) yield set_url_prefix(old_prefix) activate(old_locale)
def well_known_browserid(request): # HACK: The following code is terrible. But, I really wanted some # locale-free URLs, in order to force a locale redirect later. op = get_url_prefix() np = Prefixer(request) np.locale = 'XXX' set_url_prefix(np) authentication_url=reverse('whuru.browserid.sign_in').replace('/XXX','') provisioning_url=reverse('whuru.browserid.provision').replace('/XXX','') set_url_prefix(op) settings_root = os.path.dirname(whuru.settings.__file__) public_key_fn = os.path.join(settings_root, 'key.publickey') public_key = open(public_key_fn, 'rb').read() resp = render(request, 'browserid/well-known-browserid.json', dict( public_key=public_key, authentication_url=authentication_url, provisioning_url=provisioning_url, )) resp['Content-Type'] = 'application/json' return resp
def process_request(self, request): # Exit early if we are not in the Facebook app section of the site to # avoid clashing with the Django auth middleware. if not in_facebook_app(request): return None # Default to an anonymous user. request.user = AnonymousFacebookUser() locale = None try: user = FacebookUser.objects.get(id=request.session[SESSION_KEY]) request.user = user locale = user.locale except (FacebookUser.DoesNotExist, KeyError): pass if locale is None: # Since we can't get their locale from their user data, we'll use # funfactory's prefixer instead. prefixer = Prefixer(request) locale = prefixer.get_language() activate_locale(request, locale)
def test_get_best_language_prefix_match(self): """ Should return a language with a matching prefix from settings.LANGUAGE_URL_MAP + settings.CANONICAL_LOCALES if it exists but no exact match does """ request = self.factory.get('/') prefixer = Prefixer(request) eq_(prefixer.get_best_language('en'), 'en-US') eq_(prefixer.get_best_language('en-CA'), 'en-US') eq_(prefixer.get_best_language('en-GB'), 'en-US') eq_(prefixer.get_best_language('en-US'), 'en-US') eq_(prefixer.get_best_language('es'), 'es-ES') eq_(prefixer.get_best_language('es-AR'), 'es-AR') eq_(prefixer.get_best_language('es-CL'), 'es-ES') eq_(prefixer.get_best_language('es-MX'), 'es-ES')
def well_known_browserid(request): # HACK: The following code is terrible. But, I really wanted some # locale-free URLs, in order to force a locale redirect later. op = get_url_prefix() np = Prefixer(request) np.locale = 'XXX' set_url_prefix(np) authentication_url = reverse('whuru.browserid.sign_in').replace('/XXX', '') provisioning_url = reverse('whuru.browserid.provision').replace('/XXX', '') set_url_prefix(op) settings_root = os.path.dirname(whuru.settings.__file__) public_key_fn = os.path.join(settings_root, 'key.publickey') public_key = open(public_key_fn, 'rb').read() resp = render( request, 'browserid/well-known-browserid.json', dict( public_key=public_key, authentication_url=authentication_url, provisioning_url=provisioning_url, )) resp['Content-Type'] = 'application/json' return resp
def test_get_best_language_prefix_match(self): """ Should return a language with a matching prefix from settings.LANGUAGE_URL_MAP + settings.CANONICAL_LOCALES if it exists but no exact match does """ request = self.factory.get("/") prefixer = Prefixer(request) eq_(prefixer.get_best_language("en"), "en-US") eq_(prefixer.get_best_language("en-CA"), "en-US") eq_(prefixer.get_best_language("en-GB"), "en-US") eq_(prefixer.get_best_language("en-US"), "en-US") eq_(prefixer.get_best_language("es"), "es-ES") eq_(prefixer.get_best_language("es-AR"), "es-AR") eq_(prefixer.get_best_language("es-CL"), "es-ES") eq_(prefixer.get_best_language("es-MX"), "es-ES")
def _pre_setup(self): loading.cache.loaded = False call_command('update_badges', verbosity=0) badger.autodiscover() if get_url_prefix: # If we're in funfactoryland, make sure a locale prefix is # set for urlresolvers locale = 'en-US' self.old_prefix = get_url_prefix() self.old_locale = get_language() rf = RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale,)))) # Create a default user for tests self.user_1 = self._get_user(username="******", email="*****@*****.**", password="******") # Call the original method that does the fixtures etc. super(test.TestCase, self)._pre_setup()
def lang_activate(locale): """ Context manager that temporarily activates a locale. This is slightly different from use_lang, as it also sets the global prefixer used by reverse to determine the locale in urls. We don't use that in non-test code because it creates a test request using Django test code. This means that use_lang does not affect reverse. We get around that with a hack of using the lang GET argument when necessary, but this really should be fixed someday. """ if not locale: yield else: old_prefix = get_url_prefix() old_locale = get_language() rf = test_utils.RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale, )))) activate(locale) yield set_url_prefix(old_prefix) activate(old_locale)