signal-bot/tests/test_fake_account.py
Krzysztof kuhy Rudnicki 53e231ee14
Some checks are pending
CI / check (push) Waiting to run
feat: register the bot as its own Signal account, no phone app needed
Shelter cannot work on this phone, and it would not have helped anyway.

It cannot work: setting a Device Owner makes Android enable
no_add_managed_profile, no_add_private_profile and no_add_clone_profile on
every user, which kills Shelter, Island, Private Space and app cloning
alike. Verified on-device rather than inferred -- `pm create-user
--profileOf 0 --managed` returns "Cannot add user: no_add_managed_profile is
enabled". This is the Android 11 COMP deprecation, enforced in three places
in DevicePolicyManagerService and unchanged through Android 16.

It would not have helped: a second Signal *app* is not a second Signal
*account*. Every account needs its own phone number, so the second number
was always the real requirement -- and once you have one, signal-cli can
register it as a PRIMARY device with no phone app, no QR scan and no work
profile at all. That is strictly better than both the linked-device setup
and the Shelter plan: the bot cannot read personal DMs and never posts as
the account owner.

scripts/register_bot.sh drives that flow. It gates on signal-cli >= 0.14.7
rather than hoping: stable container tag 0.100 bundles 0.14.5, and on
2026-03-06 Signal made the post-quantum ratchet mandatory and de-registered
every account whose client lacked it -- a stale client is how a bot dies
silently months later. Verified that the recommended CI tag really does
bundle 0.14.7 instead of assuming it.

Rehearsed offline, not written blind. fake_signal grew /v1/accounts,
/v1/register/{number}, /v1/register/{number}/verify/{token} and
/v1/groups/{number}, so the whole runbook including every error path
(missing captcha, stray verify, wrong code, unregistered number, empty group
list, out-of-range and non-numeric picks) runs against the fake. The picker
was proven to resolve the group the user actually pointed at.

Prompts now read from $SIGNAL_TTY instead of /dev/tty directly. A hard-coded
/dev/tty made the interactive path untestable without a real terminal; the
seam is the same idea as the injected clock and transport on the Python side,
and is what let the picker be rehearsed at all.

live_smoke.sh and register_bot.sh share scripts/lib_signal.sh so the two
cannot drift, and shellcheck now runs with -x so the sourced lib is actually
linted from its callers.

201 tests, 100% branch coverage, all 23 gates green.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013yaBT45KhHqxTKVCTA87iy
2026-08-29 13:41:13 +02:00

101 lines
3.9 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=="]