def test_new_chunk_upload_dir_outside_storage_base(self): self.request.data = _setupRequestData({'filepond': '{}'}) self.request.META = {'HTTP_UPLOAD_LENGTH': 1048576} r = self.uploader._handle_new_chunk_upload( self.request, '../../%s' % self.upload_id, self.file_id) r = self._prep_response(r) self.assertContains(r, 'Unable to create storage for upload data.', status_code=500)
def test_get_file_obj_custom_field_name_missing(self): self.request.data = _setupRequestData({ 'fp_upload_field': 'somefield', 'a_field': '{}' }) with self.assertRaisesMessage( ParseError, 'Invalid request data has been provided.'): FilepondFileUploader._get_file_obj(self.request)
def test_new_chunk_upload_missing_len_header(self): self.request.data = _setupRequestData({'filepond': '{}'}) self.request.META = {} r = self.uploader._handle_new_chunk_upload( self.request, self.upload_id, self.file_id) r = self._prep_response(r) self.assertContains(r, 'No length for new chunked upload request.', status_code=400)
def test_new_chunk_upload_invalid_file_value(self): self.request.data = _setupRequestData({'filepond': 'Some data.'}) r = self.uploader._handle_new_chunk_upload( self.request, self.upload_id, self.file_id) r = self._prep_response(r) self.assertContains(r, ('An invalid file object has been received for a ' 'new chunked upload request.'), status_code=400)
def test_get_file_obj_custom_field_name(self): self.request.data = _setupRequestData({ 'fp_upload_field': 'somefield', 'somefield': ['{}'] }) data = FilepondFileUploader._get_file_obj(self.request) self.assertEqual( data, '{}', 'Data was not correctly extracted from ' 'the request.')
def test_get_uploader_post_req_chunk(self): self.request.method = 'POST' self.request.data = _setupRequestData({'filepond': '{}'}) self.request.META = {'HTTP_UPLOAD_LENGTH': 1048576} uploader = FilepondFileUploader.get_uploader(self.request) self.assertIsInstance( uploader, FilepondChunkedFileUploader, 'Expected a FilepondChunkedFileUploader but ' 'the returned uploader is of a different type.')
def test_get_uploader_post_req_std(self): file_obj = MagicMock(spec=InMemoryUploadedFile) self.request.method = 'POST' self.request.data = _setupRequestData({'filepond': [{}, file_obj]}) uploader = FilepondFileUploader.get_uploader(self.request) self.assertIsInstance( uploader, FilepondStandardFileUploader, 'Expected a FilepondStandardFileUploader but ' 'the returned uploader is of a different type.')
def test_new_chunk_upload_base_storage_dir_not_exists(self, mock_ope): mock_ope.return_value = False self.request.data = _setupRequestData({'filepond': '{}'}) self.request.META = {'HTTP_UPLOAD_LENGTH': 1048576} r = self.uploader._handle_new_chunk_upload( self.request, self.upload_id, self.file_id) r = self._prep_response(r) self.assertContains(r, 'Data storage error occurred.', status_code=500)
def test_handle_file_upload_invalid_file_obj(self): self.request.data = _setupRequestData( {'filepond': ['{}', 'This is a test'.encode()]}) # When run through DRF, the ParseError raised by handle_upload would # be captured and converted into a 400 response. Here we have to # capture the ParseError directly to check that this is working. with self.assertRaisesMessage(ParseError, 'Invalid data type has been parsed.'): self.uploader.handle_upload(self.request, self.upload_id, self.file_id)
def setUp(self): self.file_id = _get_file_id() self.upload_id = _get_file_id() self.file_name = 'my_uploaded_file.txt' self.request = MagicMock(spec=Request) self.request.user = AnonymousUser() file_obj = MagicMock(spec=InMemoryUploadedFile) file_obj.name = self.file_name self.request.data = _setupRequestData({'filepond': ['{}', file_obj]}) self.uploader = FilepondStandardFileUploader()
def test_get_file_obj_std_field_name(self): # The data may be a byte array, a string ('{}') or an UploadedFile # object depending on what type of filepond request we're handling. # This doesn't actually matter in this and the subsequent get_file_obj # tests, this is just checking that the data is correctly extracted. self.request.data = _setupRequestData({'filepond': '{}'}) data = FilepondFileUploader._get_file_obj(self.request) self.assertEqual( data, '{}', 'Data was not correctly extracted from ' 'the request.')
def test_new_chunk_upload_successful(self, mock_ope): mock_ope.return_value = True self.request.data = _setupRequestData({'filepond': '{}'}) self.request.META = {'HTTP_UPLOAD_LENGTH': 1048576} with patch('os.makedirs'): r = self.uploader._handle_new_chunk_upload( self.request, self.upload_id, self.file_id) r = self._prep_response(r) self.assertContains(r, self.upload_id, status_code=200)
def test_new_chunk_upload_storage_dir_exists(self, mock_ope): mock_ope.return_value = True self.request.data = _setupRequestData({'filepond': '{}'}) self.request.META = {'HTTP_UPLOAD_LENGTH': 1048576} with patch('os.makedirs') as mock_osmd: mock_osmd.side_effect = OSError('[Mock] Unable to create dir!') r = self.uploader._handle_new_chunk_upload( self.request, self.upload_id, self.file_id) r = self._prep_response(r) self.assertContains(r, 'Unable to prepare storage for upload data.', status_code=500)