Пример #1
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()
Пример #2
0
 def test_tus_patch_with_invalid_offset_returns_400(self):
     tus = TUSUpload('myuid', {'length': 2048})
     response = self.api_session.patch(
         self.upload_url + '/myuid',
         headers={'Tus-Resumable': '1.0.0',
                  'Content-Type': 'application/offset+octet-stream'})
     self.assertEqual(400, response.status_code)
     tus.cleanup()
Пример #3
0
 def test_tus_patch_with_unsupported_content_type_returns_400(self):
     tus = TUSUpload('myuid', {'length': 2048})
     response = self.api_session.patch(
         self.upload_url + '/myuid',
         headers={'Tus-Resumable': '1.0.0',
                  'Content-Type': 'application/json'})
     self.assertEqual(400, response.status_code)
     tus.cleanup()
Пример #4
0
 def test_write_appends_to_file_at_given_offset(self):
     tus = TUSUpload("myuid", {"length": 1024})
     tus.write(BytesIO(b"0123456789"))
     tus.write(BytesIO(b"abc"), 10)
     self.assertEqual(13, tus.offset())
     with open(tus.filepath, "rb") as f:
         data = f.read()
     self.assertEqual(b"0123456789abc", data)
     tus.cleanup()
Пример #5
0
 def test_write_appends_to_file_at_given_offset(self):
     tus = TUSUpload('myuid', {'length': 1024})
     tus.write(StringIO('0123456789'))
     tus.write(StringIO('abc'), 10)
     self.assertEqual(13, tus.offset())
     with open(tus.filepath, 'rb') as f:
         data = f.read()
     self.assertEqual('0123456789abc', data)
     tus.cleanup()
Пример #6
0
 def test_head_in_replace_mode_without_modify_permission_raises_401(self):
     self.folder.manage_permission('Modify portal content', [], 0)
     transaction.commit()
     tus = TUSUpload('myuid', {'mode': 'replace', 'length': 12})
     response = self.api_session.head(
         self.upload_url + '/myuid',
         headers={'Tus-Resumable': '1.0.0',
                  'Upload-Offset': '0'})
     self.assertEqual(401, response.status_code)
     tus.cleanup()
Пример #7
0
 def test_tus_head_response_includes_required_headers(self):
     tus = TUSUpload('myuid', {'length': 2048})
     response = self.api_session.head(self.upload_url + '/myuid',
                                      headers={'Tus-Resumable': '1.0.0'})
     self.assertIn('Upload-Length', response.headers)
     self.assertEqual('2048', response.headers['Upload-Length'])
     self.assertIn('Upload-Offset', response.headers)
     self.assertIn('Tus-Resumable', response.headers)
     self.assertIn('Cache-Control', response.headers)
     tus.cleanup()
Пример #8
0
 def test_tus_head_response_includes_required_headers(self):
     tus = TUSUpload("myuid", {"length": 2048})
     response = self.api_session.head(self.upload_url + "/myuid",
                                      headers={"Tus-Resumable": "1.0.0"})
     self.assertIn("Upload-Length", response.headers)
     self.assertEqual("2048", response.headers["Upload-Length"])
     self.assertIn("Upload-Offset", response.headers)
     self.assertIn("Tus-Resumable", response.headers)
     self.assertIn("Cache-Control", response.headers)
     tus.cleanup()
Пример #9
0
 def test_tus_patch_with_invalid_offset_returns_400(self):
     tus = TUSUpload("myuid", {"length": 2048})
     response = self.api_session.patch(
         self.upload_url + "/myuid",
         headers={
             "Tus-Resumable": "1.0.0",
             "Content-Type": "application/offset+octet-stream",
         },
     )
     self.assertEqual(400, response.status_code)
     tus.cleanup()
Пример #10
0
 def test_tus_patch_with_unsupported_content_type_returns_400(self):
     tus = TUSUpload("myuid", {"length": 2048})
     response = self.api_session.patch(
         self.upload_url + "/myuid",
         headers={
             "Tus-Resumable": "1.0.0",
             "Content-Type": "application/json"
         },
     )
     self.assertEqual(400, response.status_code)
     tus.cleanup()
Пример #11
0
 def test_tus_patch_unfinished_upload_returns_expires_header(self):
     tus = TUSUpload('myuid', {'length': 2048})
     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)
     self.assertIn('Upload-Expires', response.headers)
     tus.cleanup()
Пример #12
0
 def test_patch_in_replace_mode_without_modify_permission_raises_401(self):
     self.folder.manage_permission('Modify portal content', [], 0)
     transaction.commit()
     tus = TUSUpload('myuid', {'mode': 'replace', 'length': 12})
     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(401, response.status_code)
     tus.cleanup()
Пример #13
0
 def test_head_in_replace_mode_without_modify_permission_raises_401(self):
     self.folder.manage_permission("Modify portal content", [], 0)
     transaction.commit()
     tus = TUSUpload("myuid", {"mode": "replace", "length": 12})
     response = self.api_session.head(
         self.upload_url + "/myuid",
         headers={
             "Tus-Resumable": "1.0.0",
             "Upload-Offset": "0"
         },
     )
     self.assertEqual(401, response.status_code)
     tus.cleanup()
Пример #14
0
 def test_tus_patch_unfinished_upload_returns_expires_header(self):
     tus = TUSUpload("myuid", {"length": 2048})
     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)
     self.assertIn("Upload-Expires", response.headers)
     tus.cleanup()
Пример #15
0
 def test_patch_in_replace_mode_without_modify_permission_raises_401(self):
     self.folder.manage_permission("Modify portal content", [], 0)
     transaction.commit()
     tus = TUSUpload("myuid", {"mode": "replace", "length": 12})
     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(401, response.status_code)
     tus.cleanup()
Пример #16
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()
Пример #17
0
 def test_cleanup_expired_files(self):
     tus = TUSUpload('myuid')
     filepath = os.path.join(tus.tmp_dir, 'tus_upload_12345')
     metadata_path = os.path.join(tus.tmp_dir, 'tus_upload_12345.json')
     metadata_only_path = os.path.join(tus.tmp_dir, 'tus_upload_67890.json')
     open(filepath, 'wb').close()
     os.utime(filepath, (946684800.0, 946684800.0))
     open(metadata_path, 'wb').close()
     os.utime(metadata_path, (946684800.0, 946684800.0))
     open(metadata_only_path, 'wb').close()
     os.utime(metadata_only_path, (946684800.0, 946684800.0))
     tus.cleanup_expired()
     self.assertFalse(os.path.exists(filepath))
     self.assertFalse(os.path.exists(metadata_path))
     self.assertFalse(os.path.exists(metadata_only_path))
     tus.cleanup()
Пример #18
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, {"length": 8, "mode": "create"})
     upload.cleanup()
Пример #19
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()
Пример #20
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'})
     upload.cleanup()
Пример #21
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()
Пример #22
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()
Пример #23
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()
Пример #24
0
 def test_cleanup_removes_metadata_file(self):
     tus = TUSUpload('myuid', {'length': 1024})
     tus.cleanup()
     self.assertFalse(os.path.exists(tus.metadata_path))
Пример #25
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))
Пример #26
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()
Пример #27
0
 def test_metadata_returns_empty_dict_if_no_metadata_has_been_set(self):
     tus = TUSUpload('myuid')
     self.assertEqual({}, tus.metadata())
     tus.cleanup()
Пример #28
0
 def test_write_sets_finished_flag(self):
     tus = TUSUpload('myuid', {'length': 10})
     tus.write(StringIO('0123456789'))
     self.assertTrue(tus.finished)
     tus.cleanup()
Пример #29
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()
Пример #30
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()