From e5369e0a833b10ecff8a4dde7b5a156f3a27b47a Mon Sep 17 00:00:00 2001 From: Greg Tatum Date: Tue, 10 Dec 2024 13:16:02 -0600 Subject: [PATCH 1/2] Change the attachment request to pass a file handle rather than reading in the bytes --- src/kinto_http/client.py | 20 ++++++++++---------- tests/test_client.py | 28 +++++++++++++++------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/kinto_http/client.py b/src/kinto_http/client.py index 86f6a6d..35b7914 100644 --- a/src/kinto_http/client.py +++ b/src/kinto_http/client.py @@ -923,20 +923,20 @@ def add_attachment( permissions=None, mimetype=None, ): - with open(filepath, "rb") as f: - filecontent = f.read() filename = os.path.basename(filepath) if mimetype is None: mimetype, _ = mimetypes.guess_type(filepath) - multipart = [("attachment", (filename, filecontent, mimetype))] endpoint = self._get_endpoint("attachment", id=id, bucket=bucket, collection=collection) - resp, _ = self.session.request( - "post", - endpoint, - data=json.dumps(data) if data is not None else None, - permissions=json.dumps(permissions) if permissions is not None else None, - files=multipart, - ) + + with open(filepath, "rb") as file: + resp, _ = self.session.request( + "post", + endpoint, + data=json.dumps(data) if data is not None else None, + permissions=json.dumps(permissions) if permissions is not None else None, + files=[("attachment", (filename, file, mimetype))], + ) + return resp @retry_timeout diff --git a/tests/test_client.py b/tests/test_client.py index 3f52bf3..eba45d3 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -3,6 +3,7 @@ import pytest from pytest_mock.plugin import MockerFixture +from unittest.mock import patch, mock_open from kinto_http import ( BearerTokenAuth, @@ -1429,20 +1430,21 @@ def test_add_attachment_guesses_mimetype(record_setup: Client, tmp_path): p = tmp_path / "file.txt" p.write_text("hello") - client.add_attachment( - id="abc", - bucket="a", - collection="b", - filepath=p, - ) + with patch("builtins.open", mock_open(read_data="hello")) as mock_file: + client.add_attachment( + id="abc", + bucket="a", + collection="b", + filepath=p, + ) - client.session.request.assert_called_with( - "post", - "/buckets/a/collections/b/records/abc/attachment", - data=None, - permissions=None, - files=[("attachment", ("file.txt", b"hello", "text/plain"))], - ) + client.session.request.assert_called_with( + "post", + "/buckets/a/collections/b/records/abc/attachment", + data=None, + permissions=None, + files=[("attachment", ("file.txt", mock_file.return_value, "text/plain"))], + ) def test_get_permissions(client_setup: Client): From 62ca1b397bb04a432710734f7671164044d124e5 Mon Sep 17 00:00:00 2001 From: Greg Tatum Date: Wed, 11 Dec 2024 09:51:33 -0600 Subject: [PATCH 2/2] Handle test nit by removing the temporary file --- tests/test_client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index eba45d3..428bb20 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1428,14 +1428,12 @@ def test_add_attachment_guesses_mimetype(record_setup: Client, tmp_path): client = record_setup mock_response(client.session) - p = tmp_path / "file.txt" - p.write_text("hello") with patch("builtins.open", mock_open(read_data="hello")) as mock_file: client.add_attachment( id="abc", bucket="a", collection="b", - filepath=p, + filepath="file.txt", ) client.session.request.assert_called_with(