diff --git a/plugins/atshosts/workday.py b/plugins/atshosts/workday.py new file mode 100644 index 0000000..faf45c4 --- /dev/null +++ b/plugins/atshosts/workday.py @@ -0,0 +1,230 @@ +"""Workday: `workday:./`, for example +`workday:lego.wd103/LEGO_External` (the career site +https://lego.wd103.myworkdayjobs.com/LEGO_External), paged by offset with a +total that only the first page states. + +The tenant is what the company was named when Workday was set up and is not +always the company (`unitytech`, `novonesis`); the data centre is not +derivable either, and a tenant has several sites, so all three are stored +as they are found. The site is case-insensitive for the API (Red Hat's +`jobs` and `Jobs` both work) but its robots.txt paths are not, so the slug +keeps the case the URL wrote (`KEEP_CASE`). + +Written from live career sites on 2026-09-29 (`docs/plan-hosts/workday.md`). +The listing carries no description, so `detail` reads each role's own JSON. +""" + +from __future__ import annotations + +import html +import json +import re +from collections.abc import Callable +from datetime import UTC, datetime, timedelta +from typing import Any + +from desk.sources import strip_html + +from ._shared import Get, Page, Row, only, parse_dt + +NAME = "workday" +LABEL = "Workday" +KEEP_CASE = True + +#: One group for `.` and one for the site, which discovery joins +#: into the slug with "/". A locale segment (`/en-US/`, `/de-DE/`) may come +#: between; it is matched case-sensitively, and a bare `/en-US` that is no +#: site at all is refused, so a site is never mistaken for a locale or the +#: other way round. `wday/` is the API path, read by the second pattern. The +#: tenant is lower case only: a slug with capitals in it is no slug. +_TENANT = r"(?-i:[a-z0-9-]+)" +_LOCALE = r"(?-i:[a-z]{2}-[A-Z]{2})" +PATTERNS = ( + rf"(? tuple[str, str, str] | None: + """`(tenant, data centre, site)`, or None for a slug that is none.""" + match = _SLUG.fullmatch(slug) + return match.groups() if match else None # type: ignore[return-value] + + +def _host(slug: str) -> str: + tenant, dc, _site = _parts(slug) or ("", "", "") + return f"{tenant}.{dc}.myworkdayjobs.com" + + +def slug_ok(slug: str) -> bool: + return _parts(slug) is not None + + +def allowed(slug: str) -> Callable[[str], bool]: + """Only the tenant's own host. A slug that is none admits nothing, rather + than a host the company does not own.""" + return only(_host(slug)) if _parts(slug) else (lambda host: False) + + +def careers_url(slug: str) -> str: + return f"https://{_host(slug)}/{_parts(slug)[2]}" # type: ignore[index] + + +def robots_page(slug: str) -> str: + """With the trailing slash, since the rules the tenants write are + `Disallow: //` and read as prefixes: `/` alone would slip + past the very rule that forbids the site.""" + return careers_url(slug) + "/" + + +def page(get: Get, slug: str, cursor: Any) -> Page: + """`cursor` is `(offset, total)`. `total` is read on the first page only: + on the later ones it is 0 on most tenants and the real figure on a few, + so it is carried. An offset at or past the end does not answer an empty + list but the first page again, so the read stops on `offset >= total` + rather than on an empty page.""" + tenant, _dc, site = _parts(slug) # type: ignore[misc] + offset, total = cursor or (0, None) + data = json.loads(get( + f"https://{_host(slug)}/wday/cxs/{tenant}/{site}/jobs", method="POST", + payload={"appliedFacets": {}, "limit": PAGE_SIZE, "offset": offset, "searchText": ""})) + if cursor is None: + stated = data.get("total") + total = stated if isinstance(stated, int) and not isinstance(stated, bool) else None + postings = data.get("jobPostings") or [] + # Every posting is counted, a path-less one (LEGO lists one that has only + # `bulletFields`) by its position, so the parser dropping it does not + # leave the board short of its total. + results = [{"id": _posting_id(p, offset + i)} for i, p in enumerate(postings)] + following = offset + len(postings) + more = bool(postings) and (total is None or following < total) + return data, results, total, (following, total) if more else None + + +def _posting_id(posting: dict[str, Any], position: int) -> str: + return str(posting.get("externalPath") or "".join(posting.get("bulletFields") or []) + or f"#{position}") + + +def _posted(text: str) -> datetime | None: + """The day of "Posted Today", "Posted Yesterday" and "Posted 5 Days Ago" + at midnight UTC; None for "Posted 30+ Days Ago", which says only that it + is older, and for a text in another language. Workday counts the days in + its own time zone, so the day can be one off; `detail` states the date + itself and replaces this one.""" + match = re.fullmatch(r"Posted (?:(Today)|(Yesterday)|(\d+) Days? Ago)", (text or "").strip()) + if not match: + return None + days = 0 if match.group(1) else 1 if match.group(2) else int(match.group(3)) + return _today() - timedelta(days=days) + + +def _today() -> datetime: + return datetime.now(UTC).replace(hour=0, minute=0, second=0, microsecond=0) + + +#: `remoteType` -> `store._remote`'s words. Only some tenants send it, and +#: each words it its own way (Zendesk's "Fully Flexible", Axis' "On-Site"); a +#: word not listed, a bare "No" among them, says nothing and stays unknown. +_REMOTE = {"remote": "remote", "fully flexible": "remote", "hybrid": "hybrid", + "in office": "onsite", "on-site": "onsite", "onsite": "onsite"} + + +def _requisition(posting: dict[str, Any], path: str) -> str: + """The requisition id, which `bulletFields` lead with, if the path ends in + it (`..._R-059566`, and `..._R-059566-2` for a repost of it). Where a + tenant puts something else first, a place, the path is the id.""" + first = next(iter(posting.get("bulletFields") or []), None) + name = path.rsplit("/", 1)[-1] + return first if isinstance(first, str) and re.fullmatch( + rf".*_{re.escape(first)}(?:-\d+)?", name) else "" + + +def _remote(value: Any) -> str | None: + return _REMOTE.get(str(value or "").strip().lower()) + + +def rows(decoded: dict[str, Any], slug: str) -> list[Row]: + """One page of `.../wday/cxs///jobs` -> `{"total", + "jobPostings": [...], "facets": [...]}`. A posting has `title`, + `externalPath` (`/job//_<requisition id>`), `locationsText` + (a place, or "2 Locations"), `postedOn` (relative text) and + `bulletFields`, which lead with the requisition id, and on some tenants + `remoteType`. No description, no date and no country: `detail` has + those. The id is the requisition id where the path carries it, and the + path where not; a posting with no title or no path is skipped.""" + out = [] + for p in decoded.get("jobPostings") or []: + title = (p.get("title") or "").strip() + path = (p.get("externalPath") or "").strip() + if not title or not _ROLE_PATH.fullmatch(path): + continue + out.append({ + "source_id": _requisition(p, path) or path, + "title": title, + "location": (p.get("locationsText") or "").strip(), + "url": f"https://{_host(slug)}/{_parts(slug)[2]}{path}", # type: ignore[index] + "posted": _posted(p.get("postedOn") or ""), + "department": "", + "description": "", + "remote": _remote(p.get("remoteType")), + "salary_text": "", # no field for pay, only the description's text + }) + return out + + +def detail(get: Get, slug: str, row: Row) -> dict[str, Any]: + """The role's own JSON, `.../wday/cxs/<tenant>/<Site><externalPath>`. The + path is taken back off the row's URL, which was built from it. Pay is in + the description text when it is anywhere: there is no field for it on + any tenant. `startDate` is the posting date (it matched the "Posted N + Days Ago" text in every sample), not a start of employment.""" + tenant, _dc, site = _parts(slug) # type: ignore[misc] + prefix = f"https://{_host(slug)}/{site}" + path = str(row.get("url") or "").removeprefix(prefix) + if path == row.get("url") or not _ROLE_PATH.fullmatch(path) or ".." in path.split("/"): + raise ValueError(f"{row.get('url')!r} is not a role of {slug}") + info = json.loads(get(f"https://{_host(slug)}/wday/cxs/{tenant}/{site}{path}")) + posting = info.get("jobPostingInfo") + if not isinstance(posting, dict): + raise ValueError("not a Workday role: no jobPostingInfo") + out: dict[str, Any] = {} + if description := " ".join(html.unescape(strip_html(posting.get("jobDescription") or "")) + .split()): + out["description"] = description + if location := _location(posting): + out["location"] = location + if posted := parse_dt(posting.get("startDate")): + out["posted"] = posted + if remote := _remote(posting.get("remoteType")): + out["remote"] = remote + return out + + +def _location(posting: dict[str, Any]) -> str: + """The main location and the others, joined by "; " as Teamtailor does, so + that `country_of` reads them one at a time and the listing's "2 Locations" + is replaced by places. A single location that does not name its country + (LEGO's "Billund") gets it from the role's `country`.""" + places: list[str] = [] + for place in [posting.get("location"), *(posting.get("additionalLocations") or [])]: + place = str(place or "").strip() + if place and place not in places: + places.append(place) + country = str((posting.get("country") or {}).get("descriptor") or "").strip() + if len(places) == 1 and country and country.lower() not in places[0].lower(): + places[0] += f", {country}" + return "; ".join(places) diff --git a/tests/fixtures/ats/workday-detail.json b/tests/fixtures/ats/workday-detail.json new file mode 100644 index 0000000..ef1bc00 --- /dev/null +++ b/tests/fixtures/ats/workday-detail.json @@ -0,0 +1,38 @@ +{ + "jobPostingInfo": { + "id": "a3e856e804b51001b17f97f048070000", + "title": "Associate Solutions Architect", + "jobDescription": "<h2>Job Description</h2><p style=\"text-align:inherit\"></p><br /><p><span style=\"color:#000000\">The Associate Solutions Architect supports the CX ARR and NRR business by providing practical technical partnership across new business, renewals, and expansion opportunities. This junior, customer-facing role is designed for a technically curious professional developing expertise in solutions architecture, pre-sales, implementation planning, and consultative customer engagement.</span></p><p><span style=\"color:#000000\">The role helps customers understand how the CX platform, AI capabilities, automation, and integrations can support business outcomes while ensuring proposed solutions are credible, secure, and practical to implement.</span></p><br /><p><span style=\"color:#000000\"><b>Business Impact</b></span></p><ul><li style=\"color:#000000 !important\"><span style=\"color:#000000\">Support new business opportunities that contribute to Annual Recurring Revenue (ARR).</span></li><li style=\"color:#000000 !important\"><span style=\"color:#000000\">Support renewals, adoption, and expansion opportunities that contribute to Net Revenue Retention (NRR).</span></li><li style=\"color:#000000 !important\"><span style=\"color:#000000\">Create a consistent and scalable technical experience across the Mexico market.</span></li></ul><br /><p><span style=\"color:#000000\"><b>Key Responsibilities</b></span></p>", + "location": "Remote, Mexico City, Mexico", + "postedOn": "Posted Today", + "startDate": "2026-09-29", + "timeType": "Full time", + "jobReqId": "R35358", + "jobPostingId": "Associate-Solutions-Architect_R35358", + "jobPostingSiteId": "zendesk", + "country": { + "descriptor": "Mexico", + "id": "e2adff9272454660ac4fdb56fc70bb51" + }, + "canApply": true, + "posted": true, + "includeResumeParsing": true, + "jobRequisitionLocation": { + "descriptor": "Mexico - Mexico City (State) - Fully Flexible", + "country": { + "descriptor": "Mexico", + "id": "e2adff9272454660ac4fdb56fc70bb51", + "alpha2Code": "MX" + } + }, + "remoteType": "Fully Flexible", + "externalUrl": "https://zendesk.wd1.myworkdayjobs.com/zendesk/job/Remote-Mexico-City-Mexico/Associate-Solutions-Architect_R35358", + "questionnaireId": "751e02c2f2681000bb9033f4aad10000" + }, + "hiringOrganization": { + "name": "Zendesk, S. De R.L. DE CV (Mexico)", + "url": "" + }, + "similarJobs": [], + "userAuthenticated": false +} diff --git a/tests/fixtures/ats/workday-redhat-detail.json b/tests/fixtures/ats/workday-redhat-detail.json new file mode 100644 index 0000000..9843ff5 --- /dev/null +++ b/tests/fixtures/ats/workday-redhat-detail.json @@ -0,0 +1,42 @@ +{ + "jobPostingInfo": { + "id": "a31634aef9d51000f29d88d2864f0001", + "title": "Telco Cloud Consultant", + "jobDescription": "<h2>About the Job:</h2><p>The Red Hat Consulting team is looking for a Telco Cloud Consultant to join us in Italy. In this role, you will guide the adoption of Red Hat’s solutions in our key telco service provider customers, network equipment providers (NEPs), and key system integrator (SI) partners. You will focus on delivering quality work resulting in a positive customer experience, while building broad and deep technical expertise in Red Hat cloud technologies. As a Telco Cloud Consultant, you’ll need to have a thorough working knowledge of and direct experience with cloud infrastructures to support applications providing containerised and virtual network functions (CNFs and VNF) to the telco industry, you'll gain an understanding of our customers' infrastructure with a particular focus on Red Hat OpenShift Container Platform, Red Hat OpenStack Platform and other Red Hat cloud technologies and make recommendations appropriate to their environment.</p>", + "location": "Remote Italy", + "additionalLocations": [ + "Remote Spain" + ], + "postedOn": "Posted Today", + "startDate": "2026-09-29", + "timeType": "Full time", + "jobReqId": "R-059566", + "jobPostingId": "Telco-Cloud-Consultant_R-059566-2", + "jobPostingSiteId": "Jobs", + "country": { + "descriptor": "Italy", + "id": "8cd04a563fd94da7b06857a79faaf815" + }, + "canApply": true, + "posted": true, + "includeResumeParsing": true, + "jobRequisitionLocation": { + "descriptor": "Remote Italy", + "country": { + "descriptor": "Italy", + "id": "8cd04a563fd94da7b06857a79faaf815", + "alpha2Code": "IT" + } + }, + "remoteType": "Remote", + "externalUrl": "https://redhat.wd5.myworkdayjobs.com/Jobs/job/Remote-Italy/Telco-Cloud-Consultant_R-059566-2", + "questionnaireId": "b7809aee755e1056263efe3362a40000", + "endDate": "2026-10-20" + }, + "hiringOrganization": { + "name": "Red Hat S.R.L.", + "url": "" + }, + "similarJobs": [], + "userAuthenticated": false +} diff --git a/tests/fixtures/ats/workday-redhat.json b/tests/fixtures/ats/workday-redhat.json new file mode 100644 index 0000000..346824c --- /dev/null +++ b/tests/fixtures/ats/workday-redhat.json @@ -0,0 +1,46 @@ +{ + "total": 145, + "jobPostings": [ + { + "title": "Associate Deal Management Analyst", + "externalPath": "/job/Raleigh/Associate-Deal-Management-Analyst_R-059377", + "locationsText": "Raleigh", + "postedOn": "Posted Today", + "remoteType": "Onsite", + "bulletFields": [ + "R-059377" + ] + }, + { + "title": "Consulting Services Account Specialist", + "externalPath": "/job/Remote-US-TX/Consulting-Services-Account-Specialist_R-058740-1", + "locationsText": "Remote US TX", + "postedOn": "Posted Today", + "remoteType": "Remote", + "bulletFields": [ + "R-058740" + ] + }, + { + "title": "Telco Cloud Consultant", + "externalPath": "/job/Remote-Italy/Telco-Cloud-Consultant_R-059566-2", + "locationsText": "2 Locations", + "postedOn": "Posted Today", + "remoteType": "Remote", + "bulletFields": [ + "R-059566" + ] + }, + { + "title": "Consulting Services Account Specialist", + "externalPath": "/job/Seoul/Consulting-Services-Account-Specialist_R-058743-2", + "locationsText": "Seoul", + "postedOn": "Posted 30+ Days Ago", + "remoteType": "Hybrid", + "bulletFields": [ + "R-058743" + ] + } + ], + "userAuthenticated": false +} diff --git a/tests/fixtures/ats/workday-saxo.json b/tests/fixtures/ats/workday-saxo.json new file mode 100644 index 0000000..f676dcc --- /dev/null +++ b/tests/fixtures/ats/workday-saxo.json @@ -0,0 +1,51 @@ +{ + "total": 50, + "jobPostings": [ + { + "title": "Client Support and Sales - Singapore", + "externalPath": "/job/Singapore/Client-Support-and-Sales---Singapore_R-19507", + "locationsText": "Singapore", + "postedOn": "Posted Yesterday", + "bulletFields": [ + "R-19507" + ] + }, + { + "title": "Senior Site Reliability Engineer (SRE)- JSS", + "externalPath": "/job/Gurugram/DevOps-Engineer--JSS_R-19142", + "locationsText": "Gurugram", + "postedOn": "Posted Yesterday", + "bulletFields": [ + "R-19142" + ] + }, + { + "title": "Junior Relationship Manager", + "externalPath": "/job/Prague/Junior-Relationship-Manager_R-19427", + "locationsText": "Prague", + "postedOn": "Posted 4 Days Ago", + "bulletFields": [ + "R-19427" + ] + }, + { + "title": "Senior Post Trade Analyst", + "externalPath": "/job/Gurugram/Senior-Post-Trade-Analyst_R-19402-1", + "locationsText": "Gurugram", + "postedOn": "Posted 4 Days Ago", + "bulletFields": [ + "R-19402" + ] + }, + { + "title": "Market Data Analyst", + "externalPath": "/job/Gurugram/Market-Data-Analyst_R-19359-1", + "locationsText": "Gurugram", + "postedOn": "Posted 4 Days Ago", + "bulletFields": [ + "R-19359" + ] + } + ], + "userAuthenticated": false +} diff --git a/tests/fixtures/ats/workday.json b/tests/fixtures/ats/workday.json new file mode 100644 index 0000000..ed913d6 --- /dev/null +++ b/tests/fixtures/ats/workday.json @@ -0,0 +1,56 @@ +{ + "total": 124, + "jobPostings": [ + { + "title": "Associate Solutions Architect", + "externalPath": "/job/Remote-Mexico-City-Mexico/Associate-Solutions-Architect_R35358", + "locationsText": "Remote, Mexico City, Mexico", + "postedOn": "Posted Today", + "remoteType": "Fully Flexible", + "bulletFields": [ + "R35358" + ] + }, + { + "title": "Business Development Representative", + "externalPath": "/job/Lisbon-Portugal/Business-Development-Representative_R35239", + "locationsText": "Lisbon, Portugal", + "postedOn": "Posted Today", + "remoteType": "In Office", + "bulletFields": [ + "R35239" + ] + }, + { + "title": "Sales Development Representative", + "externalPath": "/job/Lisbon-Portugal/Sales-Development-Representative_R35277", + "locationsText": "Lisbon, Portugal", + "postedOn": "Posted Today", + "remoteType": "Hybrid", + "bulletFields": [ + "R35277" + ] + }, + { + "title": "FSS RevOps Customer Support Sr. Specialist", + "externalPath": "/job/Manila-Philippines/FSS-RevOps-Customer-Support-Sr-Specialist_R35551-1", + "locationsText": "Manila, Philippines", + "postedOn": "Posted Today", + "remoteType": "Fully Flexible", + "bulletFields": [ + "R35551" + ] + }, + { + "title": "FSS RevOps Deal Support Sr. Specialist", + "externalPath": "/job/Manila-Philippines/FSS-Revenue-Operations-Sr-Specialist_R35544-1", + "locationsText": "Manila, Philippines", + "postedOn": "Posted Today", + "remoteType": "Hybrid", + "bulletFields": [ + "R35544" + ] + } + ], + "userAuthenticated": false +} diff --git a/tests/test_ats_workday.py b/tests/test_ats_workday.py new file mode 100644 index 0000000..ecdff47 --- /dev/null +++ b/tests/test_ats_workday.py @@ -0,0 +1,533 @@ +"""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, robots.txt 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.atshosts import 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"] == "" + # "Fully Flexible" is Zendesk's word for remote. + assert [r["remote"] for r in rows] == ["remote", "onsite", "hybrid", "remote", "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" + assert rows[2]["location"] == "2 Locations" + 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_is_skipped() -> 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"]}, + ]} + assert [r["title"] for r in workday.rows(page, SLUG)] == ["Whole"] + + +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_page_that_is_not_json_fails_the_read( + monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: + monkeypatch.setattr(ats, "_get", lambda *a, **k: b"<html>maintenance</html>") + 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) <= set(ats.DETAIL_FIELDS) + assert set(got) == {"description", "location", "posted", "remote"} + assert got["posted"] == datetime(2026, 9, 29, tzinfo=UTC) + assert got["remote"] == "remote" + assert got["location"] == "Remote, Mexico City, Mexico" + 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" + + +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="<p>Trade & settle</p>", location="Singapore", + startDate="2026-09-28"), []), + "saxobank.wd3/CareeratSaxoBank", row) + assert got == {"description": "Trade & settle", "location": "Singapore", + "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"} + + +def test_detail_takes_no_word_for_remote_it_does_not_know() -> None: + row = workday.rows(_load("workday.json"), SLUG)[0] + assert "remote" not in workday.detail(_get_of(_detail(remoteType="No"), []), SLUG, row) + + +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"] == "remote" + 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_robots_page_is_the_site_with_its_closing_slash() -> None: + page = workday.robots_page("novonesis.wd103/Novonesis_Careers") + assert page == "https://novonesis.wd103.myworkdayjobs.com/Novonesis_Careers/" + assert httpx.URL(page).host == "novonesis.wd103.myworkdayjobs.com" + assert workday.allowed("novonesis.wd103/Novonesis_Careers")(httpx.URL(page).host) + + +@pytest.mark.parametrize(("slug", "robots", "allowed"), [ + # As the tenants write them: a site that is disallowed, and one that is not. + ("novonesis.wd103/Novonesis_Careers", + "User-agent: *\nDisallow: /Novonesis_Careers/\nDisallow: /Novonesis_Careers_Private/\n" + "Disallow: /refreshFacet/\n", False), + ("darktrace.wd3/DarktaceExternal", "User-agent: *\nDisallow: /DarktaceExternal/\n", False), + ("lego.wd103/LEGO_External", + "Sitemap: https://lego.wd103.myworkdayjobs.com/LEGO_External/siteMap.xml\n" + "User-agent: *\nAllow: /LEGO_External/\nDisallow: /LEGO_Executive/\n" + "Disallow: /refreshFacet/\n", True), + # Paths are case-sensitive: Red Hat's `Jobs` rule and its site `jobs`. + ("redhat.wd5/jobs", "User-agent: *\nAllow: /Jobs/\nDisallow: /refreshFacet/\n", True), + ("zendesk.wd1/zendesk", "User-agent: *\nAllow: /zendesk/\nDisallow: /refreshFacet/\n", True), +]) +def test_robots_txt_is_read_against_the_site_page(slug: str, robots: str, allowed: bool) -> None: + assert ats._robots_allows(robots.encode(), workday.robots_page(slug)) is allowed + + +def test_the_site_page_without_its_slash_would_have_slipped_past_the_rule() -> None: + """Why `robots_page` ends in a slash.""" + rule = b"User-agent: *\nDisallow: /Novonesis_Careers/\n" + assert ats._robots_allows(rule, "https://novonesis.wd103.myworkdayjobs.com/Novonesis_Careers") + assert not ats._robots_allows( + rule, "https://novonesis.wd103.myworkdayjobs.com/Novonesis_Careers/") + + +# -------------------------------------------------------------- discovery + +def _found(text: str) -> list[tuple[str, str]]: + return [(host, slug) for host, slug, _url in discover.boards_in(text)] + + +#: The twelve career sites of `docs/plan-hosts/workday.md`, as a person opens +#: them, and the slug discovery names for each. +TWELVE = [ + ("https://zendesk.wd1.myworkdayjobs.com/zendesk", "zendesk.wd1/zendesk"), + ("https://unitytech.wd1.myworkdayjobs.com/Unity", "unitytech.wd1/Unity"), + ("https://lego.wd103.myworkdayjobs.com/LEGO_External", "lego.wd103/LEGO_External"), + ("https://saxobank.wd3.myworkdayjobs.com/CareeratSaxoBank", + "saxobank.wd3/CareeratSaxoBank"), + ("https://novonesis.wd103.myworkdayjobs.com/Novonesis_Careers", + "novonesis.wd103/Novonesis_Careers"), + ("https://gn.wd3.myworkdayjobs.com/GN-Careers", "gn.wd3/GN-Careers"), + ("https://equinor.wd3.myworkdayjobs.com/EQNR", "equinor.wd3/EQNR"), + ("https://axis.wd3.myworkdayjobs.com/External_Career_Site", + "axis.wd3/External_Career_Site"), + ("https://misterspex.wd103.myworkdayjobs.com/de-DE/Mister_Spex_Careers", + "misterspex.wd103/Mister_Spex_Careers"), + ("https://darktrace.wd3.myworkdayjobs.com/DarktaceExternal", + "darktrace.wd3/DarktaceExternal"), + ("https://criteo.wd3.myworkdayjobs.com/Criteo_Career_Site", + "criteo.wd3/Criteo_Career_Site"), + ("https://redhat.wd5.myworkdayjobs.com/jobs", "redhat.wd5/jobs"), +] + + +@pytest.mark.parametrize(("url", "slug"), TWELVE) +def test_discovery_finds_the_slug_of_each_career_site(url: str, slug: str) -> None: + assert _found(url) == [("workday", slug)] + assert workday.slug_ok(slug) + + +@pytest.mark.parametrize("url", [ + "https://lego.wd103.myworkdayjobs.com/en-US/LEGO_External", + "https://lego.wd103.myworkdayjobs.com/de-DE/LEGO_External/", + "https://lego.wd103.myworkdayjobs.com/en-GB/LEGO_External/job/Billund/Analyst_R1", + "https://lego.wd103.myworkdayjobs.com/LEGO_External/job/Billund/Analyst_R1?source=x", + "https://lego.wd103.myworkdayjobs.com/wday/cxs/lego/LEGO_External/jobs", + '<a href="https://lego.wd103.myworkdayjobs.com/en-US/LEGO_External">Jobs</a>', + "lego.wd103.myworkdayjobs.com/LEGO_External", +]) +def test_discovery_finds_a_site_with_or_without_a_locale_or_a_role_path(url: str) -> None: + assert _found(url) == [("workday", "lego.wd103/LEGO_External")] + + +def test_discovery_finds_a_site_named_jobs_with_and_without_a_locale() -> None: + for url in ("https://redhat.wd5.myworkdayjobs.com/jobs", + "https://redhat.wd5.myworkdayjobs.com/en-US/jobs", + "https://redhat.wd5.myworkdayjobs.com/en-US/jobs/job/Raleigh/x_R-1"): + assert _found(url) == [("workday", "redhat.wd5/jobs")], url + + +@pytest.mark.parametrize("url", [ + "https://lego.wd103.myworkdayjobs.com", "https://lego.wd103.myworkdayjobs.com/", + "https://lego.wd103.myworkdayjobs.com/en-US", "https://lego.wd103.myworkdayjobs.com/en-US/", + "https://wd5.myworkdayjobs.com/recruiting/acme/site", + "https://lego.myworkdayjobs.com/LEGO_External", + "https://LEGO.wd103.myworkdayjobs.com/LEGO_External", + "https://x.lego.wd103.myworkdayjobs.com.evil.example/LEGO_External", +]) +def test_discovery_names_no_board_where_there_is_no_site_or_no_tenant(url: str) -> None: + assert _found(url) == [] + + +def test_discovery_keeps_the_case_of_the_site_and_names_each_board_once() -> None: + text = ("https://lego.wd103.myworkdayjobs.com/LEGO_External " + "https://lego.wd103.myworkdayjobs.com/en-US/LEGO_External/job/x " + "https://lego.wd103.myworkdayjobs.com/LEGO_Executive") + assert _found(text) == [("workday", "lego.wd103/LEGO_External"), + ("workday", "lego.wd103/LEGO_Executive")] + assert "workday" in discover.READABLE