signal-bot/tests/test_fake_groups.py
2026-08-29 15:30:08 +02:00

69 lines
2.4 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""The fake server's group endpoints, including the join the bot self-heals with."""
from __future__ import annotations
import pytest
from fastapi.testclient import TestClient
from fake_signal.server import create_app
from fake_signal.state import State
NUMBER = "+15550001234"
GROUP = "group.first=="
INTERNAL = "first=="
@pytest.fixture
def state() -> State:
return State(base_url="http://testserver", accounts=[NUMBER])
@pytest.fixture
def client(state: State) -> TestClient:
return TestClient(create_app(state))
def _entry(group_id: str, *, member: bool = True, joinable: bool = True) -> dict[str, object]:
return {
"id": group_id,
"internal_id": INTERNAL,
"name": "g",
"member": member,
"joinable": joinable,
}
def test_groups_require_a_registered_account(client: TestClient) -> None:
assert client.get("/v1/groups/+15559999999").status_code == 400
def test_adding_groups_keeps_the_existing_ones(client: TestClient) -> None:
"""Replacing instead of adding would delete state a later scenario needs."""
client.post("/_control/groups", json=[_entry(GROUP)])
client.post("/_control/groups/add", json=[_entry("group.second==")])
listed = client.get(f"/v1/groups/{NUMBER}").json()
assert [g["id"] for g in listed] == [GROUP, "group.second=="]
def test_adding_a_group_twice_does_not_duplicate_it(client: TestClient) -> None:
client.post("/_control/groups", json=[_entry(GROUP)])
assert client.post("/_control/groups/add", json=[_entry(GROUP)]).json() == {"groups": 1}
def test_join_requires_a_registered_account(client: TestClient) -> None:
assert client.post(f"/v1/groups/+15559999999/{GROUP}/join").status_code == 400
def test_join_turns_an_invitation_into_membership(client: TestClient) -> None:
client.post("/_control/groups", json=[_entry(GROUP, member=False)])
assert client.post(f"/v1/groups/{NUMBER}/{GROUP}/join").status_code == 201
assert client.get(f"/v1/groups/{NUMBER}").json()[0]["member"] is True
def test_join_answers_201_even_when_it_changed_nothing(client: TestClient) -> None:
"""The real endpoint does too, which is why the bot re-reads the listing."""
client.post("/_control/groups", json=[_entry(GROUP, member=False, joinable=False)])
assert client.post(f"/v1/groups/{NUMBER}/{GROUP}/join").status_code == 201
assert client.get(f"/v1/groups/{NUMBER}").json()[0]["member"] is False