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_file_id_wrong_data_type(self): # Test using bytes instead of str file_id = six.ensure_binary(_get_file_id()) self.assertFalse( FilepondFileUploader._file_id_valid(file_id), ('The provided file ID is of the wrong data type, this test ' 'should fail.'))
def test_upload_id_valid(self): # _get_file_id is currently used for getting both file+upload IDs # since the spec for both is the same at present. upload_id = _get_file_id() self.assertTrue( FilepondFileUploader._upload_id_valid(upload_id), 'A valid upload ID has been provided and this test should pass!')
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_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_upload_id_wrong_data_type(self): # _get_file_id is currently used for getting both file+upload IDs # since the spec for both is the same at present. Test using bytes # instead of str upload_id = _get_file_id().encode() self.assertFalse( FilepondFileUploader._upload_id_valid(upload_id), ('The provided upload ID is of the wrong data type, this test ' 'should fail.'))
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_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 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_file_id_invalid(self): file_id = 'sadadadasd' self.assertFalse( FilepondFileUploader._file_id_valid(file_id), 'Invalid file ID (wrong length) provided. Failure expected.')
def test_file_id_valid(self): file_id = _get_file_id() self.assertTrue( FilepondFileUploader._file_id_valid(file_id), 'A valid file ID has been provided and this test should pass!')
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 test_get_file_obj_std_field_name_missing(self): self.request.data = {'somefield': '{}'} with self.assertRaisesMessage( ParseError, 'Invalid request data has been provided.'): FilepondFileUploader._get_file_obj(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)