Skip to content

Commit

Permalink
fix: update language cookie if langauge cookie is not same as user's …
Browse files Browse the repository at this point in the history
…language preference
  • Loading branch information
muhammad-ammar committed May 8, 2024
1 parent 83f2cff commit 154a4a6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Unreleased
----------
* nothing unreleased

[4.17.4]
--------
* fix: update language cookie if langauge cookie is not same as user's language preference

[4.17.3]
--------
* feat: replacing non encrypted fields of blackboard config model with encypted ones
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Your project description goes here.
"""

__version__ = "4.17.3"
__version__ = "4.17.4"
12 changes: 12 additions & 0 deletions enterprise/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ def process_request(self, request):
# Ignore errors related to user preferences not found.
pass

# This is to handle a bug where a user is logged in multiple tabs/browsers.
# User updates the language preference in one tab. After update, user's language preference
# in the database and language cookie in the browser is set to new language.
# But in the other tab/browser, user still has the old language cookie. So when the user
# refreshes the page, the language cookie is sent to the server and overrides the user's preference.
# NOTE: The assumption here is to consider user's language preference as the single source of truth.
cookie_lang = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
if user_pref and cookie_lang and cookie_lang != user_pref:
# pylint: disable=protected-access
request._anonymous_user_cookie_lang = user_pref
request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = user_pref

# If user's language preference is not set and enterprise customer has a default language configured
# then set the default language as the learner's language
if not user_pref and not is_request_from_mobile_app(request):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ddt
from pytest import mark

from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from django.test.client import Client, RequestFactory

Expand Down Expand Up @@ -156,3 +157,19 @@ def test_cookie_not_set_for_mobile_requests(self):

# Make sure the set cookie is not called for anonymous users
assert getattr(self.request, '_anonymous_user_cookie_lang', None) is None

def test_middleware_when_cookie_lang_is_different_from_user_pref(self):
"""
Validate that when user pref and cookie language are set but have different values
then the middleware updates the cookie with user preference value.
"""
user_pref_lang = 'ar'
cookie_lang = 'en'
self.request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = cookie_lang

with mock.patch('enterprise.middleware.get_user_preference') as mock_get_user_preference:
mock_get_user_preference.return_value = user_pref_lang
self.middleware.process_request(self.request)

assert self.request.COOKIES[settings.LANGUAGE_COOKIE_NAME] == user_pref_lang
assert getattr(self.request, '_anonymous_user_cookie_lang', None) == user_pref_lang

0 comments on commit 154a4a6

Please sign in to comment.