def test_get_uploader_head_req(self): self.request.method = 'HEAD' 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_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 post(self, request): LOG.debug('Filepond API: Process view POST called...') # Check that the temporary upload directory has been set if not hasattr(local_settings, 'UPLOAD_TMP'): return Response( 'The file upload path settings are not ' 'configured correctly.', status=status.HTTP_500_INTERNAL_SERVER_ERROR) # By default, enforce that the temporary upload location must be a # sub-directory of the project base directory. # TODO: Check whether this is necessary - maybe add a security # parameter that can be disabled to turn off this check if the # developer wishes? if ((not (storage.location).startswith(local_settings.BASE_DIR)) and (local_settings.BASE_DIR != os.path.dirname( django_drf_filepond.__file__))): if not local_settings.ALLOW_EXTERNAL_UPLOAD_DIR: return Response( 'The file upload path settings are not ' 'configured correctly.', status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Check that a relative path is not being used to store the # upload outside the specified UPLOAD_TMP directory. if not getattr(local_settings, 'UPLOAD_TMP').startswith( os.path.abspath(storage.location)): return Response( 'An invalid storage location has been ' 'specified.', status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Check that we've received a file and then generate a unique ID # for it. Also generate a unique UD for the temp upload dir file_id = _get_file_id() upload_id = _get_file_id() try: uploader = FilepondFileUploader.get_uploader(request) response = uploader.handle_upload(request, file_id, upload_id) except ParseError as e: # Re-raise the ParseError to trigger a 400 response via DRF. raise e return response
def test_get_uploader_get_req(self): self.request.method = 'GET' with self.assertRaisesMessage(MethodNotAllowed, 'GET is an invalid method type'): FilepondFileUploader.get_uploader(self.request)
def head(self, request, chunk_id): LOG.debug('Filepond API: Patch view HEAD called...') uploader = FilepondFileUploader.get_uploader(request) return uploader.handle_upload(request, chunk_id)