Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
125552f2
Commit
125552f2
authored
Jul 18, 2017
by
Awais Jibran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Switching course view to specific student retains the profile language setting
https://openedx.atlassian.net/browse/EDUCATOR-891
parent
2d6b477e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
8 deletions
+57
-8
lms/djangoapps/courseware/tests/test_masquerade.py
+47
-2
openedx/core/djangoapps/lang_pref/middleware.py
+10
-6
No files found.
lms/djangoapps/courseware/tests/test_masquerade.py
View file @
125552f2
...
...
@@ -5,6 +5,7 @@ import json
import
pickle
from
datetime
import
datetime
from
django.conf
import
settings
from
django.core.urlresolvers
import
reverse
from
django.test
import
TestCase
from
django.utils.timezone
import
UTC
...
...
@@ -17,13 +18,16 @@ from courseware.masquerade import (
CourseMasquerade
,
MasqueradingKeyValueStore
,
get_masquerading_user_group
,
handle_ajax
,
setup_masquerade
)
from
courseware.tests.factories
import
StaffFactory
from
courseware.tests.helpers
import
LoginEnrollmentTestCase
,
masquerade_as_group_member
from
courseware.tests.test_submitting_problems
import
ProblemSubmissionTestMixin
from
openedx.core.djangoapps.lang_pref
import
LANGUAGE_KEY
from
openedx.core.djangoapps.self_paced.models
import
SelfPacedConfiguration
from
openedx.core.djangoapps.user_api.preferences.api
import
(
get_user_preference
,
set_user_preference
)
from
student.tests.factories
import
UserFactory
from
xmodule.modulestore.django
import
modulestore
from
xmodule.modulestore.tests.django_utils
import
SharedModuleStoreTestCase
...
...
@@ -311,6 +315,22 @@ class TestStaffMasqueradeAsSpecificStudent(StaffMasqueradeTestCase, ProblemSubmi
progress
=
'
%
s/
%
s'
%
(
str
(
json_data
[
'current_score'
]),
str
(
json_data
[
'total_possible'
]))
return
progress
def
assertExpectedLanguageInPreference
(
self
,
user
,
expected_language_code
):
"""
This method is a custom assertion verifies that a given user has expected
language code in the preference and in cookies.
Arguments:
user: User model instance
expected_language_code: string indicating a language code
"""
self
.
assertEqual
(
get_user_preference
(
user
,
LANGUAGE_KEY
),
expected_language_code
)
self
.
assertEqual
(
self
.
client
.
cookies
[
settings
.
LANGUAGE_COOKIE
]
.
value
,
expected_language_code
)
@patch.dict
(
'django.conf.settings.FEATURES'
,
{
'DISABLE_START_DATES'
:
False
})
def
test_masquerade_as_specific_user_on_self_paced
(
self
):
"""
...
...
@@ -375,6 +395,31 @@ class TestStaffMasqueradeAsSpecificStudent(StaffMasqueradeTestCase, ProblemSubmi
self
.
login_student
()
self
.
assertEqual
(
self
.
get_progress_detail
(),
u'2/2'
)
def
test_masquerading_with_language_preference
(
self
):
"""
Tests that masquerading as a specific user for the course does not update preference language
for the staff.
Login as a staff user and set user's language preference to english and visit the courseware page.
Set masquerade to view same page as a specific student having different language preference and
revisit the courseware page.
"""
english_language_code
=
'en'
set_user_preference
(
self
.
test_user
,
preference_key
=
LANGUAGE_KEY
,
preference_value
=
english_language_code
)
self
.
login_staff
()
# Reload the page and check we have expected language preference in system and in cookies.
self
.
get_courseware_page
()
self
.
assertExpectedLanguageInPreference
(
self
.
test_user
,
english_language_code
)
# Set student language preference and set masquerade to view same page the student.
set_user_preference
(
self
.
student_user
,
preference_key
=
LANGUAGE_KEY
,
preference_value
=
'es-419'
)
self
.
update_masquerade
(
role
=
'student'
,
user_name
=
self
.
student_user
.
username
)
# Reload the page and check we have expected language preference in system and in cookies.
self
.
get_courseware_page
()
self
.
assertExpectedLanguageInPreference
(
self
.
test_user
,
english_language_code
)
@patch.dict
(
'django.conf.settings.FEATURES'
,
{
'DISABLE_START_DATES'
:
False
})
def
test_masquerade_as_specific_student_course_info
(
self
):
"""
...
...
openedx/core/djangoapps/lang_pref/middleware.py
View file @
125552f2
...
...
@@ -5,11 +5,11 @@ Middleware for Language Preferences
from
django.conf
import
settings
from
django.utils.translation
import
LANGUAGE_SESSION_KEY
from
django.utils.translation.trans_real
import
parse_accept_lang_header
from
lms.djangoapps.courseware.masquerade
import
MASQUERADE_SETTINGS_KEY
from
openedx.core.djangoapps.lang_pref
import
COOKIE_DURATION
,
LANGUAGE_HEADER
,
LANGUAGE_KEY
from
openedx.core.djangoapps.user_api.errors
import
UserAPIInternalError
,
UserAPIRequestError
from
openedx.core.djangoapps.user_api.preferences.api
import
(
delete_user_preference
,
get_user_preference
,
set_user_preference
)
...
...
@@ -51,18 +51,22 @@ class LanguagePreferenceMiddleware(object):
del
request
.
session
[
LANGUAGE_SESSION_KEY
]
def
process_response
(
self
,
request
,
response
):
# If the user is logged in, check for their language preference
if
getattr
(
request
,
'user'
,
None
)
and
request
.
user
.
is_authenticated
():
user_pref
=
None
# If the user is logged in, check for their language preference. Also check for real user
# if current user is a masquerading user,
user_pref
=
None
current_user
=
None
if
hasattr
(
request
,
'user'
):
current_user
=
getattr
(
request
.
user
,
'real_user'
,
request
.
user
)
if
current_user
and
current_user
.
is_authenticated
():
anonymous_cookie_lang
=
getattr
(
request
,
'_anonymous_user_cookie_lang'
,
None
)
if
anonymous_cookie_lang
:
user_pref
=
anonymous_cookie_lang
set_user_preference
(
request
.
user
,
LANGUAGE_KEY
,
anonymous_cookie_lang
)
set_user_preference
(
current_
user
,
LANGUAGE_KEY
,
anonymous_cookie_lang
)
else
:
# Get the user's language preference
try
:
user_pref
=
get_user_preference
(
request
.
user
,
LANGUAGE_KEY
)
user_pref
=
get_user_preference
(
current_
user
,
LANGUAGE_KEY
)
except
(
UserAPIRequestError
,
UserAPIInternalError
):
# If we can't find the user preferences, then don't modify the cookie
pass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment