Skip to content

Commit

Permalink
Remove LANGUAGE_SESSION_KEY from localized_redirect (#11896)
Browse files Browse the repository at this point in the history
* Remove LANGUAGE_SESSION_KEY from localized_redirect

* Add tests
  • Loading branch information
jhonatan-lopes authored Feb 22, 2024
1 parent 91baf77 commit f0e666a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
44 changes: 44 additions & 0 deletions network-api/networkapi/wagtailpages/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -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/")
1 change: 0 additions & 1 deletion network-api/networkapi/wagtailpages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down

0 comments on commit f0e666a

Please sign in to comment.