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

Create a constant for a small png image in tests. #10834

Merged
merged 2 commits into from
Sep 16, 2021
Merged
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
1 change: 1 addition & 0 deletions changelog.d/10834.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Factor out PNG image data to a constant to be used in several tests.
18 changes: 6 additions & 12 deletions tests/replication/test_multi_media_repo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 The Matrix.org Foundation C.I.C.
# Copyright 2020-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,6 @@
# limitations under the License.
import logging
import os
from binascii import unhexlify
from typing import Optional, Tuple

from twisted.internet.protocol import Factory
Expand All @@ -28,6 +27,7 @@
from tests.http import TestServerTLSConnectionFactory, get_test_ca_cert_file
from tests.replication._base import BaseMultiWorkerStreamTestCase
from tests.server import FakeChannel, FakeSite, FakeTransport, make_request
from tests.test_utils import SMALL_PNG

logger = logging.getLogger(__name__)

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

png_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

request1.setResponseCode(200)
request1.responseHeaders.setRawHeaders(b"Content-Type", [b"image/png"])
request1.write(png_data)
request1.write(SMALL_PNG)
request1.finish()

self.pump(0.1)

self.assertEqual(channel1.code, 200, channel1.result["body"])
self.assertEqual(channel1.result["body"], png_data)
self.assertEqual(channel1.result["body"], SMALL_PNG)

request2.setResponseCode(200)
request2.responseHeaders.setRawHeaders(b"Content-Type", [b"image/png"])
request2.write(png_data)
request2.write(SMALL_PNG)
request2.finish()

self.pump(0.1)

self.assertEqual(channel2.code, 200, channel2.result["body"])
self.assertEqual(channel2.result["body"], png_data)
self.assertEqual(channel2.result["body"], SMALL_PNG)

# We expect only three new thumbnails to have been persisted.
self.assertEqual(start_count + 3, self._count_remote_thumbnails())
Expand Down
23 changes: 9 additions & 14 deletions tests/rest/admin/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 New Vector Ltd
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
import json
import os
import urllib.parse
from binascii import unhexlify
from unittest.mock import Mock

from twisted.internet.defer import Deferred
Expand All @@ -28,6 +27,7 @@

from tests import unittest
from tests.server import FakeSite, make_request
from tests.test_utils import SMALL_PNG


class VersionTestCase(unittest.HomeserverTestCase):
Expand Down Expand Up @@ -150,11 +150,6 @@ def prepare(self, reactor, clock, hs):
self.media_repo = hs.get_media_repository_resource()
self.download_resource = self.media_repo.children[b"download"]
self.upload_resource = self.media_repo.children[b"upload"]
self.image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

def make_homeserver(self, reactor, clock):

Expand Down Expand Up @@ -266,7 +261,7 @@ def test_quarantine_media_by_id(self):

# Upload some media into the room
response = self.helper.upload_media(
self.upload_resource, self.image_data, tok=admin_user_tok
self.upload_resource, SMALL_PNG, tok=admin_user_tok
)

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

# Upload some media
response_1 = self.helper.upload_media(
self.upload_resource, self.image_data, tok=non_admin_user_tok
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
)
response_2 = self.helper.upload_media(
self.upload_resource, self.image_data, tok=non_admin_user_tok
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
)

# Extract mxcs
Expand Down Expand Up @@ -381,10 +376,10 @@ def test_quarantine_all_media_by_user(self):

# Upload some media
response_1 = self.helper.upload_media(
self.upload_resource, self.image_data, tok=non_admin_user_tok
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
)
response_2 = self.helper.upload_media(
self.upload_resource, self.image_data, tok=non_admin_user_tok
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
)

# Extract media IDs
Expand Down Expand Up @@ -421,10 +416,10 @@ def test_cannot_quarantine_safe_media(self):

# Upload some media
response_1 = self.helper.upload_media(
self.upload_resource, self.image_data, tok=non_admin_user_tok
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
)
response_2 = self.helper.upload_media(
self.upload_resource, self.image_data, tok=non_admin_user_tok
self.upload_resource, SMALL_PNG, tok=non_admin_user_tok
)

# Extract media IDs
Expand Down
34 changes: 6 additions & 28 deletions tests/rest/admin/test_media.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 Dirk Klimpel
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,7 +15,6 @@

import json
import os
from binascii import unhexlify

from parameterized import parameterized

Expand All @@ -25,6 +25,7 @@

from tests import unittest
from tests.server import FakeSite, make_request
from tests.test_utils import SMALL_PNG


class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
Expand Down Expand Up @@ -110,15 +111,10 @@ def test_delete_media(self):

download_resource = self.media_repo.children[b"download"]
upload_resource = self.media_repo.children[b"upload"]
image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

# Upload some media into the room
response = self.helper.upload_media(
upload_resource, image_data, tok=self.admin_user_tok, expect_code=200
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200
)
# Extract media ID from the response
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
Expand Down Expand Up @@ -504,16 +500,10 @@ def _create_media(self):
Create a media and return media_id and server_and_media_id
"""
upload_resource = self.media_repo.children[b"upload"]
# file size is 67 Byte
image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

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

# Create media
upload_resource = media_repo.children[b"upload"]
# file size is 67 Byte
image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

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

# Create media
upload_resource = media_repo.children[b"upload"]
# file size is 67 Byte
image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

# Upload some media into the room
response = self.helper.upload_media(
upload_resource, image_data, tok=self.admin_user_tok, expect_code=200
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200
)
# Extract media ID from the response
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
Expand Down
12 changes: 3 additions & 9 deletions tests/rest/admin/test_statistics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 Dirk Klimpel
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,14 +14,14 @@
# limitations under the License.

import json
from binascii import unhexlify
from typing import Any, Dict, List, Optional

import synapse.rest.admin
from synapse.api.errors import Codes
from synapse.rest.client import login

from tests import unittest
from tests.test_utils import SMALL_PNG


class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
Expand Down Expand Up @@ -468,16 +469,9 @@ def _create_media(self, user_token: str, number_media: int):
"""
upload_resource = self.media_repo.children[b"upload"]
for _ in range(number_media):
# file size is 67 Byte
image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

# Upload some media into the room
self.helper.upload_media(
upload_resource, image_data, tok=user_token, expect_code=200
upload_resource, SMALL_PNG, tok=user_token, expect_code=200
)

def _check_fields(self, content: List[Dict[str, Any]]):
Expand Down
19 changes: 4 additions & 15 deletions tests/rest/admin/test_user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 New Vector Ltd
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@

from tests import unittest
from tests.server import FakeSite, make_request
from tests.test_utils import make_awaitable
from tests.test_utils import SMALL_PNG, make_awaitable
from tests.unittest import override_config


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

# Resolution: 1×1, MIME type: image/png, Extension: png, Size: 67 B
image_data1 = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)
image_data1 = SMALL_PNG
# Resolution: 1×1, MIME type: image/gif, Extension: gif, Size: 35 B
image_data2 = unhexlify(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should mention that I checked other images in the tests and all of them are unique, so nothing to factor out.

b"47494638376101000100800100000000"
Expand Down Expand Up @@ -2943,14 +2939,7 @@ def _create_media_for_user(self, user_token: str, number_media: int) -> List[str
"""
media_ids = []
for _ in range(number_media):
# file size is 67 Byte
image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

media_ids.append(self._create_media_and_access(user_token, image_data))
media_ids.append(self._create_media_and_access(user_token, SMALL_PNG))

return media_ids

Expand Down
18 changes: 4 additions & 14 deletions tests/rest/media/v1/test_media_storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 New Vector Ltd
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,7 @@

from tests import unittest
from tests.server import FakeSite, make_request
from tests.test_utils import SMALL_PNG
from tests.utils import default_config


Expand Down Expand Up @@ -134,11 +135,7 @@ class _TestImage:
# smoll png
(
_TestImage(
unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
),
SMALL_PNG,
b"image/png",
b".png",
unhexlify(
Expand Down Expand Up @@ -593,15 +590,8 @@ def default_config(self):

def test_upload_innocent(self):
"""Attempt to upload some innocent data that should be allowed."""

image_data = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)

self.helper.upload_media(
self.upload_resource, image_data, tok=self.tok, expect_code=200
self.upload_resource, SMALL_PNG, tok=self.tok, expect_code=200
)

def test_upload_ban(self):
Expand Down
14 changes: 12 additions & 2 deletions tests/test_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Copyright 2019 New Vector Ltd
# Copyright 2020 The Matrix.org Foundation C.I.C
# Copyright 2019-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,6 +18,7 @@
import sys
import warnings
from asyncio import Future
from binascii import unhexlify
from typing import Any, Awaitable, Callable, TypeVar
from unittest.mock import Mock

Expand Down Expand Up @@ -117,3 +117,13 @@ class FakeResponse:
def deliverBody(self, protocol):
protocol.dataReceived(self.body)
protocol.connectionLost(Failure(ResponseDone()))


# A small image used in some tests.
#
# Resolution: 1×1, MIME type: image/png, Extension: png, Size: 67 B
SMALL_PNG = unhexlify(
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
b"0000001f15c4890000000a49444154789c63000100000500010d"
b"0a2db40000000049454e44ae426082"
)