Remove lang cookie for mobile app requests. (#22731)

* Remove lang cookie for mobile app requests.

Responses for mobile app requests must not include language preference cookie.

PROD-1107
This commit is contained in:
Waheed Ahmed
2020-01-07 15:49:36 +05:00
committed by GitHub
parent 8f5a166ab4
commit 49abac5812
2 changed files with 19 additions and 20 deletions

View File

@@ -70,14 +70,13 @@ class LanguagePreferenceMiddleware(object):
pass
# If set, set the user_pref in the LANGUAGE_COOKIE
if user_pref:
if not is_request_from_mobile_app(request):
response.set_cookie(
settings.LANGUAGE_COOKIE,
value=user_pref,
domain=settings.SESSION_COOKIE_DOMAIN,
max_age=COOKIE_DURATION,
)
if user_pref and not is_request_from_mobile_app(request):
response.set_cookie(
settings.LANGUAGE_COOKIE,
value=user_pref,
domain=settings.SESSION_COOKIE_DOMAIN,
max_age=COOKIE_DURATION,
)
else:
response.delete_cookie(
settings.LANGUAGE_COOKIE,

View File

@@ -257,18 +257,18 @@ class TestUserPreferenceMiddleware(CacheIsolationTestCase):
@mock.patch('openedx.core.djangoapps.lang_pref.middleware.is_request_from_mobile_app')
@mock.patch('openedx.core.djangoapps.lang_pref.middleware.get_user_preference')
def test_lang_pref_cookie_must_not_sent_to_mobile(self, mock_get_user_preference, mock_is_mobile_request):
def test_remove_lang_cookie_for_mobile_app(self, mock_get_user_preference, mock_is_mobile_request):
"""
Test to verify language preference cookie must not be set for mobile requests
Test to verify language preference cookie removed for mobile app requests.
"""
mock_get_user_preference.return_value = 'test_value'
mock_is_mobile_request.return_value = True
response = self.client.get('/')
self.middleware.process_response(self.request, response)
self.assertFalse(response.cookies.get('openedx-language-preference'))
mock_get_user_preference.return_value = 'en'
mock_is_mobile_request.return_value = False
response = self.client.get('/')
self.middleware.process_response(self.request, response)
self.assertTrue(response.cookies.get('openedx-language-preference'))
response = mock.Mock(spec=HttpResponse)
response = self.middleware.process_response(self.request, response)
response.delete_cookie.assert_not_called()
response.set_cookie.assert_called()
mock_is_mobile_request.return_value = True
response = self.middleware.process_response(self.request, response)
response.delete_cookie.assert_called()