def test_get_local_stored_upload_no_filestore(self): fsp = local_settings.FILE_STORE_PATH local_settings.FILE_STORE_PATH = None with self.assertRaisesMessage( ConfigurationError, 'The file upload settings are not configured correctly.'): get_stored_upload_file_data(self.su) local_settings.FILE_STORE_PATH = fsp
def test_get_remote_upload_not_on_remote_store(self): # File store path for remote testing should be '' file_store_path = '' mock_storage_backend = self._setup_mock_storage_backend() mock_storage_backend.exists.return_value = False file_path = os.path.join(file_store_path, self.su.file.name) with self.assertRaisesMessage( FileNotFoundError, ('File [%s] for upload_id [%s] not found on remote ' 'file store.' % (file_path, self.su.upload_id))): get_stored_upload_file_data(self.su) local_settings.STORAGES_BACKEND = None django_drf_filepond.api.storage_backend = None
def test_store_upload_unset_file_store_path(self): (filename, bytes_io) = get_stored_upload_file_data(self.su) file_data = bytes_io.read().decode() self.assertEqual(file_data, self.file_content, 'Returned file content not correct.') self.assertEqual(filename, os.path.basename(self.test_target_filename), 'Returned file name is not correct.')
def test_storage_backend_initialised_when_required(self): mock_storage_backend = self._setup_mock_storage_backend() mock_storage_backend.open.return_value = BytesIO( self.file_content.encode()) mock_storage_backend.exists.return_value = True with patch('django_drf_filepond.api._init_storage_backend') as m: django_drf_filepond.api.storage_backend_initialised = False get_stored_upload_file_data(self.su) local_settings.STORAGES_BACKEND = None django_drf_filepond.api.storage_backend = None try: m.assert_called_once() # No assert_called_once on Python 3.5 except AttributeError: self.assertEqual( m.call_count, 1, ('Expected _init_storage_backend to be called once but ' 'it has been called %s times.' % m.call_count))
def test_store_upload_unset_file_store_path(self): # Updated to reflect changes to StoredUpload to use a FileField (#31) # As a result, get_stored_upload_file_data now returns the raw bytes # rather than a BytesIO object. (filename, byte_data) = get_stored_upload_file_data(self.su) file_data = byte_data.decode() self.assertEqual(file_data, self.file_content, 'Returned file content not correct.') self.assertEqual(filename, os.path.basename(self.test_target_filename), 'Returned file name is not correct.')
def test_get_remote_stored_upload_data(self): # Set up the mock_storage_backend.open to return the file content mock_storage_backend = self._setup_mock_storage_backend() mock_storage_backend.open.return_value = BytesIO( self.file_content.encode()) mock_storage_backend.exists.return_value = True (filename, bytes_io) = get_stored_upload_file_data(self.su) file_data = bytes_io.read().decode() local_settings.STORAGES_BACKEND = None django_drf_filepond.api.storage_backend = None self.assertEqual(file_data, self.file_content, 'Returned file content not correct.') self.assertEqual(filename, os.path.basename(self.test_target_filename), 'Returned file name is not correct.')
def get(self, request): LOG.debug('Filepond API: Load view GET called...') if LOAD_RESTORE_PARAM_NAME not in request.GET: return Response('A required parameter is missing.', status=status.HTTP_400_BAD_REQUEST) upload_id = request.GET[LOAD_RESTORE_PARAM_NAME] if (not upload_id) or (upload_id == ''): return Response('An invalid ID has been provided.', status=status.HTTP_400_BAD_REQUEST) try: su = get_stored_upload(upload_id) except StoredUpload.DoesNotExist as e: LOG.error('StoredUpload with ID [%s] not found: [%s]' % (upload_id, str(e))) return Response('Not found', status=status.HTTP_404_NOT_FOUND) # su is now the StoredUpload record for the requested file try: (filename, data_bytes_io) = get_stored_upload_file_data(su) except ConfigurationError as e: LOG.error('Error getting file upload: [%s]' % str(e)) return HttpResponseServerError('The file upload settings are ' 'not configured correctly.') except FileNotFoundError: return HttpResponseNotFound('Error accessing file, not found.') except IOError: return HttpResponseServerError('Error reading file...') ct = _get_content_type(filename) response = HttpResponse(data_bytes_io.read(), content_type=ct) response['Content-Disposition'] = ('inline; filename=%s' % filename) return response