"""The Workday host module: rows from saved listings, the paging quirks of a tenant's list endpoint against a fake `get`, `detail` from saved responses, the slug and its allow-list and discovery's patterns. No network -- every answer is a file under `tests/fixtures/ats/` or written here.""" 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 # noqa: E402 from plugins.readers import _runner, workday # noqa: E402 FIXTURES = ROOT / "tests" / "fixtures" / "ats" SLUG = "zendesk.wd1/zendesk" HOST = "zendesk.wd1.myworkdayjobs.com" TODAY = datetime(2026, 9, 29, tzinfo=UTC) def _load(name: str) -> Any: return json.loads((FIXTURES / name).read_text()) @pytest.fixture(autouse=True) def _frozen_today(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(workday, "_today", lambda: TODAY) @pytest.fixture def no_delay(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(ats, "PAGE_DELAY", 0) # ------------------------------------------------------------------- rows def test_rows_read_the_listing_of_a_tenant_that_sends_remote_type() -> None: rows = workday.rows(_load("workday.json"), SLUG) assert [r["source_id"] for r in rows] == ["R35358", "R35239", "R35277", "R35551", "R35544"] first = rows[0] assert first["title"] == "Associate Solutions Architect" assert first["location"] == "Remote, Mexico City, Mexico" assert first["url"] == (f"https://{HOST}/zendesk/job/Remote-Mexico-City-Mexico/" "Associate-Solutions-Architect_R35358") assert first["posted"] == TODAY assert first["description"] == "" and first["department"] == "" and first["salary_text"] == "" # The host's own words, "Fully Flexible" among them; prep maps them. assert [r["remote"] for r in rows] == ["Fully Flexible", "In Office", "Hybrid", "Fully Flexible", "Hybrid"] def test_rows_of_a_tenant_without_remote_type_leave_remote_unknown() -> None: rows = workday.rows(_load("workday-saxo.json"), "saxobank.wd3/CareeratSaxoBank") assert [r["source_id"] for r in rows] == [ "R-19507", "R-19142", "R-19427", "R-19402", "R-19359"] assert all(r["remote"] is None for r in rows) assert rows[0]["url"] == ("https://saxobank.wd3.myworkdayjobs.com/CareeratSaxoBank" "/job/Singapore/Client-Support-and-Sales---Singapore_R-19507") # Yesterday, and four days ago. assert rows[0]["posted"] == datetime(2026, 9, 28, tzinfo=UTC) assert rows[2]["posted"] == datetime(2026, 9, 25, tzinfo=UTC) def test_rows_of_a_site_called_jobs_state_the_requisition_id_and_no_date_past_30_days() -> None: rows = workday.rows(_load("workday-redhat.json"), "redhat.wd5/jobs") assert [r["source_id"] for r in rows] == [ "R-059377", "R-058740", "R-059566", "R-058743"] # not the reposts' "-1" and "-2" # "2 Locations" is a count, not a place: the role's own read names them. assert rows[2]["location"] == "" and rows[2]["places"] == [] assert rows[0]["places"] == [{"text": "Raleigh", "remote": False}] # Onsite assert rows[1]["places"] == [{"text": "Remote US TX", "remote": True}] assert rows[2]["url"].startswith("https://redhat.wd5.myworkdayjobs.com/jobs/job/") assert rows[3]["posted"] is None # "Posted 30+ Days Ago" says only that it is older assert [r["remote"] for r in rows] == ["Onsite", "Remote", "Remote", "Hybrid"] @pytest.mark.parametrize(("text", "expected"), [ ("Posted Today", datetime(2026, 9, 29, tzinfo=UTC)), ("Posted Yesterday", datetime(2026, 9, 28, tzinfo=UTC)), ("Posted 1 Day Ago", datetime(2026, 9, 28, tzinfo=UTC)), ("Posted 21 Days Ago", datetime(2026, 9, 8, tzinfo=UTC)), ("Posted 30+ Days Ago", None), ("Vor 3 Tagen veroeffentlicht", None), ("", None), ]) def test_posted_is_read_only_where_the_text_is_exact(text: str, expected: datetime | None) -> None: assert workday._posted(text) == expected def test_a_posting_without_a_title_or_a_path_reaches_the_runner_and_is_dropped() -> None: page = {"jobPostings": [ {"bulletFields": ["0000036369"]}, # LEGO's, no title {"title": "No path", "bulletFields": ["R1"]}, {"externalPath": "/job/X/y_R2", "bulletFields": ["R2"]}, # no title {"title": " ", "externalPath": "/job/X/y_R3"}, {"title": "Whole", "externalPath": "/job/X/Whole_R4", "locationsText": "X", "postedOn": "Posted Today", "bulletFields": ["R4"]}, ]} rows = workday.rows(page, SLUG) assert len(rows) == 5 assert [(r["source_id"], r["url"]) for r in rows[:2]] == [("", ""), ("", "")] assert [r["title"] for r in rows if _runner.valid(r)] == ["Whole"] # Through the runner: four dropped and counted, and the read complete, # since every posting counts by its position towards the stated total. body = json.dumps(page | {"total": 5}).encode() listing = _runner.read_listing(workday, SLUG, lambda url, **kw: body) assert [r["title"] for r in listing.rows] == ["Whole"] assert (listing.handed, listing.dropped, listing.complete) == (5, 4, True) def test_the_id_is_the_path_where_the_requisition_id_is_not_in_it() -> None: """`bulletFields` sometimes lead with something else, a location.""" page = {"jobPostings": [ {"title": "A", "externalPath": "/job/Oslo/A_R1", "bulletFields": ["Oslo"]}, {"title": "B", "externalPath": "/job/Oslo/B_R2"}, {"title": "C", "externalPath": "/job/Oslo/C_R3-1", "bulletFields": ["R3", "Oslo"]}]} assert [r["source_id"] for r in workday.rows(page, SLUG)] == [ "/job/Oslo/A_R1", "/job/Oslo/B_R2", "R3"] # ----------------------------------------------------------------- paging def _list_page(total: int, start: int, count: int, *, path_less: int | None = None ) -> bytes: postings: list[dict[str, Any]] = [] for n in range(start, min(start + count, total)): if n == path_less: postings.append({"bulletFields": [f"P{n}"]}) else: postings.append({"title": f"Role {n}", "externalPath": f"/job/Oslo/Role-{n}_R{n}", "locationsText": "Oslo", "postedOn": "Posted Today", "bulletFields": [f"R{n}"]}) return json.dumps({"total": total, "jobPostings": postings}).encode() class Tenant: """A tenant's list endpoint with the quirks of the real ones: `total` on the first page only (`0` after it), and an offset past the end answered with the first page again rather than an empty one.""" def __init__(self, total: int, *, path_less: int | None = None, repeat_total: bool = False) -> None: self.total, self.path_less, self.repeat_total = total, path_less, repeat_total self.calls: list[tuple[str, str, Any]] = [] def __call__(self, client: Any, url: str, host: str, slug: str, *, method: str = "GET", payload: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> bytes: self.calls.append((method, url, payload)) assert payload is not None offset = payload["offset"] if offset >= self.total: offset = 0 body = json.loads(_list_page(self.total, offset, payload["limit"], path_less=self.path_less)) if payload["offset"] and not self.repeat_total: body["total"] = 0 return json.dumps(body).encode() def test_a_board_is_read_page_by_page_to_its_total_and_is_complete( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: tenant = Tenant(45) monkeypatch.setattr(ats, "_get", tenant) rows, complete = ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] assert complete and len(rows) == 45 assert [c[2]["offset"] for c in tenant.calls] == [0, 20, 40] def test_the_post_body_and_url_are_what_the_tenant_expects( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: tenant = Tenant(25) monkeypatch.setattr(ats, "_get", tenant) ats._fetch_rows(object(), "workday:lego.wd103/LEGO_External") # type: ignore[arg-type] url = "https://lego.wd103.myworkdayjobs.com/wday/cxs/lego/LEGO_External/jobs" body = {"appliedFacets": {}, "limit": 20, "searchText": ""} assert tenant.calls == [("POST", url, body | {"offset": 0}), ("POST", url, body | {"offset": 20})] def test_the_total_is_kept_from_the_first_page_though_later_ones_state_none_or_zero( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: for repeat in (False, True): tenant = Tenant(41, repeat_total=repeat) monkeypatch.setattr(ats, "_get", tenant) rows, complete = ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] assert (len(rows), complete, len(tenant.calls)) == (41, True, 3) def test_the_read_stops_at_the_total_and_never_asks_past_the_end( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: """Past the end the host answers the first page again, which is never empty: a read that waited for an empty page would run to `MAX_PAGES`.""" tenant = Tenant(40) monkeypatch.setattr(ats, "_get", tenant) rows, complete = ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] assert (len(rows), complete) == (40, True) assert [c[2]["offset"] for c in tenant.calls] == [0, 20] def test_a_page_wrapping_to_the_first_one_leaves_the_board_incomplete( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: """The first page states 60, the tenant holds 40: the third page is the first again, adds nothing, and ends the read short of the total.""" real = Tenant(40) def tenant(client: Any, url: str, host: str, slug: str, *, method: str = "GET", payload: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> bytes: body = json.loads(real(client, url, host, slug, method=method, payload=payload)) if payload and payload["offset"] == 0: body["total"] = 60 return json.dumps(body).encode() monkeypatch.setattr(ats, "_get", tenant) rows, complete = ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] assert (len(rows), complete) == (40, False) def test_a_posting_without_a_title_still_counts_toward_the_total( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: """LEGO lists 453 postings of which one has no title and no path: 452 rows, and the board is complete all the same.""" tenant = Tenant(45, path_less=7) monkeypatch.setattr(ats, "_get", tenant) rows, complete = ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] assert (len(rows), complete) == (44, True) def test_an_empty_site_is_complete_at_zero( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: tenant = Tenant(0) monkeypatch.setattr(ats, "_get", tenant) rows, complete = ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] assert (rows, complete, len(tenant.calls)) == ([], True, 1) def test_a_first_page_stating_no_total_is_read_to_a_page_that_adds_nothing( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: real = Tenant(30) def tenant(client: Any, url: str, host: str, slug: str, *, method: str = "GET", payload: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> bytes: body = json.loads(real(client, url, host, slug, method=method, payload=payload)) body.pop("total") return json.dumps(body).encode() monkeypatch.setattr(ats, "_get", tenant) rows, complete = ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] assert len(rows) == 30 and not complete def test_a_board_over_the_page_cap_is_swept_from_its_cursor_as_json_gives_it_back( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: """The cursor `(offset, total)` is a tuple, kept in state as a list, and the total later pages state as 0 is carried in it. The sweep is kept, and the next pull reads on from it to the first page's total.""" tenant = Tenant(45) monkeypatch.setattr(ats, "_get", tenant) monkeypatch.setattr(ats, "MAX_PAGES", 2) target = f"workday:{SLUG}" state: dict[str, str] = {} def pull_once() -> list[Any]: source = ats.ATSSource({"boards": [{"url": target, "label": "Zendesk", "params": {}}], "run": "r"}) source.state = state return list(source.fetch()) assert len(pull_once()) == 40 assert json.loads(state[f"ats:sweep:{target}"])["cursor"] == [40, 45] assert len(pull_once()) == 5 assert (state[f"ats:complete:{target}"], state[f"ats:swept:{target}"]) == ("1", "1") assert len(json.loads(state[f"ats:ids:{target}"])) == 45 assert [c[2]["offset"] for c in tenant.calls] == [0, 20, 40] def test_a_page_that_is_not_json_fails_the_read( monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: monkeypatch.setattr(ats, "_get", lambda *a, **k: b"maintenance") with pytest.raises(ValueError): ats._fetch_rows(object(), f"workday:{SLUG}") # type: ignore[arg-type] # ----------------------------------------------------------------- detail def _get_of(answer: bytes, asked: list[str]) -> Any: def get(url: str, *, method: str = "GET", payload: Any = None, headers: Any = None) -> bytes: asked.append(url) assert method == "GET" return answer return get def test_detail_reads_the_description_date_remote_and_place_of_the_role() -> None: row = workday.rows(_load("workday.json"), SLUG)[0] asked: list[str] = [] got = workday.detail(_get_of((FIXTURES / "workday-detail.json").read_bytes(), asked), SLUG, row) assert asked == [f"https://{HOST}/wday/cxs/zendesk/zendesk" "/job/Remote-Mexico-City-Mexico/Associate-Solutions-Architect_R35358"] assert set(got) == {"description", "location", "places", "posted", "remote", "employment"} assert got["posted"] == datetime(2026, 9, 29, tzinfo=UTC) assert got["remote"] == "Fully Flexible" assert got["location"] == "Remote, Mexico City, Mexico" assert got["places"] == [{"text": "Remote, Mexico City, Mexico", "remote": True}] assert got["employment"] == "Full time" and "country" not in got assert got["description"].startswith("Job Description") or len(got["description"]) > 200 assert "<" not in got["description"] and " " not in got["description"] assert " " not in got["description"] def test_detail_names_every_location_of_a_role_listed_as_several() -> None: row = workday.rows(_load("workday-redhat.json"), "redhat.wd5/jobs")[2] asked: list[str] = [] got = workday.detail(_get_of((FIXTURES / "workday-redhat-detail.json").read_bytes(), asked), "redhat.wd5/jobs", row) assert asked == ["https://redhat.wd5.myworkdayjobs.com/wday/cxs/redhat/jobs" "/job/Remote-Italy/Telco-Cloud-Consultant_R-059566-2"] assert got["location"] == "Remote Italy; Remote Spain" assert [p["text"] for p in got["places"]] == ["Remote Italy", "Remote Spain"] assert "country" not in got def _detail(**info: Any) -> bytes: return json.dumps({"jobPostingInfo": {"title": "T", **info}}).encode() def test_detail_of_a_tenant_without_remote_type_leaves_it_out() -> None: row = workday.rows(_load("workday-saxo.json"), "saxobank.wd3/CareeratSaxoBank")[0] got = workday.detail( _get_of(_detail(jobDescription="
Trade & settle
", location="Singapore", startDate="2026-09-28"), []), "saxobank.wd3/CareeratSaxoBank", row) assert got == {"description": "Trade & settle", "location": "Singapore", "places": [{"text": "Singapore", "remote": None}], "posted": datetime(2026, 9, 28, tzinfo=UTC)} def test_detail_adds_the_country_to_a_place_that_does_not_name_it() -> None: row = workday.rows(_load("workday.json"), SLUG)[0] got = workday.detail( _get_of(_detail(location="Billund", country={"descriptor": "Denmark"}), []), SLUG, row) assert got == {"location": "Billund, Denmark", "places": [{"text": "Billund, Denmark", "remote": None}]} def test_a_country_dash_city_place_is_turned_round() -> None: row = workday.rows(_load("workday.json"), SLUG)[0] got = workday.detail( _get_of((FIXTURES / "workday-places-detail.json").read_bytes(), []), SLUG, row) # Twice the same city is once; a place that starts with a work mode is kept as written. assert [p["text"] for p in got["places"]] == [ "Copenhagen, Denmark", "Stockholm, Sweden", "Remote, Mexico City, Mexico"] assert got["location"] == "Copenhagen, Denmark; Stockholm, Sweden; Remote, Mexico City, Mexico" assert all(p["remote"] is False for p in got["places"]) # remoteType Hybrid assert workday._place("USA - Texas - Austin", None)["text"] == "Austin, Texas, USA" assert workday._place("Remote - Germany", None)["text"] == "Remote - Germany" assert workday._place("Denmark - Copenhagen, DK", None)["text"] == "Denmark - Copenhagen, DK" assert workday._place("12 Locations", None) is None def test_detail_hands_on_a_word_for_remote_it_does_not_know() -> None: row = workday.rows(_load("workday.json"), SLUG)[0] assert workday.detail(_get_of(_detail(remoteType="No"), []), SLUG, row)["remote"] == "No" def test_detail_refuses_a_row_that_is_not_a_role_of_the_board() -> None: asked: list[str] = [] for url in ("https://evil.example/zendesk/job/x_R1", f"https://{HOST}/other/job/x_R1", f"https://{HOST}/zendesk/job/../../x", f"https://{HOST}/zendesk", f"https://{HOST}/zendesk/job/x?y=1", ""): with pytest.raises(ValueError): workday.detail(_get_of(b"{}", asked), SLUG, {"url": url}) assert asked == [] def test_detail_refuses_an_answer_that_is_not_a_role() -> None: row = workday.rows(_load("workday.json"), SLUG)[0] with pytest.raises(ValueError, match="jobPostingInfo"): workday.detail(_get_of(b'{"errorCode": "S21"}', []), SLUG, row) def test_detail_lets_a_404_through_for_the_shared_code_to_count_as_read() -> None: row = workday.rows(_load("workday.json"), SLUG)[0] request = httpx.Request("GET", f"https://{HOST}/") def get(url: str, **kwargs: Any) -> bytes: raise httpx.HTTPStatusError("404", request=request, response=httpx.Response(404, request=request)) with pytest.raises(httpx.HTTPStatusError): workday.detail(get, SLUG, row) def test_detail_is_read_through_the_getter_ats_binds( monkeypatch: pytest.MonkeyPatch) -> None: seen: list[tuple[str, str, str]] = [] def fake_get(client: Any, url: str, host: str, slug: str, *, method: str = "GET", **kwargs: Any) -> bytes: seen.append((host, slug, url)) return (FIXTURES / "workday-detail.json").read_bytes() monkeypatch.setattr(ats, "_get", fake_get) row = workday.rows(_load("workday.json"), SLUG)[0] got = workday.detail(ats._getter(object(), "workday", SLUG), SLUG, row) # type: ignore[arg-type] assert got["remote"] == "Fully Flexible" assert seen[0][:2] == ("workday", SLUG) # ------------------------------------------- slugs, hosts and robots.txt GOOD = ["lego.wd103/LEGO_External", "redhat.wd5/jobs", "unitytech.wd1/Unity", "gn.wd3/GN-Careers", "misterspex.wd103/Mister_Spex_Careers", "a-b.wd1/x"] @pytest.mark.parametrize("slug", GOOD) def test_a_slug_is_tenant_data_centre_and_site(slug: str) -> None: assert workday.slug_ok(slug) and ats.is_known_target(f"workday:{slug}") @pytest.mark.parametrize("slug", [ "", "lego", "lego.wd103", "lego.wd103/", "lego.wd103/LEGO_External/", "/LEGO_External", "lego.wd/LEGO_External", "lego.wdx/x", "lego.WD103/x", "LEGO.wd103/x", "le_go.wd103/x", "lego.wd103/LEGO External", " lego.wd103/x", "lego.wd103/x ", "lego.wd103/x\n", "lego.wd103/x\r\n", "lego.wd103\n/x", "lego.wd103/x/y", "lego.wd103/x?y=1", "lego.wd103/x#y", "lego.wd103/..", "lego.wd103/.", "lego.wd103/x%2fy", "lego.wd103/%2f", "lego.wd103/x@evil.example", "lego@evil.example.wd103/x", "lego.wd103:443/x", "lego.wd103/x:80", "localhost/x", "lego.wd103.example.com/x", "127.0.0.1/x", "127.wd1/../x", "lego.evil.wd103/x", "lego..wd103/x", "lego.wd103/x;y", "lego.wd103/x\\y", ]) def test_a_hostile_or_malformed_slug_is_refused(slug: str) -> None: assert not workday.slug_ok(slug) assert not ats.is_known_target(f"workday:{slug}") assert not workday.allowed(slug)(f"{slug.split('/')[0].split('@')[-1]}.myworkdayjobs.com") def test_a_fetch_may_reach_the_tenants_own_host_only() -> None: allow = workday.allowed("lego.wd103/LEGO_External") assert allow("lego.wd103.myworkdayjobs.com") and allow("LEGO.WD103.MYWORKDAYJOBS.COM") for other in ("lego.wd3.myworkdayjobs.com", "novonesis.wd103.myworkdayjobs.com", "wd103.myworkdayjobs.com", "myworkdayjobs.com", "lego.wd103.evil.example", "lego.wd103.myworkdayjobs.com.evil.example", "evil.lego.wd103.myworkdayjobs.com", "www.lego.wd103.myworkdayjobs.com", "localhost", "127.0.0.1", ""): assert not allow(other), other assert ats._allowed_host("workday", "lego.wd103/LEGO_External")("lego.wd103.myworkdayjobs.com") assert not ats._allowed_host("workday", "lego.wd103/LEGO_External")( "redhat.wd5.myworkdayjobs.com") def test_a_redirect_off_the_tenant_is_refused() -> None: def handler(request: httpx.Request) -> httpx.Response: return httpx.Response(302, headers={"location": "https://evil.example/wday"}) with httpx.Client(transport=httpx.MockTransport(handler)) as client, \ pytest.raises(ats._Refused): ats._get(client, "https://lego.wd103.myworkdayjobs.com/wday/cxs/lego/LEGO_External/jobs", "workday", "lego.wd103/LEGO_External", method="POST", payload={}) def test_the_case_of_the_site_is_kept() -> None: assert workday.KEEP_CASE is True assert workday.careers_url("lego.wd103/LEGO_External") == \ "https://lego.wd103.myworkdayjobs.com/LEGO_External" assert ats.careers_url("workday:redhat.wd5/jobs") == \ "https://redhat.wd5.myworkdayjobs.com/jobs" assert "workday" in ats.HOSTS and ats.HOSTS["workday"] == "Workday" def test_the_robots_record_names_the_api_path() -> None: """Decided when the reader was built (`READS.robots`), for the paths it requests, not only the career site's.""" assert "/wday/cxs/