Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
d6a49491
Commit
d6a49491
authored
9 years ago
by
John Baker
Browse files
Options
Downloads
Plain Diff
Merge pull request #9079 from edx/release
Release
parents
ce0cc0a3
13e80bef
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/djangoapps/embargo/api.py
+5
-0
5 additions, 0 deletions
common/djangoapps/embargo/api.py
common/djangoapps/embargo/tests/test_api.py
+45
-1
45 additions, 1 deletion
common/djangoapps/embargo/tests/test_api.py
with
50 additions
and
1 deletion
common/djangoapps/embargo/api.py
+
5
−
0
View file @
d6a49491
...
...
@@ -14,6 +14,7 @@ from rest_framework.response import Response
from
rest_framework
import
status
from
ipware.ip
import
get_ip
from
student.auth
import
has_course_author_access
from
embargo.models
import
CountryAccessRule
,
RestrictedCourse
...
...
@@ -70,6 +71,10 @@ def check_course_access(course_key, user=None, ip_address=None, url=None):
if
not
course_is_restricted
:
return
True
# Always give global and course staff access, regardless of embargo settings.
if
user
is
not
None
and
has_course_author_access
(
user
,
course_key
):
return
True
if
ip_address
is
not
None
:
# Retrieve the country code from the IP address
# and check it against the allowed countries list for a course
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/embargo/tests/test_api.py
+
45
−
1
View file @
d6a49491
...
...
@@ -18,6 +18,11 @@ from xmodule.modulestore.tests.factories import CourseFactory
from
xmodule.modulestore.tests.django_utils
import
(
ModuleStoreTestCase
,
mixed_store_config
)
from
student.roles
import
(
GlobalStaff
,
CourseRole
,
OrgRole
,
CourseStaffRole
,
CourseInstructorRole
,
OrgStaffRole
,
OrgInstructorRole
)
from
embargo.models
import
(
RestrictedCourse
,
Country
,
CountryAccessRule
,
...
...
@@ -166,7 +171,7 @@ class EmbargoCheckAccessApiTests(ModuleStoreTestCase):
# (restricted course, but pass all the checks)
# This is the worst case, so it will hit all of the
# caching code.
with
self
.
assertNumQueries
(
3
):
with
self
.
assertNumQueries
(
4
):
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'
0.0.0.0
'
)
with
self
.
assertNumQueries
(
0
):
...
...
@@ -182,6 +187,45 @@ class EmbargoCheckAccessApiTests(ModuleStoreTestCase):
with
self
.
assertNumQueries
(
0
):
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'
0.0.0.0
'
)
@ddt.data
(
GlobalStaff
,
CourseStaffRole
,
CourseInstructorRole
,
OrgStaffRole
,
OrgInstructorRole
,
)
def
test_staff_access_country_block
(
self
,
staff_role_cls
):
# Add a country to the blacklist
CountryAccessRule
.
objects
.
create
(
rule_type
=
CountryAccessRule
.
BLACKLIST_RULE
,
restricted_course
=
self
.
restricted_course
,
country
=
Country
.
objects
.
get
(
country
=
'
US
'
)
)
# Appear to make a request from an IP in the blocked country
with
self
.
_mock_geoip
(
'
US
'
):
result
=
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'
0.0.0.0
'
)
# Expect that the user is blocked, because the user isn't staff
self
.
assertFalse
(
result
,
msg
=
"
User should not have access because the user isn
'
t staff.
"
)
# Instantiate the role, configuring it for this course or org
if
issubclass
(
staff_role_cls
,
CourseRole
):
staff_role
=
staff_role_cls
(
self
.
course
.
id
)
elif
issubclass
(
staff_role_cls
,
OrgRole
):
staff_role
=
staff_role_cls
(
self
.
course
.
id
.
org
)
else
:
staff_role
=
staff_role_cls
()
# Add the user to the role
staff_role
.
add_users
(
self
.
user
)
# Now the user should have access
with
self
.
_mock_geoip
(
'
US
'
):
result
=
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'
0.0.0.0
'
)
self
.
assertTrue
(
result
,
msg
=
"
User should have access because the user is staff.
"
)
@contextmanager
def
_mock_geoip
(
self
,
country_code
):
with
mock
.
patch
.
object
(
pygeoip
.
GeoIP
,
'
country_code_by_addr
'
)
as
mock_ip
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment