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
1a0b752a
Commit
1a0b752a
authored
Sep 24, 2013
by
Julian Arni
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Review fixes
parent
e5c90d33
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
60 deletions
+83
-60
common/djangoapps/contentserver/middleware.py
+6
-8
common/djangoapps/contentserver/tests/test.py
+62
-48
common/djangoapps/student/models.py
+4
-4
common/djangoapps/student/tests/tests.py
+11
-0
No files found.
common/djangoapps/contentserver/middleware.py
View file @
1a0b752a
from
django.http
import
HttpResponse
,
HttpResponseNotModified
from
django.shortcuts
import
redirect
from
django.http
import
(
HttpResponse
,
HttpResponseNotModified
,
HttpResponseForbidden
)
from
student.models
import
CourseEnrollment
from
xmodule.contentstore.django
import
contentstore
...
...
@@ -46,13 +46,11 @@ class StaticContentServer(object):
# Check that user has access to content
if
getattr
(
content
,
"locked"
,
False
):
if
not
hasattr
(
request
,
"user"
)
or
not
request
.
user
.
is_authenticated
():
return
HttpResponse
(
'Unauthorized'
,
status
=
403
)
return
HttpResponse
Forbidden
(
'Unauthorized'
)
course_partial_id
=
"/"
.
join
([
loc
.
org
,
loc
.
course
])
if
not
CourseEnrollment
.
is_enrolled_by_partial
(
request
.
user
,
course_partial_id
):
return
HttpResponse
(
'Unauthorized'
,
status
=
403
)
# see if the last-modified at hasn't changed, if not return a 302 (Not Modified)
if
not
request
.
user
.
is_staff
and
not
CourseEnrollment
.
is_enrolled_by_partial
(
request
.
user
,
course_partial_id
):
return
HttpResponseForbidden
(
'Unauthorized'
)
# convert over the DB persistent last modified timestamp to a HTTP compatible
# timestamp, so we can simply compare the strings
...
...
common/djangoapps/contentserver/tests/test.py
View file @
1a0b752a
...
...
@@ -9,7 +9,6 @@ from pymongo import MongoClient
from
django.contrib.auth.models
import
User
from
django.conf
import
settings
from
django.core.urlresolvers
import
reverse
from
django.test.client
import
Client
from
django.test.utils
import
override_settings
...
...
@@ -20,7 +19,7 @@ from xmodule.modulestore import Location
from
xmodule.contentstore.content
import
StaticContent
from
xmodule.modulestore.django
import
modulestore
from
xmodule.modulestore.tests.django_utils
import
(
studio_store_config
,
ModuleStoreTestCase
)
ModuleStoreTestCase
)
from
xmodule.modulestore.xml_importer
import
import_from_xml
log
=
logging
.
getLogger
(
__name__
)
...
...
@@ -45,28 +44,39 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase):
settings
.
MODULESTORE
[
'default'
][
'OPTIONS'
][
'fs_root'
]
=
path
(
'common/test/data'
)
settings
.
MODULESTORE
[
'direct'
][
'OPTIONS'
][
'fs_root'
]
=
path
(
'common/test/data'
)
base
=
"http://127.0.0.1:8000"
self
.
client
=
Client
()
self
.
contentstore
=
contentstore
()
# A locked the asset
loc
=
Location
(
'c4x'
,
'edX'
,
'toy'
,
'asset'
,
'sample_static.txt'
)
self
.
loc
=
loc
rel_url
=
StaticContent
.
get_url_path_from_location
(
loc
)
self
.
url
=
base
+
rel_url
# A locked asset
self
.
loc_locked
=
Location
(
'c4x'
,
'edX'
,
'toy'
,
'asset'
,
'sample_static.txt'
)
self
.
url_locked
=
StaticContent
.
get_url_path_from_location
(
self
.
loc_locked
)
# An unlocked asset
loc2
=
Location
(
'c4x'
,
'edX'
,
'toy'
,
'asset'
,
'another_static.txt'
)
self
.
loc2
=
loc2
rel_url2
=
StaticContent
.
get_url_path_from_location
(
loc2
)
self
.
url2
=
base
+
rel_url2
self
.
loc_unlocked
=
Location
(
'c4x'
,
'edX'
,
'toy'
,
'asset'
,
'another_static.txt'
)
self
.
url_unlocked
=
StaticContent
.
get_url_path_from_location
(
self
.
loc_unlocked
)
import_from_xml
(
modulestore
(
'direct'
),
'common/test/data/'
,
[
'toy'
],
static_content_store
=
self
.
contentstore
,
verbose
=
True
)
self
.
contentstore
.
set_attr
(
self
.
loc
,
'locked'
,
True
)
self
.
contentstore
.
set_attr
(
self
.
loc
_locked
,
'locked'
,
True
)
# Create user
self
.
usr
=
'testuser'
self
.
pwd
=
'foo'
email
=
'test+courses@edx.org'
self
.
user
=
User
.
objects
.
create_user
(
self
.
usr
,
email
,
self
.
pwd
)
self
.
user
.
is_active
=
True
self
.
user
.
save
()
# Create staff user
self
.
staff_usr
=
'stafftestuser'
self
.
staff_pwd
=
'foo'
staff_email
=
'stafftest+courses@edx.org'
self
.
staff_user
=
User
.
objects
.
create_user
(
self
.
staff_usr
,
staff_email
,
self
.
staff_pwd
)
self
.
staff_user
.
is_active
=
True
self
.
staff_user
.
is_staff
=
True
self
.
staff_user
.
save
()
def
tearDown
(
self
):
...
...
@@ -77,46 +87,50 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase):
"""
Test that unlocked assets are being served.
"""
# Logout user
self
.
client
.
logout
()
resp
=
self
.
client
.
get
(
self
.
url_unlocked
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
#pylint: disable=E1103
resp
=
self
.
client
.
get
(
self
.
url2
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
def
test_locked_asset_not_logged_in
(
self
):
"""
Test that locked assets behave appropriately in case the user is not
logged in.
"""
self
.
client
.
logout
()
resp
=
self
.
client
.
get
(
self
.
url_locked
)
self
.
assertEqual
(
resp
.
status_code
,
403
)
#pylint: disable=E1103
def
test_locked_asset_not_registered
(
self
):
"""
Test that locked assets behave appropriately in case user is logged in
in but not registered for the course.
"""
self
.
client
.
login
(
username
=
self
.
usr
,
password
=
self
.
pwd
)
resp
=
self
.
client
.
get
(
self
.
url_locked
)
self
.
assertEqual
(
resp
.
status_code
,
403
)
#pylint: disable=E1103
def
test_locked_asset
(
self
):
def
test_locked_asset
_registered
(
self
):
"""
Test that locked assets behave appropriately in case:
(1) User is not logged in
(2) User is logged in in but not registerd for the course
(3) User is logged in and registered
Test that locked assets behave appropriately in case user is logged in
and registered for the course.
"""
#pylint: disable=E1101
course_id
=
"/"
.
join
([
self
.
loc_locked
.
org
,
self
.
loc_locked
.
course
,
'2012_Fall'
])
CourseEnrollment
.
enroll
(
self
.
user
,
course_id
)
self
.
assertTrue
(
CourseEnrollment
.
is_enrolled
(
self
.
user
,
course_id
))
# Case (1
)
resp
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEqual
(
resp
.
status_code
,
403
)
self
.
client
.
login
(
username
=
self
.
usr
,
password
=
self
.
pwd
)
resp
=
self
.
client
.
get
(
self
.
url
_locked
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
#pylint: disable=E1103
# Case (2)
# Create user and login
uname
=
'testuser'
email
=
'test+courses@edx.org'
password
=
'foo'
user
=
User
.
objects
.
create_user
(
uname
,
email
,
password
)
user
.
is_active
=
True
user
.
save
()
self
.
client
.
login
(
username
=
uname
,
password
=
password
)
log
.
debug
(
"User logged in"
)
resp
=
self
.
client
.
get
(
self
.
url
)
log
.
debug
(
"Received response
%
s"
,
resp
)
self
.
assertEqual
(
resp
.
status_code
,
403
)
# Case (3)
# Enroll student
course_id
=
"/"
.
join
([
self
.
loc
.
org
,
self
.
loc
.
course
,
'2012_Fall'
])
CourseEnrollment
.
enroll
(
user
,
course_id
)
self
.
assertTrue
(
CourseEnrollment
.
is_enrolled
(
user
,
course_id
))
resp
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
def
test_locked_asset_staff
(
self
):
"""
Test that locked assets behave appropriately in case user is staff.
"""
#pylint: disable=E1101
course_id
=
"/"
.
join
([
self
.
loc_locked
.
org
,
self
.
loc_locked
.
course
,
'2012_Fall'
])
self
.
client
.
login
(
username
=
self
.
staff_usr
,
password
=
self
.
staff_pwd
)
resp
=
self
.
client
.
get
(
self
.
url_locked
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
#pylint: disable=E1103
common/djangoapps/student/models.py
View file @
1a0b752a
...
...
@@ -861,10 +861,10 @@ class CourseEnrollment(models.Model):
"""
try
:
return
CourseEnrollment
.
objects
.
filter
(
user
=
user
,
course_id__startswith
=
course_id_partial
,
is_active
=
1
)
.
exists
()
user
=
user
,
course_id__startswith
=
course_id_partial
,
is_active
=
1
)
.
exists
()
except
cls
.
DoesNotExist
:
return
False
...
...
common/djangoapps/student/tests/tests.py
View file @
1a0b752a
...
...
@@ -213,23 +213,34 @@ class EnrollInCourseTest(TestCase):
def
test_enrollment
(
self
):
user
=
User
.
objects
.
create_user
(
"joe"
,
"joe@joe.com"
,
"password"
)
course_id
=
"edX/Test101/2013"
course_id_partial
=
"edX/Test101"
# Test basic enrollment
self
.
assertFalse
(
CourseEnrollment
.
is_enrolled
(
user
,
course_id
))
self
.
assertFalse
(
CourseEnrollment
.
is_enrolled_by_partial
(
user
,
course_id_partial
))
CourseEnrollment
.
enroll
(
user
,
course_id
)
self
.
assertTrue
(
CourseEnrollment
.
is_enrolled
(
user
,
course_id
))
self
.
assertTrue
(
CourseEnrollment
.
is_enrolled_by_partial
(
user
,
course_id_partial
))
# Enrolling them again should be harmless
CourseEnrollment
.
enroll
(
user
,
course_id
)
self
.
assertTrue
(
CourseEnrollment
.
is_enrolled
(
user
,
course_id
))
self
.
assertTrue
(
CourseEnrollment
.
is_enrolled_by_partial
(
user
,
course_id_partial
))
# Now unenroll the user
CourseEnrollment
.
unenroll
(
user
,
course_id
)
self
.
assertFalse
(
CourseEnrollment
.
is_enrolled
(
user
,
course_id
))
self
.
assertFalse
(
CourseEnrollment
.
is_enrolled_by_partial
(
user
,
course_id_partial
))
# Unenrolling them again should also be harmless
CourseEnrollment
.
unenroll
(
user
,
course_id
)
self
.
assertFalse
(
CourseEnrollment
.
is_enrolled
(
user
,
course_id
))
self
.
assertFalse
(
CourseEnrollment
.
is_enrolled_by_partial
(
user
,
course_id_partial
))
# The enrollment record should still exist, just be inactive
enrollment_record
=
CourseEnrollment
.
objects
.
get
(
...
...
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