mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 18:17:02 +01:00
Today the bot was up, healthy and answering nothing for three hours. A host
firewall change (fixed in testsAndMisc 334c0cd9) cut every Docker container
off the internet; signal-cli lost Signal and reconnected in a loop from
15:32:50 onward. Nothing noticed:
- the container's own healthcheck answered 204
- /v1/groups answered 200 out of local state, so discovery saw no change
- the bot's WebSocket to the container stayed OPEN, so the transport's
reconnect logic never fired
- /healthz answered {"status": "ok"} the entire time
The only symptom was silence, which is also what a quiet evening looks like.
signal_bot/liveness.py asks the one question that cannot be answered from a
cache -- GET /v1/devices, which needs a live round trip -- but only after
SILENCE_SECONDS (120) with no frame of any kind, so a busy group costs
nothing and an idle one costs one request per window. Three consecutive
failures and /healthz answers 503 with the reason. Verified end to end in
scripts/e2e.sh against the real process: 503 after 84s with the fake's
connection cut, back to 200 on its own 30s after restoring it.
scripts/signal_api_watchdog.sh + a two-minute user timer do the recovery the
bot cannot do for itself, since a wedged bot cannot restart the container it
depends on. Two tiers, so the steady state costs no Signal round trip at
all: the bot's own /healthz first, the authenticated probe only when that is
not 200. It stops after 3 restarts in 6 hours and escalates instead -- a
deregistered account or a blocked network is not fixable by restarting, and
a restart loop would hide it. Its own bash suite stubs curl and docker and
drives every branch including the give-up cap.
signal_bot/trust.py answers the other half: TRUSTED_INVITERS decides who may
put this bot in a group. The gate sits in GroupDirectory._permitted, which
both _join_pending and _adopt already route through -- gating only invites
would gate the door nobody uses, since prime_profile.sh exists precisely so
that an add becomes a direct join. Evidence is either the group-update
envelope, which is the only frame naming an inviter, or a trusted identity
being a full member in the listing. A refusal is logged once, with that
group's members, so the operator can see which string to add.
verify_allowlist moved to membership.py to keep discovery.py under the
250-line gate; it was already the module that owns MembershipError.
Verified live, by stopping the container by hand and touching nothing else:
18:54:35 docker stop signal-api
18:56:36 liveness: failure 1 of 3 after 123s of silence
18:57:36 failure 3 of 3 -> /healthz answers 503 from 18:57:45
18:58:18 watchdog: probe failed (1/2)
19:00:28 watchdog: probe failed (2/2) -> docker restart signal-api
19:00:36 liveness: receive path recovered after 8 failed probe(s)
19:01:13 watchdog: signal-api is back
Six minutes, unattended, against three hours of silence this afternoon. All
four real groups are still adopted with TRUSTED_INVITERS=+48123456789 set,
and no group was refused.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KbjfWdAyHbCubac7wJKJZW
144 lines
5.8 KiB
Python
144 lines
5.8 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""Registration and group endpoints on the fake server."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from fake_signal.routes_account import CAPTCHA_REQUIRED, expected_code
|
|
from fake_signal.server import create_app
|
|
from fake_signal.state import State
|
|
|
|
NUMBER = "+15550001234"
|
|
|
|
|
|
@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_no_accounts_before_registration(client: TestClient) -> None:
|
|
assert client.get("/v1/accounts").json() == []
|
|
|
|
|
|
def test_register_without_captcha_is_refused(client: TestClient) -> None:
|
|
"""The exact failure the real container returns, so the script can match it."""
|
|
response = client.post(f"/v1/register/{NUMBER}", json={})
|
|
assert response.status_code == 400
|
|
assert "signalcaptchas.org" in response.json()["error"]
|
|
assert response.json()["error"] == CAPTCHA_REQUIRED
|
|
|
|
|
|
def test_register_with_captcha_starts_verification(client: TestClient, state: State) -> None:
|
|
response = client.post(f"/v1/register/{NUMBER}", json={"captcha": "signalcaptcha://x"})
|
|
assert response.status_code == 201
|
|
assert state.pending[NUMBER] == "001234"
|
|
|
|
|
|
def test_verify_completes_registration(client: TestClient) -> None:
|
|
client.post(f"/v1/register/{NUMBER}", json={"captcha": "signalcaptcha://x"})
|
|
code = expected_code(NUMBER)
|
|
assert client.post(f"/v1/register/{NUMBER}/verify/{code}").status_code == 201
|
|
assert client.get("/v1/accounts").json() == [NUMBER]
|
|
|
|
|
|
def test_verify_is_idempotent_on_the_account_list(client: TestClient) -> None:
|
|
for _ in range(2):
|
|
client.post(f"/v1/register/{NUMBER}", json={"captcha": "c"})
|
|
client.post(f"/v1/register/{NUMBER}/verify/{expected_code(NUMBER)}")
|
|
assert client.get("/v1/accounts").json() == [NUMBER]
|
|
|
|
|
|
def test_verify_rejects_a_wrong_code(client: TestClient) -> None:
|
|
client.post(f"/v1/register/{NUMBER}", json={"captcha": "c"})
|
|
response = client.post(f"/v1/register/{NUMBER}/verify/000000")
|
|
assert response.status_code == 400
|
|
assert "invalid verification code" in response.json()["error"]
|
|
|
|
|
|
def test_verify_without_a_pending_registration_is_refused(client: TestClient) -> None:
|
|
response = client.post(f"/v1/register/{NUMBER}/verify/001234")
|
|
assert response.status_code == 400
|
|
assert "no registration in progress" in response.json()["error"]
|
|
|
|
|
|
def test_groups_require_a_registered_account(client: TestClient) -> None:
|
|
response = client.get(f"/v1/groups/{NUMBER}")
|
|
assert response.status_code == 400
|
|
assert "not registered" in response.json()["error"]
|
|
|
|
|
|
def test_groups_are_listed_once_registered(client: TestClient, state: State) -> None:
|
|
state.accounts.append(NUMBER)
|
|
state.groups.extend([{"id": "gA==", "name": "Alpha"}, {"id": "gB==", "name": "Bravo"}])
|
|
body = client.get(f"/v1/groups/{NUMBER}").json()
|
|
assert [g["name"] for g in body] == ["Alpha", "Bravo"]
|
|
|
|
|
|
def test_expected_code_uses_the_last_six_digits() -> None:
|
|
assert expected_code("+48 123 456 789") == "456789"
|
|
|
|
|
|
def test_control_can_seed_groups(client: TestClient, state: State) -> None:
|
|
"""The picker in scripts/lib_signal.sh is rehearsed against this."""
|
|
state.accounts.append(NUMBER)
|
|
body = [{"id": "gA==", "name": "Alpha"}, {"id": "gB==", "name": "Bravo"}]
|
|
assert client.post("/_control/groups", json=body).json() == {"groups": 2}
|
|
assert client.get(f"/v1/groups/{NUMBER}").json() == body
|
|
|
|
|
|
def test_seeding_groups_replaces_the_previous_list(client: TestClient, state: State) -> None:
|
|
state.accounts.append(NUMBER)
|
|
client.post("/_control/groups", json=[{"id": "old==", "name": "Old"}])
|
|
client.post("/_control/groups", json=[{"id": "new==", "name": "New"}])
|
|
assert [g["id"] for g in client.get(f"/v1/groups/{NUMBER}").json()] == ["new=="]
|
|
|
|
|
|
def test_control_seeds_registered_accounts(client: TestClient) -> None:
|
|
"""/v1/groups answers 400 for an unknown account, so the fake needs this."""
|
|
assert client.get("/v1/groups/+15550000000").status_code == 400
|
|
assert client.post("/_control/accounts", json=["+15550000000"]).json() == {"accounts": 1}
|
|
assert client.get("/v1/groups/+15550000000").status_code == 200
|
|
|
|
|
|
def test_control_replaces_accounts_rather_than_appending(client: TestClient) -> None:
|
|
client.post("/_control/accounts", json=["+15550000001"])
|
|
client.post("/_control/accounts", json=["+15550000002"])
|
|
assert client.get("/v1/accounts").json() == ["+15550000002"]
|
|
|
|
|
|
def test_devices_needs_a_registered_account(client: TestClient) -> None:
|
|
response = client.get(f"/v1/devices/{NUMBER}")
|
|
assert response.status_code == 400
|
|
assert "not registered" in response.json()["error"]
|
|
|
|
|
|
def test_devices_answers_while_the_connection_is_alive(client: TestClient, state: State) -> None:
|
|
state.accounts.append(NUMBER)
|
|
response = client.get(f"/v1/devices/{NUMBER}")
|
|
assert response.status_code == 200
|
|
assert response.json()[0]["id"] == 1
|
|
|
|
|
|
def test_devices_reproduces_the_outage_when_the_connection_is_cut(
|
|
client: TestClient, state: State
|
|
) -> None:
|
|
"""The 2026-08-29 shape: local state fine, anything upstream 400s."""
|
|
state.accounts.append(NUMBER)
|
|
assert client.post("/_control/connection", json={"alive": False}).json() == {"alive": False}
|
|
|
|
response = client.get(f"/v1/devices/{NUMBER}")
|
|
assert response.status_code == 400
|
|
assert response.json()["error"] == "Failed to get linked devices: Closed unexpectedly"
|
|
# Local state keeps answering, which is what made this invisible.
|
|
assert client.get("/v1/accounts").status_code == 200
|
|
|
|
assert client.post("/_control/connection", json={"alive": True}).json() == {"alive": True}
|
|
assert client.get(f"/v1/devices/{NUMBER}").status_code == 200
|