Every import, test and live doc follows the new path; behaviour is unchanged. tests/test_atshosts.py becomes tests/test_readers.py. plan-readers.md and review-ingest.md keep the old name, since they describe the state before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
181 lines
8.0 KiB
Python
181 lines
8.0 KiB
Python
"""The Gem host module against Productboard's saved listing and two saved role
|
|
answers. No network."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from datetime import UTC, datetime
|
|
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 app.jobs import discover # noqa: E402
|
|
from plugins import ats, readers # noqa: E402
|
|
from plugins.readers import gem # noqa: E402
|
|
|
|
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
|
|
API = "https://jobs.gem.com/api/public/graphql"
|
|
|
|
|
|
def _read(name: str) -> bytes:
|
|
return (FIXTURES / name).read_bytes()
|
|
|
|
|
|
def _get_of(answers: dict[str, bytes | Exception], asked: list[dict[str, Any]]) -> Any:
|
|
"""A `get` that answers a GraphQL call by its operation name."""
|
|
def get(url: str, *, method: str = "GET", payload: Any = None,
|
|
headers: dict[str, str] | None = None) -> bytes:
|
|
assert url == API and method == "POST"
|
|
asked.append(payload | {"headers": headers})
|
|
answer = answers[payload["operationName"]]
|
|
if isinstance(answer, Exception):
|
|
raise answer
|
|
return answer
|
|
return get
|
|
|
|
|
|
def _listing() -> dict[str, bytes | Exception]:
|
|
return {"JobBoardList": _read("gem.json")}
|
|
|
|
|
|
def test_a_listing_becomes_rows() -> None:
|
|
asked: list[dict[str, Any]] = []
|
|
decoded, results, total, cursor = gem.page(_get_of(_listing(), asked), "productboard", None)
|
|
assert asked == [{"operationName": "JobBoardList", "variables": {"boardId": "productboard"},
|
|
"query": gem.LIST_QUERY, "headers": None}]
|
|
assert (total, cursor) == (None, None) and len(results) == 11
|
|
rows = gem.rows(decoded, "productboard")
|
|
assert len(rows) == 11
|
|
first, backend = rows[0], rows[2]
|
|
assert first["source_id"] == "8080046002" # numeric, from a synced Greenhouse
|
|
assert first["title"] == "Staff AI Product Engineer (Prague/Brno/Remote EU)"
|
|
assert first["url"] == "https://jobs.gem.com/productboard/8080046002"
|
|
assert first["department"] == "Product Engineering" and first["remote"] == "hybrid"
|
|
assert first["location"] == "Prague"
|
|
assert (first["posted"], first["description"], first["salary_text"]) == (None, "", "")
|
|
assert backend["source_id"] == "am9icG9zdDqWC1V_RM7U6ORvTd1Iu64r" # opaque
|
|
assert backend["location"] == "Prague; Remote Europe; Brno"
|
|
assert len({r["source_id"] for r in rows}) == 11
|
|
|
|
|
|
@pytest.mark.parametrize(("kind", "flags", "remote"), [
|
|
("IN_OFFICE", [False], "onsite"), ("HYBRID", [True], "hybrid"), ("REMOTE", [False], "remote"),
|
|
(None, [True, True], "remote"), (None, [True, False], None), (None, [], None),
|
|
("SOMETHING_NEW", [False], None)])
|
|
def test_remote_is_the_location_type_then_the_locations(
|
|
kind: str | None, flags: list[bool], remote: str | None) -> None:
|
|
posting = {"extId": "1", "title": "Role", "job": {"locationType": kind},
|
|
"locations": [{"city": "X", "isRemote": f} for f in flags]}
|
|
assert gem.rows([posting], "acme")[0]["remote"] == remote
|
|
|
|
|
|
def test_a_posting_without_a_title_or_an_ext_id_is_left_out() -> None:
|
|
assert gem.rows([{"extId": "1", "title": " "}, {"extId": "", "title": "Role"}], "acme") == []
|
|
|
|
|
|
def test_the_board_is_read_through_the_app_without_origin_or_referer() -> None:
|
|
seen: list[httpx.Request] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
seen.append(request)
|
|
return httpx.Response(200, content=_read("gem.json"))
|
|
|
|
with httpx.Client(transport=httpx.MockTransport(handler)) as client:
|
|
rows, complete = ats._fetch_rows(client, "gem:productboard")
|
|
assert len(rows) == 11 and complete
|
|
(request,) = seen
|
|
assert str(request.url) == API and request.method == "POST"
|
|
assert json.loads(request.content)["variables"] == {"boardId": "productboard"}
|
|
assert "origin" not in request.headers and "referer" not in request.headers
|
|
assert request.headers["content-type"] == "application/json"
|
|
|
|
|
|
def test_an_empty_board_is_complete_at_zero() -> None:
|
|
body = b'{"data":{"oatsExternalJobPostings":{"jobPostings":[]}}}'
|
|
with httpx.Client(transport=httpx.MockTransport(
|
|
lambda request: httpx.Response(200, content=body))) as client:
|
|
assert ats._fetch_rows(client, "gem:nobody") == ([], True)
|
|
|
|
|
|
@pytest.mark.parametrize(("body", "message"), [
|
|
(b'{"errors":[{"message":"Cannot query field \\"extId\\""}]}', "refused: Cannot query field"),
|
|
(b'{"data":{"somethingElse":{}}}', "no oatsExternalJobPostings"),
|
|
(b'{"data":null}', "no oatsExternalJobPostings"),
|
|
(b'{"data":{"oatsExternalJobPostings":{"postings":[]}}}', "no jobPostings list"),
|
|
(b'{"data":{"oatsExternalJobPostings":null}}', "no jobPostings list"),
|
|
(b'[]', "not a Gem answer"),
|
|
])
|
|
def test_an_answer_of_another_shape_fails_with_a_clear_error(body: bytes, message: str) -> None:
|
|
with pytest.raises(ValueError, match=message):
|
|
gem.page(_get_of({"JobBoardList": body}, []), "productboard", None)
|
|
|
|
|
|
def test_detail_reads_the_text_and_the_date() -> None:
|
|
row = {"source_id": "am9icG9zdDq5-IOakBvxHzWhMPnTzoPp"}
|
|
asked: list[dict[str, Any]] = []
|
|
found = gem.detail(_get_of({"ExternalJobPosting": _read("gem-detail-2.json")}, asked),
|
|
"productboard", row)
|
|
assert asked[0]["operationName"] == "ExternalJobPosting"
|
|
assert asked[0]["variables"] == {"boardId": "productboard", "extId": row["source_id"]}
|
|
assert asked[0]["query"] == gem.DETAIL_QUERY
|
|
assert set(found) <= set(ats.DETAIL_FIELDS)
|
|
assert found["posted"] == datetime.fromtimestamp(1787574069, tz=UTC)
|
|
assert found["description"].startswith("Only the best engineers are being built here.")
|
|
assert "<" not in found["description"] and "&" not in found["description"]
|
|
|
|
|
|
def test_detail_of_a_role_that_is_not_there_raises() -> None:
|
|
body = b'{"data":{"oatsExternalJobPosting":null}}'
|
|
with pytest.raises(ValueError, match="no role gone on acme"):
|
|
gem.detail(_get_of({"ExternalJobPosting": body}, []), "acme", {"source_id": "gone"})
|
|
|
|
|
|
def test_detail_reads_the_numeric_role_too() -> None:
|
|
found = gem.detail(_get_of({"ExternalJobPosting": _read("gem-detail-1.json")}, []),
|
|
"productboard", {"source_id": "8080046002"})
|
|
assert found["posted"] == datetime.fromtimestamp(1753349342, tz=UTC)
|
|
assert found["description"]
|
|
|
|
|
|
def test_the_slug_the_urls_and_the_allow_list() -> None:
|
|
assert gem.KEEP_CASE
|
|
for slug in ("productboard", "Productboard", "senndertechnologies-gmbh", "a_b", "1"):
|
|
assert gem.slug_ok(slug)
|
|
assert gem.careers_url("Productboard") == "https://jobs.gem.com/Productboard"
|
|
assert gem.robots_page("productboard") == "https://jobs.gem.com/productboard"
|
|
allow = gem.allowed("productboard")
|
|
assert allow("jobs.gem.com") and allow("JOBS.gem.com")
|
|
assert not allow("gem.com") and not allow("productboard.gem.com")
|
|
assert not allow("jobs.gem.com.evil.example") and not allow("localhost")
|
|
assert readers.HOSTS["gem"] is gem and ats.HOSTS["gem"] == "Gem"
|
|
|
|
|
|
@pytest.mark.parametrize("slug", [
|
|
"", "a@b", "a#b", "a?b", "..", ".", "a/b", "a%2fb", "a:443", "a b", " a", "a\n", "a\r\n",
|
|
"a.b", "127.0.0.1", "[::1]", "localhost.", "a\\b", "a\"b"])
|
|
def test_a_hostile_slug_is_refused(slug: str) -> None:
|
|
assert not gem.slug_ok(slug)
|
|
assert not ats.is_known_target(f"gem:{slug}")
|
|
|
|
|
|
def test_a_local_word_as_a_slug_still_reaches_gems_host_only() -> None:
|
|
assert ats.is_known_target("gem:localhost")
|
|
allow = gem.allowed("localhost")
|
|
assert allow("jobs.gem.com") and not allow("localhost")
|
|
|
|
|
|
def test_discovery_finds_the_board_keeping_its_case() -> None:
|
|
page = ('<a href="https://jobs.gem.com/Productboard">Jobs</a>'
|
|
'<a href="https://jobs.gem.com/api/public/graphql">x</a>'
|
|
'<a href="https://jobs.gem.com/sennder-gmbh/8080046002">y</a>')
|
|
assert [(h, s) for h, s, _ in discover.boards_in(page)] == [
|
|
("gem", "Productboard"), ("gem", "sennder-gmbh")]
|
|
assert "gem" not in {h for h, _ in discover._UNREADABLE if h not in readers.HOSTS}
|