Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit bfb4b85

Browse files
authored
Create a constant for a small png image in tests. (#10834)
To avoid duplicating it between a few tests.
1 parent 3eba047 commit bfb4b85

File tree

8 files changed

+45
-94
lines changed

8 files changed

+45
-94
lines changed

changelog.d/10834.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Factor out PNG image data to a constant to be used in several tests.

tests/replication/test_multi_media_repo.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 The Matrix.org Foundation C.I.C.
1+
# Copyright 2020-2021 The Matrix.org Foundation C.I.C.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414
import logging
1515
import os
16-
from binascii import unhexlify
1716
from typing import Optional, Tuple
1817

1918
from twisted.internet.protocol import Factory
@@ -28,6 +27,7 @@
2827
from tests.http import TestServerTLSConnectionFactory, get_test_ca_cert_file
2928
from tests.replication._base import BaseMultiWorkerStreamTestCase
3029
from tests.server import FakeChannel, FakeSite, FakeTransport, make_request
30+
from tests.test_utils import SMALL_PNG
3131

3232
logger = logging.getLogger(__name__)
3333

@@ -190,31 +190,25 @@ def test_download_image_race(self):
190190
channel1, request1 = self._get_media_req(hs1, "example.com:443", "PIC1")
191191
channel2, request2 = self._get_media_req(hs2, "example.com:443", "PIC1")
192192

193-
png_data = unhexlify(
194-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
195-
b"0000001f15c4890000000a49444154789c63000100000500010d"
196-
b"0a2db40000000049454e44ae426082"
197-
)
198-
199193
request1.setResponseCode(200)
200194
request1.responseHeaders.setRawHeaders(b"Content-Type", [b"image/png"])
201-
request1.write(png_data)
195+
request1.write(SMALL_PNG)
202196
request1.finish()
203197

204198
self.pump(0.1)
205199

206200
self.assertEqual(channel1.code, 200, channel1.result["body"])
207-
self.assertEqual(channel1.result["body"], png_data)
201+
self.assertEqual(channel1.result["body"], SMALL_PNG)
208202

209203
request2.setResponseCode(200)
210204
request2.responseHeaders.setRawHeaders(b"Content-Type", [b"image/png"])
211-
request2.write(png_data)
205+
request2.write(SMALL_PNG)
212206
request2.finish()
213207

214208
self.pump(0.1)
215209

216210
self.assertEqual(channel2.code, 200, channel2.result["body"])
217-
self.assertEqual(channel2.result["body"], png_data)
211+
self.assertEqual(channel2.result["body"], SMALL_PNG)
218212

219213
# We expect only three new thumbnails to have been persisted.
220214
self.assertEqual(start_count + 3, self._count_remote_thumbnails())

tests/rest/admin/test_admin.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 New Vector Ltd
1+
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
import json
1616
import os
1717
import urllib.parse
18-
from binascii import unhexlify
1918
from unittest.mock import Mock
2019

2120
from twisted.internet.defer import Deferred
@@ -28,6 +27,7 @@
2827

2928
from tests import unittest
3029
from tests.server import FakeSite, make_request
30+
from tests.test_utils import SMALL_PNG
3131

3232

3333
class VersionTestCase(unittest.HomeserverTestCase):
@@ -150,11 +150,6 @@ def prepare(self, reactor, clock, hs):
150150
self.media_repo = hs.get_media_repository_resource()
151151
self.download_resource = self.media_repo.children[b"download"]
152152
self.upload_resource = self.media_repo.children[b"upload"]
153-
self.image_data = unhexlify(
154-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
155-
b"0000001f15c4890000000a49444154789c63000100000500010d"
156-
b"0a2db40000000049454e44ae426082"
157-
)
158153

159154
def make_homeserver(self, reactor, clock):
160155

@@ -266,7 +261,7 @@ def test_quarantine_media_by_id(self):
266261

267262
# Upload some media into the room
268263
response = self.helper.upload_media(
269-
self.upload_resource, self.image_data, tok=admin_user_tok
264+
self.upload_resource, SMALL_PNG, tok=admin_user_tok
270265
)
271266

272267
# Extract media ID from the response
@@ -314,10 +309,10 @@ def test_quarantine_all_media_in_room(self, override_url_template=None):
314309

315310
# Upload some media
316311
response_1 = self.helper.upload_media(
317-
self.upload_resource, self.image_data, tok=non_admin_user_tok
312+
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
318313
)
319314
response_2 = self.helper.upload_media(
320-
self.upload_resource, self.image_data, tok=non_admin_user_tok
315+
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
321316
)
322317

323318
# Extract mxcs
@@ -381,10 +376,10 @@ def test_quarantine_all_media_by_user(self):
381376

382377
# Upload some media
383378
response_1 = self.helper.upload_media(
384-
self.upload_resource, self.image_data, tok=non_admin_user_tok
379+
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
385380
)
386381
response_2 = self.helper.upload_media(
387-
self.upload_resource, self.image_data, tok=non_admin_user_tok
382+
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
388383
)
389384

390385
# Extract media IDs
@@ -421,10 +416,10 @@ def test_cannot_quarantine_safe_media(self):
421416

422417
# Upload some media
423418
response_1 = self.helper.upload_media(
424-
self.upload_resource, self.image_data, tok=non_admin_user_tok
419+
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
425420
)
426421
response_2 = self.helper.upload_media(
427-
self.upload_resource, self.image_data, tok=non_admin_user_tok
422+
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
428423
)
429424

430425
# Extract media IDs

tests/rest/admin/test_media.py

+6-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright 2020 Dirk Klimpel
2+
# Copyright 2021 The Matrix.org Foundation C.I.C.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -14,7 +15,6 @@
1415

1516
import json
1617
import os
17-
from binascii import unhexlify
1818

1919
from parameterized import parameterized
2020

@@ -25,6 +25,7 @@
2525

2626
from tests import unittest
2727
from tests.server import FakeSite, make_request
28+
from tests.test_utils import SMALL_PNG
2829

2930

3031
class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
@@ -110,15 +111,10 @@ def test_delete_media(self):
110111

111112
download_resource = self.media_repo.children[b"download"]
112113
upload_resource = self.media_repo.children[b"upload"]
113-
image_data = unhexlify(
114-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
115-
b"0000001f15c4890000000a49444154789c63000100000500010d"
116-
b"0a2db40000000049454e44ae426082"
117-
)
118114

119115
# Upload some media into the room
120116
response = self.helper.upload_media(
121-
upload_resource, image_data, tok=self.admin_user_tok, expect_code=200
117+
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200
122118
)
123119
# Extract media ID from the response
124120
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
@@ -504,16 +500,10 @@ def _create_media(self):
504500
Create a media and return media_id and server_and_media_id
505501
"""
506502
upload_resource = self.media_repo.children[b"upload"]
507-
# file size is 67 Byte
508-
image_data = unhexlify(
509-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
510-
b"0000001f15c4890000000a49444154789c63000100000500010d"
511-
b"0a2db40000000049454e44ae426082"
512-
)
513503

514504
# Upload some media into the room
515505
response = self.helper.upload_media(
516-
upload_resource, image_data, tok=self.admin_user_tok, expect_code=200
506+
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200
517507
)
518508
# Extract media ID from the response
519509
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
@@ -584,16 +574,10 @@ def prepare(self, reactor, clock, hs):
584574

585575
# Create media
586576
upload_resource = media_repo.children[b"upload"]
587-
# file size is 67 Byte
588-
image_data = unhexlify(
589-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
590-
b"0000001f15c4890000000a49444154789c63000100000500010d"
591-
b"0a2db40000000049454e44ae426082"
592-
)
593577

594578
# Upload some media into the room
595579
response = self.helper.upload_media(
596-
upload_resource, image_data, tok=self.admin_user_tok, expect_code=200
580+
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200
597581
)
598582
# Extract media ID from the response
599583
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
@@ -711,16 +695,10 @@ def prepare(self, reactor, clock, hs):
711695

712696
# Create media
713697
upload_resource = media_repo.children[b"upload"]
714-
# file size is 67 Byte
715-
image_data = unhexlify(
716-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
717-
b"0000001f15c4890000000a49444154789c63000100000500010d"
718-
b"0a2db40000000049454e44ae426082"
719-
)
720698

721699
# Upload some media into the room
722700
response = self.helper.upload_media(
723-
upload_resource, image_data, tok=self.admin_user_tok, expect_code=200
701+
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200
724702
)
725703
# Extract media ID from the response
726704
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'

tests/rest/admin/test_statistics.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright 2020 Dirk Klimpel
2+
# Copyright 2021 The Matrix.org Foundation C.I.C.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -13,14 +14,14 @@
1314
# limitations under the License.
1415

1516
import json
16-
from binascii import unhexlify
1717
from typing import Any, Dict, List, Optional
1818

1919
import synapse.rest.admin
2020
from synapse.api.errors import Codes
2121
from synapse.rest.client import login
2222

2323
from tests import unittest
24+
from tests.test_utils import SMALL_PNG
2425

2526

2627
class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
@@ -468,16 +469,9 @@ def _create_media(self, user_token: str, number_media: int):
468469
"""
469470
upload_resource = self.media_repo.children[b"upload"]
470471
for _ in range(number_media):
471-
# file size is 67 Byte
472-
image_data = unhexlify(
473-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
474-
b"0000001f15c4890000000a49444154789c63000100000500010d"
475-
b"0a2db40000000049454e44ae426082"
476-
)
477-
478472
# Upload some media into the room
479473
self.helper.upload_media(
480-
upload_resource, image_data, tok=user_token, expect_code=200
474+
upload_resource, SMALL_PNG, tok=user_token, expect_code=200
481475
)
482476

483477
def _check_fields(self, content: List[Dict[str, Any]]):

tests/rest/admin/test_user.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 New Vector Ltd
1+
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@
3333

3434
from tests import unittest
3535
from tests.server import FakeSite, make_request
36-
from tests.test_utils import make_awaitable
36+
from tests.test_utils import SMALL_PNG, make_awaitable
3737
from tests.unittest import override_config
3838

3939

@@ -2835,11 +2835,7 @@ def test_order_by(self):
28352835
other_user_tok = self.login("user", "pass")
28362836

28372837
# Resolution: 1×1, MIME type: image/png, Extension: png, Size: 67 B
2838-
image_data1 = unhexlify(
2839-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
2840-
b"0000001f15c4890000000a49444154789c63000100000500010d"
2841-
b"0a2db40000000049454e44ae426082"
2842-
)
2838+
image_data1 = SMALL_PNG
28432839
# Resolution: 1×1, MIME type: image/gif, Extension: gif, Size: 35 B
28442840
image_data2 = unhexlify(
28452841
b"47494638376101000100800100000000"
@@ -2943,14 +2939,7 @@ def _create_media_for_user(self, user_token: str, number_media: int) -> List[str
29432939
"""
29442940
media_ids = []
29452941
for _ in range(number_media):
2946-
# file size is 67 Byte
2947-
image_data = unhexlify(
2948-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
2949-
b"0000001f15c4890000000a49444154789c63000100000500010d"
2950-
b"0a2db40000000049454e44ae426082"
2951-
)
2952-
2953-
media_ids.append(self._create_media_and_access(user_token, image_data))
2942+
media_ids.append(self._create_media_and_access(user_token, SMALL_PNG))
29542943

29552944
return media_ids
29562945

tests/rest/media/v1/test_media_storage.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 New Vector Ltd
1+
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838

3939
from tests import unittest
4040
from tests.server import FakeSite, make_request
41+
from tests.test_utils import SMALL_PNG
4142
from tests.utils import default_config
4243

4344

@@ -134,11 +135,7 @@ class _TestImage:
134135
# smoll png
135136
(
136137
_TestImage(
137-
unhexlify(
138-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
139-
b"0000001f15c4890000000a49444154789c63000100000500010d"
140-
b"0a2db40000000049454e44ae426082"
141-
),
138+
SMALL_PNG,
142139
b"image/png",
143140
b".png",
144141
unhexlify(
@@ -593,15 +590,8 @@ def default_config(self):
593590

594591
def test_upload_innocent(self):
595592
"""Attempt to upload some innocent data that should be allowed."""
596-
597-
image_data = unhexlify(
598-
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
599-
b"0000001f15c4890000000a49444154789c63000100000500010d"
600-
b"0a2db40000000049454e44ae426082"
601-
)
602-
603593
self.helper.upload_media(
604-
self.upload_resource, image_data, tok=self.tok, expect_code=200
594+
self.upload_resource, SMALL_PNG, tok=self.tok, expect_code=200
605595
)
606596

607597
def test_upload_ban(self):

tests/test_utils/__init__.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Copyright 2019 New Vector Ltd
2-
# Copyright 2020 The Matrix.org Foundation C.I.C
1+
# Copyright 2019-2021 The Matrix.org Foundation C.I.C.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
54
# you may not use this file except in compliance with the License.
@@ -19,6 +18,7 @@
1918
import sys
2019
import warnings
2120
from asyncio import Future
21+
from binascii import unhexlify
2222
from typing import Any, Awaitable, Callable, TypeVar
2323
from unittest.mock import Mock
2424

@@ -117,3 +117,13 @@ class FakeResponse:
117117
def deliverBody(self, protocol):
118118
protocol.dataReceived(self.body)
119119
protocol.connectionLost(Failure(ResponseDone()))
120+
121+
122+
# A small image used in some tests.
123+
#
124+
# Resolution: 1×1, MIME type: image/png, Extension: png, Size: 67 B
125+
SMALL_PNG = unhexlify(
126+
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
127+
b"0000001f15c4890000000a49444154789c63000100000500010d"
128+
b"0a2db40000000049454e44ae426082"
129+
)

0 commit comments

Comments
 (0)