-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_location_policy.py
More file actions
128 lines (95 loc) · 4.43 KB
/
Copy pathtest_location_policy.py
File metadata and controls
128 lines (95 loc) · 4.43 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
"""Location-policy evaluation: prototype semantics, config-driven (M1.2 / ADR-0012)."""
from __future__ import annotations
from hypothesis import given
from hypothesis import strategies as st
from openjobradar.policy import DENY, LocationVerdict, evaluate_location
def _policy(**overrides):
policy = {"location_policy": {"allowed_metros": ["Davis", "Sacramento", "Bay Area"]}}
policy["location_policy"].update(overrides)
return policy
def test_missing_section_is_permissive() -> None:
assert evaluate_location("Boise, ID", {}).allowed
assert evaluate_location("Boise, ID", {"other": 1}).rule == "default_allow"
def test_remote_passes_anywhere() -> None:
verdict = evaluate_location("Remote — US only", _policy(remote_only=True))
assert verdict.allowed and verdict.rule == "remote"
def test_in_metro_hybrid_within_default_zero_ceiling_rejects() -> None:
verdict = evaluate_location("Davis, CA (Hybrid 2 days/week)", _policy())
assert not verdict.allowed
assert verdict.rule == "onsite_exceeds_limit"
assert "exceeds your limit of 0" in verdict.reason
def test_ceiling_allows_configured_cadence() -> None:
policy = _policy(max_onsite_days_per_week=2)
verdict = evaluate_location("Sacramento, CA — hybrid, 2 days onsite/week", policy)
assert verdict.allowed
assert verdict.rule == "metro_allowed"
assert "2 day(s)/week" in verdict.reason
def test_exception_raises_ceiling_and_is_cited() -> None:
policy = _policy(
max_onsite_days_per_week=2,
exceptions=[
{
"match": "Downtown Sacramento",
"max_onsite_days_per_week": 4,
"note": "near transit",
}
],
)
allowed = evaluate_location(
"Downtown Sacramento, CA — 4 days onsite per week", policy
)
assert allowed.allowed
assert allowed.rule == "exception_applied"
assert "'Downtown Sacramento'" in allowed.reason
assert "near transit" in allowed.reason
rejected = evaluate_location(
"Downtown Sacramento, CA — 5 days onsite per week", policy
)
assert not rejected.allowed
assert "even with exception" in rejected.reason
def test_outside_whitelist_rejects_with_reason() -> None:
verdict = evaluate_location("Austin, TX (Hybrid)", _policy(max_onsite_days_per_week=3))
assert not verdict.allowed
assert verdict.rule == "outside_metros"
assert "outside your allowed metros" in verdict.reason
def test_empty_whitelist_rejects_site_based_roles() -> None:
policy = {"location_policy": {}}
verdict = evaluate_location("New York, NY", policy)
assert not verdict.allowed
assert verdict.rule == "no_metros_configured"
def test_unstated_cadence_in_configured_metro_passes() -> None:
verdict = evaluate_location("Davis, CA", _policy())
assert verdict.allowed
assert "unstated cadence" in verdict.reason
def test_bare_onsite_word_counts_as_five_days() -> None:
policy = _policy(max_onsite_days_per_week=3)
verdict = evaluate_location("Denver-Aurora, CO — in office", policy)
assert not verdict.allowed
assert "5 onsite day(s)" in verdict.reason
def test_empty_location_string_fails_closed() -> None:
verdict = evaluate_location(" ", _policy())
assert not verdict.allowed
assert verdict.rule == "no_location"
@given(
text=st.text(max_size=80),
metros=st.lists(st.text(min_size=1, max_size=20), max_size=4),
ceiling=st.integers(0, 5),
)
def test_arbitrary_inputs_never_raise(text, metros, ceiling) -> None:
policy = {"location_policy": {"allowed_metros": metros, "max_onsite_days_per_week": ceiling}}
verdict = evaluate_location(text, policy)
assert isinstance(verdict, LocationVerdict)
assert verdict.status in ("allow", "deny")
assert verdict.reason
assert verdict.rule
@given(st.text(min_size=1, max_size=60), st.integers(0, 5))
def test_evaluation_is_deterministic(text, ceiling) -> None:
policy = {"location_policy": {"allowed_metros": ["Exampleville"], "max_onsite_days_per_week": ceiling}}
first = evaluate_location(text, policy)
second = evaluate_location(text, policy)
assert (first.status, first.rule, first.reason) == (second.status, second.rule, second.reason)
def test_verdict_truthiness_matches_status() -> None:
assert evaluate_location("Remote", _policy()).allowed is True
assert evaluate_location(None, _policy()).allowed is False
def test_constants_are_stable() -> None:
assert DENY == "deny"