Include utility modules, shared channels, prompts, read-only skills, wardrobe memory and regression tests. Externalize legacy credentials and exclude private runtime state.
63 lines
2.3 KiB
Python
63 lines
2.3 KiB
Python
"""MyPixiv limited placeholder responses must never be delivered as artwork."""
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
BASE = Path(__file__).resolve().parents[1]
|
|
sys.path[:0] = [str(BASE / "bot-utility"), str(BASE)]
|
|
|
|
import pixiv
|
|
|
|
|
|
class FakeResponse:
|
|
def __init__(self, body):
|
|
self.status_code = 200
|
|
self.headers = {"Content-Type": "image/png"}
|
|
self.body = body
|
|
self.closed = False
|
|
def iter_content(self, size):
|
|
yield self.body[:3]
|
|
yield self.body[3:]
|
|
def close(self):
|
|
self.closed = True
|
|
|
|
|
|
class PixivRestrictedTests(unittest.IsolatedAsyncioTestCase):
|
|
async def test_placeholder_is_deleted_and_raises_restricted(self):
|
|
body = b"my-pixiv-limited-placeholder"
|
|
response = FakeResponse(body)
|
|
with tempfile.TemporaryDirectory() as directory, \
|
|
patch.object(pixiv, "PIXIV_CACHE_DIR", directory), \
|
|
patch.object(pixiv, "RESTRICTED_IMAGE_SHA256", {hashlib.sha256(body).hexdigest()}), \
|
|
patch("requests.get", return_value=response):
|
|
with self.assertRaises(pixiv.PixivRestrictedError):
|
|
pixiv.download_image(148292005)
|
|
self.assertEqual(list(Path(directory).iterdir()), [])
|
|
self.assertTrue(response.closed)
|
|
|
|
async def test_handler_returns_permission_error_without_image(self):
|
|
sender = AsyncMock(return_value=True)
|
|
with patch.object(pixiv, "_load_proxy_ports", return_value=[]), \
|
|
patch.object(pixiv, "download_image", side_effect=pixiv.PixivRestrictedError("limited")), \
|
|
patch("channel.send_message", new=sender):
|
|
await pixiv.handle_pixiv(None, "private", 10001, 1, ".pixiv 148292005")
|
|
self.assertEqual(sender.await_count, 2)
|
|
for call in sender.await_args_list:
|
|
content = call.args[3]
|
|
self.assertIsInstance(content, str)
|
|
self.assertIn("MyPixiv 限定", sender.await_args.args[3])
|
|
|
|
def test_known_placeholder_hash_is_registered(self):
|
|
self.assertIn(
|
|
"caa0bd60e0a0c28a4bd80b6b7024fb4956ffff7c536f4fe9d2d2588b660524c7",
|
|
pixiv.RESTRICTED_IMAGE_SHA256,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|