Read a role's own page once for a host with detail, and fail a board whose robots.txt disallows it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-09-29 17:05:44 +02:00
1 parent b02b72b4aa
commit 8b14d8575b
2 files changed
+578 -6

No files matched your search

+425
View File
@@ -0,0 +1,425 @@
"""The host modules of `plugins/atshosts/` and what `plugins/ats.py` does for
every host: the registry and its contract check, the fetch a host is handed,
the per-role detail reads, robots.txt, and Lever's EU instance. No network —
a fake host stands in where a behaviour belongs to no real host yet."""
from __future__ import annotations
import json
import sys
import types
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any
import httpx
import pytest
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from plugins import ats, atshosts # noqa: E402
from plugins.atshosts import lever # noqa: E402
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
# --------------------------------------------------------------- registry
def test_the_registry_finds_every_module_in_order_of_name() -> None:
names = sorted(p.stem for p in (ROOT / "plugins" / "atshosts").glob("*.py")
if not p.stem.startswith("_"))
assert list(atshosts.HOSTS) == names
assert set(names) >= {"greenhouse", "lever", "ashby", "workable", "smartrecruiters",
"recruitee", "personio", "teamtailor"}
assert all(not atshosts.problems(module, name) for name, module in atshosts.HOSTS.items())
# The Sources page's select and the plugin's description follow the modules.
assert {name: module.LABEL for name, module in atshosts.HOSTS.items()} == ats.HOSTS
assert "Teamtailor" in ats.ATSSource.description
def _package(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, name: str,
modules: dict[str, str]) -> list[str]:
package = tmp_path / name
package.mkdir()
(package / "__init__.py").write_text("")
for module, source in modules.items():
(package / f"{module}.py").write_text(source)
monkeypatch.syspath_prepend(str(tmp_path))
return [str(package)]
_GOOD = """
NAME = "{name}"
LABEL = "Good"
PATTERNS = (r"good\\.example/([a-z]+)",)
def slug_ok(slug): return True
def allowed(slug): return lambda h: h == "good.example"
def careers_url(slug): return "https://good.example/" + slug
def feed_url(slug): return "https://good.example/" + slug + ".json"
def parse(body, slug): return []
"""
def test_the_registry_loads_a_package_by_name_and_skips_private_modules(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
path = _package(tmp_path, monkeypatch, "hostsok", {
"zeta": _GOOD.format(name="zeta"), "alpha": _GOOD.format(name="alpha"),
"_helpers": "X = 1\n"})
assert list(atshosts.load(path, "hostsok")) == ["alpha", "zeta"]
def test_the_registry_refuses_a_module_missing_a_required_name(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
broken = _GOOD.format(name="broken").replace("def careers_url", "def not_careers_url")
path = _package(tmp_path, monkeypatch, "hostsbad", {"broken": broken})
with pytest.raises(ImportError, match=r"hostsbad\.broken .*no careers_url"):
atshosts.load(path, "hostsbad")
def test_the_contract_check_names_each_problem() -> None:
module = types.ModuleType("odd")
module.NAME, module.LABEL, module.PATTERNS = "other", "", ("(",)
module.slug_ok = module.allowed = module.careers_url = lambda slug: ""
module.page = lambda get, slug, cursor: None
module.probe_ok = lambda body: True
found = atshosts.problems(module, "odd")
assert "NAME is 'other', not the module's name 'odd'" in found
assert "LABEL is not a non-empty string" in found
assert any(p.startswith("PATTERNS has '('") for p in found)
assert "page without rows" in found
assert "probe_ok without probe_url" in found
# ------------------------------------------------------------ the fetch
def _client(handler: Callable[[httpx.Request], httpx.Response]) -> httpx.Client:
return httpx.Client(transport=httpx.MockTransport(handler))
def test_get_sends_extra_headers_on_top_of_its_own() -> None:
seen: list[httpx.Headers] = []
def handler(request: httpx.Request) -> httpx.Response:
seen.append(request.headers)
return httpx.Response(200, content=b"ok")
with _client(handler) as client:
get = ats._getter(client, "lever", "acme")
assert get("https://api.lever.co/v0/postings/acme",
headers={"Accept": "text/plain", "X-Board": "acme"}) == b"ok"
get("https://api.lever.co/v0/postings/acme")
assert seen[0]["accept"] == "text/plain" and seen[0]["x-board"] == "acme"
assert seen[0]["user-agent"] == seen[1]["user-agent"] == ats.USER_AGENT
assert seen[1]["accept"] == "application/json, text/xml"
assert "x-board" not in seen[1]
def test_the_bound_get_reaches_a_replaced_ats_get(monkeypatch: pytest.MonkeyPatch) -> None:
"""Bound before the replacement, and still reaching it: `_get` is looked
up when a request is made."""
client = object()
get = ats._getter(client, "lever", "acme") # type: ignore[arg-type]
calls: list[tuple[Any, ...]] = []
def replaced(*args: Any, **kwargs: Any) -> bytes:
calls.append((args, kwargs))
return b""
monkeypatch.setattr(ats, "_get", replaced)
get("https://api.lever.co/v0/postings/acme", headers={"X": "1"})
get("https://api.lever.co/v0/postings/acme", method="POST", payload={})
assert calls == [
((client, "https://api.lever.co/v0/postings/acme", "lever", "acme"),
{"method": "GET", "headers": {"X": "1"}}),
((client, "https://api.lever.co/v0/postings/acme", "lever", "acme"),
{"method": "POST", "payload": {}})]
# ------------------------------------------------------- a fake host
def _fake_host(*, detail: Callable[..., Any] | None = None,
robots: bool = False) -> types.ModuleType:
"""A host `fake` whose feed is a JSON list of ids, each role with a
teaser for a description, and, with `detail`, a per-role page; with
`robots`, its listing is the page robots.txt must allow."""
module = types.ModuleType("fake")
module.NAME, module.LABEL, module.PATTERNS = "fake", "Fake", ()
module.slug_ok = lambda slug: slug.isalnum()
module.allowed = lambda slug: lambda h: h == "jobs.fake.example"
module.careers_url = lambda slug: f"https://jobs.fake.example/{slug}"
module.feed_url = lambda slug: f"https://jobs.fake.example/{slug}/list"
module.parse = lambda body, slug: [
{"source_id": i, "title": f"Role {i}", "location": "", "department": "",
"url": f"https://jobs.fake.example/{slug}/{i}", "posted": None,
"description": "teaser", "remote": None, "salary_text": ""}
for i in json.loads(body)]
if detail is not None:
module.detail = detail
if robots:
module.robots_page = lambda slug: f"https://jobs.fake.example/{slug}/list"
return module
def _status(code: int, url: str = "https://jobs.fake.example/") -> httpx.HTTPStatusError:
request = httpx.Request("GET", url)
return httpx.HTTPStatusError(str(code), request=request,
response=httpx.Response(code, request=request))
@pytest.fixture
def web(monkeypatch: pytest.MonkeyPatch) -> dict[str, Any]:
"""`ats._get` answering from a dict of URL -> body or exception, and
remembering what it was asked. No delay between requests."""
answers: dict[str, Any] = {}
asked: list[str] = []
def fake_get(client, url, host, slug, *, method="GET", payload=None, headers=None):
asked.append(url)
if not ats._allowed_host(host, slug)(httpx.URL(url).host):
raise ats._Refused(url)
answer = answers.get(url, _status(404, url))
if isinstance(answer, BaseException):
raise answer
return answer
monkeypatch.setattr(ats, "_get", fake_get)
monkeypatch.setattr(ats, "PAGE_DELAY", 0)
return {"answers": answers, "asked": asked}
def _run(monkeypatch: pytest.MonkeyPatch, module: types.ModuleType,
state: dict[str, str] | None = None) -> tuple[list[Any], dict[str, str]]:
monkeypatch.setitem(atshosts.HOSTS, "fake", module)
source = ats.ATSSource({"boards": [{"url": "fake:acme", "label": "Acme"}], "run": "r"})
if state is not None:
source.state = state
return list(source.fetch()), source.state
def _detail(get: Any, slug: str, row: dict[str, Any]) -> dict[str, Any]:
"""The fake host's detail: the role's own page is its description."""
return {"description": get(f"https://jobs.fake.example/{slug}/{row['source_id']}").decode(),
"location": "Copenhagen"}
# ---------------------------------------------------------------- detail
def test_detail_is_read_for_unread_roles_only(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
base = "https://jobs.fake.example/acme"
web["answers"].update({f"{base}/list": b'["1", "2", "3"]', f"{base}/1": b"one in full",
f"{base}/2": b"two in full", f"{base}/3": b"three in full"})
module = _fake_host(detail=_detail)
state = {"ats:detailed:fake:acme": json.dumps(["fake:acme:2"])}
items, state = _run(monkeypatch, module, state)
assert web["asked"] == [f"{base}/list", f"{base}/1", f"{base}/3"]
assert [(i.source_id, i.body) for i in items] == [
("fake:acme:1", "one in full"), ("fake:acme:2", ""), ("fake:acme:3", "three in full")]
# What detail returns replaces the listing's field; a role not read keeps the listing's.
assert items[0].raw["place"] == "Copenhagen" and items[1].raw["place"] == ""
assert json.loads(state["ats:detailed:fake:acme"]) == [
"fake:acme:1", "fake:acme:2", "fake:acme:3"]
assert state["ats:complete:fake:acme"] == "1"
def test_detail_reads_stop_at_the_cap_and_the_rest_wait(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
base = "https://jobs.fake.example/acme"
ids = [str(n) for n in range(5)]
web["answers"].update({f"{base}/list": json.dumps(ids).encode()}
| {f"{base}/{i}": f"full {i}".encode() for i in ids})
monkeypatch.setattr(ats, "DETAIL_CAP", 2)
module = _fake_host(detail=_detail)
items, state = _run(monkeypatch, module)
assert [i.body for i in items] == ["full 0", "full 1", "", "", ""]
assert json.loads(state["ats:detailed:fake:acme"]) == ["fake:acme:0", "fake:acme:1"]
web["asked"].clear()
items, state = _run(monkeypatch, module, state)
assert web["asked"] == [f"{base}/list", f"{base}/2", f"{base}/3"]
assert [i.body for i in items] == ["", "", "full 2", "full 3", ""]
def test_a_detail_that_fails_skips_the_role_and_not_the_board(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
base = "https://jobs.fake.example/acme"
web["answers"].update({f"{base}/list": b'["1", "2", "3"]', f"{base}/1": b"one",
f"{base}/2": ValueError("no such page shape"), f"{base}/3": b"three"})
items, state = _run(monkeypatch, _fake_host(detail=_detail))
assert [(i.source_id, i.body) for i in items] == [
("fake:acme:1", "one"), ("fake:acme:2", ""), ("fake:acme:3", "three")]
assert json.loads(state["ats:detailed:fake:acme"]) == ["fake:acme:1", "fake:acme:3"]
assert "ats:failing:fake:acme" not in state and state["ats:ok:fake:acme"] == "r"
assert state["ats:complete:fake:acme"] == "1"
def test_a_429_on_a_detail_stops_the_reads_for_the_pull(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
base = "https://jobs.fake.example/acme"
web["answers"].update({f"{base}/list": b'["1", "2", "3"]', f"{base}/1": b"one",
f"{base}/2": _status(429), f"{base}/3": b"three"})
items, state = _run(monkeypatch, _fake_host(detail=_detail))
assert web["asked"] == [f"{base}/list", f"{base}/1", f"{base}/2"]
assert [i.body for i in items] == ["one", "", ""]
assert json.loads(state["ats:detailed:fake:acme"]) == ["fake:acme:1"]
assert state["ats:complete:fake:acme"] == "1"
def test_ids_no_longer_listed_leave_the_detailed_list(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
base = "https://jobs.fake.example/acme"
web["answers"].update({f"{base}/list": b'["2"]'})
state = {"ats:detailed:fake:acme": json.dumps(["fake:acme:1", "fake:acme:2"])}
_items, state = _run(monkeypatch, _fake_host(detail=_detail), state)
assert json.loads(state["ats:detailed:fake:acme"]) == ["fake:acme:2"]
def test_an_incomplete_listing_keeps_the_ids_it_did_not_list(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
"""A listing cut short says nothing about the roles it left out, so their
ids stay, and the next pull does not read them in detail again."""
base = "https://jobs.fake.example/acme"
web["answers"].update({f"{base}/list": b'["2"]'})
monkeypatch.setattr(ats, "_fetch_rows", lambda client, target: (
atshosts.HOSTS["fake"].parse(b'["2"]', "acme"), False))
state = {"ats:detailed:fake:acme": json.dumps(["fake:acme:1", "fake:acme:2"])}
_items, state = _run(monkeypatch, _fake_host(detail=_detail), state)
assert json.loads(state["ats:detailed:fake:acme"]) == ["fake:acme:1", "fake:acme:2"]
assert state["ats:complete:fake:acme"] == ""
def test_fetch_board_reads_the_listing_only(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
base = "https://jobs.fake.example/acme"
web["answers"].update({f"{base}/list": b'["1"]', f"{base}/1": b"one"})
monkeypatch.setitem(atshosts.HOSTS, "fake", _fake_host(detail=_detail))
rows, complete = ats.fetch_board("fake:acme")
assert ([r["description"] for r in rows], complete) == (["teaser"], True)
assert web["asked"] == [f"{base}/list"]
def test_a_host_without_detail_keeps_the_listings_description(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
web["answers"]["https://jobs.fake.example/acme/list"] = b'["1"]'
items, state = _run(monkeypatch, _fake_host())
assert [i.body for i in items] == ["teaser"]
assert "ats:detailed:fake:acme" not in state
# ------------------------------------------------------------ robots.txt
ROBOTS = "https://jobs.fake.example/robots.txt"
def test_robots_that_allow_the_page_let_the_board_be_read(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
web["answers"].update({ROBOTS: b"User-agent: *\nDisallow: /admin/\n",
"https://jobs.fake.example/acme/list": b'["1"]'})
items, state = _run(monkeypatch, _fake_host(robots=True))
assert len(items) == 1
assert json.loads(state["ats:robots:fake:acme"])["allowed"] is True
@pytest.mark.parametrize("robots", [
b"User-agent: *\nDisallow: /acme/\n",
# A rule for this app by name counts as well as the one for everyone.
f"User-agent: {ats.USER_AGENT.split('/')[0]}\nDisallow: /\n\nUser-agent: *\nAllow: /\n"
.encode(),
])
def test_robots_that_disallow_the_page_fail_the_board(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any], robots: bytes) -> None:
web["answers"].update({ROBOTS: robots, "https://jobs.fake.example/acme/list": b'["1"]'})
items, state = _run(monkeypatch, _fake_host(robots=True))
assert items == []
assert web["asked"] == [ROBOTS] # the listing is never fetched
assert json.loads(state["ats:failing:fake:acme"])["reason"] == "robots.txt disallows it"
assert "ats:ok:fake:acme" not in state
def test_a_robots_verdict_is_kept_for_a_day(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
web["answers"].update({ROBOTS: b"User-agent: *\nDisallow: /\n",
"https://jobs.fake.example/acme/list": b'["1"]'})
module = _fake_host(robots=True)
_items, state = _run(monkeypatch, module)
assert web["asked"] == [ROBOTS]
# Within the day the kept verdict holds, even though the site changed.
web["answers"][ROBOTS] = b"User-agent: *\nAllow: /\n"
web["asked"].clear()
items, state = _run(monkeypatch, module, state)
assert (items, web["asked"]) == ([], [])
# A day on, robots.txt is read again.
kept = json.loads(state["ats:robots:fake:acme"])
kept["checked"] = (datetime.now(UTC) - timedelta(days=1, minutes=1)).isoformat()
state["ats:robots:fake:acme"] = json.dumps(kept)
items, state = _run(monkeypatch, module, state)
assert web["asked"] == [ROBOTS, "https://jobs.fake.example/acme/list"]
assert len(items) == 1 and "ats:failing:fake:acme" not in state
def test_a_missing_robots_txt_allows_and_is_kept(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
web["answers"]["https://jobs.fake.example/acme/list"] = b'["1"]' # robots.txt: 404
items, state = _run(monkeypatch, _fake_host(robots=True))
assert len(items) == 1
assert json.loads(state["ats:robots:fake:acme"])["allowed"] is True
def test_a_robots_txt_that_cannot_be_read_allows_and_is_asked_again(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
web["answers"].update({ROBOTS: _status(503), "https://jobs.fake.example/acme/list": b'["1"]'})
items, state = _run(monkeypatch, _fake_host(robots=True))
assert len(items) == 1 and "ats:robots:fake:acme" not in state
def test_fetch_board_honours_robots_txt(
monkeypatch: pytest.MonkeyPatch, web: dict[str, Any]) -> None:
web["answers"].update({ROBOTS: b"User-agent: *\nDisallow: /\n",
"https://jobs.fake.example/acme/list": b'["1"]'})
monkeypatch.setitem(atshosts.HOSTS, "fake", _fake_host(robots=True))
with pytest.raises(ats._Disallowed) as caught:
ats.fetch_board("fake:acme")
assert ats.failure_reason(caught.value) == "robots.txt disallows it"
# ----------------------------------------------------------------- Lever EU
def test_a_lever_slug_may_name_the_eu_instance() -> None:
assert ats.is_known_target("lever:acme") and ats.is_known_target("lever:eu/tomtom")
assert not ats.is_known_target("lever:us/acme")
assert not ats.is_known_target("lever:eu/")
assert not ats.is_known_target("lever:eu/a/b")
assert ats.careers_url("lever:acme") == "https://jobs.lever.co/acme"
assert ats.careers_url("lever:eu/tomtom") == "https://jobs.eu.lever.co/tomtom"
assert lever.feed_url("eu/tomtom") == "https://api.eu.lever.co/v0/postings/tomtom?mode=json"
eu = ats._allowed_host("lever", "eu/tomtom")
assert eu("api.eu.lever.co") and not eu("api.lever.co")
assert not ats._allowed_host("lever", "tomtom")("api.eu.lever.co")
def test_a_lever_eu_board_is_read_from_the_eu_api(monkeypatch: pytest.MonkeyPatch) -> None:
urls: list[str] = []
def fake_get(client, url, host, slug, *, method="GET"):
urls.append(url)
assert ats._allowed_host(host, slug)(httpx.URL(url).host)
return (FIXTURES / "lever.json").read_bytes()
monkeypatch.setattr(ats, "_get", fake_get)
rows, complete = ats._fetch_rows(object(), "lever:eu/tomtom") # type: ignore[arg-type]
assert urls == ["https://api.eu.lever.co/v0/postings/tomtom?mode=json"]
assert rows and complete
def test_the_eu_api_is_refused_for_a_us_board_and_the_reverse() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(302, headers={"location": "https://api.lever.co/v0/postings/x"})
with _client(handler) as client, pytest.raises(ats._Refused):
ats._get(client, "https://api.eu.lever.co/v0/postings/x", "lever", "eu/x")