mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 20:17:02 +01:00
Some checks are pending
CI / check (push) Waiting to run
Send a photo captioned "!add_photo alfa wakacje" in one of the album's own groups and it lands in the pack tagged with both, retrievable through !alfa and the new !losowe immediately -- no restart, and no tagger session on the PC. A label matching nobody becomes a custom tag, and !losowe is the only door to one, which is what keeps a live swap a *data* swap: it can never register a trigger or widen a command's groups. No typed label travels. Labels become pseudonym keys in a new registry arg_resolver, at the dispatcher, before the line that logs the command name -- so no handler, journal entry, command_uses row or reply body ever holds a word somebody typed. The reply is counts and pseudonyms: "dodano · p1: 4 · t1: 1". Stripping is exiftool with a *positive* check on the output, never an exit code. ffmpeg was tried first and fails silently on photos: given a JPEG with a real APP1 Exif segment it returned a byte-identical file, GPS intact, exit 0. The surviving-tag allowlist is per tag *name* rather than per group, because measurement showed a group-level "must be empty" rule rejects every format except JPEG -- a valid PNG always reports its IHDR, an MP4 some fifty QuickTime tags that are the container itself. Codec configuration (CompressorName, VendorID, the HEVC parameter set) is on the list by decision: CompressorName lives in the avc1 sample description and exiftool cannot write it at all, so the alternative was refusing every video and every iPhone HEIC. Location and identity tags are still refused, and the e2e run re-reads the stored files to prove a QuickTime geotag did not survive. .webm and .svg stay on the format allowlist and are stored *unstripped*: exiftool 13.55 can write neither, and its SVG reader does not even surface <metadata>. That is a deliberate, documented hole, not an oversight. Two things this had to defuse along the way: - personal_data_sources now reads custom.json, so "!add_photo kot <photo>" would have outlawed the word "kot" and refused every future commit, fixable only by hand-editing an album the committer cannot see. Minting now refuses a word that is already a command, and the gate subtracts the repo's own alias vocabulary from custom labels only -- curated people.json names are never excused. - signal_bot/external.py is now the single subprocess site in the repository, so ruff's S603 needs exactly one scoped per-file-ignore instead of one per module that shells out. A test asserts nothing else imports subprocess. A failed add changes nothing: media and the three manifests are written, the album is reloaded, and only a load that succeeds is published; a load that fails rolls all four back and keeps serving the pack that was already working, without raising -- Restart=always would turn a ConfigError into a loop. 977 tests, 100% branch coverage, no omits and no suppressions. scripts/e2e.sh gains 8 scenarios driven against the real exiftool on media generated at run time, and asserts the invariants a reply cannot: that a refused upload is not stored, that a message with no attachment leaves custom.json byte-identical, and that signal-cli's own copy of every ingested attachment is gone. Closes TODO-add-photo.md. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JqqE22jp94B1ypwxppdr1o
154 lines
6.3 KiB
Python
154 lines
6.3 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""The fake Signal server's own behaviour."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from fake_signal.server import create_app, default_state
|
|
from fake_signal.state import PIXEL_PNG, State
|
|
|
|
|
|
@pytest.fixture
|
|
def state() -> State:
|
|
return State(base_url="http://testserver")
|
|
|
|
|
|
@pytest.fixture
|
|
def client(state: State) -> TestClient:
|
|
return TestClient(create_app(state))
|
|
|
|
|
|
def test_about_reports_a_websocket_mode(client: TestClient) -> None:
|
|
body = client.get("/v1/about").json()
|
|
assert body["mode"] == "json-rpc"
|
|
|
|
|
|
def test_qrcodelink_returns_a_png(client: TestClient) -> None:
|
|
"""The real endpoint returns an image, which is why the runbook opens it."""
|
|
response = client.get("/v1/qrcodelink", params={"device_name": "bot"})
|
|
assert response.headers["content-type"] == "image/png"
|
|
assert response.headers["X-Device-Name"] == "bot"
|
|
assert response.content == PIXEL_PNG
|
|
|
|
|
|
def test_send_is_captured_and_mints_attachment_ids(client: TestClient, state: State) -> None:
|
|
client.post("/v2/send", json={"message": "hi", "recipients": ["g"]})
|
|
client.post("/v2/send", json={"base64_attachments": ["A"], "recipients": ["g"]})
|
|
assert len(state.sent) == 2
|
|
assert state.attachments == ["attach-2-0"]
|
|
|
|
|
|
def test_attachments_can_be_listed_and_deleted(client: TestClient) -> None:
|
|
client.post("/v2/send", json={"base64_attachments": ["A"], "recipients": ["g"]})
|
|
listed = client.get("/v1/attachments").json()
|
|
assert listed == ["attach-1-0"]
|
|
assert client.delete(f"/v1/attachments/{listed[0]}").status_code == 204
|
|
assert client.get("/v1/attachments").json() == []
|
|
|
|
|
|
def test_deleting_an_unknown_attachment_is_a_noop(client: TestClient) -> None:
|
|
assert client.delete("/v1/attachments/nope").status_code == 204
|
|
|
|
|
|
def test_reset_clears_captured_state(client: TestClient, state: State) -> None:
|
|
client.post("/v2/send", json={"base64_attachments": ["A"], "recipients": ["g"]})
|
|
client.post("/_control/reset")
|
|
assert state.sent == []
|
|
assert state.attachments == []
|
|
|
|
|
|
def test_websocket_subscribes_and_receives_injected_frames(client: TestClient) -> None:
|
|
with client.websocket_connect("/v1/receive/+15550000000") as socket:
|
|
assert socket.receive_json()["envelope"]["subscribed"] == "+15550000000"
|
|
assert client.get("/_control/ready").json() == {"sockets": 1}
|
|
assert client.post("/_control/inject", json={"raw": "FRAME"}).json() == {"delivered": 1}
|
|
assert socket.receive_text() == "FRAME"
|
|
|
|
|
|
def test_socket_is_forgotten_after_disconnect(client: TestClient) -> None:
|
|
with client.websocket_connect("/v1/receive/+1") as socket:
|
|
socket.receive_json()
|
|
assert client.get("/_control/ready").json() == {"sockets": 0}
|
|
|
|
|
|
def test_inject_with_no_listeners_delivers_nothing(client: TestClient) -> None:
|
|
assert client.post("/_control/inject", json={"raw": "x"}).json() == {"delivered": 0}
|
|
|
|
|
|
def test_fixture_apis_mirror_the_real_response_shapes(client: TestClient) -> None:
|
|
cat = client.get("/_fixtures/cat-api").json()
|
|
dog = client.get("/_fixtures/dog-api").json()
|
|
gecko = client.get("/_fixtures/gecko-api").json()
|
|
assert cat[0]["url"].endswith("/_fixtures/image.png")
|
|
assert dog["message"].endswith("/_fixtures/image.png")
|
|
assert gecko["results"][0]["photos"][0]["url"].endswith("/_fixtures/photos/square.png")
|
|
commons = client.get("/_fixtures/commons-api", params={"titles": "File:8-St.jpg"}).json()
|
|
page = commons["query"]["pages"][0]
|
|
# thumburl only, never url: the bot must ask for a bounded thumbnail.
|
|
# One committed portrait is 37 MB, which is not a Signal attachment.
|
|
assert page["imageinfo"][0]["thumburl"].endswith("/_fixtures/image.png")
|
|
assert "url" not in page["imageinfo"][0]
|
|
assert page["title"] == "File:8-St.jpg"
|
|
assert client.get("/_fixtures/image.png").content == PIXEL_PNG
|
|
# The square URL is deliberately absent: the bot must rewrite it.
|
|
assert client.get("/_fixtures/photos/large.png").content == PIXEL_PNG
|
|
assert client.get("/_fixtures/photos/square.png").status_code == 404
|
|
|
|
|
|
def test_create_app_makes_its_own_state_when_none_given() -> None:
|
|
with TestClient(create_app()) as client:
|
|
assert client.get("/v1/about").status_code == 200
|
|
|
|
|
|
def test_default_state_reads_the_port_from_the_environment(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setenv("FAKE_PORT", "9123")
|
|
assert default_state().base_url.endswith(":9123")
|
|
|
|
|
|
def test_control_sent_exposes_captured_payloads(client: TestClient) -> None:
|
|
client.post("/v2/send", json={"message": "hi", "recipients": ["R=="]})
|
|
assert client.get("/_control/sent").json() == [{"message": "hi", "recipients": ["R=="]}]
|
|
|
|
|
|
def test_an_inbound_attachment_can_be_stored_and_fetched(client: TestClient) -> None:
|
|
"""Without this the fake could name an attachment but never hand it over."""
|
|
payload = b"\xff\xd8\xffphoto"
|
|
stored = client.post(
|
|
"/_control/attachment",
|
|
json={"id": "att-1", "base64": base64.b64encode(payload).decode("ascii")},
|
|
)
|
|
assert stored.status_code == 200
|
|
assert client.get("/_control/attachments").json() == ["att-1"]
|
|
assert client.get("/v1/attachments/att-1").content == payload
|
|
assert client.get("/v1/attachments").json() == ["att-1"]
|
|
|
|
|
|
def test_fetching_an_attachment_that_is_not_there_is_a_404(client: TestClient) -> None:
|
|
assert client.get("/v1/attachments/nope").status_code == 404
|
|
|
|
|
|
def test_deleting_an_attachment_takes_its_bytes_too(client: TestClient) -> None:
|
|
""" "The second copy is gone" has to be assertable, not assumed."""
|
|
client.post(
|
|
"/_control/attachment",
|
|
json={"id": "att-1", "base64": base64.b64encode(b"\xff\xd8\xffx").decode("ascii")},
|
|
)
|
|
assert client.delete("/v1/attachments/att-1").status_code == 204
|
|
assert client.get("/_control/attachments").json() == []
|
|
assert client.get("/v1/attachments/att-1").status_code == 404
|
|
|
|
|
|
def test_uploading_the_same_id_twice_does_not_duplicate_the_listing(
|
|
client: TestClient,
|
|
) -> None:
|
|
body = {"id": "att-1", "base64": base64.b64encode(b"\xff\xd8\xffx").decode("ascii")}
|
|
client.post("/_control/attachment", json=body)
|
|
client.post("/_control/attachment", json=body)
|
|
assert client.get("/v1/attachments").json() == ["att-1"]
|