Skip to content

Commit f85e012

Browse files
committed
Merge branch 'gregtatum-attachment-truncation'
2 parents 858b925 + 62ca1b3 commit f85e012

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/kinto_http/client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -923,20 +923,20 @@ def add_attachment(
923923
permissions=None,
924924
mimetype=None,
925925
):
926-
with open(filepath, "rb") as f:
927-
filecontent = f.read()
928926
filename = os.path.basename(filepath)
929927
if mimetype is None:
930928
mimetype, _ = mimetypes.guess_type(filepath)
931-
multipart = [("attachment", (filename, filecontent, mimetype))]
932929
endpoint = self._get_endpoint("attachment", id=id, bucket=bucket, collection=collection)
933-
resp, _ = self.session.request(
934-
"post",
935-
endpoint,
936-
data=json.dumps(data) if data is not None else None,
937-
permissions=json.dumps(permissions) if permissions is not None else None,
938-
files=multipart,
939-
)
930+
931+
with open(filepath, "rb") as file:
932+
resp, _ = self.session.request(
933+
"post",
934+
endpoint,
935+
data=json.dumps(data) if data is not None else None,
936+
permissions=json.dumps(permissions) if permissions is not None else None,
937+
files=[("attachment", (filename, file, mimetype))],
938+
)
939+
940940
return resp
941941

942942
@retry_timeout

tests/test_client.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
from unittest.mock import mock_open, patch
34

45
import pytest
56
from pytest_mock.plugin import MockerFixture
@@ -1427,22 +1428,21 @@ def test_add_attachment_guesses_mimetype(record_setup: Client, tmp_path):
14271428
client = record_setup
14281429
mock_response(client.session)
14291430

1430-
p = tmp_path / "file.txt"
1431-
p.write_text("hello")
1432-
client.add_attachment(
1433-
id="abc",
1434-
bucket="a",
1435-
collection="b",
1436-
filepath=p,
1437-
)
1431+
with patch("builtins.open", mock_open(read_data="hello")) as mock_file:
1432+
client.add_attachment(
1433+
id="abc",
1434+
bucket="a",
1435+
collection="b",
1436+
filepath="file.txt",
1437+
)
14381438

1439-
client.session.request.assert_called_with(
1440-
"post",
1441-
"/buckets/a/collections/b/records/abc/attachment",
1442-
data=None,
1443-
permissions=None,
1444-
files=[("attachment", ("file.txt", b"hello", "text/plain"))],
1445-
)
1439+
client.session.request.assert_called_with(
1440+
"post",
1441+
"/buckets/a/collections/b/records/abc/attachment",
1442+
data=None,
1443+
permissions=None,
1444+
files=[("attachment", ("file.txt", mock_file.return_value, "text/plain"))],
1445+
)
14461446

14471447

14481448
def test_get_permissions(client_setup: Client):

0 commit comments

Comments
 (0)