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
090d0d44
Commit
090d0d44
authored
Jun 26, 2013
by
David Baumgold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix up unit tests
parent
a608d8a3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
156 additions
and
10 deletions
+156
-10
cms/djangoapps/contentstore/tests/test_assets.py
+83
-0
cms/djangoapps/contentstore/tests/test_textbooks.py
+0
-0
cms/djangoapps/contentstore/views/assets.py
+4
-5
cms/djangoapps/contentstore/views/course.py
+6
-5
common/djangoapps/util/tests/test_json_request.py
+42
-0
common/lib/xmodule/xmodule/modulestore/tests/django_utils.py
+21
-0
No files found.
cms/djangoapps/contentstore/tests/test_assets.py
0 → 100644
View file @
090d0d44
import
json
from
datetime
import
datetime
from
io
import
BytesIO
from
pytz
import
UTC
from
unittest
import
TestCase
from
.utils
import
CourseTestCase
from
django.core.urlresolvers
import
reverse
from
contentstore.views
import
assets
class
AssetsTestCase
(
CourseTestCase
):
def
setUp
(
self
):
super
(
AssetsTestCase
,
self
)
.
setUp
()
self
.
url
=
reverse
(
"asset_index"
,
kwargs
=
{
'org'
:
self
.
course
.
location
.
org
,
'course'
:
self
.
course
.
location
.
course
,
'name'
:
self
.
course
.
location
.
name
,
})
def
test_basic
(
self
):
resp
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
def
test_json
(
self
):
resp
=
self
.
client
.
get
(
self
.
url
,
HTTP_ACCEPT
=
"application/json"
,
HTTP_X_REQUESTED_WITH
=
"XMLHttpRequest"
,
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
content
=
json
.
loads
(
resp
.
content
)
self
.
assertIsInstance
(
content
,
list
)
class
UploadTestCase
(
CourseTestCase
):
def
setUp
(
self
):
super
(
UploadTestCase
,
self
)
.
setUp
()
self
.
url
=
reverse
(
"upload_asset"
,
kwargs
=
{
'org'
:
self
.
course
.
location
.
org
,
'course'
:
self
.
course
.
location
.
course
,
'coursename'
:
self
.
course
.
location
.
name
,
})
def
test_happy_path
(
self
):
f
=
BytesIO
(
"sample content"
)
f
.
name
=
"sample.txt"
resp
=
self
.
client
.
post
(
self
.
url
,
{
"name"
:
"my-name"
,
"file"
:
f
})
self
.
assert2XX
(
resp
.
status_code
)
def
test_no_file
(
self
):
resp
=
self
.
client
.
post
(
self
.
url
,
{
"name"
:
"file.txt"
})
self
.
assert4XX
(
resp
.
status_code
)
def
test_get
(
self
):
resp
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEquals
(
resp
.
status_code
,
405
)
class
AssetsToJsonTestCase
(
TestCase
):
def
test_basic
(
self
):
upload_date
=
datetime
(
2013
,
6
,
1
,
10
,
30
,
tzinfo
=
UTC
)
asset
=
{
"displayname"
:
"foo"
,
"chunkSize"
:
512
,
"filename"
:
"foo.png"
,
"length"
:
100
,
"uploadDate"
:
upload_date
,
"_id"
:
{
"course"
:
"course"
,
"org"
:
"org"
,
"revision"
:
12
,
"category"
:
"category"
,
"name"
:
"name"
,
"tag"
:
"tag"
,
}
}
output
=
assets
.
assets_to_json_dict
([
asset
])
self
.
assertEquals
(
len
(
output
),
1
)
compare
=
output
[
0
]
self
.
assertEquals
(
compare
[
"name"
],
"foo"
)
self
.
assertEquals
(
compare
[
"path"
],
"foo.png"
)
self
.
assertEquals
(
compare
[
"uploaded"
],
upload_date
.
isoformat
())
self
.
assertEquals
(
compare
[
"id"
],
"/tag/org/course/12/category/name"
)
cms/djangoapps/contentstore/tests/test_textbooks.py
View file @
090d0d44
This diff is collapsed.
Click to expand it.
cms/djangoapps/contentstore/views/assets.py
View file @
090d0d44
...
...
@@ -13,6 +13,7 @@ from django_future.csrf import ensure_csrf_cookie
from
django.core.urlresolvers
import
reverse
from
django.core.servers.basehttp
import
FileWrapper
from
django.core.files.temp
import
NamedTemporaryFile
from
django.views.decorators.http
import
require_POST
from
mitxmako.shortcuts
import
render_to_response
from
cache_toolbox.core
import
del_cached_content
...
...
@@ -30,6 +31,7 @@ from xmodule.exceptions import NotFoundError
from
..utils
import
get_url_reverse
from
.access
import
get_location_and_verify_access
from
util.json_request
import
JsonResponse
__all__
=
[
'asset_index'
,
'upload_asset'
,
'import_course'
,
'generate_export_course'
,
'export_course'
]
...
...
@@ -89,7 +91,7 @@ def asset_index(request, org, course, name):
assets
=
sorted
(
assets
,
key
=
lambda
asset
:
asset
[
'uploadDate'
],
reverse
=
True
)
if
request
.
META
.
get
(
'HTTP_ACCEPT'
,
""
)
.
startswith
(
"application/json"
):
return
HttpResponse
(
json
.
dumps
(
assets_to_json_dict
(
assets
)),
content_type
=
"application/json"
)
return
JsonResponse
(
assets_to_json_dict
(
assets
)
)
asset_display
=
[]
for
asset
in
assets
:
...
...
@@ -120,6 +122,7 @@ def asset_index(request, org, course, name):
})
@require_POST
@ensure_csrf_cookie
@login_required
def
upload_asset
(
request
,
org
,
course
,
coursename
):
...
...
@@ -127,10 +130,6 @@ def upload_asset(request, org, course, coursename):
This method allows for POST uploading of files into the course asset library, which will
be supported by GridFS in MongoDB.
'''
if
request
.
method
!=
'POST'
:
# (cdodge) @todo: Is there a way to do a - say - 'raise Http400'?
return
HttpResponseBadRequest
()
# construct a location from the passed in path
location
=
get_location_and_verify_access
(
request
,
org
,
course
,
coursename
)
...
...
cms/djangoapps/contentstore/views/course.py
View file @
090d0d44
...
...
@@ -8,7 +8,7 @@ import string
from
django.contrib.auth.decorators
import
login_required
from
django_future.csrf
import
ensure_csrf_cookie
from
django.conf
import
settings
from
django.views.decorators.http
import
require_http_methods
from
django.views.decorators.http
import
require_http_methods
,
require_POST
from
django.core.exceptions
import
PermissionDenied
from
django.core.urlresolvers
import
reverse
from
django.http
import
HttpResponse
,
HttpResponseBadRequest
...
...
@@ -521,9 +521,9 @@ def textbook_index(request, org, course, name):
})
@require_POST
@login_required
@ensure_csrf_cookie
@require_http_methods
((
"POST"
,))
def
create_textbook
(
request
,
org
,
course
,
name
):
location
=
get_location_and_verify_access
(
request
,
org
,
course
,
name
)
store
=
get_modulestore
(
location
)
...
...
@@ -531,7 +531,7 @@ def create_textbook(request, org, course, name):
try
:
textbook
=
validate_textbook_json
(
request
.
body
)
except
TextbookValidationError
:
except
TextbookValidationError
as
e
:
return
JsonResponse
({
"error"
:
e
.
message
},
status
=
400
)
if
not
textbook
.
get
(
"id"
):
tids
=
set
(
t
[
"id"
]
for
t
in
course_module
.
pdf_textbooks
if
"id"
in
t
)
...
...
@@ -555,7 +555,8 @@ def textbook_by_id(request, org, course, name, tid):
location
=
get_location_and_verify_access
(
request
,
org
,
course
,
name
)
store
=
get_modulestore
(
location
)
course_module
=
store
.
get_item
(
location
,
depth
=
3
)
matching_id
=
[
tb
for
tb
in
course_module
.
pdf_textbooks
if
tb
.
get
(
"id"
)
==
tid
]
matching_id
=
[
tb
for
tb
in
course_module
.
pdf_textbooks
if
str
(
tb
.
get
(
"id"
))
==
str
(
tid
)]
if
matching_id
:
textbook
=
matching_id
[
0
]
else
:
...
...
@@ -589,4 +590,4 @@ def textbook_by_id(request, org, course, name, tid):
new_textbooks
.
extend
(
course_module
.
pdf_textbooks
[
i
+
1
:])
course_module
.
pdf_textbooks
=
new_textbooks
store
.
update_metadata
(
course_module
.
location
,
own_metadata
(
course_module
))
return
JsonResponse
(
new_textbook
)
return
JsonResponse
()
common/djangoapps/util/tests/test_json_request.py
0 → 100644
View file @
090d0d44
from
django.http
import
HttpResponse
from
util.json_request
import
JsonResponse
import
json
import
unittest
class
JsonResponseTestCase
(
unittest
.
TestCase
):
def
test_empty
(
self
):
resp
=
JsonResponse
()
self
.
assertIsInstance
(
resp
,
HttpResponse
)
self
.
assertEqual
(
resp
.
content
,
""
)
self
.
assertEqual
(
resp
.
status_code
,
204
)
self
.
assertEqual
(
resp
[
"content-type"
],
"application/json"
)
def
test_empty_string
(
self
):
resp
=
JsonResponse
(
""
)
self
.
assertIsInstance
(
resp
,
HttpResponse
)
self
.
assertEqual
(
resp
.
content
,
""
)
self
.
assertEqual
(
resp
.
status_code
,
204
)
self
.
assertEqual
(
resp
[
"content-type"
],
"application/json"
)
def
test_string
(
self
):
resp
=
JsonResponse
(
"foo"
)
self
.
assertEqual
(
resp
.
content
,
'"foo"'
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
self
.
assertEqual
(
resp
[
"content-type"
],
"application/json"
)
def
test_dict
(
self
):
obj
=
{
"foo"
:
"bar"
}
resp
=
JsonResponse
(
obj
)
compare
=
json
.
loads
(
resp
.
content
)
self
.
assertEqual
(
obj
,
compare
)
self
.
assertEqual
(
resp
.
status_code
,
200
)
self
.
assertEqual
(
resp
[
"content-type"
],
"application/json"
)
def
test_set_status
(
self
):
obj
=
{
"error"
:
"resource not found"
}
resp
=
JsonResponse
(
obj
,
status
=
404
)
compare
=
json
.
loads
(
resp
.
content
)
self
.
assertEqual
(
obj
,
compare
)
self
.
assertEqual
(
resp
.
status_code
,
404
)
self
.
assertEqual
(
resp
[
"content-type"
],
"application/json"
)
common/lib/xmodule/xmodule/modulestore/tests/django_utils.py
View file @
090d0d44
...
...
@@ -6,6 +6,7 @@ from django.test import TestCase
from
django.conf
import
settings
import
xmodule.modulestore.django
from
xmodule.templates
import
update_templates
from
unittest.util
import
safe_repr
def
mongo_store_config
(
data_dir
):
...
...
@@ -183,3 +184,23 @@ class ModuleStoreTestCase(TestCase):
# Call superclass implementation
super
(
ModuleStoreTestCase
,
self
)
.
_post_teardown
()
def
assert2XX
(
self
,
status_code
,
msg
=
None
):
if
not
200
<=
status_code
<
300
:
msg
=
self
.
_formatMessage
(
msg
,
"
%
s is not a success status"
%
safe_repr
(
status_code
))
raise
self
.
failureExecption
(
msg
)
def
assert3XX
(
self
,
status_code
,
msg
=
None
):
if
not
300
<=
status_code
<
400
:
msg
=
self
.
_formatMessage
(
msg
,
"
%
s is not a redirection status"
%
safe_repr
(
status_code
))
raise
self
.
failureExecption
(
msg
)
def
assert4XX
(
self
,
status_code
,
msg
=
None
):
if
not
400
<=
status_code
<
500
:
msg
=
self
.
_formatMessage
(
msg
,
"
%
s is not a client error status"
%
safe_repr
(
status_code
))
raise
self
.
failureExecption
(
msg
)
def
assert5XX
(
self
,
status_code
,
msg
=
None
):
if
not
500
<=
status_code
<
600
:
msg
=
self
.
_formatMessage
(
msg
,
"
%
s is not a server error status"
%
safe_repr
(
status_code
))
raise
self
.
failureExecption
(
msg
)
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