feat(qq): prefer file_data for local uploads
Some checks failed
Test Suite / test (3.12) (push) Has been cancelled
Test Suite / test (3.13) (push) Has been cancelled
Test Suite / test (3.11) (push) Has been cancelled

This commit is contained in:
Hua
2026-03-20 08:39:14 +08:00
parent e910769a9e
commit 73af8c574e
4 changed files with 194 additions and 27 deletions

View File

@@ -1,4 +1,5 @@
import os
from base64 import b64encode
from types import SimpleNamespace
import pytest
@@ -15,6 +16,24 @@ class _FakeApi:
self.group_calls: list[dict] = []
self.c2c_file_calls: list[dict] = []
self.group_file_calls: list[dict] = []
self.raw_file_upload_calls: list[dict] = []
self.raise_on_raw_file_upload = False
self._http = SimpleNamespace(request=self._request)
async def _request(self, route, json=None, **kwargs) -> dict:
if self.raise_on_raw_file_upload:
raise RuntimeError("raw upload failed")
self.raw_file_upload_calls.append(
{
"method": route.method,
"path": route.path,
"params": route.parameters,
"json": json,
}
)
if "/groups/" in route.path:
return {"file_info": "group-file-info", "file_uuid": "group-file", "ttl": 60}
return {"file_info": "c2c-file-info", "file_uuid": "c2c-file", "ttl": 60}
async def post_c2c_message(self, **kwargs) -> None:
self.c2c_calls.append(kwargs)
@@ -210,14 +229,20 @@ async def test_send_local_media_under_public_dir_uses_c2c_file_api(
)
)
assert channel._client.api.c2c_file_calls == [
assert channel._client.api.raw_file_upload_calls == [
{
"openid": "user123",
"file_type": 1,
"url": "https://files.example.com/public/qq/demo.png",
"srv_send_msg": False,
"method": "POST",
"path": "/v2/users/{openid}/files",
"params": {"openid": "user123"},
"json": {
"file_type": 1,
"url": "https://files.example.com/public/qq/demo.png",
"file_data": b64encode(b"fake-png").decode("ascii"),
"srv_send_msg": False,
},
}
]
assert channel._client.api.c2c_file_calls == []
assert channel._client.api.c2c_calls == [
{
"openid": "user123",
@@ -272,14 +297,20 @@ async def test_send_local_media_from_out_auto_links_into_public_then_uses_c2c_fi
assert published[0].name.startswith("outside-")
assert published[0].suffix == ".png"
assert os.stat(source).st_ino == os.stat(published[0]).st_ino
assert channel._client.api.c2c_file_calls == [
assert channel._client.api.raw_file_upload_calls == [
{
"openid": "user123",
"file_type": 1,
"url": f"https://files.example.com/public/qq/{published[0].name}",
"srv_send_msg": False,
"method": "POST",
"path": "/v2/users/{openid}/files",
"params": {"openid": "user123"},
"json": {
"file_type": 1,
"url": f"https://files.example.com/public/qq/{published[0].name}",
"file_data": b64encode(b"\x89PNG\r\n\x1a\nfake-png").decode("ascii"),
"srv_send_msg": False,
},
}
]
assert channel._client.api.c2c_file_calls == []
assert channel._client.api.c2c_calls == [
{
"openid": "user123",
@@ -342,6 +373,64 @@ async def test_send_local_media_outside_public_and_out_falls_back_to_text_notice
]
@pytest.mark.asyncio
async def test_send_local_media_falls_back_to_url_only_upload_when_file_data_upload_fails(
monkeypatch,
tmp_path,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
public_dir = workspace / "public" / "qq"
public_dir.mkdir(parents=True)
source = public_dir / "demo.png"
source.write_bytes(b"fake-png")
channel = QQChannel(
QQConfig(
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/public/qq",
media_public_dir="public/qq",
media_ttl_seconds=0,
),
MessageBus(),
workspace=workspace,
)
channel._client = _FakeClient()
channel._client.api.raise_on_raw_file_upload = True
monkeypatch.setattr("nanobot.channels.qq.validate_url_target", lambda url: (True, ""))
await channel.send(
OutboundMessage(
channel="qq",
chat_id="user123",
content="hello",
media=[str(source)],
metadata={"message_id": "msg1"},
)
)
assert channel._client.api.c2c_file_calls == [
{
"openid": "user123",
"file_type": 1,
"url": "https://files.example.com/public/qq/demo.png",
"srv_send_msg": False,
}
]
assert channel._client.api.c2c_calls == [
{
"openid": "user123",
"msg_type": 7,
"content": "hello",
"media": {"file_info": "c2c-file-info", "file_uuid": "c2c-file", "ttl": 60},
"msg_id": "msg1",
"msg_seq": 2,
}
]
@pytest.mark.asyncio
async def test_send_local_media_symlink_to_outside_public_dir_is_rejected(
monkeypatch,