Beispiel #1
0
 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()
Beispiel #3
0
 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'})
Beispiel #4
0
 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()
Beispiel #5
0
    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()
Beispiel #6
0
 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()
Beispiel #7
0
 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()
Beispiel #8
0
 def test_offset_returns_zero_if_file_doesnt_exist(self):
     tus = TUSUpload('myuid', {'length': 1024})
     self.assertEqual(0, tus.offset())
     tus.cleanup()
Beispiel #9
0
 def test_length_returns_zero_if_not_set(self):
     tus = TUSUpload('myuid')
     self.assertEqual(0, tus.length())
     tus.cleanup()
Beispiel #10
0
 def test_length_returns_total_length_if_set(self):
     tus = TUSUpload('myuid', {'length': 1024})
     self.assertEqual(1024, tus.length())
     tus.cleanup()
Beispiel #11
0
 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()
Beispiel #12
0
 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()
Beispiel #13
0
 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()
Beispiel #14
0
 def test_write_sets_finished_flag(self):
     tus = TUSUpload('myuid', {'length': 10})
     tus.write(StringIO('0123456789'))
     self.assertTrue(tus.finished)
     tus.cleanup()
Beispiel #15
0
 def test_cleanup_removes_metadata_file(self):
     tus = TUSUpload('myuid', {'length': 1024})
     tus.cleanup()
     self.assertFalse(os.path.exists(tus.metadata_path))
Beispiel #16
0
 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))
Beispiel #17
0
 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()
Beispiel #18
0
 def test_write_sets_finished_flag(self):
     tus = TUSUpload("myuid", {"length": 10})
     tus.write(BytesIO(b"0123456789"))
     self.assertTrue(tus.finished)
     tus.cleanup()
Beispiel #19
0
 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()
Beispiel #20
0
 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()
Beispiel #21
0
 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()
Beispiel #22
0
 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()
Beispiel #23
0
 def test_tmp_dir_gets_created_in_client_home(self):
     tus = TUSUpload('myuid')
     self.assertTrue(os.path.isdir(tus.tmp_dir))
     tus.cleanup()
Beispiel #24
0
 def test_metadata_returns_empty_dict_if_no_metadata_has_been_set(self):
     tus = TUSUpload('myuid')
     self.assertEqual({}, tus.metadata())
     tus.cleanup()
Beispiel #25
0
 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()
Beispiel #26
0
 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))
Beispiel #27
0
 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()
Beispiel #28
0
 def get_tus_upload_instance(self, uid):
     return TUSUpload(uid)
Beispiel #29
0
 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()