"""The Eightfold host module against a saved page of Ericsson's search (five
of its roles) and one saved role answer. 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, readers # noqa: E402
from plugins.readers import Ask, eightfold # noqa: E402
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
SLUG = "jobs.ericsson.com/ericsson.com"
def _saved() -> dict[str, Any]:
return json.loads((FIXTURES / "eightfold.json").read_text())
def _position(n: int) -> dict[str, Any]:
return {"id": 5000 + n, "displayJobId": str(n), "name": f"Role {n}",
"locations": ["Lund,Skane,Sweden"], "postedTs": 1790000000 - n,
"department": "76 Product Development", "workLocationOption": "hybrid",
"positionUrl": f"/careers/job/{5000 + n}"}
def _search(total: int, seen: list[str] | None = None) -> httpx.MockTransport:
"""A search that ignores `num` and answers ten roles from `start`."""
def handler(request: httpx.Request) -> httpx.Response:
if seen is not None:
seen.append(str(request.url))
start = int(request.url.params["start"])
positions = [_position(n) for n in range(start, min(start + 10, total))]
return httpx.Response(200, json={"status": 200, "data": {
"positions": positions, "count": total}})
return httpx.MockTransport(handler)
def _crawl(slug: str, transport: httpx.MockTransport) -> tuple[list[dict[str, Any]], bool]:
"""What the runner does with a paged reader: pages until the cursor ends,
complete when as many distinct ids were read as the host stated."""
rows: list[dict[str, Any]] = []
ids: set[str] = set()
total: Any = None
with httpx.Client(transport=transport) as client:
def get(url: str, **_: Any) -> bytes:
return client.get(url).content
cursor: Any = None
while True:
decoded, results, total, cursor = eightfold.page(get, slug, cursor, Ask())
rows += eightfold.rows(decoded, slug)
ids |= {r["id"] for r in results}
if cursor is None or (total is not None and len(ids) >= total):
break
return rows, total is not None and len(ids) >= total
def test_a_page_becomes_rows() -> None:
rows = eightfold.rows(_saved()["data"]["positions"], SLUG)
assert len(rows) == 5
first, mexico, many = rows[0], rows[1], rows[4]
assert first["source_id"] == "563121777024688"
assert first["title"] == "Systems Architect - RAN Semantic Data"
assert first["url"] == "https://jobs.ericsson.com/careers/job/563121777024688"
assert first["location"] == "Stockholm, Sweden" # the city is its own region
assert first["department"] == "76 Product Development"
# `creationTs`, the posting date; `postedTs` (1790703782) is refreshed in bulk.
assert first["posted"] == datetime.fromtimestamp(1785822052, tz=UTC)
assert first["places"] == [{"text": "Stockholm, Sweden", "remote": None}]
assert first["remote"] == "onsite" and (first["description"], first["salary_text"]) == ("", "")
assert mexico["location"] == "Mexico City, Mexico"
assert many["location"] == ("Tecnoparque, Mexico City, Mexico; Santa Fe, Mexico City, Mexico; "
"Mexico City, Mexico; Tlalnepantla, Estado de México, Mexico")
assert [p["text"] for p in many["places"]] == many["location"].split("; ")
@pytest.mark.parametrize(("option", "remote"), [
("onsite", "onsite"), ("Hybrid", "Hybrid"), (None, None), ("odd", "odd"), (" ", None)])
def test_the_work_location_option_is_the_hosts_word(
option: str | None, remote: str | None) -> None:
assert eightfold.rows([_position(1) | {"workLocationOption": option}], SLUG)[0]["remote"] == remote
def test_a_position_without_a_name_an_id_or_a_path_is_left_out() -> None:
bad = [_position(1) | {"name": " "}, _position(2) | {"id": None},
_position(3) | {"positionUrl": ""}, _position(4) | {"positionUrl": "//evil.example/x"}]
assert eightfold.rows(bad, SLUG) == []
def test_the_standardized_location_stands_in_when_there_is_no_raw_one() -> None:
row = eightfold.rows([_position(1) | {"locations": [], "standardizedLocations": ["Lund, SE"]}],
SLUG)[0]
assert row["location"] == "Lund, SE"
def test_a_page_asks_for_the_newest_first_from_its_offset() -> None:
asked: list[str] = []
def get(url: str, **_: Any) -> bytes:
asked.append(url)
return json.dumps(_saved()).encode()
decoded, results, total, cursor = eightfold.page(get, SLUG, None, Ask())
assert asked == ["https://jobs.ericsson.com/api/pcsx/search?domain=ericsson.com"
"&sort_by=timestamp&start=0"]
assert (len(decoded), len(results), total, cursor) == (5, 5, 637, 5)
assert eightfold.page(get, SLUG, 630, Ask())[3] == 635
assert asked[-1].endswith("&start=630")
def test_a_board_is_read_ten_at_a_time_to_its_total() -> None:
seen: list[str] = []
rows, complete = _crawl(SLUG, _search(25, seen))
assert (len(rows), complete) == (25, True)
assert [u.rsplit("=", 1)[1] for u in seen] == ["0", "10", "20"]
def test_an_empty_board_is_complete_at_zero() -> None:
assert _crawl(SLUG, _search(0)) == ([], True)
@pytest.mark.parametrize("body", [
b"Not found", b"[]", b'{"status": 403, "message": "Not authorized for PCSX"}',
b'{"data": {"positions": null}}'])
def test_an_answer_that_is_not_a_search_raises(body: bytes) -> None:
with pytest.raises(ValueError):
eightfold.page(lambda url, **_: body, SLUG, None, Ask())
def test_detail_reads_the_text_of_the_ad() -> None:
asked: list[str] = []
def get(url: str, **_: Any) -> bytes:
asked.append(url)
return (FIXTURES / "eightfold-detail.json").read_bytes()
found = eightfold.detail(get, SLUG, {"source_id": "563121777283313"})
assert asked == ["https://jobs.ericsson.com/api/pcsx/position_details"
"?position_id=563121777283313&domain=ericsson.com&hl=en"]
assert set(found) <= set(ats.DETAIL_FIELDS)
text = found["description"]
assert text.startswith("Join our Team")
assert "<" not in text and "&" not in text and "\r" not in text and " " not in text
@pytest.mark.parametrize("body", [b"", b"[]", b'{"data": {}}'])
def test_detail_refuses_an_answer_that_is_not_a_position(body: bytes) -> None:
with pytest.raises(ValueError):
eightfold.detail(lambda url, **_: body, SLUG, {"source_id": "1"})
@pytest.mark.parametrize("role", ["", "12a", "1&domain=evil.example", "../x", "1 2"])
def test_detail_refuses_an_id_that_is_not_a_number(role: str) -> None:
def get(url: str, **_: Any) -> bytes:
raise AssertionError("no request may be made")
with pytest.raises(ValueError):
eightfold.detail(get, SLUG, {"source_id": role})
def test_the_slug_the_urls_and_the_allow_list() -> None:
for slug in (SLUG, "infineon.eightfold.ai/infineon.com", "Apply.Careers.Microsoft.com/microsoft.com"):
assert eightfold.slug_ok(slug) and ats.is_known_target(f"eightfold:{slug}")
assert eightfold.careers_url("Jobs.Ericsson.com/ericsson.com") == "https://jobs.ericsson.com/careers"
allow = eightfold.allowed("Jobs.Ericsson.com/ericsson.com")
assert allow("jobs.ericsson.com") and allow("JOBS.ERICSSON.COM")
assert not allow("ericsson.com") and not allow("evil.example")
assert not allow("jobs.ericsson.com.evil.example") and not allow("localhost")
assert readers.HOSTS["eightfold"] is eightfold and ats.HOSTS["eightfold"] == "Eightfold"
@pytest.mark.parametrize("slug", [
"", "jobs.ericsson.com", "jobs.ericsson.com/", "/ericsson.com", "jobs.ericsson.com/ericsson",
"jobs.ericsson.com/ericsson.com/", "jobs.ericsson.com/ericsson.com/x", "a/b/c.com",
"a@b.com/ericsson.com", "a.com#x/ericsson.com", "a.com?x/ericsson.com", "../ericsson.com",
"a.com%2fx/b.com", "a.com:443/b.com", "a.com/b.com:80", " a.com/b.com", "a.com/b.com ",
"a.com/b.com\n", "a.com/b.com\r\n", "127.0.0.1/ericsson.com", "a.com/127.0.0.1",
"localhost/ericsson.com", "a.com/localhost", "a.com/b.com&start=9", "a.com/b.com?x=1",
"[::1]/a.com", "a.com/b.com#", "app.localhost/a.com", "printer.local/a.com",
"meta.internal/a.com", "a.com/x.internal", "a_b.com/c.com", "évil.com/a.com"])
def test_a_hostile_slug_is_refused(slug: str) -> None:
assert not eightfold.slug_ok(slug)
assert not ats.is_known_target(f"eightfold:{slug}")
def test_discovery_reads_the_host_and_domain_from_a_link_that_has_both() -> None:
page = ('Jobs'
'no domain')
found = [(h, s) for h, s, _ in discover.boards_in(page)]
assert ("eightfold", "infineon.eightfold.ai/infineon.com") in found
assert ("eightfold", "") in found
assert "eightfold" not in {h for h, _ in discover._UNREADABLE if h not in readers.HOSTS}
def test_it_declares_a_paged_read_of_ten_with_a_total_and_one_country_a_request() -> None:
reads = eightfold.READS
assert (reads.listing, reads.page_size, reads.text, reads.complete, reads.places) == (
"paged", 10, "detail", "stated", "many")
assert reads.filters.declared() == {"country": "one", "query": "one"}
assert reads.fields.posted == "listed"
assert reads.robots.startswith("read: robots.txt allows /careers")
assert not hasattr(eightfold, "robots_page")