"""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": "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), ({"mode": "all"}, 422), # every role is the company's tag now ]) 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": "agents"}).status_code == 404 assert client.post("/api/sources/999/reach", json={"mode": "agents"}).status_code == 404 #: Every state a job may be in, for a "was one queued at all" check that does #: not race a background worker draining it. _ANY_STATE = ("queued", "running", "done", "failed", "cancelled") def test_the_all_roles_tag_reads_the_wide_reach_and_on_queues_a_pull( client: TestClient, app: FastAPI, store: Store, bosch: int) -> None: queue = app.state.queue res = client.post("/api/companies/bosch/all-roles", json={"on": True}) assert res.status_code == 200, res.text assert res.json()["all_roles"] == 1 # On reopens roles, so it queues a pull as switching a board on does. assert queue.jobs.last("pull", states=_ANY_STATE) is not None row = _row(client) # Wolf requires no place, so the wide reach is everywhere. assert (row["all_roles"], row["reach"]) == (True, "all roles · company tag") # A board's own countries win over the tag. client.post(f"/api/sources/{bosch}/reach", json={"mode": "own", "countries": ["DE"]}) assert _row(client)["reach"] == "DE · set here" def test_turning_the_tag_off_queues_nothing( client: TestClient, app: FastAPI, store: Store, bosch: int) -> None: store.set_all_roles("bosch", True) res = client.post("/api/companies/bosch/all-roles", json={"on": False}) assert res.status_code == 200 and res.json()["all_roles"] == 0 assert app.state.queue.jobs.last("pull", states=_ANY_STATE) is None assert _row(client)["reach"] == "DK · agents" assert client.post("/api/companies/nobody/all-roles", json={"on": True}).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", "query": "one"}, "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