From f0e666a4f4f3049e602d5734969415ba19147bbd Mon Sep 17 00:00:00 2001 From: Jhonatan Lopes Date: Thu, 22 Feb 2024 13:19:34 -0300 Subject: [PATCH] Remove `LANGUAGE_SESSION_KEY` from `localized_redirect` (#11896) * Remove LANGUAGE_SESSION_KEY from localized_redirect * Add tests --- .../wagtailpages/tests/test_views.py | 44 +++++++++++++++++++ network-api/networkapi/wagtailpages/views.py | 1 - 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 network-api/networkapi/wagtailpages/tests/test_views.py diff --git a/network-api/networkapi/wagtailpages/tests/test_views.py b/network-api/networkapi/wagtailpages/tests/test_views.py new file mode 100644 index 00000000000..90dc278afe5 --- /dev/null +++ b/network-api/networkapi/wagtailpages/tests/test_views.py @@ -0,0 +1,44 @@ +from django.test import RequestFactory, SimpleTestCase + +from networkapi.wagtailpages.views import localized_redirect + + +class LocalizedRedirectTests(SimpleTestCase): + def setUp(self): + self.factory = RequestFactory() + + def test_redirect_with_subpath_and_query_string(self): + # Create a mock request object + request = self.factory.get("/destination/") + request.LANGUAGE_CODE = "en" + request.META = {"QUERY_STRING": "param=value"} + + # Call the localized_redirect function + result = localized_redirect(request, "subpath", "destination") + + # Assert that the redirect URL is correct + self.assertEqual(result.url, "/en/destination/subpath?param=value") + + def test_redirect_with_empty_subpath_and_query_string(self): + # Create a mock request object + request = self.factory.get("/destination/") + request.LANGUAGE_CODE = "en" + request.META = {"QUERY_STRING": ""} + + # Call the localized_redirect function + result = localized_redirect(request, "", "destination") + + # Assert that the redirect URL is correct + self.assertEqual(result.url, "/en/destination/") + + def test_redirect_with_active_language(self): + # Create a mock request object + request = self.factory.get("/en/destination/") + request.LANGUAGE_CODE = "fr" + request.META = {"QUERY_STRING": ""} + + # Call the localized_redirect function + result = localized_redirect(request, "", "destination") + + # Assert that the redirect URL is correct + self.assertEqual(result.url, "/fr/destination/") diff --git a/network-api/networkapi/wagtailpages/views.py b/network-api/networkapi/wagtailpages/views.py index ed4c26c28cf..08b9b9c79d3 100644 --- a/network-api/networkapi/wagtailpages/views.py +++ b/network-api/networkapi/wagtailpages/views.py @@ -29,7 +29,6 @@ def custom404_view(request, exception): def localized_redirect(request, subpath, destination_path): lang = request.LANGUAGE_CODE translation.activate(lang) - request.session[translation.LANGUAGE_SESSION_KEY] = lang query_string = "" if request.META["QUERY_STRING"]: