def test_tus_post_initialization(self): response = self.api_session.post( self.upload_url, headers={ "Tus-Resumable": "1.0.0", "Upload-Length": str(UPLOAD_LENGTH) }, ) self.assertEqual(response.status_code, 201) location = response.headers["Location"] url_base, uid = location.rsplit("/", 1) self.assertEqual(url_base, self.upload_url) self.assertEqual(len(uid), 32) upload = TUSUpload(uid) stored_metadata = upload.metadata() self.assertEqual(stored_metadata, {u"length": 8, u"mode": u"create"}) upload.cleanup()
def test_tus_post_initialization(self): response = self.api_session.post( self.upload_url, headers={ 'Tus-Resumable': '1.0.0', 'Upload-Length': str(UPLOAD_LENGTH), }) self.assertEqual(response.status_code, 201) location = response.headers['Location'] url_base, uid = location.rsplit('/', 1) self.assertEqual(url_base, self.upload_url) self.assertEqual(len(uid), 32) upload = TUSUpload(uid) stored_metadata = upload.metadata() self.assertEqual(stored_metadata, {u'length': 8, u'mode': u'create'}) upload.cleanup()
def test_tus_post_replace(self): self.file = api.content.create(container=self.portal, type='File', id='testfile', title='Testfile') transaction.commit() response = self.api_session.post( '{}/@tus-replace'.format(self.file.absolute_url()), headers={ 'Tus-Resumable': '1.0.0', 'Upload-Length': str(UPLOAD_LENGTH), }) self.assertEqual(response.status_code, 201) location = response.headers['Location'] url_base, uid = location.rsplit('/', 1) upload = TUSUpload(uid) stored_metadata = upload.metadata() self.assertEqual(stored_metadata, {u'length': 8, u'mode': u'replace'})
def test_tus_post_initialization_with_metadata(self): metadata = _prepare_metadata(UPLOAD_FILENAME, UPLOAD_MIMETYPE) response = self.api_session.post( self.upload_url, headers={'Tus-Resumable': '1.0.0', 'Upload-Length': str(UPLOAD_LENGTH), 'Upload-Metadata': metadata} ) self.assertEqual(response.status_code, 201) uid = self.get_tus_uid_from_url(response.headers['Location']) upload = TUSUpload(uid) stored_metadata = upload.metadata() self.assertEqual(stored_metadata, {u'content-type': u'text/plain', u'filename': u'test.txt', u'length': 8, u'mode': u'create'}) upload.cleanup()
def test_tus_patch_non_primary_field(self): tus = TUSUpload('myuid', { '@type': 'DXTestDocument', 'length': 12, 'fieldname': 'test_namedblobfile_field'}) response = self.api_session.patch( self.upload_url + '/myuid', headers={'Tus-Resumable': '1.0.0', 'Content-Type': 'application/offset+octet-stream', 'Upload-Offset': '0'}, data=BytesIO(b'abcdefghijkl')) self.assertEqual(204, response.status_code) transaction.commit() self.assertEqual(1, len(self.folder.objectIds())) id_ = self.folder.objectIds()[0] self.assertEqual( b'abcdefghijkl', self.folder[id_].test_namedblobfile_field.data) tus.cleanup()
def test_tus_post_replace(self): self.file = api.content.create(container=self.portal, type="File", id="testfile", title="Testfile") transaction.commit() response = self.api_session.post( f"{self.file.absolute_url()}/@tus-replace", headers={ "Tus-Resumable": "1.0.0", "Upload-Length": str(UPLOAD_LENGTH) }, ) self.assertEqual(response.status_code, 201) location = response.headers["Location"] url_base, uid = location.rsplit("/", 1) upload = TUSUpload(uid) stored_metadata = upload.metadata() self.assertEqual(stored_metadata, {"length": 8, "mode": "replace"}) upload.cleanup()
def test_tus_post_initialization_with_metadata(self): metadata = _prepare_metadata(UPLOAD_FILENAME, UPLOAD_MIMETYPE) response = self.api_session.post( self.upload_url, headers={ "Tus-Resumable": "1.0.0", "Upload-Length": str(UPLOAD_LENGTH), "Upload-Metadata": metadata, }, ) self.assertEqual(response.status_code, 201) uid = self.get_tus_uid_from_url(response.headers["Location"]) upload = TUSUpload(uid) stored_metadata = upload.metadata() self.assertEqual( stored_metadata, { "content-type": "text/plain", "filename": "test.txt", "length": 8, "mode": "create", }, ) upload.cleanup()
def test_offset_returns_zero_if_file_doesnt_exist(self): tus = TUSUpload('myuid', {'length': 1024}) self.assertEqual(0, tus.offset()) tus.cleanup()
def test_length_returns_zero_if_not_set(self): tus = TUSUpload('myuid') self.assertEqual(0, tus.length()) tus.cleanup()
def test_length_returns_total_length_if_set(self): tus = TUSUpload('myuid', {'length': 1024}) self.assertEqual(1024, tus.length()) tus.cleanup()
def test_metadata_gets_stored_if_provided(self): tus = TUSUpload('myuid', {'length': 1024, 'filename': 'test.pdf'}) self.assertIn('filename', tus.metadata()) self.assertEqual('test.pdf', tus.metadata()['filename']) tus.cleanup()
def test_metadata_gets_stored_if_provided(self): tus = TUSUpload("myuid", {"length": 1024, "filename": "test.pdf"}) self.assertIn("filename", tus.metadata()) self.assertEqual("test.pdf", tus.metadata()["filename"]) tus.cleanup()
def test_expires_returns_expiration_time_of_current_upload(self): tus = TUSUpload('myuid', {'length': 1024}) tus.write(StringIO('0123456789')) self.assertGreater(DateTime(tus.expires()), DateTime()) tus.cleanup()
def test_write_sets_finished_flag(self): tus = TUSUpload('myuid', {'length': 10}) tus.write(StringIO('0123456789')) self.assertTrue(tus.finished) tus.cleanup()
def test_cleanup_removes_metadata_file(self): tus = TUSUpload('myuid', {'length': 1024}) tus.cleanup() self.assertFalse(os.path.exists(tus.metadata_path))
def test_cleanup_removes_upload_file(self): tus = TUSUpload("myuid", {"length": 1024}) tus.write(BytesIO(b"0123456789")) tus.cleanup() self.assertFalse(os.path.exists(tus.filepath))
def test_expires_returns_expiration_time_of_current_upload(self): tus = TUSUpload("myuid", {"length": 1024}) tus.write(BytesIO(b"0123456789")) self.assertGreater(DateTime(tus.expires()), DateTime()) tus.cleanup()
def test_write_sets_finished_flag(self): tus = TUSUpload("myuid", {"length": 10}) tus.write(BytesIO(b"0123456789")) self.assertTrue(tus.finished) tus.cleanup()
def test_write_creates_new_file(self): tus = TUSUpload("myuid", {"length": 1024}) tus.write(BytesIO(b"0123456789")) self.assertTrue(os.path.isfile(tus.filepath)) tus.cleanup()
def test_offset_returns_size_of_current_file(self): tus = TUSUpload("myuid", {"length": 1024}) tus.write(BytesIO(b"0123456789")) self.assertEqual(10, tus.offset()) tus.cleanup()
def test_offset_returns_size_of_current_file(self): tus = TUSUpload('myuid', {'length': 1024}) tus.write(StringIO('0123456789')) self.assertEqual(10, tus.offset()) tus.cleanup()
def test_write_creates_new_file(self): tus = TUSUpload('myuid', {'length': 1024}) tus.write(StringIO('0123456789')) self.assertTrue(os.path.isfile(tus.filepath)) tus.cleanup()
def test_tmp_dir_gets_created_in_client_home(self): tus = TUSUpload('myuid') self.assertTrue(os.path.isdir(tus.tmp_dir)) tus.cleanup()
def test_metadata_returns_empty_dict_if_no_metadata_has_been_set(self): tus = TUSUpload('myuid') self.assertEqual({}, tus.metadata()) tus.cleanup()
def test_use_tus_tmp_dir_if_provided(self): tus_upload_dir = tempfile.mkdtemp() os.environ['TUS_TMP_FILE_DIR'] = tus_upload_dir tus = TUSUpload('myuid') self.assertEqual(tus_upload_dir, tus.tmp_dir) tus.cleanup()
def test_cleanup_removes_upload_file(self): tus = TUSUpload('myuid', {'length': 1024}) tus.write(StringIO('0123456789')) tus.cleanup() self.assertFalse(os.path.exists(tus.filepath))
def test_tus_patch_with_unsupported_version_returns_412(self): tus = TUSUpload("myuid", {"length": 2048}) response = self.api_session.patch(self.upload_url + "/myuid", headers={"Tus-Resumable": "0.2.2"}) self.assertEqual(412, response.status_code) tus.cleanup()
def get_tus_upload_instance(self, uid): return TUSUpload(uid)
def test_tus_patch_with_unsupported_version_returns_412(self): tus = TUSUpload('myuid', {'length': 2048}) response = self.api_session.patch(self.upload_url + '/myuid', headers={'Tus-Resumable': '0.2.2'}) self.assertEqual(412, response.status_code) tus.cleanup()