fix(media): bound attachment and request resources
This commit is contained in:
1 parent
2065fe2acd
commit
a105be4822
5 files changed
+110
-4
No files matched your search
@@ -1,8 +1,11 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from app.belki.image_host import ImageHostClient, ImageHostError
|
||||
from app.main import MAX_JSON_BODY_BYTES, detect_raster_image_type, safe_upload_name
|
||||
from app.belki.image_host import MAX_MIRRORED_IMAGE_BYTES, ImageHostClient, ImageHostError
|
||||
from app.main import (MAX_CONCURRENT_REQUESTS, MAX_JSON_BODY_BYTES, REQUEST_READ_TIMEOUT_SECONDS,
|
||||
BelkiHTTPServer, Handler, _ISSUED_WEB_UPLOAD_URLS, detect_raster_image_type,
|
||||
safe_upload_name, validate_web_attachments)
|
||||
from app.belki.api import ApiError
|
||||
|
||||
|
||||
class ImageHostClientTests(unittest.TestCase):
|
||||
@@ -15,6 +18,24 @@ class ImageHostClientTests(unittest.TestCase):
|
||||
self.assertEqual(safe_upload_name("", "image/webp"), "screenshot.webp")
|
||||
self.assertEqual(MAX_JSON_BODY_BYTES, 20 * 1024 * 1024)
|
||||
|
||||
def test_web_attachments_must_come_from_image_host_upload(self) -> None:
|
||||
_ISSUED_WEB_UPLOAD_URLS.clear()
|
||||
_ISSUED_WEB_UPLOAD_URLS.add("https://img.nyan404.ru/photo/known.jpg")
|
||||
self.assertEqual(
|
||||
validate_web_attachments([{"type": "photo", "url": "https://img.nyan404.ru/photo/known.jpg"}]),
|
||||
[{"type": "photo", "url": "https://img.nyan404.ru/photo/known.jpg"}],
|
||||
)
|
||||
with self.assertRaisesRegex(ApiError, "через форму Белок"):
|
||||
validate_web_attachments([{"type": "photo", "url": "http://127.0.0.1:8080/private"}])
|
||||
|
||||
def test_http_server_limits_read_time_and_concurrent_requests(self) -> None:
|
||||
server = BelkiHTTPServer(("127.0.0.1", 0), Handler)
|
||||
try:
|
||||
self.assertEqual(REQUEST_READ_TIMEOUT_SECONDS, 15)
|
||||
self.assertEqual(MAX_CONCURRENT_REQUESTS, 32)
|
||||
finally:
|
||||
server.server_close()
|
||||
|
||||
def test_default_client_targets_belki_image_host(self) -> None:
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
self.assertEqual(ImageHostClient().api_url, "https://nyan.nyan404.ru")
|
||||
@@ -36,6 +57,27 @@ class ImageHostClientTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(ImageHostError, "image host upload failed"):
|
||||
client.upload_bytes(b"image", "screenshot.jpg")
|
||||
|
||||
def test_mirror_stops_before_reading_an_oversized_vk_image(self) -> None:
|
||||
client = ImageHostClient(api_url="https://api.example.test")
|
||||
response = MagicMock()
|
||||
response.__enter__.return_value = response
|
||||
response.headers.get.side_effect = lambda name: str(MAX_MIRRORED_IMAGE_BYTES + 1) if name == "Content-Length" else None
|
||||
with patch("app.belki.image_host.urlopen", return_value=response):
|
||||
with self.assertRaisesRegex(ImageHostError, "too large"):
|
||||
client.mirror({"url": "https://vk.example.test/photo.jpg"})
|
||||
response.read.assert_not_called()
|
||||
|
||||
def test_mirror_reads_at_most_the_configured_limit_plus_sentinel(self) -> None:
|
||||
client = ImageHostClient(api_url="https://api.example.test")
|
||||
response = MagicMock()
|
||||
response.__enter__.return_value = response
|
||||
response.headers.get.return_value = None
|
||||
response.headers.get_content_type.return_value = "image/jpeg"
|
||||
response.read.return_value = b"image"
|
||||
with patch("app.belki.image_host.urlopen", return_value=response), patch.object(client, "upload_bytes", return_value="https://img.example.test/a.jpg"):
|
||||
client.mirror({"url": "https://vk.example.test/photo.jpg", "owner_id": 1, "id": 2})
|
||||
response.read.assert_called_once_with(MAX_MIRRORED_IMAGE_BYTES + 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user