167 lines
7.4 KiB
Python
167 lines
7.4 KiB
Python
"""The Pinpoint host module against a saved listing (Xeneta's whole board, two roles). No network."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
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 pinpoint # noqa: E402
|
|
|
|
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
|
|
|
|
|
|
def _body() -> bytes:
|
|
return (FIXTURES / "pinpoint.json").read_bytes()
|
|
|
|
|
|
def test_a_listing_becomes_rows() -> None:
|
|
rows = pinpoint.parse(_body(), "xeneta")
|
|
assert [r["source_id"] for r in rows] == ["581878", "587635"]
|
|
first = rows[0]
|
|
assert first["title"] == "Data Operations Analyst - LSP"
|
|
assert first["url"] == ("https://xeneta.pinpointhq.com/en/postings/"
|
|
"a85d5d45-b334-4f5c-aaea-d0b399b08a2e")
|
|
assert first["location"] == "Bucharest" # the province repeats the name
|
|
assert first["department"] == "RMT - LSP"
|
|
assert first["remote"] == "hybrid"
|
|
assert first["posted"] is None # the listing has no date
|
|
assert (first["salary_text"], first["salary_min"], first["salary_max"],
|
|
first["currency"]) == ("", None, None, "")
|
|
|
|
|
|
def test_the_description_joins_the_sections_as_plain_text() -> None:
|
|
text = pinpoint.parse(_body(), "xeneta")[0]["description"]
|
|
assert text.startswith("Data Detective Wanted to join our Data & Analytics Team")
|
|
assert "<" not in text and "&" not in text and "\xa0" not in text
|
|
assert "\n\nKey Responsibilities: Join our Liner Data & Analytics Team" in text
|
|
assert "\n\nSkills, Knowledge & Expertise: In this role" in text
|
|
assert "\n\nJob Benefits: We will help you grow your career:" in text
|
|
|
|
|
|
def test_a_region_is_kept_when_it_says_more_than_the_name() -> None:
|
|
body = json.loads(_body())
|
|
body["data"][0]["location"] = {"name": "London", "city": "London", "province": "England"}
|
|
body["data"][1]["location"] = {"name": "", "city": "Oslo", "province": "Norway"}
|
|
rows = pinpoint.parse(json.dumps(body).encode(), "xeneta")
|
|
assert [r["location"] for r in rows] == ["London, England", "Oslo, Norway"]
|
|
|
|
|
|
@pytest.mark.parametrize(("kind", "remote"), [
|
|
("onsite", "onsite"), ("hybrid", "hybrid"), ("remote", "remote"), ("", None), (None, None)])
|
|
def test_the_workplace_type_maps_to_the_remote_words(kind: str | None, remote: str | None) -> None:
|
|
body = json.loads(_body())
|
|
body["data"][0]["workplace_type"] = kind
|
|
assert pinpoint.parse(json.dumps(body).encode(), "xeneta")[0]["remote"] == remote
|
|
|
|
|
|
def test_pay_is_read_only_when_the_board_shows_it() -> None:
|
|
body = json.loads(_body())
|
|
pay = {"compensation_minimum": 60000, "compensation_maximum": 80000,
|
|
"compensation_currency": "EUR", "compensation_frequency": "annually"}
|
|
body["data"][0] |= pay | {"compensation_visible": True}
|
|
body["data"][1] |= pay # hidden: `compensation_visible` is false
|
|
first, second = pinpoint.parse(json.dumps(body).encode(), "xeneta")
|
|
assert (first["salary_min"], first["salary_max"], first["currency"]) == (60000, 80000, "EUR")
|
|
assert first["salary_text"] == "EUR 60,000 to 80,000 per year"
|
|
assert (second["salary_min"], second["salary_text"]) == (None, "")
|
|
|
|
|
|
def test_an_unknown_pay_frequency_keeps_the_text_and_leaves_the_columns() -> None:
|
|
body = json.loads(_body())
|
|
body["data"][0] |= {"compensation_minimum": 40, "compensation_maximum": 50,
|
|
"compensation_currency": "GBP", "compensation_frequency": "hourly",
|
|
"compensation_visible": True}
|
|
row = pinpoint.parse(json.dumps(body).encode(), "xeneta")[0]
|
|
assert row["salary_text"] == "GBP 40 to 50 per hour"
|
|
assert (row["salary_min"], row["salary_max"], row["currency"]) == (None, None, "")
|
|
|
|
|
|
def test_a_posting_without_a_title_or_an_id_or_a_web_url_is_left_out() -> None:
|
|
body = json.loads(_body())
|
|
body["data"][0]["title"] = ""
|
|
body["data"][1]["url"] = "javascript:alert(1)"
|
|
assert pinpoint.parse(json.dumps(body).encode(), "xeneta") == []
|
|
|
|
|
|
def test_an_empty_board_is_no_rows_and_a_page_that_is_not_a_listing_raises() -> None:
|
|
assert pinpoint.parse(b'{"data": []}', "xeneta") == []
|
|
for body in (b"<html></html>", b"[]", b'{"error": "nope"}'):
|
|
with pytest.raises(ValueError):
|
|
pinpoint.parse(body, "xeneta")
|
|
|
|
|
|
def test_the_board_is_read_through_the_app_and_is_complete() -> None:
|
|
seen: list[str] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
seen.append(str(request.url))
|
|
return httpx.Response(200, content=_body())
|
|
|
|
with httpx.Client(transport=httpx.MockTransport(handler)) as client:
|
|
rows, complete = ats._fetch_rows(client, "pinpoint:xeneta")
|
|
assert seen == ["https://xeneta.pinpointhq.com/postings.json"]
|
|
assert len(rows) == 2 and complete
|
|
|
|
|
|
def test_the_slug_the_urls_and_the_allow_list() -> None:
|
|
assert pinpoint.slug_ok("xeneta") and pinpoint.slug_ok("Xeneta") and pinpoint.slug_ok("a-b1")
|
|
assert pinpoint.careers_url("xeneta") == "https://xeneta.pinpointhq.com/"
|
|
assert not hasattr(pinpoint, "robots_page") # decided when built, see READS.robots
|
|
assert pinpoint.feed_url("Xeneta") == "https://xeneta.pinpointhq.com/postings.json"
|
|
allow = pinpoint.allowed("Xeneta")
|
|
assert allow("xeneta.pinpointhq.com") and allow("XENETA.pinpointhq.com")
|
|
assert not allow("pinpointhq.com") and not allow("other.pinpointhq.com")
|
|
assert not allow("xeneta.pinpointhq.com.evil.example") and not allow("localhost")
|
|
assert readers.HOSTS["pinpoint"] is pinpoint and ats.HOSTS["pinpoint"] == "Pinpoint"
|
|
|
|
|
|
@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 pinpoint.slug_ok(slug)
|
|
assert not ats.is_known_target(f"pinpoint:{slug}")
|
|
|
|
|
|
@pytest.mark.parametrize("slug", ["localhost", "www"])
|
|
def test_a_slug_that_is_a_local_word_still_only_reaches_pinpoints_own_host(slug: str) -> None:
|
|
"""`localhost` is a fine DNS label, but the host it names is
|
|
`localhost.pinpointhq.com`, which is Pinpoint's."""
|
|
assert pinpoint.allowed(slug)(f"{slug}.pinpointhq.com")
|
|
assert not pinpoint.allowed(slug)(slug)
|
|
|
|
|
|
def test_discovery_finds_the_subdomain() -> None:
|
|
page = ('<a href="https://Xeneta.pinpointhq.com/en/postings/a85d5d45">Jobs</a>'
|
|
'<script src="https://www.pinpointhq.com/embed.js"></script>')
|
|
assert [(h, s) for h, s, _ in discover.boards_in(page)] == [("pinpoint", "xeneta")]
|
|
assert discover.boards_in("https://pinpointhq.com/") == []
|
|
assert "pinpoint" not in {h for h, _ in discover._UNREADABLE if h not in readers.HOSTS}
|
|
|
|
|
|
def test_a_pinpoint_board_is_not_probed() -> None:
|
|
assert not hasattr(pinpoint, "probe_url")
|
|
|
|
|
|
|
|
|
|
def test_a_role_has_its_one_place_with_the_work_mode_of_the_role() -> None:
|
|
rows = pinpoint.parse((FIXTURES / "pinpoint.json").read_bytes(), "xeneta")
|
|
assert rows[0]["places"] == [{"text": "Bucharest", "remote": False}] # hybrid
|
|
assert rows[0]["location"] == "Bucharest"
|
|
|
|
|
|
def test_employment_is_the_hosts_type_as_given() -> None:
|
|
rows = pinpoint.parse((FIXTURES / "pinpoint.json").read_bytes(), "xeneta")
|
|
assert [r["employment"] for r in rows] == ["permanent_full_time", "fixed_term_full_time"]
|