Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Websites UFRPE
Django Private Storage
Commits
14de8b7c
Commit
14de8b7c
authored
Feb 06, 2018
by
Diederik van der Boor
Browse files
Added tests for the views
parent
9b1a73f2
Changes
5
Hide whitespace changes
Inline
Side-by-side
private_storage/tests/test_models.py
View file @
14de8b7c
import
os
import
shutil
from
django.conf
import
settings
from
django.core.files.uploadedfile
import
SimpleUploadedFile
from
django.test
import
TestCase
from
private_storage.tests.models
import
CustomerDossier
,
SimpleDossier
,
UploadToDossier
class
PrivateFileTestCase
(
TestCase
):
def
tearDown
(
self
):
"""
Empty the test folder after each test case.
"""
super
(
PrivateFileTestCase
,
self
).
tearDown
()
shutil
.
rmtree
(
settings
.
PRIVATE_STORAGE_ROOT
)
def
assertExists
(
self
,
*
parts
):
"""
Extra assert, check whether a path exists.
"""
path
=
os
.
path
.
join
(
settings
.
PRIVATE_STORAGE_ROOT
,
*
parts
)
if
not
os
.
path
.
exists
(
path
):
raise
self
.
failureException
(
"Path {} does not exist"
.
format
(
path
))
from
private_storage.tests.utils
import
PrivateFileTestCase
class
ModelTests
(
PrivateFileTestCase
):
...
...
private_storage/tests/test_views.py
0 → 100644
View file @
14de8b7c
from
django.contrib.auth.models
import
User
from
django.core.files.uploadedfile
import
SimpleUploadedFile
from
django.test
import
RequestFactory
from
private_storage.tests.models
import
CustomerDossier
from
private_storage.tests.utils
import
PrivateFileTestCase
from
private_storage.views
import
PrivateStorageDetailView
,
PrivateStorageView
class
ViewTests
(
PrivateFileTestCase
):
def
test_detail_view
(
self
):
"""
Test the detail view that returns the object
"""
CustomerDossier
.
objects
.
create
(
customer
=
'cust1'
,
file
=
SimpleUploadedFile
(
'test4.txt'
,
b
'test4'
)
)
request
=
RequestFactory
().
get
(
'/cust1/file/'
)
request
.
user
=
User
.
objects
.
create_superuser
(
'admin'
,
'admin@example.com'
,
'admin'
)
# Initialize locally, no need for urls.py etc..
# This behaves like a standard DetailView
view
=
PrivateStorageDetailView
.
as_view
(
model
=
CustomerDossier
,
slug_url_kwarg
=
'customer'
,
slug_field
=
'customer'
,
model_file_field
=
'file'
)
response
=
view
(
request
,
customer
=
'cust1'
,
)
self
.
assertEqual
(
list
(
response
.
streaming_content
),
[
b
'test4'
])
self
.
assertEqual
(
response
[
'Content-Type'
],
'text/plain'
)
self
.
assertEqual
(
response
[
'Content-Length'
],
'5'
)
self
.
assertIn
(
'Last-Modified'
,
response
)
def
test_private_file_view
(
self
):
"""
Test the detail view that returns the object
"""
obj
=
CustomerDossier
.
objects
.
create
(
customer
=
'cust2'
,
file
=
SimpleUploadedFile
(
'test5.txt'
,
b
'test5'
)
)
self
.
assertExists
(
'dossier3'
,
'cust2'
,
'test5.txt'
)
request
=
RequestFactory
().
get
(
'/cust1/file/'
)
request
.
user
=
User
.
objects
.
create_superuser
(
'admin'
,
'admin@example.com'
,
'admin'
)
request
.
META
[
'HTTP_USER_AGENT'
]
=
'Test'
# Initialize locally, no need for urls.py etc..
# This behaves like a standard DetailView
view
=
PrivateStorageView
.
as_view
(
content_disposition
=
'attachment'
,
)
response
=
view
(
request
,
path
=
'dossier3/cust2/test5.txt'
)
self
.
assertEqual
(
list
(
response
.
streaming_content
),
[
b
'test5'
])
self
.
assertEqual
(
response
[
'Content-Type'
],
'text/plain'
)
self
.
assertEqual
(
response
[
'Content-Length'
],
'5'
)
self
.
assertEqual
(
response
[
'Content-Disposition'
],
"attachment; filename*=UTF-8''test5.txt"
)
self
.
assertIn
(
'Last-Modified'
,
response
)
private_storage/tests/utils.py
0 → 100644
View file @
14de8b7c
import
os
import
shutil
from
django.conf
import
settings
from
django.test
import
TestCase
class
PrivateFileTestCase
(
TestCase
):
def
tearDown
(
self
):
"""
Empty the test folder after each test case.
"""
super
(
PrivateFileTestCase
,
self
).
tearDown
()
shutil
.
rmtree
(
settings
.
PRIVATE_STORAGE_ROOT
)
def
assertExists
(
self
,
*
parts
):
"""
Extra assert, check whether a path exists.
"""
path
=
os
.
path
.
join
(
settings
.
PRIVATE_STORAGE_ROOT
,
*
parts
)
if
not
os
.
path
.
exists
(
path
):
raise
self
.
failureException
(
"Path {} does not exist"
.
format
(
path
))
private_storage/views.py
View file @
14de8b7c
...
...
@@ -19,8 +19,6 @@ except ImportError:
from
urllib
import
quote
# Python 2
class
PrivateStorageView
(
View
):
"""
Return the uploaded files
...
...
@@ -102,11 +100,12 @@ class PrivateStorageView(View):
The filename, encoded to use in a ``Content-Disposition`` header.
"""
# Based on https://www.djangosnippets.org/snippets/1710/
if
'WebKit'
in
self
.
request
.
META
[
'HTTP_USER_AGENT'
]:
user_agent
=
self
.
request
.
META
.
get
(
'HTTP_USER_AGENT'
,
None
)
if
'WebKit'
in
user_agent
:
# Support available for UTF-8 encoded strings.
utf8_filename
=
filename
.
encode
(
"utf-8"
)
return
'filename={}'
.
format
(
utf8_filename
)
elif
'MSIE'
in
se
lf
.
request
.
META
[
'HTTP_USER_AGENT'
]
:
elif
'MSIE'
in
u
se
r_agent
:
# IE does not support internationalized filename at all.
# It can only recognize internationalized URL, so we should perform a trick via URL names.
return
''
...
...
runtests.py
View file @
14de8b7c
...
...
@@ -16,6 +16,8 @@ if not settings.configured:
}
},
INSTALLED_APPS
=
(
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'private_storage'
,
),
TEST_RUNNER
=
'django.test.runner.DiscoverRunner'
,
...
...
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