testcase baseclass for the url tests make sure the cache is empty before we are doing our tests make sure we will leave an empty cache for other testcases tests if the i18npatterns is adding the prefix correctly if no i18npatterns is used in root urlconfs then no language activation activation happens based on url prefix tests if the patternstrings are translated correctly within the i18npatterns and the normal patterns function namespaced url path url pattern url with parameters links to the same page in other languages would previously error re error missing unterminated subpattern at position 1 tests if the translations are still working within namespaces tests if the user gets redirected to the right url when there is no languageprefix in the request url language from outside of the supported languages list acceptlanguage is not added to the vary header when using prefixed urls the redirect to a prefixed url depends on acceptlanguage and cookie but once prefixed no header is set tests the redirect when the requested url doesn t end with a slash settings appendslashtrue we only want one redirect bypassing commonmiddleware tests the redirect when the requested url doesn t end with a slash settings appendslashfalse tests if the response has the correct language code def testnotprefixedwithprefixself response self client getennotprefixed self assertequalresponse statuscode 404 def testenurlself response self client getenaccountregister self assertequalresponse statuscode 200 self assertequalresponse headerscontentlanguage en self assertequalresponse contextlanguagecode en def testnlurlself response self client getnlprofielregistreren self assertequalresponse statuscode 200 self assertequalresponse headerscontentlanguage nl self assertequalresponse contextlanguagecode nl def testwrongenprefixself response self client getenprofielregistreren self assertequalresponse statuscode 404 def testwrongnlprefixself response self client getnlaccountregister self assertequalresponse statuscode 404 def testptbrurlself response self client getptbrcontaregistrese self assertequalresponse statuscode 200 self assertequalresponse headerscontentlanguage ptbr self assertequalresponse contextlanguagecode ptbr def testenpathself response self client getenaccountregisteraspath self assertequalresponse statuscode 200 self assertequalresponse headerscontentlanguage en self assertequalresponse contextlanguagecode en def testnlpathself response self client getnlprofielregistrerenalspad self assertequalresponse statuscode 200 self assertequalresponse headerscontentlanguage nl self assertequalresponse contextlanguagecode nl overridesettingsrooturlconfi18n urlsdefaultunprefixed languagecodenl class urlprefixedfalsetranslatedtestsurltestcasebase def testtranslatedpathunprefixedlanguageotherthanacceptedheaderself response self client getgebruikers headersacceptlanguage en self assertequalresponse statuscode 200 def testtranslatedpathunprefixedlanguageotherthancookielanguageself self client cookies loadsettings languagecookiename en response self client getgebruikers self assertequalresponse statuscode 200 def testtranslatedpathprefixedlanguageotherthanacceptedheaderself response self client getenusers headersacceptlanguage nl self assertequalresponse statuscode 200 def testtranslatedpathprefixedlanguageotherthancookielanguageself self client cookies loadsettings languagecookiename nl response self client getenusers self assertequalresponse statuscode 200 class urlredirectwithscriptaliastestsurltestcasebase 21579 localemiddleware should respect the script prefix def testlanguageprefixwithscriptprefixself prefix scriptprefix with overridescriptprefixprefix response self client get prefixed headersacceptlanguage en scriptnameprefix self assertredirects response senprefixed prefix targetstatuscode404 class urltagtestsurltestcasebase def teststringsonlyself t template load i18n language lang1 url noprefixtranslated endlanguage language lang2 url noprefixtranslated endlanguage self assertequal tpl renderctx strip split vertaald traduzidos def testargsself tpl template self assertequal tpl rendercontext strip split vertaaldapo traduzidosapo def testkwargsself tpl template self assertequal tpl rendercontext strip split vertaaldapo traduzidosapo testcase base class for the url tests make sure the cache is empty before we are doing our tests make sure we will leave an empty cache for other testcases tests if the i18n_patterns is adding the prefix correctly if no i18n_patterns is used in root urlconfs then no language activation activation happens based on url prefix tests if the pattern strings are translated correctly within the i18n_patterns and the normal patterns function namespaced url path url pattern url with parameters links to the same page in other languages would previously error re error missing unterminated subpattern at position 1 tests if the translations are still working within namespaces tests if the user gets redirected to the right url when there is no language prefix in the request url language from outside of the supported languages list accept language is not added to the vary header when using prefixed urls the redirect to a prefixed url depends on accept language and cookie but once prefixed no header is set tests the redirect when the requested url doesn t end with a slash settings append_slash true we only want one redirect bypassing commonmiddleware tests the redirect when the requested url doesn t end with a slash settings append_slash false tests if the response has the correct language code 21579 localemiddleware should respect the script prefix test if the language tag works load i18n language nl url no prefix translated endlanguage language pt br url no prefix translated endlanguage load i18n language lang1 url no prefix translated endlanguage language lang2 url no prefix translated endlanguage load i18n language nl url no prefix translated slug apo endlanguage language pt br url no prefix translated slug apo endlanguage load i18n language nl url no prefix translated slug slug apo endlanguage language pt br url no prefix translated slug slug apo endlanguage
import os

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse, HttpResponsePermanentRedirect
from django.middleware.locale import LocaleMiddleware
from django.template import Context, Template
from django.test import SimpleTestCase, override_settings
from django.test.client import RequestFactory
from django.test.utils import override_script_prefix
from django.urls import clear_url_caches, resolve, reverse, translate_url
from django.utils import translation


class PermanentRedirectLocaleMiddleWare(LocaleMiddleware):
    response_redirect_class = HttpResponsePermanentRedirect


@override_settings(
    USE_I18N=True,
    LOCALE_PATHS=[
        os.path.join(os.path.dirname(__file__), "locale"),
    ],
    LANGUAGE_CODE="en-us",
    LANGUAGES=[
        ("nl", "Dutch"),
        ("en", "English"),
        ("pt-br", "Brazilian Portuguese"),
    ],
    MIDDLEWARE=[
        "django.middleware.locale.LocaleMiddleware",
        "django.middleware.common.CommonMiddleware",
    ],
    ROOT_URLCONF="i18n.patterns.urls.default",
    TEMPLATES=[
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
            "OPTIONS": {
                "context_processors": [
                    "django.template.context_processors.i18n",
                ],
            },
        }
    ],
)
class URLTestCaseBase(SimpleTestCase):
    

    def setUp(self):
        
        clear_url_caches()

    def tearDown(self):
        
        clear_url_caches()


class URLPrefixTests(URLTestCaseBase):
    

    def test_not_prefixed(self):
        with translation.override("en"):
            self.assertEqual(reverse("not-prefixed"), "/not-prefixed/")
            self.assertEqual(
                reverse("not-prefixed-included-url"), "/not-prefixed-include/foo/"
            )
        with translation.override("nl"):
            self.assertEqual(reverse("not-prefixed"), "/not-prefixed/")
            self.assertEqual(
                reverse("not-prefixed-included-url"), "/not-prefixed-include/foo/"
            )

    def test_prefixed(self):
        with translation.override("en"):
            self.assertEqual(reverse("prefixed"), "/en/prefixed/")
        with translation.override("nl"):
            self.assertEqual(reverse("prefixed"), "/nl/prefixed/")
        with translation.override(None):
            self.assertEqual(
                reverse("prefixed"), "/%s/prefixed/" % settings.LANGUAGE_CODE
            )

    @override_settings(ROOT_URLCONF="i18n.patterns.urls.wrong")
    def test_invalid_prefix_use(self):
        msg = "Using i18n_patterns in an included URLconf is not allowed."
        with self.assertRaisesMessage(ImproperlyConfigured, msg):
            reverse("account:register")


@override_settings(ROOT_URLCONF="i18n.patterns.urls.disabled")
class URLDisabledTests(URLTestCaseBase):
    @override_settings(USE_I18N=False)
    def test_prefixed_i18n_disabled(self):
        with translation.override("en"):
            self.assertEqual(reverse("prefixed"), "/prefixed/")
        with translation.override("nl"):
            self.assertEqual(reverse("prefixed"), "/prefixed/")


class RequestURLConfTests(SimpleTestCase):
    @override_settings(ROOT_URLCONF="i18n.patterns.urls.path_unused")
    def test_request_urlconf_considered(self):
        request = RequestFactory().get("/nl/")
        request.urlconf = "i18n.patterns.urls.default"
        middleware = LocaleMiddleware(lambda req: HttpResponse())
        with translation.override("nl"):
            middleware.process_request(request)
        self.assertEqual(request.LANGUAGE_CODE, "nl")


@override_settings(ROOT_URLCONF="i18n.patterns.urls.path_unused")
class PathUnusedTests(URLTestCaseBase):
    

    def test_no_lang_activate(self):
        response = self.client.get("/nl/foo/")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers["content-language"], "en")
        self.assertEqual(response.context["LANGUAGE_CODE"], "en")


class URLTranslationTests(URLTestCaseBase):
    

    def test_no_prefix_translated(self):
        with translation.override("en"):
            self.assertEqual(reverse("no-prefix-translated"), "/translated/")
            self.assertEqual(
                reverse("no-prefix-translated-slug", kwargs={"slug": "yeah"}),
                "/translated/yeah/",
            )

        with translation.override("nl"):
            self.assertEqual(reverse("no-prefix-translated"), "/vertaald/")
            self.assertEqual(
                reverse("no-prefix-translated-slug", kwargs={"slug": "yeah"}),
                "/vertaald/yeah/",
            )

        with translation.override("pt-br"):
            self.assertEqual(reverse("no-prefix-translated"), "/traduzidos/")
            self.assertEqual(
                reverse("no-prefix-translated-slug", kwargs={"slug": "yeah"}),
                "/traduzidos/yeah/",
            )

    def test_users_url(self):
        with translation.override("en"):
            self.assertEqual(reverse("users"), "/en/users/")

        with translation.override("nl"):
            self.assertEqual(reverse("users"), "/nl/gebruikers/")
            self.assertEqual(reverse("prefixed_xml"), "/nl/prefixed.xml")

        with translation.override("pt-br"):
            self.assertEqual(reverse("users"), "/pt-br/usuarios/")

    def test_translate_url_utility(self):
        with translation.override("en"):
            self.assertEqual(
                translate_url("/en/nonexistent/", "nl"), "/en/nonexistent/"
            )
            self.assertEqual(translate_url("/en/users/", "nl"), "/nl/gebruikers/")
            
            self.assertEqual(
                translate_url("/en/account/register/", "nl"), "/nl/profiel/registreren/"
            )
            
            self.assertEqual(
                translate_url("/en/account/register-as-path/", "nl"),
                "/nl/profiel/registreren-als-pad/",
            )
            self.assertEqual(translation.get_language(), "en")
            
            self.assertEqual(
                translate_url("/en/with-arguments/regular-argument/", "nl"),
                "/nl/with-arguments/regular-argument/",
            )
            self.assertEqual(
                translate_url(
                    "/en/with-arguments/regular-argument/optional.html", "nl"
                ),
                "/nl/with-arguments/regular-argument/optional.html",
            )

        with translation.override("nl"):
            self.assertEqual(translate_url("/nl/gebruikers/", "en"), "/en/users/")
            self.assertEqual(translation.get_language(), "nl")

    def test_reverse_translated_with_captured_kwargs(self):
        with translation.override("en"):
            match = resolve("/translated/apo/")
        
        tests = [
            ("nl", "/vertaald/apo/"),
            ("pt-br", "/traduzidos/apo/"),
        ]
        for lang, expected_link in tests:
            with translation.override(lang):
                self.assertEqual(
                    reverse(
                        match.url_name, args=match.args, kwargs=match.captured_kwargs
                    ),
                    expected_link,
                )

    def test_locale_not_interepreted_as_regex(self):
        with translation.override("e("):
            
            
            reverse("users")


class URLNamespaceTests(URLTestCaseBase):
    

    def test_account_register(self):
        with translation.override("en"):
            self.assertEqual(reverse("account:register"), "/en/account/register/")
            self.assertEqual(
                reverse("account:register-as-path"), "/en/account/register-as-path/"
            )

        with translation.override("nl"):
            self.assertEqual(reverse("account:register"), "/nl/profiel/registreren/")
            self.assertEqual(
                reverse("account:register-as-path"), "/nl/profiel/registreren-als-pad/"
            )


class URLRedirectTests(URLTestCaseBase):
    

    def test_no_prefix_response(self):
        response = self.client.get("/not-prefixed/")
        self.assertEqual(response.status_code, 200)

    def test_en_redirect(self):
        response = self.client.get(
            "/account/register/", headers={"accept-language": "en"}
        )
        self.assertRedirects(response, "/en/account/register/")

        response = self.client.get(response.headers["location"])
        self.assertEqual(response.status_code, 200)

    def test_en_redirect_wrong_url(self):
        response = self.client.get(
            "/profiel/registreren/", headers={"accept-language": "en"}
        )
        self.assertEqual(response.status_code, 404)

    def test_nl_redirect(self):
        response = self.client.get(
            "/profiel/registreren/", headers={"accept-language": "nl"}
        )
        self.assertRedirects(response, "/nl/profiel/registreren/")

        response = self.client.get(response.headers["location"])
        self.assertEqual(response.status_code, 200)

    def test_nl_redirect_wrong_url(self):
        response = self.client.get(
            "/account/register/", headers={"accept-language": "nl"}
        )
        self.assertEqual(response.status_code, 404)

    def test_pt_br_redirect(self):
        response = self.client.get(
            "/conta/registre-se/", headers={"accept-language": "pt-br"}
        )
        self.assertRedirects(response, "/pt-br/conta/registre-se/")

        response = self.client.get(response.headers["location"])
        self.assertEqual(response.status_code, 200)

    def test_pl_pl_redirect(self):
        
        response = self.client.get(
            "/account/register/", headers={"accept-language": "pl-pl"}
        )
        self.assertRedirects(response, "/en/account/register/")

        response = self.client.get(response.headers["location"])
        self.assertEqual(response.status_code, 200)

    @override_settings(
        MIDDLEWARE=[
            "i18n.patterns.tests.PermanentRedirectLocaleMiddleWare",
            "django.middleware.common.CommonMiddleware",
        ],
    )
    def test_custom_redirect_class(self):
        response = self.client.get(
            "/account/register/", headers={"accept-language": "en"}
        )
        self.assertRedirects(response, "/en/account/register/", 301)


class URLVaryAcceptLanguageTests(URLTestCaseBase):
    

    def test_no_prefix_response(self):
        response = self.client.get("/not-prefixed/")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.get("Vary"), "Accept-Language")

    def test_en_redirect(self):
        
        response = self.client.get(
            "/account/register/", headers={"accept-language": "en"}
        )
        self.assertRedirects(response, "/en/account/register/")
        self.assertEqual(response.get("Vary"), "Accept-Language, Cookie")

        response = self.client.get(response.headers["location"])
        self.assertEqual(response.status_code, 200)
        self.assertFalse(response.get("Vary"))


class URLRedirectWithoutTrailingSlashTests(URLTestCaseBase):
    

    def test_not_prefixed_redirect(self):
        response = self.client.get("/not-prefixed", headers={"accept-language": "en"})
        self.assertRedirects(response, "/not-prefixed/", 301)

    def test_en_redirect(self):
        response = self.client.get(
            "/account/register", headers={"accept-language": "en"}, follow=True
        )
        
        self.assertEqual(response.redirect_chain, [("/en/account/register/", 302)])
        self.assertRedirects(response, "/en/account/register/", 302)

        response = self.client.get(
            "/prefixed.xml", headers={"accept-language": "en"}, follow=True
        )
        self.assertRedirects(response, "/en/prefixed.xml", 302)


class URLRedirectWithoutTrailingSlashSettingTests(URLTestCaseBase):
    

    @override_settings(APPEND_SLASH=False)
    def test_not_prefixed_redirect(self):
        response = self.client.get("/not-prefixed", headers={"accept-language": "en"})
        self.assertEqual(response.status_code, 404)

    @override_settings(APPEND_SLASH=False)
    def test_en_redirect(self):
        response = self.client.get(
            "/account/register-without-slash", headers={"accept-language": "en"}
        )
        self.assertRedirects(response, "/en/account/register-without-slash", 302)

        response = self.client.get(response.headers["location"])
        self.assertEqual(response.status_code, 200)


class URLResponseTests(URLTestCaseBase):
    

    def test_not_prefixed_with_prefix(self):
        response = self.client.get("/en/not-prefixed/")
        self.assertEqual(response.status_code, 404)

    def test_en_url(self):
        response = self.client.get("/en/account/register/")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers["content-language"], "en")
        self.assertEqual(response.context["LANGUAGE_CODE"], "en")

    def test_nl_url(self):
        response = self.client.get("/nl/profiel/registreren/")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers["content-language"], "nl")
        self.assertEqual(response.context["LANGUAGE_CODE"], "nl")

    def test_wrong_en_prefix(self):
        response = self.client.get("/en/profiel/registreren/")
        self.assertEqual(response.status_code, 404)

    def test_wrong_nl_prefix(self):
        response = self.client.get("/nl/account/register/")
        self.assertEqual(response.status_code, 404)

    def test_pt_br_url(self):
        response = self.client.get("/pt-br/conta/registre-se/")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers["content-language"], "pt-br")
        self.assertEqual(response.context["LANGUAGE_CODE"], "pt-br")

    def test_en_path(self):
        response = self.client.get("/en/account/register-as-path/")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers["content-language"], "en")
        self.assertEqual(response.context["LANGUAGE_CODE"], "en")

    def test_nl_path(self):
        response = self.client.get("/nl/profiel/registreren-als-pad/")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers["content-language"], "nl")
        self.assertEqual(response.context["LANGUAGE_CODE"], "nl")


@override_settings(ROOT_URLCONF="i18n.urls_default_unprefixed", LANGUAGE_CODE="nl")
class URLPrefixedFalseTranslatedTests(URLTestCaseBase):
    def test_translated_path_unprefixed_language_other_than_accepted_header(self):
        response = self.client.get("/gebruikers/", headers={"accept-language": "en"})
        self.assertEqual(response.status_code, 200)

    def test_translated_path_unprefixed_language_other_than_cookie_language(self):
        self.client.cookies.load({settings.LANGUAGE_COOKIE_NAME: "en"})
        response = self.client.get("/gebruikers/")
        self.assertEqual(response.status_code, 200)

    def test_translated_path_prefixed_language_other_than_accepted_header(self):
        response = self.client.get("/en/users/", headers={"accept-language": "nl"})
        self.assertEqual(response.status_code, 200)

    def test_translated_path_prefixed_language_other_than_cookie_language(self):
        self.client.cookies.load({settings.LANGUAGE_COOKIE_NAME: "nl"})
        response = self.client.get("/en/users/")
        self.assertEqual(response.status_code, 200)


class URLRedirectWithScriptAliasTests(URLTestCaseBase):
    

    def test_language_prefix_with_script_prefix(self):
        prefix = "/script_prefix"
        with override_script_prefix(prefix):
            response = self.client.get(
                "/prefixed/", headers={"accept-language": "en"}, SCRIPT_NAME=prefix
            )
            self.assertRedirects(
                response, "%s/en/prefixed/" % prefix, target_status_code=404
            )


class URLTagTests(URLTestCaseBase):
    

    def test_strings_only(self):
        t = Template(
            
        )
        self.assertEqual(
            t.render(Context({})).strip().split(), ["/vertaald/", "/traduzidos/"]
        )

    def test_context(self):
        ctx = Context({"lang1": "nl", "lang2": "pt-br"})
        tpl = Template(
            
        )
        self.assertEqual(
            tpl.render(ctx).strip().split(), ["/vertaald/", "/traduzidos/"]
        )

    def test_args(self):
        tpl = Template(
            
        )
        self.assertEqual(
            tpl.render(Context({})).strip().split(),
            ["/vertaald/apo/", "/traduzidos/apo/"],
        )

    def test_kwargs(self):
        tpl = Template(
            
        )
        self.assertEqual(
            tpl.render(Context({})).strip().split(),
            ["/vertaald/apo/", "/traduzidos/apo/"],
        )