diff --git a/alexandria/core/tests/test_dav.py b/alexandria/core/tests/test_dav.py index 2ba878b1..305b6ecc 100644 --- a/alexandria/core/tests/test_dav.py +++ b/alexandria/core/tests/test_dav.py @@ -228,10 +228,24 @@ def test_dav_url_schemes_unconfigured(db, file_factory, manabi, settings): def test_dav_without_content(db, manabi, settings, file_factory): settings.ALEXANDRIA_MANABI_DAV_URI_SCHEMES = {"text/plain": "ms-word:ofe|u|"} - file = file_factory(name="test.txt", mime_type="text/plain") + file = file_factory( + name="test.txt", + mime_type="text/plain", + modified_by_user="some-user", + modified_by_group="some-group", + ) + dav_app = TestApp(get_dav()) with pytest.raises(AppError) as e: - dav_app.put(get_webdav_url_without_uri_scheme(file, "admin", "admin"), b"") + dav_app.put( + get_webdav_url_without_uri_scheme( + file, + "some-other-user", + "some-other-group", + ), + b"", + ) assert "400 Bad Request" in str(e) + assert file.document.files.filter(variant=File.Variant.ORIGINAL).count() == 1 diff --git a/alexandria/dav_provider.py b/alexandria/dav_provider.py index 43c779cf..89bb1909 100644 --- a/alexandria/dav_provider.py +++ b/alexandria/dav_provider.py @@ -98,36 +98,37 @@ def begin_write(self, *, content_type=None): return self.memory_file def end_write(self, *, with_errors): - file = self.file - if ( - self.file.modified_by_user != self.user - or self.file.modified_by_group != self.group - ): - file = File( - variant=self.file.variant, - original=self.file.original, - name=self.file.name, - document=self.file.document, - encryption_status=self.file.encryption_status, - mime_type=self.file.mime_type, - created_by_user=self.user, - created_by_group=self.group, - modified_by_user=self.user, - modified_by_group=self.group, - ) - file.size = self.memory_file.getbuffer().nbytes - self.memory_file.seek(0) - django_file = DjangoFile(name=file.name, file=self.memory_file) - file.content = django_file - - try: - validate_file(file) - except ValidationError: - raise DAVError(HTTP_FORBIDDEN) - - file.save() - self.file = file - self.memory_file.do_close() + if not with_errors: + file = self.file + if ( + self.file.modified_by_user != self.user + or self.file.modified_by_group != self.group + ): + file = File( + variant=self.file.variant, + original=self.file.original, + name=self.file.name, + document=self.file.document, + encryption_status=self.file.encryption_status, + mime_type=self.file.mime_type, + created_by_user=self.user, + created_by_group=self.group, + modified_by_user=self.user, + modified_by_group=self.group, + ) + file.size = self.memory_file.getbuffer().nbytes + self.memory_file.seek(0) + django_file = DjangoFile(name=file.name, file=self.memory_file) + file.content = django_file + + try: + validate_file(file) + except ValidationError: + raise DAVError(HTTP_FORBIDDEN) + + file.save() + self.file = file + self.memory_file.do_close() super().end_write(with_errors=with_errors)