Files
jobboard/tests/test_web_reach.py
T
WolfandClaude Opus 5.5 dafca5435e Show and set each board's reach on the Companies list, list the readers' declarations on Sources, and document filtering at the source
A company row says what a pull asks its board for; its editor sets the board's own reach (the agents', all roles, or countries and remote) through /api/sources/{id}/reach. /api/readers lists each reader's listing, text, filters, places and robots decision, folded under the Follow form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 10:03:41 +02:00

108 lines
4.4 KiB
Python

"""A board's own reach on the Companies list, and the readers' declarations
on the Sources page, through the JSON API. No network."""
from __future__ import annotations
from collections.abc import Iterator
from pathlib import Path
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.config import Config, ServerConfig
from app.main import create_app
from app.models import Gate
from app.storage import Store
@pytest.fixture
def app(tmp_path: Path) -> FastAPI:
return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
database=tmp_path / "roledesk.db"))
@pytest.fixture
def store(app: FastAPI) -> Store:
return app.state.store
@pytest.fixture
def client(app: FastAPI) -> Iterator[TestClient]:
with TestClient(app) as c:
yield c
@pytest.fixture
def bosch(store: Store) -> int:
"""A big SmartRecruiters board and an agent that wants Denmark or remote."""
store.save_agent("Wolf", gates=[Gate(field="country", values=["DK"], mode="any"),
Gate(field="remote", values=["remote"], mode="any")])
sub = store.add_subscription("ats", "smartrecruiters:BoschGroup", label="Bosch",
company="Bosch", board="smartrecruiters",
params={"note": "kept"})
store.add_company("Bosch")
store.state["ats:size:smartrecruiters:BoschGroup"] = "4830"
return sub.id
def _row(client: TestClient) -> dict[str, object]:
return next(c for c in client.get("/api/companies").json() if c["key"] == "bosch")
def test_a_company_row_says_what_its_board_is_asked_for(client: TestClient, bosch: int) -> None:
row = _row(client)
# SmartRecruiters cannot ask for remote, so only the country is asked.
assert (row["reach"], row["reach_params"]) == ("DK · agents", {})
def test_a_boards_own_reach_is_kept_in_its_params_and_wins(
client: TestClient, store: Store, bosch: int) -> None:
res = client.post(f"/api/sources/{bosch}/reach",
json={"mode": "own", "countries": ["de", "se"], "remote": True})
assert res.status_code == 200, res.text
assert res.json()["reach"] == "DE, SE · set here"
sub = store.subscription(bosch)
assert sub is not None
assert sub.params == {"note": "kept", "countries": ["DE", "SE"], "remote": True}
assert _row(client)["reach_params"] == {"countries": ["DE", "SE"], "remote": True}
res = client.post(f"/api/sources/{bosch}/reach", json={"mode": "all"})
assert res.json()["reach"] == "all roles · set here"
assert store.subscription(bosch).params == {"note": "kept", "reach": "all"} # type: ignore[union-attr]
res = client.post(f"/api/sources/{bosch}/reach", json={"mode": "agents"})
assert res.json()["reach"] == "DK · agents"
assert store.subscription(bosch).params == {"note": "kept"} # type: ignore[union-attr]
@pytest.mark.parametrize("body, status", [
({"mode": "own"}, 422),
({"mode": "own", "countries": ["XX"]}, 422),
({"mode": "own", "countries": ["EU"]}, 422),
({"mode": "nowhere"}, 422),
])
def test_a_reach_that_names_nothing_is_refused(
client: TestClient, bosch: int, body: dict[str, object], status: int) -> None:
assert client.post(f"/api/sources/{bosch}/reach", json=body).status_code == status
def test_a_search_or_an_unknown_board_has_no_reach(client: TestClient, store: Store) -> None:
search = store.add_subscription("search", "jobindex:x", label="X", board="jobindex",
params={"q": "python"})
assert client.post(f"/api/sources/{search.id}/reach", json={"mode": "all"}).status_code == 404
assert client.post("/api/sources/999/reach", json={"mode": "all"}).status_code == 404
def test_the_readers_are_listed_with_what_they_declare(client: TestClient) -> None:
found = {r["name"]: r for r in client.get("/api/readers").json()}
assert found["workday"] == {
"name": "workday", "label": "Workday", "scope": "board", "listing": "paged",
"text": "detail", "filters": {"country": "ids", "remote": "ids"}, "places": "many",
"robots": found["workday"]["robots"]}
assert found["workday"]["robots"].startswith("read:")
assert found["greenhouse"]["filters"] == {}
scopes = [r["scope"] for r in client.get("/api/readers").json()]
assert scopes == sorted(scopes) # boards first, then the searches
assert "search" in scopes