|
| 1 | +# Copyright 2023 New Vector Ltd |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from http import HTTPStatus |
| 15 | +from unittest.mock import patch |
| 16 | + |
| 17 | +from aiohttp.test_utils import AioHTTPTestCase |
| 18 | +from aiohttp.web_app import Application |
| 19 | +from multidict import CIMultiDict |
| 20 | + |
| 21 | +from matrix_content_scanner.httpserver import HTTPServer |
| 22 | +from matrix_content_scanner.utils.constants import ErrCode |
| 23 | +from matrix_content_scanner.utils.errors import ContentScannerRestError |
| 24 | +from tests.testutils import get_content_scanner |
| 25 | + |
| 26 | +SERVER_NAME = "test" |
| 27 | + |
| 28 | + |
| 29 | +class TestScanHandler(AioHTTPTestCase): |
| 30 | + def setUp(self) -> None: |
| 31 | + # Bypass well-known lookups. |
| 32 | + self.scanner = get_content_scanner( |
| 33 | + {"download": {"base_homeserver_url": "http://my-site.com"}} |
| 34 | + ) |
| 35 | + |
| 36 | + async def get_application(self) -> Application: |
| 37 | + return HTTPServer(self.scanner)._app |
| 38 | + |
| 39 | + async def test_media_not_found_on_remote_homeserver(self) -> None: |
| 40 | + """Missing media on the remote HS should be presented as a 404 to the client.""" |
| 41 | + patch_downloader = patch.object( |
| 42 | + self.scanner.file_downloader, |
| 43 | + "_get", |
| 44 | + return_value=(HTTPStatus.NOT_FOUND, b"", CIMultiDict()), |
| 45 | + ) |
| 46 | + |
| 47 | + with patch_downloader: |
| 48 | + async with self.client.get( |
| 49 | + f"/_matrix/media_proxy/unstable/download/{SERVER_NAME}/media-does-not-exist" |
| 50 | + ) as resp: |
| 51 | + self.assertEqual(resp.status, 404) |
| 52 | + body = await resp.json() |
| 53 | + self.assertEqual(body["reason"], "M_NOT_FOUND", body) |
| 54 | + |
| 55 | + async def test_remote_homeserver_unreachable(self) -> None: |
| 56 | + """An unreachable HS should be presented as a 502 to the client.""" |
| 57 | + patch_downloader = patch.object( |
| 58 | + self.scanner.file_downloader, |
| 59 | + "_get", |
| 60 | + side_effect=ContentScannerRestError( |
| 61 | + HTTPStatus.BAD_GATEWAY, |
| 62 | + ErrCode.REQUEST_FAILED, |
| 63 | + "dodgy network timeout :(((", |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + with patch_downloader: |
| 68 | + async with self.client.get( |
| 69 | + f"/_matrix/media_proxy/unstable/download/{SERVER_NAME}/media-does-not-exist" |
| 70 | + ) as resp: |
| 71 | + self.assertEqual(resp.status, 502) |
| 72 | + body = await resp.json() |
| 73 | + self.assertEqual(body["reason"], "MCS_MEDIA_REQUEST_FAILED", body) |
0 commit comments