diff --git a/osis_document/contrib/forms.py b/osis_document/contrib/forms.py index b7752c3..3b467d0 100644 --- a/osis_document/contrib/forms.py +++ b/osis_document/contrib/forms.py @@ -83,6 +83,7 @@ def __init__(self, **kwargs): self.post_process_params = kwargs.pop('post_process_params', None) self.with_cropping = kwargs.pop('with_cropping', False) self.cropping_options = kwargs.pop('cropping_options', None) + self.for_modified_upload = kwargs.pop('for_modified_upload', False) kwargs.setdefault( 'widget', FileUploadWidget( @@ -101,6 +102,7 @@ def __init__(self, **kwargs): post_process_params=self.post_process_params, with_cropping=self.with_cropping, cropping_options=self.cropping_options, + for_modified_upload=self.for_modified_upload, ), ) base_field = TokenField( @@ -146,5 +148,14 @@ def prepare_value(self, value): if isinstance(value, list): from osis_document.api.utils import get_remote_token - return [get_remote_token(v, write_token=True) if is_uuid(v) else v for v in value] + return [ + get_remote_token( + v, + write_token=True, + for_modified_upload=self.for_modified_upload + ) + if is_uuid(v) + else v + for v in value + ] return value diff --git a/osis_document/contrib/widgets.py b/osis_document/contrib/widgets.py index 52b3c1b..9ae3773 100644 --- a/osis_document/contrib/widgets.py +++ b/osis_document/contrib/widgets.py @@ -80,6 +80,7 @@ def __init__(self, **kwargs): self.post_process_params = kwargs.pop('post_process_params', None) self.with_cropping = kwargs.pop('with_cropping', False) self.cropping_options = kwargs.pop('cropping_options', None) + self.for_modified_upload = kwargs.pop('for_modified_upload', False) if kwargs.get('size', None) is None: kwargs['size'] = 0 super().__init__(widget=forms.TextInput, **kwargs) @@ -156,5 +157,14 @@ def format_value(self, values): return filter( None, - [get_remote_token(value, write_token=True) if isinstance(value, uuid.UUID) else value for value in values], + [ + get_remote_token( + value, + write_token=True, + for_modified_upload=self.for_modified_upload, + ) + if isinstance(value, uuid.UUID) + else value + for value in values + ], ) diff --git a/osis_document/tests/test_fields.py b/osis_document/tests/test_fields.py index 77e0cd8..fcae847 100644 --- a/osis_document/tests/test_fields.py +++ b/osis_document/tests/test_fields.py @@ -89,7 +89,7 @@ def test_model_form_submit( get_remote_metadata, ): # For the sake of simplicity, let's say a remote confirm is local - confirm_remote_upload.side_effect = lambda token, upload_to, **_: confirm_upload(token, upload_to) + confirm_remote_upload.side_effect = lambda token, upload_to, **_: str(confirm_upload(token, upload_to)) token = WriteTokenFactory() get_remote_metadata.return_value = { @@ -238,7 +238,7 @@ def test_create_from_uuid_saving( instance = TestDocument(documents=[token.token]) with patch('osis_document.api.utils.confirm_remote_upload') as confirm_remote_upload: # For the sake of simplicity, let's say a remote confirm is local - confirm_remote_upload.side_effect = lambda token, upload_to, **_: confirm_upload(token, upload_to) + confirm_remote_upload.side_effect = lambda token, upload_to, **_: str(confirm_upload(token, upload_to)) instance.save() self.assertEqual(len(instance.documents), 1) diff --git a/osis_document/tests/test_forms.py b/osis_document/tests/test_forms.py index ed273d8..26de1a7 100644 --- a/osis_document/tests/test_forms.py +++ b/osis_document/tests/test_forms.py @@ -29,7 +29,7 @@ from django import forms from django.test import TestCase, override_settings from osis_document.contrib.forms import FileUploadField, TokenField -from osis_document.tests.factories import WriteTokenFactory +from osis_document.tests.factories import WriteTokenFactory, PdfUploadFactory, ModifiedUploadFactory @override_settings(OSIS_DOCUMENT_BASE_URL='http://dummyurl.com/document/') @@ -271,3 +271,47 @@ class TestForm(forms.Form): self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data.get('media'), ['a:token']) + + + @patch('osis_document.api.utils.get_remote_token') + def test_for_modified_upload(self, get_remote_token): + get_remote_token.return_value = 'a:token' + + class TestFormWithModifiedUpload(forms.Form): + media = FileUploadField(for_modified_upload=True) + + upload = PdfUploadFactory() + modified_upload = ModifiedUploadFactory(upload=upload) + + form = TestFormWithModifiedUpload( + initial={ + 'media': [upload.uuid], + }, + ) + + form.as_p() + + get_remote_token.assert_called_once_with( + upload.uuid, + write_token=True, + for_modified_upload=True, + ) + + get_remote_token.reset_mock() + + class TestFormWithoutModifiedUpload(forms.Form): + media = FileUploadField() + + form = TestFormWithoutModifiedUpload( + initial={ + 'media': [upload.uuid], + }, + ) + + form.as_p() + + get_remote_token.assert_called_once_with( + upload.uuid, + write_token=True, + for_modified_upload=False, + ) diff --git a/osis_document/tests/test_widgets.py b/osis_document/tests/test_widgets.py index 58f6df3..1372001 100644 --- a/osis_document/tests/test_widgets.py +++ b/osis_document/tests/test_widgets.py @@ -47,6 +47,23 @@ def test_widget_should_not_expose_uuid(self, mock_remote_token): render = widget.render('foo', [stub_uuid]) self.assertNotIn(str(stub_uuid), render) self.assertIn('data-values="some:token"', render) + mock_remote_token.assert_called_once_with( + stub_uuid, + write_token=True, + for_modified_upload=False, + ) + + @patch('osis_document.api.utils.get_remote_token') + def test_widget_with_modified_upload(self, mock_remote_token): + mock_remote_token.return_value = 'some:token' + widget = FileUploadWidget(size=2, for_modified_upload=True) + stub_uuid = PdfUploadFactory().uuid + widget.render('foo', [stub_uuid]) + mock_remote_token.assert_called_once_with( + stub_uuid, + write_token=True, + for_modified_upload=True, + ) def test_widget_renders_attributes(self): widget = FileUploadWidget(size=1, mimetypes=['application/pdf']) diff --git a/osis_document/tests/views/test_request_upload_view.py b/osis_document/tests/views/test_request_upload_view.py index 126aaa4..4d6ae7b 100644 --- a/osis_document/tests/views/test_request_upload_view.py +++ b/osis_document/tests/views/test_request_upload_view.py @@ -48,6 +48,7 @@ def test_request_upload_with_bad_extension(self): response = self.client.post(resolve_url('request-upload'), {'file': file}) self.assertEqual(400, response.status_code) + @override_settings(ENABLE_MIMETYPE_VALIDATION=True) def test_request_upload_with_mime_smuggling(self): file = ContentFile(SMALLEST_PDF, 'foo.doc') response = self.client.post(resolve_url('request-upload'), {'file': file}) diff --git a/setup.py b/setup.py index 87915fb..411eb43 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ setup( name='OSIS Document', - version='0.10.5', + version='0.10.6', description='Document management API and widget', url='http://github.com/uclouvain/osis-document', author='Université catholique de Louvain',