186 lines
8.4 KiB
Python
186 lines
8.4 KiB
Python
"""The BambooHR host module against Configit's saved listing and two saved
|
|
role pages. 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 bamboohr # noqa: E402
|
|
|
|
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
|
|
|
|
|
|
def _read(name: str) -> bytes:
|
|
return (FIXTURES / name).read_bytes()
|
|
|
|
|
|
def _get_of(answers: dict[str, bytes | Exception], asked: list[tuple[str, Any]]) -> Any:
|
|
def get(url: str, *, method: str = "GET", payload: Any = None,
|
|
headers: dict[str, str] | None = None) -> bytes:
|
|
asked.append((url, headers))
|
|
answer = answers[url]
|
|
if isinstance(answer, Exception):
|
|
raise answer
|
|
return answer
|
|
return get
|
|
|
|
|
|
def test_a_listing_becomes_rows() -> None:
|
|
rows = bamboohr.parse(_read("bamboohr.json"), "configit")
|
|
assert [r["source_id"] for r in rows] == ["26", "37"]
|
|
# The standing invitation to apply without a role is a role like any other.
|
|
assert rows[0]["title"] == "Unsolicited application - All Configit locations"
|
|
second = rows[1]
|
|
assert second["title"] == "Demand Generation Specialist - 12-Month Maternity Cover"
|
|
assert second["url"] == "https://configit.bamboohr.com/careers/37"
|
|
assert second["department"] == "Marketing" and rows[0]["department"] == ""
|
|
assert second["location"] == "København Ø"
|
|
assert second["remote"] == "hybrid"
|
|
assert (second["posted"], second["description"], second["salary_text"]) == (None, "", "")
|
|
|
|
|
|
@pytest.mark.parametrize(("flag", "kind", "remote"), [
|
|
(True, "0", "remote"), (None, "0", "onsite"), (None, "1", "remote"), (None, "2", "hybrid"),
|
|
(None, None, None), (False, "7", None)])
|
|
def test_remote_is_the_flag_then_the_location_type(
|
|
flag: bool | None, kind: str | None, remote: str | None) -> None:
|
|
body = json.loads(_read("bamboohr.json"))
|
|
body["result"][0] |= {"isRemote": flag, "locationType": kind}
|
|
assert bamboohr.parse(json.dumps(body).encode(), "configit")[0]["remote"] == remote
|
|
|
|
|
|
def test_the_location_reads_the_state_and_the_ats_location_too() -> None:
|
|
body = json.loads(_read("bamboohr.json"))
|
|
body["result"][0]["location"] = {"city": "Austin", "state": "Texas"}
|
|
body["result"][1]["location"] = {"city": None, "state": None}
|
|
body["result"][1]["atsLocation"] = {"city": "Lyon", "state": None, "country": "France"}
|
|
rows = bamboohr.parse(json.dumps(body).encode(), "configit")
|
|
assert [r["location"] for r in rows] == ["Austin, Texas", "Lyon, France"]
|
|
|
|
|
|
def test_a_list_shorter_than_its_count_is_refused_not_read_as_the_board() -> None:
|
|
body = json.loads(_read("bamboohr.json"))
|
|
body["meta"]["totalCount"] = 3
|
|
with pytest.raises(ValueError, match="2 of 3"):
|
|
bamboohr.parse(json.dumps(body).encode(), "configit")
|
|
|
|
|
|
def test_a_page_that_is_not_a_listing_raises_and_an_empty_board_is_no_rows() -> None:
|
|
assert bamboohr.parse(b'{"meta": {"totalCount": 0}, "result": []}', "configit") == []
|
|
for body in (b"<html></html>", b"[]", b'{"result": null}'):
|
|
with pytest.raises(ValueError):
|
|
bamboohr.parse(body, "configit")
|
|
|
|
|
|
def test_a_role_without_a_title_or_an_id_is_left_out() -> None:
|
|
body = json.loads(_read("bamboohr.json"))
|
|
body["result"][0]["jobOpeningName"] = " "
|
|
body["result"][1]["id"] = None
|
|
assert bamboohr.parse(json.dumps(body).encode(), "configit") == []
|
|
|
|
|
|
def test_detail_reads_the_text_the_date_and_the_country() -> None:
|
|
row = bamboohr.parse(_read("bamboohr.json"), "configit")[1]
|
|
asked: list[tuple[str, Any]] = []
|
|
url = "https://configit.bamboohr.com/careers/37/detail"
|
|
found = bamboohr.detail(_get_of({url: _read("bamboohr-detail-37.json")}, asked),
|
|
"configit", row)
|
|
assert asked == [(url, {"Accept": "application/json"})]
|
|
assert set(found) <= set(ats.DETAIL_FIELDS)
|
|
assert found["posted"] == datetime(2026, 9, 23, tzinfo=UTC)
|
|
assert found["location"] == "København Ø, Denmark"
|
|
assert found["remote"] == "hybrid"
|
|
text = found["description"]
|
|
assert text.startswith("Our Global Marketing Team is looking for")
|
|
assert "Skills & Experience" in text and "&" not in text and "<" not in text
|
|
|
|
|
|
def test_detail_of_the_standing_invitation() -> None:
|
|
row = bamboohr.parse(_read("bamboohr.json"), "configit")[0]
|
|
url = "https://configit.bamboohr.com/careers/26/detail"
|
|
found = bamboohr.detail(_get_of({url: _read("bamboohr-detail-26.json")}, []), "configit", row)
|
|
assert found["posted"] == datetime(2025, 10, 10, tzinfo=UTC)
|
|
assert found["description"].startswith("Hi, If there is currently no vacant position")
|
|
assert "UK (remote)" in found["description"]
|
|
|
|
|
|
def test_detail_refuses_an_answer_that_is_not_a_role() -> None:
|
|
url = "https://configit.bamboohr.com/careers/37/detail"
|
|
row = {"source_id": "37"}
|
|
for body in (b"<html></html>", b'{"result": {}}', b"[]"):
|
|
with pytest.raises(ValueError):
|
|
bamboohr.detail(_get_of({url: body}, []), "configit", row)
|
|
|
|
|
|
def test_the_board_and_its_details_are_read_through_the_app(
|
|
monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(ats, "PAGE_DELAY", 0)
|
|
base = "https://configit.bamboohr.com/careers"
|
|
seen: list[str] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
seen.append(str(request.url))
|
|
if request.url.path == "/careers/list":
|
|
return httpx.Response(200, content=_read("bamboohr.json"))
|
|
if request.url.path == "/careers/26/detail":
|
|
return httpx.Response(404)
|
|
assert request.headers["accept"] == "application/json"
|
|
return httpx.Response(200, content=_read("bamboohr-detail-37.json"))
|
|
|
|
with httpx.Client(transport=httpx.MockTransport(handler)) as client:
|
|
rows, complete = ats._fetch_rows(client, "bamboohr:configit")
|
|
assert complete and len(rows) == 2 and seen == [f"{base}/list"]
|
|
filled, done, failed = ats._read_details(client, "bamboohr:configit", rows, complete,
|
|
set(), set())
|
|
assert seen[1:] == [f"{base}/26/detail", f"{base}/37/detail"]
|
|
# A 404 counts as read; the other role is filled from its own page.
|
|
assert done == {"bamboohr:configit:26", "bamboohr:configit:37"} and failed == set()
|
|
assert filled[0]["description"] == "" and filled[1]["location"] == "København Ø, Denmark"
|
|
assert filled[1]["posted"] == datetime(2026, 9, 23, tzinfo=UTC)
|
|
|
|
|
|
def test_the_slug_the_urls_and_the_allow_list() -> None:
|
|
assert bamboohr.slug_ok("configit") and bamboohr.slug_ok("Configit")
|
|
assert bamboohr.careers_url("configit") == "https://configit.bamboohr.com/careers"
|
|
assert bamboohr.robots_page("configit") == "https://configit.bamboohr.com/careers"
|
|
assert bamboohr.feed_url("Configit") == "https://configit.bamboohr.com/careers/list"
|
|
allow = bamboohr.allowed("Configit")
|
|
assert allow("configit.bamboohr.com") and allow("CONFIGIT.bamboohr.com")
|
|
assert not allow("bamboohr.com") and not allow("other.bamboohr.com")
|
|
assert not allow("configit.bamboohr.com.evil.example") and not allow("localhost")
|
|
assert atshosts.HOSTS["bamboohr"] is bamboohr and ats.HOSTS["bamboohr"] == "BambooHR"
|
|
|
|
|
|
@pytest.mark.parametrize("slug", [
|
|
"", "a@b", "a#b", "a?b", "..", "a/b", "a%2fb", "a:443", "a b", " a", "a\n", "a\r\n",
|
|
"a.b", "127.0.0.1", "[::1]", "a_b", "2130706433.evil"])
|
|
def test_a_hostile_slug_is_refused(slug: str) -> None:
|
|
assert not bamboohr.slug_ok(slug)
|
|
assert not ats.is_known_target(f"bamboohr:{slug}")
|
|
|
|
|
|
def test_a_local_word_as_a_slug_only_reaches_bamboohrs_own_host() -> None:
|
|
allow = bamboohr.allowed("localhost")
|
|
assert allow("localhost.bamboohr.com") and not allow("localhost")
|
|
|
|
|
|
def test_discovery_finds_the_subdomain_and_not_bamboohrs_own_hosts() -> None:
|
|
page = ('<script src="https://Configit.bamboohr.com/js/embed.js"></script>'
|
|
'<a href="https://www.bamboohr.com/">x</a><a href="https://app.bamboohr.com/x">y</a>'
|
|
'<a href="https://resources.bamboohr.com/y">z</a>')
|
|
assert [(h, s) for h, s, _ in discover.boards_in(page)] == [("bamboohr", "configit")]
|
|
assert "bamboohr" not in {h for h, _ in discover._UNREADABLE if h not in atshosts.HOSTS}
|