|
21 | 21 | from twisted.test.proto_helpers import AccumulatingProtocol
|
22 | 22 |
|
23 | 23 | from synapse.config.oembed import OEmbedEndpointConfig
|
| 24 | +from synapse.util.stringutils import parse_and_validate_mxc_uri |
24 | 25 |
|
25 | 26 | from tests import unittest
|
26 | 27 | from tests.server import FakeTransport
|
@@ -721,3 +722,132 @@ def test_oembed_format(self):
|
721 | 722 | "og:description": "Content Preview",
|
722 | 723 | },
|
723 | 724 | )
|
| 725 | + |
| 726 | + def _download_image(self): |
| 727 | + """Downloads an image into the URL cache. |
| 728 | +
|
| 729 | + Returns: |
| 730 | + A (host, media_id) tuple representing the MXC URI of the image. |
| 731 | + """ |
| 732 | + self.lookups["cdn.twitter.com"] = [(IPv4Address, "10.1.2.3")] |
| 733 | + |
| 734 | + channel = self.make_request( |
| 735 | + "GET", |
| 736 | + "preview_url?url=http://cdn.twitter.com/matrixdotorg", |
| 737 | + shorthand=False, |
| 738 | + await_result=False, |
| 739 | + ) |
| 740 | + self.pump() |
| 741 | + |
| 742 | + client = self.reactor.tcpClients[0][2].buildProtocol(None) |
| 743 | + server = AccumulatingProtocol() |
| 744 | + server.makeConnection(FakeTransport(client, self.reactor)) |
| 745 | + client.makeConnection(FakeTransport(server, self.reactor)) |
| 746 | + client.dataReceived( |
| 747 | + b"HTTP/1.0 200 OK\r\nContent-Length: %d\r\nContent-Type: image/png\r\n\r\n" |
| 748 | + % (len(SMALL_PNG),) |
| 749 | + + SMALL_PNG |
| 750 | + ) |
| 751 | + |
| 752 | + self.pump() |
| 753 | + self.assertEqual(channel.code, 200) |
| 754 | + body = channel.json_body |
| 755 | + mxc_uri = body["og:image"] |
| 756 | + host, _port, media_id = parse_and_validate_mxc_uri(mxc_uri) |
| 757 | + self.assertIsNone(_port) |
| 758 | + return host, media_id |
| 759 | + |
| 760 | + def test_storage_providers_exclude_files(self): |
| 761 | + """Test that files are not stored in or fetched from storage providers.""" |
| 762 | + host, media_id = self._download_image() |
| 763 | + |
| 764 | + rel_file_path = self.preview_url.filepaths.url_cache_filepath_rel(media_id) |
| 765 | + media_store_path = os.path.join(self.media_store_path, rel_file_path) |
| 766 | + storage_provider_path = os.path.join(self.storage_path, rel_file_path) |
| 767 | + |
| 768 | + # Check storage |
| 769 | + self.assertTrue(os.path.isfile(media_store_path)) |
| 770 | + self.assertFalse( |
| 771 | + os.path.isfile(storage_provider_path), |
| 772 | + "URL cache file was unexpectedly stored in a storage provider", |
| 773 | + ) |
| 774 | + |
| 775 | + # Check fetching |
| 776 | + channel = self.make_request( |
| 777 | + "GET", |
| 778 | + f"download/{host}/{media_id}", |
| 779 | + shorthand=False, |
| 780 | + await_result=False, |
| 781 | + ) |
| 782 | + self.pump() |
| 783 | + self.assertEqual(channel.code, 200) |
| 784 | + |
| 785 | + # Move cached file into the storage provider |
| 786 | + os.makedirs(os.path.dirname(storage_provider_path), exist_ok=True) |
| 787 | + os.rename(media_store_path, storage_provider_path) |
| 788 | + |
| 789 | + channel = self.make_request( |
| 790 | + "GET", |
| 791 | + f"download/{host}/{media_id}", |
| 792 | + shorthand=False, |
| 793 | + await_result=False, |
| 794 | + ) |
| 795 | + self.pump() |
| 796 | + self.assertEqual( |
| 797 | + channel.code, |
| 798 | + 404, |
| 799 | + "URL cache file was unexpectedly retrieved from a storage provider", |
| 800 | + ) |
| 801 | + |
| 802 | + def test_storage_providers_exclude_thumbnails(self): |
| 803 | + """Test that thumbnails are not stored in or fetched from storage providers.""" |
| 804 | + host, media_id = self._download_image() |
| 805 | + |
| 806 | + rel_thumbnail_path = ( |
| 807 | + self.preview_url.filepaths.url_cache_thumbnail_directory_rel(media_id) |
| 808 | + ) |
| 809 | + media_store_thumbnail_path = os.path.join( |
| 810 | + self.media_store_path, rel_thumbnail_path |
| 811 | + ) |
| 812 | + storage_provider_thumbnail_path = os.path.join( |
| 813 | + self.storage_path, rel_thumbnail_path |
| 814 | + ) |
| 815 | + |
| 816 | + # Check storage |
| 817 | + self.assertTrue(os.path.isdir(media_store_thumbnail_path)) |
| 818 | + self.assertFalse( |
| 819 | + os.path.isdir(storage_provider_thumbnail_path), |
| 820 | + "URL cache thumbnails were unexpectedly stored in a storage provider", |
| 821 | + ) |
| 822 | + |
| 823 | + # Check fetching |
| 824 | + channel = self.make_request( |
| 825 | + "GET", |
| 826 | + f"thumbnail/{host}/{media_id}?width=32&height=32&method=scale", |
| 827 | + shorthand=False, |
| 828 | + await_result=False, |
| 829 | + ) |
| 830 | + self.pump() |
| 831 | + self.assertEqual(channel.code, 200) |
| 832 | + |
| 833 | + # Remove the original, otherwise thumbnails will regenerate |
| 834 | + rel_file_path = self.preview_url.filepaths.url_cache_filepath_rel(media_id) |
| 835 | + media_store_path = os.path.join(self.media_store_path, rel_file_path) |
| 836 | + os.remove(media_store_path) |
| 837 | + |
| 838 | + # Move cached thumbnails into the storage provider |
| 839 | + os.makedirs(os.path.dirname(storage_provider_thumbnail_path), exist_ok=True) |
| 840 | + os.rename(media_store_thumbnail_path, storage_provider_thumbnail_path) |
| 841 | + |
| 842 | + channel = self.make_request( |
| 843 | + "GET", |
| 844 | + f"thumbnail/{host}/{media_id}?width=32&height=32&method=scale", |
| 845 | + shorthand=False, |
| 846 | + await_result=False, |
| 847 | + ) |
| 848 | + self.pump() |
| 849 | + self.assertEqual( |
| 850 | + channel.code, |
| 851 | + 404, |
| 852 | + "URL cache thumbnail was unexpectedly retrieved from a storage provider", |
| 853 | + ) |
0 commit comments