"""The Eightfold host module against a saved page of Ericsson's search (five of its roles) and one saved role answer. 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, atshosts # noqa: E402 from plugins.atshosts import eightfold # noqa: E402 FIXTURES = ROOT / "tests" / "fixtures" / "ats" SLUG = "jobs.ericsson.com/ericsson.com" def _saved() -> dict[str, Any]: return json.loads((FIXTURES / "eightfold.json").read_text()) def _position(n: int) -> dict[str, Any]: return {"id": 5000 + n, "displayJobId": str(n), "name": f"Role {n}", "locations": ["Lund,Skane,Sweden"], "postedTs": 1790000000 - n, "department": "76 Product Development", "workLocationOption": "hybrid", "positionUrl": f"/careers/job/{5000 + n}"} def _search(total: int, seen: list[str] | None = None) -> httpx.MockTransport: """A search that ignores `num` and answers ten roles from `start`.""" def handler(request: httpx.Request) -> httpx.Response: if seen is not None: seen.append(str(request.url)) start = int(request.url.params["start"]) positions = [_position(n) for n in range(start, min(start + 10, total))] return httpx.Response(200, json={"status": 200, "data": { "positions": positions, "count": total}}) return httpx.MockTransport(handler) def test_a_page_becomes_rows() -> None: rows = eightfold.rows(_saved()["data"]["positions"], SLUG) assert len(rows) == 5 first, mexico, many = rows[0], rows[1], rows[4] assert first["source_id"] == "563121777024688" assert first["title"] == "Systems Architect - RAN Semantic Data" assert first["url"] == "https://jobs.ericsson.com/careers/job/563121777024688" assert first["location"] == "Stockholm, Sweden" # the city is its own region assert first["department"] == "76 Product Development" assert first["posted"] == datetime.fromtimestamp(1790703782, tz=UTC) assert first["remote"] == "onsite" and (first["description"], first["salary_text"]) == ("", "") assert mexico["location"] == "Mexico City, Mexico" assert many["location"] == ("Tecnoparque, Mexico City, Mexico; Santa Fe, Mexico City, Mexico; " "Mexico City, Mexico; Tlalnepantla, Estado de México, Mexico") @pytest.mark.parametrize(("option", "remote"), [ ("onsite", "onsite"), ("hybrid", "hybrid"), ("remote", "remote"), (None, None), ("odd", None)]) def test_the_work_location_option_maps_to_the_remote_words( option: str | None, remote: str | None) -> None: assert eightfold.rows([_position(1) | {"workLocationOption": option}], SLUG)[0]["remote"] == remote def test_a_position_without_a_name_an_id_or_a_path_is_left_out() -> None: bad = [_position(1) | {"name": " "}, _position(2) | {"id": None}, _position(3) | {"positionUrl": ""}, _position(4) | {"positionUrl": "//evil.example/x"}] assert eightfold.rows(bad, SLUG) == [] def test_the_standardized_location_stands_in_when_there_is_no_raw_one() -> None: row = eightfold.rows([_position(1) | {"locations": [], "standardizedLocations": ["Lund, SE"]}], SLUG)[0] assert row["location"] == "Lund, SE" def test_a_page_asks_for_the_newest_first_from_its_offset() -> None: asked: list[str] = [] def get(url: str, **_: Any) -> bytes: asked.append(url) return json.dumps(_saved()).encode() decoded, results, total, cursor = eightfold.page(get, SLUG, None) assert asked == ["https://jobs.ericsson.com/api/pcsx/search?domain=ericsson.com" "&sort_by=timestamp&start=0"] assert (len(decoded), len(results), total, cursor) == (5, 5, 637, 5) assert eightfold.page(get, SLUG, 630)[3] == 635 assert asked[-1].endswith("&start=630") def test_a_board_is_read_ten_at_a_time_to_its_total(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(ats, "PAGE_DELAY", 0) seen: list[str] = [] with httpx.Client(transport=_search(25, seen)) as client: rows, complete = ats._fetch_rows(client, f"eightfold:{SLUG}") assert (len(rows), complete) == (25, True) assert [u.rsplit("=", 1)[1] for u in seen] == ["0", "10", "20"] def test_a_board_over_the_page_cap_is_incomplete(monkeypatch: pytest.MonkeyPatch) -> None: """The cap is 100 pages of ten: a board of 1,292 (Infineon's) or 2,319 (Microsoft's) is read as its first thousand and never as the whole.""" monkeypatch.setattr(ats, "PAGE_DELAY", 0) seen: list[str] = [] with httpx.Client(transport=_search(1292, seen)) as client: rows, complete = ats._fetch_rows(client, "eightfold:infineon.eightfold.ai/infineon.com") assert (len(rows), complete) == (1000, False) and len(seen) == ats.MAX_PAGES == 100 def test_an_empty_board_is_complete_at_zero(monkeypatch: pytest.MonkeyPatch) -> None: with httpx.Client(transport=_search(0)) as client: assert ats._fetch_rows(client, f"eightfold:{SLUG}") == ([], True) @pytest.mark.parametrize("body", [ b"Not found", b"[]", b'{"status": 403, "message": "Not authorized for PCSX"}', b'{"data": {"positions": null}}']) def test_an_answer_that_is_not_a_search_raises(body: bytes) -> None: with pytest.raises(ValueError): eightfold.page(lambda url, **_: body, SLUG, None) def test_detail_reads_the_text_of_the_ad() -> None: asked: list[str] = [] def get(url: str, **_: Any) -> bytes: asked.append(url) return (FIXTURES / "eightfold-detail.json").read_bytes() found = eightfold.detail(get, SLUG, {"source_id": "563121777283313"}) assert asked == ["https://jobs.ericsson.com/api/pcsx/position_details" "?position_id=563121777283313&domain=ericsson.com&hl=en"] assert set(found) <= set(ats.DETAIL_FIELDS) text = found["description"] assert text.startswith("Join our Team") assert "<" not in text and "&" not in text and "\r" not in text and " " not in text @pytest.mark.parametrize("body", [b"", b"[]", b'{"data": {}}']) def test_detail_refuses_an_answer_that_is_not_a_position(body: bytes) -> None: with pytest.raises(ValueError): eightfold.detail(lambda url, **_: body, SLUG, {"source_id": "1"}) @pytest.mark.parametrize("role", ["", "12a", "1&domain=evil.example", "../x", "1 2"]) def test_detail_refuses_an_id_that_is_not_a_number(role: str) -> None: def get(url: str, **_: Any) -> bytes: raise AssertionError("no request may be made") with pytest.raises(ValueError): eightfold.detail(get, SLUG, {"source_id": role}) def test_the_slug_the_urls_and_the_allow_list() -> None: for slug in (SLUG, "infineon.eightfold.ai/infineon.com", "Apply.Careers.Microsoft.com/microsoft.com"): assert eightfold.slug_ok(slug) and ats.is_known_target(f"eightfold:{slug}") assert eightfold.careers_url("Jobs.Ericsson.com/ericsson.com") == "https://jobs.ericsson.com/careers" assert eightfold.robots_page(SLUG) == "https://jobs.ericsson.com/careers" allow = eightfold.allowed("Jobs.Ericsson.com/ericsson.com") assert allow("jobs.ericsson.com") and allow("JOBS.ERICSSON.COM") assert not allow("ericsson.com") and not allow("evil.example") assert not allow("jobs.ericsson.com.evil.example") and not allow("localhost") assert atshosts.HOSTS["eightfold"] is eightfold and ats.HOSTS["eightfold"] == "Eightfold" @pytest.mark.parametrize("slug", [ "", "jobs.ericsson.com", "jobs.ericsson.com/", "/ericsson.com", "jobs.ericsson.com/ericsson", "jobs.ericsson.com/ericsson.com/", "jobs.ericsson.com/ericsson.com/x", "a/b/c.com", "a@b.com/ericsson.com", "a.com#x/ericsson.com", "a.com?x/ericsson.com", "../ericsson.com", "a.com%2fx/b.com", "a.com:443/b.com", "a.com/b.com:80", " a.com/b.com", "a.com/b.com ", "a.com/b.com\n", "a.com/b.com\r\n", "127.0.0.1/ericsson.com", "a.com/127.0.0.1", "localhost/ericsson.com", "a.com/localhost", "a.com/b.com&start=9", "a.com/b.com?x=1", "[::1]/a.com", "a.com/b.com#", "app.localhost/a.com", "printer.local/a.com", "meta.internal/a.com", "a.com/x.internal", "a_b.com/c.com", "évil.com/a.com"]) def test_a_hostile_slug_is_refused(slug: str) -> None: assert not eightfold.slug_ok(slug) assert not ats.is_known_target(f"eightfold:{slug}") def test_discovery_reads_the_host_and_domain_from_a_link_that_has_both() -> None: page = ('Jobs' 'no domain') found = [(h, s) for h, s, _ in discover.boards_in(page)] assert ("eightfold", "infineon.eightfold.ai/infineon.com") in found assert ("eightfold", "") in found assert "eightfold" not in {h for h, _ in discover._UNREADABLE if h not in atshosts.HOSTS}