Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
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
c381ccf7
Commit
c381ccf7
authored
10 years ago
by
Christina Roberts
Browse files
Options
Downloads
Plain Diff
Merge pull request #6215 from edx/christina/test-file-cleanup
Add DEFAULT_FILE_STORAGE configuration.
parents
0836a896
07b036f3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
cms/envs/test.py
+2
-1
2 additions, 1 deletion
cms/envs/test.py
common/djangoapps/util/file.py
+2
-1
2 additions, 1 deletion
common/djangoapps/util/file.py
common/djangoapps/util/tests/test_file.py
+30
-10
30 additions, 10 deletions
common/djangoapps/util/tests/test_file.py
with
34 additions
and
12 deletions
cms/envs/test.py
+
2
−
1
View file @
c381ccf7
...
...
@@ -19,7 +19,8 @@ from warnings import filterwarnings, simplefilter
from
uuid
import
uuid4
# import settings from LMS for consistent behavior with CMS
from
lms.envs.test
import
(
WIKI_ENABLED
,
PLATFORM_NAME
,
SITE_NAME
)
# pylint: disable=unused-import
from
lms.envs.test
import
(
WIKI_ENABLED
,
PLATFORM_NAME
,
SITE_NAME
,
DEFAULT_FILE_STORAGE
,
MEDIA_ROOT
,
MEDIA_URL
)
# mongo connection settings
MONGO_PORT_NUM
=
int
(
os
.
environ
.
get
(
'
EDXAPP_TEST_MONGO_PORT
'
,
'
27017
'
))
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/util/file.py
+
2
−
1
View file @
c381ccf7
...
...
@@ -70,7 +70,8 @@ def store_uploaded_file(
stored_file_name
=
base_storage_filename
+
file_extension
file_storage
=
DefaultStorage
()
file_storage
.
save
(
stored_file_name
,
uploaded_file
)
# If a file already exists with the supplied name, file_storage will make the filename unique.
stored_file_name
=
file_storage
.
save
(
stored_file_name
,
uploaded_file
)
if
validator
:
try
:
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/util/tests/test_file.py
+
30
−
10
View file @
c381ccf7
...
...
@@ -151,7 +151,7 @@ class StoreUploadedFileTestCase(TestCase):
def
success_validator
(
storage
,
filename
):
"""
Validation test function that is a no-op
"""
self
.
assert
Equal
(
"
success_file
.csv
"
,
os
.
path
.
basename
(
filename
))
self
.
assert
True
(
"
success_file
"
in
os
.
path
.
basename
(
filename
))
store_file_data
(
storage
,
filename
)
with
self
.
assertRaises
(
FileValidationException
)
as
error
:
...
...
@@ -173,26 +173,46 @@ class StoreUploadedFileTestCase(TestCase):
"""
Tests uploading a file with lower case extension. Verifies that the stored file contents are correct.
"""
self
.
file_storage
,
self
.
stored_file_name
=
store_uploaded_file
(
file_storage
,
stored_file_name
=
store_uploaded_file
(
self
.
request
,
"
uploaded_file
"
,
[
"
.csv
"
],
"
stored_file
"
,
self
.
default_max_size
)
self
.
_verify_successful_upload
()
self
.
_verify_successful_upload
(
file_storage
,
stored_file_name
,
self
.
file_content
)
def
test_file_upload_upper_case_extension
(
self
):
"""
Tests uploading a file with upper case extension. Verifies that the stored file contents are correct.
"""
self
.
request
.
FILES
=
{
"
uploaded_file
"
:
SimpleUploadedFile
(
"
tempfile.CSV
"
,
self
.
file_content
)}
self
.
file_storage
,
self
.
stored_file_name
=
store_uploaded_file
(
file_content
=
"
uppercase
"
self
.
request
.
FILES
=
{
"
uploaded_file
"
:
SimpleUploadedFile
(
"
tempfile.CSV
"
,
file_content
)}
file_storage
,
stored_file_name
=
store_uploaded_file
(
self
.
request
,
"
uploaded_file
"
,
[
"
.gif
"
,
"
.csv
"
],
"
second_stored_file
"
,
self
.
default_max_size
)
self
.
_verify_successful_upload
()
self
.
_verify_successful_upload
(
file_storage
,
stored_file_name
,
file_content
)
def
_verify_successful_upload
(
self
):
def
test_unique_filenames
(
self
):
"""
Test that the file storage method will create a unique filename if the file already exists.
"""
requested_file_name
=
"
nonunique_store
"
file_content
=
"
copy
"
self
.
request
.
FILES
=
{
"
nonunique_file
"
:
SimpleUploadedFile
(
"
nonunique.txt
"
,
file_content
)}
_
,
first_stored_file_name
=
store_uploaded_file
(
self
.
request
,
"
nonunique_file
"
,
[
"
.txt
"
],
requested_file_name
,
self
.
default_max_size
)
file_storage
,
second_stored_file_name
=
store_uploaded_file
(
self
.
request
,
"
nonunique_file
"
,
[
"
.txt
"
],
requested_file_name
,
self
.
default_max_size
)
self
.
assertNotEqual
(
first_stored_file_name
,
second_stored_file_name
)
self
.
assertTrue
(
requested_file_name
in
second_stored_file_name
)
self
.
_verify_successful_upload
(
file_storage
,
second_stored_file_name
,
file_content
)
def
_verify_successful_upload
(
self
,
storage
,
file_name
,
expected_content
):
"""
Helper method that checks that the stored version of the uploaded file has the correct content
"""
self
.
assertTrue
(
self
.
file_
storage
.
exists
(
self
.
stored_
file_name
))
with
self
.
file_
storage
.
open
(
self
.
stored_
file_name
,
'
r
'
)
as
f
:
self
.
assertEqual
(
self
.
file
_content
,
f
.
read
())
self
.
assertTrue
(
storage
.
exists
(
file_name
))
with
storage
.
open
(
file_name
,
'
r
'
)
as
f
:
self
.
assertEqual
(
expected
_content
,
f
.
read
())
@ddt.ddt
...
...
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