Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dav): don't create new file if errors were raised #748

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions alexandria/core/tests/test_dav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 31 additions & 30 deletions alexandria/dav_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
Loading