Files
jobboard/tests/test_ats_deel.py

167 lines
7.4 KiB
Python

"""The Deel host module against a saved page (trimmed from Klarna's board,
2026-09-29), and the fetch around it. No network."""
from __future__ import annotations
import json
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, _runner, deel # noqa: E402
PAGE = (ROOT / "tests" / "fixtures" / "ats" / "deel.html").read_bytes()
URL = "https://jobs.deel.com/klarna"
def test_rows_come_from_the_payload_with_descriptions_resolved() -> None:
rows = deel.parse(PAGE, "klarna")
assert [r["source_id"][:8] for r in rows] == [
"21584046", "a66b42b6", "f96bb9d4", "a8be45ac", "a6649d86"]
first = rows[0]
assert first["title"] == "Incident Manager"
assert first["url"] == ("https://jobs.deel.com/klarna/job-details/"
"21584046-56d5-44c4-8b18-7e15dc659827/overview")
assert first["location"] == "Stockholm" and first["department"] == "Engineering"
assert first["remote"] == "ON_SITE"
assert first["description"].startswith("## Klarna, briefly\nAt Klarna")
assert "<" not in first["description"] and "$" not in first["description"][:3]
assert all(r["description"] for r in rows)
# The day the posting was published, not the day it was drafted (12:27).
assert first["posted"] is not None and first["posted"].isoformat() == \
"2026-09-07T12:33:38.484000+00:00"
def test_several_locations_are_joined_and_remote_is_the_hosts_word() -> None:
rows = {r["source_id"][:8]: r for r in deel.parse(PAGE, "klarna")}
assert rows["a8be45ac"]["location"] == "Stockholm; London; Milan; Warsaw"
assert rows["f96bb9d4"]["remote"] == "REMOTE"
assert rows["f96bb9d4"]["location"] == "Ohio"
# Every place is kept, in the host's order; the work arrangement is the
# places' remote flag.
assert rows["a8be45ac"]["places"] == [
{"text": name, "remote": False} for name in ("Stockholm", "London", "Milan", "Warsaw")]
assert rows["f96bb9d4"]["places"] == [{"text": "Ohio", "remote": True}]
assert rows["f96bb9d4"]["employment"] == "Consultant"
assert rows["21584046"]["employment"] == "Full-time"
def test_pay_is_only_text_since_the_period_is_not_stated() -> None:
rows = {r["source_id"][:8]: r for r in deel.parse(PAGE, "klarna")}
assert rows["21584046"]["salary_text"] == "SEK 775,398 to 1,108,832"
assert rows["a66b42b6"]["salary_text"] == "GBP from 105,197"
assert rows["f96bb9d4"]["salary_text"] == "" # a role with no range
assert all("salary_min" not in r for r in rows.values())
def _page_with(body: dict[str, Any]) -> bytes:
"""A page whose one payload row holds `body`, and a description row of
multibyte text whose length is given in bytes, as the stream does."""
text = "Zo\u00eb\u2019s caf\u00e9 \u2014 100 \u20ac" # 2, 3 and 1 bytes wide
row = f"5:T{len(text.encode()):x},{text}" + \
'6:["$","div",null,' + json.dumps(body, ensure_ascii=False, separators=(",", ":")) + "]\n"
return f'<script>self.__next_f.push([1,{json.dumps(row, ensure_ascii=False)}])</script>'.encode()
def test_a_text_row_is_read_by_its_length_in_bytes() -> None:
body = {"jobPostings": [{"id": "a", "title": "Baker", "richtextDescription": "$5",
"job": {}}, {"id": "b", "title": "Cook",
"richtextDescription": "<p>Inline &amp; done</p>",
"job": {}}]}
rows = deel.parse(_page_with(body), "acme")
assert [r["description"] for r in rows] == ["Zo\u00eb\u2019s caf\u00e9 \u2014 100 \u20ac",
"Inline & done"]
assert rows[0]["remote"] is None and rows[0]["location"] == "" and rows[0]["posted"] is None
def test_a_board_without_roles_is_an_empty_board_but_another_page_is_not_a_board() -> None:
assert deel.parse(_page_with({"jobPostings": []}), "acme") == []
with pytest.raises(ValueError, match="not a Deel job board"):
deel.parse(b"<html><title>Job Board Not Found</title></html>", "nobody")
def test_a_posting_without_a_title_or_id_is_handed_over_for_the_runner_to_drop() -> None:
body = {"jobPostings": [{"id": "a", "title": " ", "job": {}}, {"title": "x", "job": {}},
{"id": "c", "title": "Cook", "job": {}}]}
rows = deel.parse(_page_with(body), "acme")
assert [r["source_id"] for r in rows] == ["a", "", "c"]
assert [r["source_id"] for r in rows if _runner.valid(r)] == ["c"]
# ------------------------------------------------------------ the fetch
def test_the_board_is_one_request() -> None:
asked: list[str] = []
def get(url: str, **kwargs: Any) -> bytes:
asked.append(url)
return PAGE
rows = deel.parse(get(deel.feed_url("klarna", Ask())), "klarna")
assert len(rows) == 5 and asked == [URL]
def test_it_declares_a_whole_listing_read_from_the_page() -> None:
reads = deel.READS
assert (reads.listing, reads.text, reads.places, reads.complete) == (
"whole", "listing", "many", "whole")
assert reads.fields.pay == "line" and "/api/ is disallowed and not read" in reads.robots
# ------------------------------------------------------- slugs and hosts
def test_the_module_is_registered_as_a_feed_host() -> None:
assert readers.HOSTS["deel"] is deel and ats.HOSTS["deel"] == "Deel"
assert deel.NAME == "deel" and deel.KEEP_CASE
assert not hasattr(deel, "page") and not hasattr(deel, "detail")
def test_the_urls_are_the_boards_own() -> None:
assert deel.careers_url("klarna") == URL
assert deel.feed_url("klarna", Ask()) == URL
assert deel.allowed("klarna")("jobs.deel.com") and deel.allowed("klarna")("JOBS.DEEL.COM")
for host in ("deel.com", "api.deel.com", "jobs.deel.com.evil.example", "evil.example"):
assert not deel.allowed("klarna")(host)
@pytest.mark.parametrize("slug", ["klarna", "Klarna", "acme-2", "a_b", "9lives", "localhost"])
def test_a_slug_is_one_path_segment(slug: str) -> None:
"""`localhost` is only a path here: the host of every URL is fixed."""
assert ats.is_known_target(f"deel:{slug}")
@pytest.mark.parametrize("slug", [
"", "a@b", "a#b", "a?b", "..", ".", "a%2fb", "a/b", "a:443", "a b", "acme\n", " acme",
"127.0.0.1", "a.b", "_next", "-x", "a\\b", "évil",
])
def test_a_hostile_slug_is_refused(slug: str) -> None:
assert not deel.slug_ok(slug)
assert not ats.is_known_target(f"deel:{slug}")
def test_no_slug_reaches_another_host() -> None:
"""The host is fixed in every URL the module builds; a slug only ever
lands in the path."""
for slug in ("klarna", "Klarna", "a-b_c"):
for url in (deel.careers_url(slug), deel.feed_url(slug, Ask())):
assert httpx.URL(url).host == "jobs.deel.com"
def test_discovery_finds_the_slug_in_a_career_site_url() -> None:
page = ('<a href="https://jobs.deel.com/job-boards/klarna">All open positions</a>'
'<a href="https://jobs.deel.com/acme/job-details/1/overview">Role</a>'
'<a href="https://jobs.deel.com/Beta">Beta</a>'
'<script src="https://jobs.deel.com/_next/static/x.js"></script>'
'<a href="https://jobs.deel.com/job-boards">List</a>')
assert [(h, s) for h, s, _ in boards_in(page)] == [
("deel", "klarna"), ("deel", "acme"), ("deel", "Beta")]