84 lines
4.8 KiB
Python
84 lines
4.8 KiB
Python
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
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):
|
|
def test_upload_input_helpers_accept_only_supported_raster_signatures(self) -> None:
|
|
self.assertEqual(detect_raster_image_type(b"\x89PNG\r\n\x1a\ncontent"), "image/png")
|
|
self.assertEqual(detect_raster_image_type(b"\xff\xd8\xffcontent"), "image/jpeg")
|
|
self.assertEqual(detect_raster_image_type(b"RIFF\x00\x00\x00\x00WEBPcontent"), "image/webp")
|
|
self.assertIsNone(detect_raster_image_type(b"<svg xmlns='http://www.w3.org/2000/svg'>"))
|
|
self.assertEqual(safe_upload_name("../../report.png", "image/png"), "report.png")
|
|
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")
|
|
|
|
def test_initiate_identifies_the_belki_client(self) -> None:
|
|
client = ImageHostClient(api_url="https://api.example.test")
|
|
response = MagicMock()
|
|
response.__enter__.return_value = response
|
|
with patch("app.belki.image_host.urlopen", return_value=response) as urlopen:
|
|
with patch("app.belki.image_host.json.load", return_value={"files": []}):
|
|
client._request_json("/api/uploads/initiate", {"files": []})
|
|
request = urlopen.call_args.args[0]
|
|
self.assertEqual(request.get_header("User-agent"), "BelkiCalculator/1.0 (+https://cat.nyan404.ru/belki)")
|
|
|
|
def test_upload_wraps_presigned_put_failure(self) -> None:
|
|
client = ImageHostClient(api_url="https://api.example.test")
|
|
with patch.object(client, "_request_json", return_value={"files": [{"uploadUrl": "https://storage.example.test/upload"}]}):
|
|
with patch("app.belki.image_host.urlopen", side_effect=OSError("network unavailable")):
|
|
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()
|