"""The SuccessFactors host: `plugins/readers/successfactors.py` against saved career-site pages. No network. The fixtures are trimmed pages of Vestas and SAP (table layout), Demant and Scania (tile layout), Teradyne (a shared site with a path prefix), and the job pages of Vestas (`addressCountry`), Systematic (`streetAddress`) and Novo Nordisk (no microdata).""" from __future__ import annotations 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.discover import boards_in # noqa: E402 from plugins import ats, readers # noqa: E402 from plugins.readers import successfactors # noqa: E402 FIXTURES = ROOT / "tests" / "fixtures" / "ats" def _read(name: str) -> str: return (FIXTURES / name).read_text(encoding="utf-8") # ------------------------------------------------------------------- rows def test_the_table_layout_gives_title_location_date_and_department() -> None: rows = successfactors.rows(_read("successfactors-table.html"), "careers.vestas.com") assert len(rows) == 5 first = rows[0] assert first["source_id"] == "1431772533" assert first["title"] == "Director, Service Commercial" # The site's "City, Region, CC, zip" becomes the country's name, so that # `country_of` reads it (a bare "DE" would be Delaware), and the postcode goes. assert first["location"] == "Seoul, Seoul-teukpyolsi, South Korea" assert first["url"] == ("https://careers.vestas.com/job/" "Seoul-Director%2C-Service-Commercial-Seou-04323/1431772533/") assert first["department"] == "Commercial" assert first["posted"] == datetime(2026, 9, 29, tzinfo=UTC) # "29 Sept 2026" assert first["description"] == "" and first["salary_text"] == "" assert first["remote"] is None def test_a_row_is_listed_once_though_the_table_links_it_twice() -> None: """The title link is in the row twice, once for the phone.""" text = _read("successfactors-table.html") assert text.count("/1431772533/") >= 2 rows = successfactors.rows(text, "careers.vestas.com") assert len({r["source_id"] for r in rows}) == len(rows) == 5 def test_a_table_without_a_date_or_department_column_still_reads() -> None: """SAP's own site: a title and a location, and an apostrophe in the link which the page writes as an entity.""" rows = successfactors.rows(_read("successfactors-table-nodate.html"), "careers.sap.com") assert len(rows) == 4 assert all(r["posted"] is None and r["department"] == "" for r in rows) assert rows[0]["location"] == "Budapest, Hungary" assert rows[1]["url"].startswith("https://careers.sap.com/job/'s-Hertogenbosch-HR-") assert rows[1]["source_id"] == "1401271733" def test_the_tile_layout_gives_the_city_and_the_country_of_its_own_fields() -> None: """Demant's tiles have a "Location" that is the city and a "Country" that is its name; the job link carries a brand segment.""" rows = successfactors.rows(_read("successfactors-tile.html"), "careers.demant.com") assert len(rows) == 5 first = rows[0] assert first["source_id"] == "1431925433" assert first["title"] == "Regulatory Affairs Specialist" assert first["location"] == "Szczecin, Poland" assert first["posted"] == datetime(2026, 9, 29, tzinfo=UTC) assert first["url"].startswith("https://careers.demant.com/DemantPoland/job/Szczecin-") assert first["department"] == "" # "Brand" is not a department def test_a_tile_location_that_ends_in_a_country_code_is_read_with_its_name() -> None: rows = successfactors.rows(_read("successfactors-tile-scania.html"), "jobs.scania.com") assert len(rows) == 3 assert rows[0]["location"] == "Eichelbergstr. 4, 09212 Limbac, Germany" assert rows[1]["location"] == "Tonalá, JAL, Mexico" assert rows[1]["department"] == "Parts and Service" # the `department` field assert rows[1]["posted"] == datetime(2026, 9, 29, tzinfo=UTC) # "Sep 29, 2026" @pytest.mark.parametrize(("text", "want"), [ ("29 Sept 2026", datetime(2026, 9, 29, tzinfo=UTC)), ("5 Jan 2026", datetime(2026, 1, 5, tzinfo=UTC)), ("Sep 29, 2026", datetime(2026, 9, 29, tzinfo=UTC)), ("2026/09/29", datetime(2026, 9, 29, tzinfo=UTC)), ("29.09.2026", datetime(2026, 9, 29, tzinfo=UTC)), ("09/29/2026", None), # day or month first is not guessed ("31 Foo 2026", None), ("", None), ]) def test_a_listing_date_is_read_as_the_site_writes_it(text: str, want: datetime | None) -> None: assert successfactors._date(text) == want @pytest.mark.parametrize(("text", "want"), [ ("Smørum, DK, 2765", "Smørum, Denmark"), ("Boston, MA, US, 02110", "Boston, MA, United States"), ("Boston, MA, US", "Boston, MA, United States"), ("Haag, AT, 4680", "Haag, Austria"), ("Accra, GH, KA DTD 200", "Accra, Ghana"), ("Angers, FR", "Angers, France"), ("Kraków", "Kraków"), ("Paris, France", "Paris, France"), ("Xyz, ZZ, 1", "Xyz, ZZ, 1"), # not a country code: as written ]) def test_a_location_ends_in_the_countrys_name(text: str, want: str) -> None: assert successfactors._place(text) == want def test_a_link_off_the_site_is_not_a_role() -> None: """A title link is taken as written, and one that names another host is dropped.""" text = _read("successfactors-tile.html").replace( 'href="/DemantPoland/job/Szczecin-Regulatory-Affairs-Specialist-zach-71-612/1431925433/"', 'href="https://elsewhere.example/job/x/1431925433/"') rows = successfactors.rows(text, "careers.demant.com") assert "1431925433" not in {r["source_id"] for r in rows} assert len(rows) == 4 same = text.replace("https://elsewhere.example/", "https://careers.demant.com/") assert "1431925433" in {r["source_id"] for r in successfactors.rows(same, "careers.demant.com")} # -------------------------------------------------------------- path prefix def test_a_prefix_keeps_the_roles_under_it_though_the_site_lists_others() -> None: """A site that does not scope on the server: the page lists Teradyne's roles of several brands, and `jobs.teradyne.com/MiR` reads the MiR ones.""" text = _read("successfactors-shared.html") everything = successfactors.rows(text, "jobs.teradyne.com") assert len(everything) == 6 mir = successfactors.rows(text, "jobs.teradyne.com/MiR") assert 0 < len(mir) < len(everything) assert all(r["url"].startswith("https://jobs.teradyne.com/MiR/job/") for r in mir) def test_a_scoped_site_lists_only_the_brand_and_states_its_total() -> None: """`/Universal-Robots/search/` answers 30 of Teradyne's 486 on the server.""" text = _read("successfactors-prefix.html") rows = successfactors.rows(text, "jobs.teradyne.com/Universal-Robots") assert len(rows) == 4 assert successfactors._total(text) == 30 assert rows[0]["location"] == "Bangalore, India" assert rows[0]["department"] == "Information Technology" # ------------------------------------------------------------------- paging def _table_page(ids: list[int], total: int, prefix: str = "") -> str: body = "".join( f'
Build things.
' 'NestedFooter
') got = successfactors.detail(lambda url, **kw: page.encode(), "careers.acme.com", {"url": "https://careers.acme.com/job/x/1/"}) assert got == {"description": "Build things. Nested"} def test_detail_of_a_page_that_is_not_a_role_is_empty() -> None: got = successfactors.detail(lambda url, **kw: b"Search", "careers.acme.com", {"url": "https://careers.acme.com/job/x/1/"}) assert got == {} def test_detail_is_read_through_the_shared_code_for_the_roles_not_read( monkeypatch: pytest.MonkeyPatch) -> None: """`ats._read_details` binds `get` and takes only `DETAIL_FIELDS`.""" text = _read("successfactors-job.html") monkeypatch.setattr(ats, "_get", lambda client, url, host, slug, **kw: text.encode()) monkeypatch.setattr(ats, "PAGE_DELAY", 0) rows = successfactors.rows(_read("successfactors-table.html"), "careers.vestas.com")[:2] out, done, failed = ats._read_details(object(), "successfactors:careers.vestas.com", # type: ignore[arg-type] rows, True, set(), set()) assert done == {f"successfactors:careers.vestas.com:{r['source_id']}" for r in rows} assert not failed assert out[0]["location"] == "Seoul, South Korea" assert out[0]["description"].startswith("Senior Management") assert out[0]["title"] == rows[0]["title"] # ------------------------------------------------------------ slugs and hosts GOOD = ["careers.vestas.com", "jobs.teradyne.com/MiR", "jobs.teradyne.com/Universal-Robots", "jobb.dnb.no", "careers.acme.co.uk", "a-b.example.com/x_y/z1"] HOSTILE = [ "careers.vestas.com@evil.example", "evil.example#careers.vestas.com", "careers.vestas.com?x=1", "careers.vestas.com/..", "careers.vestas.com/../x", "careers.vestas.com/%2f", "careers.vestas.com/a%2fb", "careers.vestas.com:8443", "careers.vestas.com:443", "careers.vestas.com ", " careers.vestas.com", "careers vestas.com", "careers.vestas.com\n", "careers.vestas.com/MiR\n", "careers.vestas.com/", "careers.vestas.com//x", "careers.vestas.com/a b", "careers.vestas.com/a.b", "careers.vestas.com\\x", "localhost", "localhost/MiR", "foo.localhost", "127.0.0.1", "127.0.0.1/MiR", "2130706433", "1.2.3.4", "[::1]", "printer.local", "router.lan", "metadata.google.internal", "nas.home.arpa", "foo.0x7f000001", "Careers.Vestas.com", "vestas", "", ".", "..", "-a.example.com", "a-.example.com", "a..example.com", "example.com.", ".example.com", ] @pytest.mark.parametrize("slug", GOOD) def test_a_dns_name_with_an_optional_prefix_is_a_slug(slug: str) -> None: assert successfactors.slug_ok(slug) assert ats.is_known_target(f"successfactors:{slug}") @pytest.mark.parametrize("slug", HOSTILE) def test_a_slug_that_could_reach_another_host_is_refused(slug: str) -> None: assert not successfactors.slug_ok(slug), repr(slug) assert not ats.is_known_target(f"successfactors:{slug}"), repr(slug) def test_allowed_admits_the_sites_own_host_only() -> None: allow = ats._allowed_host("successfactors", "jobs.teradyne.com/MiR") assert allow("jobs.teradyne.com") and allow("JOBS.teradyne.com") for other in ("teradyne.com", "www.teradyne.com", "career41.sapsf.com", "career5.successfactors.eu", "jobs.teradyne.com.evil.example", "evil.example", "127.0.0.1"): assert not allow(other), other def test_a_redirect_to_the_back_end_is_refused() -> None: """The back end's robots.txt disallows everything, so `get` never lands there, whatever the career site redirects to.""" def handler(request: httpx.Request) -> httpx.Response: return httpx.Response(302, headers={ "location": "https://career5.successfactors.eu/career?company=x"}) with httpx.Client(transport=httpx.MockTransport(handler)) as client, \ pytest.raises(ats._Refused): ats._get(client, "https://careers.vestas.com/search/", "successfactors", "careers.vestas.com") def test_careers_url_and_robots_page_are_the_search_page() -> None: assert ats.careers_url("successfactors:careers.vestas.com") == \ "https://careers.vestas.com/search/" assert ats.careers_url("successfactors:jobs.teradyne.com/MiR") == \ "https://jobs.teradyne.com/MiR/search/" for slug in GOOD: page = successfactors.robots_page(slug) assert httpx.URL(page).host == slug.split("/")[0] assert successfactors.allowed(slug)(httpx.URL(page).host) def test_the_module_is_case_sensitive_about_the_prefix() -> None: assert successfactors.KEEP_CASE is True assert readers.HOSTS["successfactors"] is successfactors assert successfactors._parts("jobs.teradyne.com/MiR") == ("jobs.teradyne.com", "/MiR") # ------------------------------------------------------------------ robots def test_the_robots_txt_of_these_sites_allows_search_and_jobs_not_services() -> None: robots = (FIXTURES / "successfactors-robots.txt").read_bytes() for slug in GOOD: assert ats._robots_allows(robots, successfactors.robots_page(slug)), slug assert ats._robots_allows(robots, "https://careers.vestas.com/job/x/1431772533/") assert not ats._robots_allows(robots, "https://careers.vestas.com/services/rss/job/") assert not ats._robots_allows(robots, "https://careers.vestas.com/talentcommunity/apply/1/") def test_a_site_that_disallows_its_search_is_refused(monkeypatch: pytest.MonkeyPatch) -> None: robots = b"User-agent: *\nDisallow: /search/\n" monkeypatch.setattr(ats, "_get", lambda client, url, host, slug, **kw: robots) with pytest.raises(ats._Disallowed): ats._check_robots(object(), "successfactors:careers.acme.com") # type: ignore[arg-type] # ---------------------------------------------------------------- discovery def test_discovery_finds_a_board_from_a_link_to_one_of_its_roles() -> None: page = ('Director') assert [(h, s) for h, s, _ in boards_in(page)] == [("successfactors", "careers.vestas.com")] @pytest.mark.parametrize(("link", "slug"), [ # The `careers_url` of the seed's companies that name a search or a landing page. ("https://careers.vestas.com/search/?q=&locationsearch=", "careers.vestas.com"), ("https://jobs.systematic.com/search/?createNewAlert=false&q=&locationsearch=DE+%2B+AT", "jobs.systematic.com"), ("https://jobs.scania.com/go/Jobs-at-Scania/9096601/", "jobs.scania.com"), ("https://careers.tryg.com/Tryg/go/All-open-positions/9394801/", "careers.tryg.com/Tryg"), ("https://careers.ovhcloud.com/search/?createNewAlert=false&q=&locationsearch=&locale=en_US", "careers.ovhcloud.com"), ]) def test_discovery_finds_a_board_from_a_link_to_its_search_or_landing_page( link: str, slug: str) -> None: assert [(h, s) for h, s, _ in boards_in(f'Jobs')] == [ ("successfactors", slug)] def test_discovery_takes_the_brand_segment_and_keeps_its_case() -> None: page = 'x' assert [(h, s) for h, s, _ in boards_in(page)] == [("successfactors", "jobs.teradyne.com/MiR")] def test_a_link_to_the_back_end_names_the_host_without_a_slug() -> None: """Bang & Olufsen's page: only the back end, which is not read, and a page of a career site names it too. The host is named, no board.""" page = 'Jobs' assert [(h, s) for h, s, _ in boards_in(page)] == [("successfactors", "")] page = '
'
assert [(h, s) for h, s, _ in boards_in(page)] == [("successfactors", "")]
@pytest.mark.parametrize("page", [
'short id',
'Greenhouse',
'Workday',
'only a search link',
])
def test_a_link_that_is_no_successfactors_role_names_nothing(page: str) -> None:
assert not [found for found in boards_in(page) if found[0] == "successfactors"]