-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return 404s for any files not found in the zip, and have BadZipfile e…
…rrors report the name, size, mode and download state to help us narrow down problems with opening. Also, tests. (#942)
- Loading branch information
Showing
5 changed files
with
140 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import datetime | ||
|
||
from django.conf import settings | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from django.core.management import call_command | ||
from django.core.urlresolvers import reverse_lazy | ||
from django.test import TestCase | ||
|
||
from contentcuration.models import User | ||
from contentcuration.utils import minio_utils | ||
from contentcuration.utils.policies import get_latest_policies | ||
from rest_framework.authtoken.models import Token | ||
|
@@ -42,6 +45,8 @@ class StudioTestCase(TestCase, BucketTestMixin): | |
def setUpClass(cls): | ||
super(StudioTestCase, cls).setUpClass() | ||
call_command('loadconstants') | ||
cls.url = "http://127.0.0.1:8000" | ||
cls.admin_user = User.objects.create_superuser('big_shot', '[email protected]', 'password') | ||
|
||
def setUp(self): | ||
if not self.persist_bucket: | ||
|
@@ -51,6 +56,22 @@ def tearDown(self): | |
if not self.persist_bucket: | ||
self.delete_bucket() | ||
|
||
def admin_client(self): | ||
client = APIClient() | ||
client.force_authenticate(self.admin_user) | ||
return client | ||
|
||
def upload_temp_file(self, data, ext='.txt'): | ||
""" | ||
Uploads a file to the server using an authorized client. | ||
""" | ||
fileobj_temp = testdata.create_temp_file(data, ext=ext) | ||
name = fileobj_temp['name'] | ||
|
||
f = SimpleUploadedFile(name, data) | ||
file_upload_url = self.url + str(reverse_lazy('api_file_upload')) | ||
return fileobj_temp, self.admin_client().post(file_upload_url, {"file": f}) | ||
|
||
|
||
class StudioAPITestCase(APITestCase, BucketTestMixin): | ||
|
||
|
@@ -67,6 +88,7 @@ def tearDown(self): | |
if not self.persist_bucket: | ||
self.delete_bucket() | ||
|
||
|
||
class BaseTestCase(StudioTestCase): | ||
def setUp(self): | ||
super(BaseTestCase, self).setUp() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,11 @@ | |
from mixer.backend.django import mixer | ||
from contentcuration import models | ||
|
||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from django.core.urlresolvers import reverse_lazy | ||
|
||
from rest_framework.test import APIClient | ||
|
||
from base import StudioTestCase | ||
from base import BaseTestCase | ||
from testdata import create_temp_file | ||
from contentcuration import models as cc | ||
|
||
|
@@ -52,15 +51,12 @@ def add_field_defaults_to_node(node): | |
# Tests | ||
### | ||
|
||
class CreateChannelTestCase(StudioTestCase): | ||
class CreateChannelTestCase(BaseTestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super(CreateChannelTestCase, cls).setUpClass() | ||
|
||
cls.url = "http://127.0.0.1:8000" | ||
cls.admin_user = models.User.objects.create_superuser('big_shot', '[email protected]', 'password') | ||
|
||
cls.channel_metadata = { | ||
"name": "Aron's cool channel", | ||
"id": "fasdfada", | ||
|
@@ -78,21 +74,6 @@ def setUp(self): | |
self.fileobj_document = create_temp_file("ghi", 'document', 'pdf', 'application/pdf') | ||
self.fileobj_exercise = create_temp_file("jkl", 'exercise', 'perseus', 'application/perseus') | ||
|
||
def admin_client(self): | ||
client = APIClient() | ||
client.force_authenticate(self.admin_user) | ||
return client | ||
|
||
def upload_file(self): | ||
""" | ||
Uploads a file to the server using an authorized client. | ||
""" | ||
fileobj_temp = create_temp_file(":)") | ||
name = fileobj_temp['name'] | ||
file_upload_url = self.url + str(reverse_lazy('api_file_upload')) | ||
f = SimpleUploadedFile(name, fileobj_temp['data']) | ||
return self.admin_client().post(file_upload_url, {"file": f}) | ||
|
||
def create_channel(self): | ||
create_channel_url = self.url + str(reverse_lazy('api_create_channel')) | ||
payload = { | ||
|
@@ -103,7 +84,7 @@ def create_channel(self): | |
return response | ||
|
||
def test_api_file_upload_status(self): | ||
response = self.upload_file() | ||
fileobj, response = self.upload_temp_file(":)") | ||
assert response.status_code == requests.codes.ok | ||
|
||
def test_channel_create_channel_created(self): | ||
|
74 changes: 74 additions & 0 deletions
74
contentcuration/contentcuration/tests/test_zipcontentview.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
import zipfile | ||
|
||
from base import BaseTestCase | ||
|
||
|
||
class ZipFileTestCase(BaseTestCase): | ||
def setUp(self): | ||
super(ZipFileTestCase, self).setUp() | ||
self.zipfile_url = '/zipcontent/' | ||
|
||
self.temp_files = [] | ||
|
||
def tearDown(self): | ||
for temp_file in self.temp_files: | ||
os.remove(temp_file) | ||
|
||
def do_create_zip(self): | ||
zip_handle, zip_filename = tempfile.mkstemp(suffix='.zip') | ||
self.temp_files.append(zip_filename) | ||
os.close(zip_handle) | ||
|
||
with zipfile.ZipFile(zip_filename, 'w') as zip: | ||
zip.writestr("index.html", "<html><head></head><body><p>Hello World!</p></body></html>") | ||
|
||
return zip_filename | ||
|
||
def test_invalid_zip(self): | ||
temp_file, response = self.upload_temp_file("Hello!", ext="zip") | ||
url = '{}{}/'.format(self.zipfile_url, temp_file['name']) | ||
response = self.get(url) | ||
assert response.status_code == 500 | ||
|
||
def test_valid_zipfile(self): | ||
myzip = self.do_create_zip() | ||
|
||
self.sign_in() | ||
temp_file, response = self.upload_temp_file(open(myzip, 'rb').read(), ext='.zip') | ||
assert response.status_code == 200 | ||
url = '{}{}/'.format(self.zipfile_url, temp_file['name']) | ||
response = self.get(url) | ||
assert response.status_code == 200 | ||
|
||
def test_valid_zipfile_file_access(self): | ||
myzip = self.do_create_zip() | ||
|
||
self.sign_in() | ||
temp_file, response = self.upload_temp_file(open(myzip, 'rb').read(), ext='.zip') | ||
assert response.status_code == 200 | ||
url = '{}{}/index.html'.format(self.zipfile_url, temp_file['name']) | ||
response = self.get(url) | ||
assert response.status_code == 200 | ||
|
||
def test_valid_zipfile_missing_file(self): | ||
myzip = self.do_create_zip() | ||
|
||
self.sign_in() | ||
temp_file, response = self.upload_temp_file(open(myzip, 'rb').read(), ext='.zip') | ||
assert response.status_code == 200 | ||
url = '{}{}/iamjustanillusion.txt'.format(self.zipfile_url, temp_file['name']) | ||
response = self.get(url) | ||
assert response.status_code == 404 | ||
|
||
def test_valid_zipfile_access_outside_zip_fails(self): | ||
myzip = self.do_create_zip() | ||
|
||
self.sign_in() | ||
temp_file, response = self.upload_temp_file(open(myzip, 'rb').read(), ext='.zip') | ||
assert response.status_code == 200 | ||
url = '{}{}/../outsidejson.js'.format(self.zipfile_url, temp_file['name']) | ||
response = self.get(url) | ||
assert response.status_code == 404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters