"""The HR Manager host module against saved feeds (STS Group's, trimmed to five roles, and NNIT's, which had none), 2026-09-29. No network.""" from __future__ import annotations import sys 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 Ask, hrmanager # noqa: E402 FIXTURES = ROOT / "tests" / "fixtures" / "ats" BOARD = (FIXTURES / "hrmanager.xml").read_bytes() EMPTY = (FIXTURES / "hrmanager-empty.xml").read_bytes() FEED = "https://api.hr-manager.net/JobPortal.svc/stsgroup_tr/PositionList/rss/?incads=true" def _feed(skip: int, slug: str = "stsgroup_tr") -> str: return (f"https://api.hr-manager.net/JobPortal.svc/{slug}/PositionList/rss/" f"?incads=true&skip={skip}&take=100") def test_rows_read_the_advertisement_and_the_publication_date() -> None: rows = hrmanager.parse(BOARD, "stsgroup_tr") assert [r["source_id"] for r in rows] == ["143857", "143844", "143854", "143849", "143848"] first = rows[0] assert first["title"] == "Commissioning Engineer- T&D" # `&` in the XML assert first["url"] == ("https://candidate.hr-manager.net/ApplicationInit.aspx" "?cid=2523&ProjectId=143857&DepartmentId=18958&MediaId=5") assert first["description"].startswith("About the Position We have an exciting opportunity") assert "<" not in first["description"] and "&" not in first["description"] assert "T&D Division" in first["description"] assert first["posted"] is not None and first["posted"].isoformat() == \ "2025-03-18T17:31:36+01:00" # The feed names no place, department, pay or way of working. assert (first["location"], first["department"], first["salary_text"], first["remote"]) == \ ("", "", "", None) def test_a_channel_with_no_items_is_an_empty_board_and_another_answer_is_not_a_feed() -> None: assert hrmanager.parse(EMPTY, "nnit") == [] with pytest.raises(ValueError, match="not an HR Manager feed"): hrmanager.parse(b"404 - File or directory not found.", "x") def test_an_item_without_a_title_or_a_web_link_is_skipped() -> None: body = (b"1 https://a.example/1" b"2Bjavascript:alert(1)" b"3Chttps://a.example/3" b"") assert [r["source_id"] for r in hrmanager.parse(body, "x")] == ["3"] # ------------------------------------------------------------- paging def _paged(answers: dict[str, bytes], asked: list[str]) -> Any: def get(url: str, *, method: str = "GET", payload: Any = None, headers: dict[str, str] | None = None) -> bytes: asked.append(url) return answers[url] return get def test_the_page_moves_on_by_what_it_returned_and_ends_on_an_empty_one() -> None: asked: list[str] = [] get = _paged({_feed(0): BOARD, _feed(5): EMPTY}, asked) data, results, total, cursor = hrmanager.page(get, "stsgroup_tr", None, Ask()) assert len(results) == 5 and total is None and cursor == 5 assert len(hrmanager.rows(data, "stsgroup_tr")) == 5 _, results, total, cursor = hrmanager.page(get, "stsgroup_tr", cursor, Ask()) assert results == [] and total is None and cursor is None assert asked == [_feed(0), _feed(5)] def _crawl(get: Any, slug: str) -> list[dict[str, Any]]: """Pages until the cursor ends, as the runner does, giving the rows.""" out: list[dict[str, Any]] = [] cursor: Any = None while True: data, _results, _total, cursor = hrmanager.page(get, slug, cursor, Ask()) out += hrmanager.rows(data, slug) if cursor is None: return out def test_a_board_is_read_to_an_empty_page() -> None: asked: list[str] = [] rows = _crawl(_paged({_feed(0): BOARD, _feed(5): EMPTY}, asked), "stsgroup_tr") assert len(rows) == 5 and asked == [_feed(0), _feed(5)] def test_a_board_with_no_roles_is_empty() -> None: asked: list[str] = [] assert _crawl(_paged({_feed(0, "seaborg_tr"): EMPTY}, asked), "seaborg_tr") == [] assert asked == [_feed(0, "seaborg_tr")] def test_a_429_after_the_first_page_reaches_the_caller() -> None: request = httpx.Request("GET", _feed(5)) def get(url: str, **kwargs: Any) -> bytes: if url == _feed(5): raise httpx.HTTPStatusError("429", request=request, response=httpx.Response(429, request=request)) return BOARD with pytest.raises(httpx.HTTPStatusError): _crawl(get, "stsgroup_tr") def test_the_feed_states_no_place_and_the_robots_decision_is_the_owners() -> None: rows = hrmanager.parse(BOARD, "stsgroup_tr") assert all(r["places"] == [] for r in rows) reads = hrmanager.READS assert (reads.listing, reads.page_size, reads.text, reads.places) == ( "paged", 100, "listing", "none") assert "api.hr-manager.net" in reads.robots and "owner, 2026-09-29" in reads.robots # ------------------------------------------------------- slugs and hosts def test_the_module_is_registered_as_a_paged_host() -> None: assert readers.HOSTS["hrmanager"] is hrmanager and ats.HOSTS["hrmanager"] == "HR Manager" assert hrmanager.NAME == "hrmanager" and not getattr(hrmanager, "KEEP_CASE", False) assert hasattr(hrmanager, "page") and not hasattr(hrmanager, "detail") def test_the_urls_and_the_allowed_host() -> None: assert hrmanager.careers_url("seaborg_tr") == \ "https://candidate.hr-manager.net/vacancies/list.aspx?customer=seaborg_tr" allow = hrmanager.allowed("stsgroup_tr") assert allow("api.hr-manager.net") and allow("API.HR-MANAGER.NET") for host in ("candidate.hr-manager.net", "hr-manager.net", "api.hr-manager.net.evil.example", "localhost"): assert not allow(host) @pytest.mark.parametrize("slug", ["seaborg_tr", "kpmg", "FlyingTiger", "a-b", "9x"]) def test_a_customer_is_one_path_segment(slug: str) -> None: assert ats.is_known_target(f"hrmanager:{slug}") @pytest.mark.parametrize("slug", [ "", "a@b", "a#b", "a?b", "a&skip=1", "..", ".", "a%2fb", "%2f", "a/b", "a:443", "a b", "acme\n", " acme", "acme ", "127.0.0.1", "a.b", "_x", "-x", "a\\b", "évil", ]) def test_a_hostile_slug_is_refused(slug: str) -> None: assert not hrmanager.slug_ok(slug) assert not ats.is_known_target(f"hrmanager:{slug}") def test_discovery_finds_the_customer() -> None: page = ('Jobs' 'KPMG' 'N' 'R' 'A') assert [(h, s) for h, s, _ in boards_in(page)] == [ ("hrmanager", "seaborg_tr"), ("hrmanager", "kpmg"), ("hrmanager", "nnit"), ("hrmanager", "flyingtiger")]