-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_poll_adapters.py
More file actions
188 lines (138 loc) · 6.61 KB
/
Copy pathtest_poll_adapters.py
File metadata and controls
188 lines (138 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""ATS adapters: Greenhouse and Lever (roadmap M4, ADR-0025).
Fixtures (`tests/fixtures/{greenhouse,lever}_sample.json`) are real recorded responses from
each platform's own public demo/reference board — GitLab's Greenhouse board and Lever's own
`leverdemo` board — truncated for size, not fabricated. Only `poll.http.get_json` is mocked;
everything downstream of it (parsing, the adapter's own logic) runs for real against that data.
"""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from openjobradar.poll import Posting, UnknownAtsTypeError, adapter_for
from openjobradar.poll.adapters import greenhouse, lever
from openjobradar.poll.adapters.base import InvalidBoardIdError, validate_board_id
from openjobradar.poll.http import UnsupportedUrlError, get_json, strip_html
FIXTURES = Path(__file__).parent / "fixtures"
def _load(name: str):
return json.loads((FIXTURES / name).read_text(encoding="utf-8"))
# --- registry -----------------------------------------------------------------
def test_registry_resolves_both_adapters() -> None:
assert adapter_for("greenhouse") is greenhouse
assert adapter_for("lever") is lever
def test_registry_rejects_unknown_ats_type() -> None:
with pytest.raises(UnknownAtsTypeError):
adapter_for("workday")
# --- board id validation --------------------------------------------------------
@pytest.mark.parametrize("bad", ["", "has space", "has/slash", "a" * 101, None])
def test_invalid_board_ids_rejected(bad) -> None:
with pytest.raises(InvalidBoardIdError):
validate_board_id(bad)
def test_valid_board_id_passes_through() -> None:
assert validate_board_id("gitlab") == "gitlab"
# --- greenhouse -----------------------------------------------------------------
def test_greenhouse_parses_real_fixture_into_postings() -> None:
with patch("openjobradar.poll.adapters.greenhouse.get_json", return_value=_load("greenhouse_sample.json")):
postings = greenhouse.list_postings("gitlab", "gitlab", "GitLab")
assert len(postings) == 2
first = postings[0]
assert isinstance(first, Posting)
assert first.org_slug == "gitlab"
assert first.ats_type == "greenhouse"
assert first.job_id
assert first.title
assert "Remote" in first.location_raw
assert first.apply_url.startswith("https://")
assert "<" not in first.jd_text # HTML was stripped
assert first.stable_id == f"greenhouse:gitlab:{first.job_id}"
def test_greenhouse_falls_back_to_offices_when_location_name_missing() -> None:
data = {
"jobs": [
{
"id": 1,
"title": "Remote Engineer",
"location": {"name": ""},
"offices": [{"name": "Remote - US"}],
"absolute_url": "https://boards.greenhouse.io/acme/jobs/1",
"content": "<p>Build things.</p>",
"departments": [{"name": "Engineering"}],
}
]
}
with patch("openjobradar.poll.adapters.greenhouse.get_json", return_value=data):
postings = greenhouse.list_postings("acme", "acme", "Acme")
assert postings[0].location_raw == "Remote - US"
assert postings[0].department == "Engineering"
assert postings[0].jd_text == "Build things."
def test_greenhouse_handles_empty_board() -> None:
with patch("openjobradar.poll.adapters.greenhouse.get_json", return_value={"jobs": []}):
assert greenhouse.list_postings("acme", "acme", "Acme") == []
# --- lever ------------------------------------------------------------------------
def test_lever_parses_real_fixture_into_postings() -> None:
with patch("openjobradar.poll.adapters.lever.get_json", return_value=_load("lever_sample.json")):
postings = lever.list_postings("leverdemo", "leverdemo", "Lever")
assert len(postings) == 2
first = postings[0]
assert first.org_slug == "leverdemo"
assert first.ats_type == "lever"
assert first.job_id
assert first.title
assert first.apply_url.startswith("https://")
assert first.stable_id == f"lever:leverdemo:{first.job_id}"
def test_lever_location_prefers_all_locations_over_primary_only() -> None:
data = [
{
"id": "1",
"text": "Engineer",
"categories": {"location": "San Francisco", "allLocations": ["San Francisco", "Remote"]},
"hostedUrl": "https://jobs.lever.co/acme/1",
"descriptionPlain": "Do the work.",
}
]
with patch("openjobradar.poll.adapters.lever.get_json", return_value=data):
postings = lever.list_postings("acme", "acme", "Acme")
assert postings[0].location_raw == "San Francisco / Remote"
def test_lever_falls_back_to_remote_when_no_locations_but_workplace_type_remote() -> None:
data = [
{
"id": "1",
"text": "Engineer",
"categories": {},
"workplaceType": "remote",
"hostedUrl": "https://jobs.lever.co/acme/1",
}
]
with patch("openjobradar.poll.adapters.lever.get_json", return_value=data):
postings = lever.list_postings("acme", "acme", "Acme")
assert postings[0].location_raw == "Remote"
def test_lever_jd_text_assembles_lists_when_description_plain_absent() -> None:
data = [
{
"id": "1",
"text": "Engineer",
"categories": {},
"hostedUrl": "https://jobs.lever.co/acme/1",
"description": "<p>Intro</p>",
"lists": [{"text": "Requirements", "content": "<ul><li>Python</li></ul>"}],
"additionalPlain": "Equal opportunity employer.",
}
]
with patch("openjobradar.poll.adapters.lever.get_json", return_value=data):
postings = lever.list_postings("acme", "acme", "Acme")
jd = postings[0].jd_text
assert "Intro" in jd
assert "Requirements" in jd and "Python" in jd
assert "Equal opportunity employer." in jd
def test_lever_handles_empty_board() -> None:
with patch("openjobradar.poll.adapters.lever.get_json", return_value=[]):
assert lever.list_postings("acme", "acme", "Acme") == []
# --- http helpers ------------------------------------------------------------------
def test_strip_html_removes_tags_and_collapses_whitespace() -> None:
assert strip_html("<p>Hello <b>world</b></p>\n<p>!</p>") == "Hello world !"
def test_strip_html_of_empty_string_is_empty() -> None:
assert strip_html("") == ""
def test_get_json_rejects_non_https_urls() -> None:
with pytest.raises(UnsupportedUrlError):
get_json("http://example.com/insecure")
with pytest.raises(UnsupportedUrlError):
get_json("file:///etc/passwd")