Skip to content

Commit 2369b85

Browse files
committed
tests
1 parent 573ebca commit 2369b85

File tree

3 files changed

+419
-84
lines changed

3 files changed

+419
-84
lines changed

tests/federation/test_federation_media.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from synapse.util import Clock
3636

3737
from tests import unittest
38+
from tests.media.test_media_storage import small_png
3839
from tests.test_utils import SMALL_PNG
3940

4041

@@ -146,3 +147,112 @@ def test_file_download(self) -> None:
146147
# check that the png file exists and matches what was uploaded
147148
found_file = any(SMALL_PNG in field for field in stripped_bytes)
148149
self.assertTrue(found_file)
150+
151+
152+
class FederationThumbnailTest(unittest.FederatingHomeserverTestCase):
153+
154+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
155+
super().prepare(reactor, clock, hs)
156+
self.test_dir = tempfile.mkdtemp(prefix="synapse-tests-")
157+
self.addCleanup(shutil.rmtree, self.test_dir)
158+
self.primary_base_path = os.path.join(self.test_dir, "primary")
159+
self.secondary_base_path = os.path.join(self.test_dir, "secondary")
160+
161+
hs.config.media.media_store_path = self.primary_base_path
162+
163+
storage_providers = [
164+
StorageProviderWrapper(
165+
FileStorageProviderBackend(hs, self.secondary_base_path),
166+
store_local=True,
167+
store_remote=False,
168+
store_synchronous=True,
169+
)
170+
]
171+
172+
self.filepaths = MediaFilePaths(self.primary_base_path)
173+
self.media_storage = MediaStorage(
174+
hs, self.primary_base_path, self.filepaths, storage_providers
175+
)
176+
self.media_repo = hs.get_media_repository()
177+
178+
def test_thumbnail_download_scaled(self) -> None:
179+
content = io.BytesIO(small_png.data)
180+
content_uri = self.get_success(
181+
self.media_repo.create_content(
182+
"image/png",
183+
"test_png_thumbnail",
184+
content,
185+
67,
186+
UserID.from_string("@user_id:whatever.org"),
187+
)
188+
)
189+
# test with an image file
190+
channel = self.make_signed_federation_request(
191+
"GET",
192+
f"/_matrix/federation/v1/media/thumbnail/{content_uri.media_id}?width=32&height=32&method=scale",
193+
)
194+
self.pump()
195+
self.assertEqual(200, channel.code)
196+
197+
content_type = channel.headers.getRawHeaders("content-type")
198+
assert content_type is not None
199+
assert "multipart/mixed" in content_type[0]
200+
assert "boundary" in content_type[0]
201+
202+
# extract boundary
203+
boundary = content_type[0].split("boundary=")[1]
204+
# split on boundary and check that json field and expected value exist
205+
body = channel.result.get("body")
206+
assert body is not None
207+
stripped_bytes = body.split(b"\r\n" + b"--" + boundary.encode("utf-8"))
208+
found_json = any(
209+
b"\r\nContent-Type: application/json\r\n\r\n{}" in field
210+
for field in stripped_bytes
211+
)
212+
self.assertTrue(found_json)
213+
214+
# check that the png file exists and matches the expected scaled bytes
215+
found_file = any(small_png.expected_scaled in field for field in stripped_bytes)
216+
self.assertTrue(found_file)
217+
218+
def test_thumbnail_download_cropped(self) -> None:
219+
content = io.BytesIO(small_png.data)
220+
content_uri = self.get_success(
221+
self.media_repo.create_content(
222+
"image/png",
223+
"test_png_thumbnail",
224+
content,
225+
67,
226+
UserID.from_string("@user_id:whatever.org"),
227+
)
228+
)
229+
# test with an image file
230+
channel = self.make_signed_federation_request(
231+
"GET",
232+
f"/_matrix/federation/v1/media/thumbnail/{content_uri.media_id}?width=32&height=32&method=crop",
233+
)
234+
self.pump()
235+
self.assertEqual(200, channel.code)
236+
237+
content_type = channel.headers.getRawHeaders("content-type")
238+
assert content_type is not None
239+
assert "multipart/mixed" in content_type[0]
240+
assert "boundary" in content_type[0]
241+
242+
# extract boundary
243+
boundary = content_type[0].split("boundary=")[1]
244+
# split on boundary and check that json field and expected value exist
245+
body = channel.result.get("body")
246+
assert body is not None
247+
stripped_bytes = body.split(b"\r\n" + b"--" + boundary.encode("utf-8"))
248+
found_json = any(
249+
b"\r\nContent-Type: application/json\r\n\r\n{}" in field
250+
for field in stripped_bytes
251+
)
252+
self.assertTrue(found_json)
253+
254+
# check that the png file exists and matches the expected cropped bytes
255+
found_file = any(
256+
small_png.expected_cropped in field for field in stripped_bytes
257+
)
258+
self.assertTrue(found_file)

tests/media/test_media_storage.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
# [This file includes modifications made by New Vector Limited]
1919
#
2020
#
21-
import itertools
2221
import os
2322
import shutil
2423
import tempfile
@@ -227,19 +226,15 @@ class TestImage:
227226
empty_file,
228227
SVG,
229228
]
230-
urls = [
231-
"_matrix/media/r0/thumbnail",
232-
"_matrix/client/unstable/org.matrix.msc3916/media/thumbnail",
233-
]
229+
input_values = [(x,) for x in test_images]
234230

235231

236-
@parameterized_class(("test_image", "url"), itertools.product(test_images, urls))
232+
@parameterized_class(("test_image",), input_values)
237233
class MediaRepoTests(unittest.HomeserverTestCase):
238234
servlets = [media.register_servlets]
239235
test_image: ClassVar[TestImage]
240236
hijack_auth = True
241237
user_id = "@test:user"
242-
url: ClassVar[str]
243238

244239
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
245240
self.fetches: List[
@@ -304,7 +299,6 @@ def write_err(f: Failure) -> Failure:
304299
"config": {"directory": self.storage_path},
305300
}
306301
config["media_storage_providers"] = [provider_config]
307-
config["experimental_features"] = {"msc3916_authenticated_media_enabled": True}
308302

309303
hs = self.setup_test_homeserver(config=config, federation_http_client=client)
310304

@@ -509,7 +503,7 @@ def test_thumbnail_repeated_thumbnail(self) -> None:
509503
params = "?width=32&height=32&method=scale"
510504
channel = self.make_request(
511505
"GET",
512-
f"/{self.url}/{self.media_id}{params}",
506+
f"/_matrix/media/r0/thumbnail/{self.media_id}{params}",
513507
shorthand=False,
514508
await_result=False,
515509
)
@@ -537,7 +531,7 @@ def test_thumbnail_repeated_thumbnail(self) -> None:
537531

538532
channel = self.make_request(
539533
"GET",
540-
f"/{self.url}/{self.media_id}{params}",
534+
f"/_matrix/media/r0/thumbnail/{self.media_id}{params}",
541535
shorthand=False,
542536
await_result=False,
543537
)
@@ -573,7 +567,7 @@ def _test_thumbnail(
573567
params = "?width=32&height=32&method=" + method
574568
channel = self.make_request(
575569
"GET",
576-
f"/{self.url}/{self.media_id}{params}",
570+
f"/_matrix/media/r0/thumbnail/{self.media_id}{params}",
577571
shorthand=False,
578572
await_result=False,
579573
)
@@ -608,7 +602,7 @@ def _test_thumbnail(
608602
channel.json_body,
609603
{
610604
"errcode": "M_UNKNOWN",
611-
"error": f"Cannot find any thumbnails for the requested media ('/{self.url}/example.com/12345'). This might mean the media is not a supported_media_format=(image/jpeg, image/jpg, image/webp, image/gif, image/png) or that thumbnailing failed for some other reason. (Dynamic thumbnails are disabled on this server.)",
605+
"error": "Cannot find any thumbnails for the requested media ('/_matrix/media/r0/thumbnail/example.com/12345'). This might mean the media is not a supported_media_format=(image/jpeg, image/jpg, image/webp, image/gif, image/png) or that thumbnailing failed for some other reason. (Dynamic thumbnails are disabled on this server.)",
612606
},
613607
)
614608
else:
@@ -618,7 +612,7 @@ def _test_thumbnail(
618612
channel.json_body,
619613
{
620614
"errcode": "M_NOT_FOUND",
621-
"error": f"Not found '/{self.url}/example.com/12345'",
615+
"error": "Not found '/_matrix/media/r0/thumbnail/example.com/12345'",
622616
},
623617
)
624618

0 commit comments

Comments
 (0)