forked from ChelseaKR/fare-policy-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_smoke_release_identity.py
More file actions
432 lines (376 loc) · 12.3 KB
/
Copy pathtest_smoke_release_identity.py
File metadata and controls
432 lines (376 loc) · 12.3 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""Focused public-smoke tests for strict and legacy release identity modes."""
from __future__ import annotations
import json
import os
import subprocess
from pathlib import Path
import pytest
from assistant import config
SMOKE = config.REPO_ROOT / "scripts" / "smoke-production.sh"
SOURCE = "a" * 40
CONFIG = "b" * 64
CONTENT = "c" * 64
SNAPSHOT = "d" * 64
RELEASE = "e" * 64
ARTIFACT = "A" * 43 + "="
FUNCTION_VERSION = "10"
FAKE_CURL = r"""#!/usr/bin/env python3
import json
import os
import pathlib
import sys
args = sys.argv[1:]
headers_path = pathlib.Path(args[args.index("--dump-header") + 1])
body_path = pathlib.Path(args[args.index("--output") + 1])
url = args[-1]
payload = args[args.index("--data") + 1] if "--data" in args else ""
state_path = pathlib.Path(os.environ["FAKE_CURL_STATE"])
state = json.loads(state_path.read_text(encoding="utf-8"))
state.setdefault("requests", []).append(url)
state_path.write_text(json.dumps(state), encoding="utf-8")
security = [
"cache-control: no-store",
"content-security-policy: default-src 'none'; connect-src 'self'; "
"form-action 'self'; base-uri 'none'; frame-ancestors 'self'",
"referrer-policy: no-referrer",
"x-content-type-options: nosniff",
]
if url.endswith("/version"):
content_type = "application/json"
body = os.environ["FAKE_VERSION_BODY"]
response_headers = security + ["x-frame-options: DENY"]
elif url.endswith("/api/ask"):
content_type = "application/json"
question = json.loads(payload)["question"]
if "Social Security" in question:
body = json.dumps(
{
"answer": "Please leave personal details out.",
"kind": "refused_input",
"citations": [],
}
)
elif "Yolobus" in question:
body = json.dumps(
{
"answer": "No current source support.",
"kind": "refused_no_support",
"citations": [],
}
)
else:
body = json.dumps(
{
"answer": "Bring published proof.",
"kind": "answered",
"corpus_version": "test-corpus",
"as_of_date": "2026-07-29",
"citations": [
{
"agency": "MST",
"title": "Veteran fares",
"url": "https://mst.org/fares/",
"fetch_date": "2026-07-29",
}
],
}
)
response_headers = security + ["x-frame-options: DENY"]
else:
content_type = "text/html"
markers = {
"/": "Transit Fare Policy Assistant",
"/offline": "Offline fare reference",
"/guide": "Which fare applies to me?",
"/embed": "Transit fare policy assistant",
}
path = "/" + url.split("/", 3)[-1] if url.count("/") >= 3 else "/"
body = f"<html><title>{markers[path]}</title></html>"
response_headers = security
if path != "/embed":
response_headers.append("x-frame-options: DENY")
headers_path.write_text(
"\r\n".join(
["HTTP/1.1 200 OK", f"content-type: {content_type}", *response_headers, "", ""]
),
encoding="utf-8",
)
body_path.write_text(body, encoding="utf-8")
sys.stdout.write("200")
"""
def _install_fake_curl(tmp_path: Path) -> tuple[Path, Path]:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
fake_curl = bin_dir / "curl"
fake_curl.write_text(FAKE_CURL, encoding="utf-8")
fake_curl.chmod(0o755)
state_path = tmp_path / "curl-state.json"
state_path.write_text(json.dumps({"requests": []}), encoding="utf-8")
return bin_dir, state_path
def _version_body(**overrides: object) -> dict[str, object]:
body: dict[str, object] = {
"corpus_version": "test-corpus",
"as_of": "2026-07-29",
"agencies": ["MST"],
"matches_pin": True,
"disabled_documents": ["yolobus-fares"],
"identity_status": "verified",
"source_revision": SOURCE,
"config_version": CONFIG,
"content_version": CONTENT,
"snapshot_version": SNAPSHOT,
"release_version": RELEASE,
"artifact_code_sha256": ARTIFACT,
"function_version": FUNCTION_VERSION,
}
body.update(overrides)
return body
def _strict_args() -> list[str]:
return [
"--require-release-identity",
"--expected-source",
SOURCE,
"--expected-config",
CONFIG,
"--expected-content",
CONTENT,
"--expected-snapshot",
SNAPSHOT,
"--expected-release",
RELEASE,
"--expected-artifact",
ARTIFACT,
"--expected-function-version",
FUNCTION_VERSION,
]
def _without_option(arguments: list[str], option: str) -> list[str]:
index = arguments.index(option)
return arguments[:index] + arguments[index + 2 :]
def _run_smoke(
tmp_path: Path,
*,
mode_args: list[str],
version_body: dict[str, object] | None = None,
disabled_docs: str = "yolobus-fares",
) -> tuple[subprocess.CompletedProcess[str], dict]:
fake_bin, state_path = _install_fake_curl(tmp_path)
body = version_body if version_body is not None else _version_body()
result = subprocess.run(
[
str(SMOKE),
"--assistant-only",
"--assistant-base-url",
"http://assistant.test",
"--connect-timeout",
"1",
"--max-time",
"2",
"--expected-disabled-docs",
disabled_docs,
*mode_args,
],
cwd=config.REPO_ROOT,
env={
**os.environ,
"LC_ALL": "C",
"PATH": f"{fake_bin}:{os.environ['PATH']}",
"FAKE_CURL_STATE": str(state_path),
"FAKE_VERSION_BODY": json.dumps(body),
},
check=False,
capture_output=True,
text=True,
timeout=30,
)
return result, json.loads(state_path.read_text(encoding="utf-8"))
def test_strict_public_smoke_requires_the_exact_verified_release(tmp_path: Path) -> None:
result, state = _run_smoke(tmp_path, mode_args=_strict_args())
assert result.returncode == 0, result.stderr
assert result.stdout.rstrip().endswith("smoke: PASS")
assert any(url.endswith("/version") for url in state["requests"])
assert sum(url.endswith("/api/ask") for url in state["requests"]) == 3
@pytest.mark.parametrize(
("mode_args", "message"),
[
([], "choose exactly one"),
(
["--allow-legacy-release-identity", *_strict_args()],
"choose exactly one",
),
],
)
def test_public_smoke_identity_mode_is_explicit_and_exclusive(
tmp_path: Path,
mode_args: list[str],
message: str,
) -> None:
result, state = _run_smoke(tmp_path, mode_args=mode_args)
assert result.returncode != 0
assert message in result.stderr
assert state["requests"] == []
@pytest.mark.parametrize(
"option",
[
"--expected-source",
"--expected-config",
"--expected-content",
"--expected-snapshot",
"--expected-release",
"--expected-artifact",
"--expected-function-version",
],
)
def test_strict_public_smoke_requires_every_expected_release_field(
tmp_path: Path,
option: str,
) -> None:
result, state = _run_smoke(
tmp_path,
mode_args=_without_option(_strict_args(), option),
)
assert result.returncode != 0
assert "requires every expected release identity argument" in result.stderr
assert state["requests"] == []
@pytest.mark.parametrize(
("option", "invalid", "message"),
[
("--expected-source", "A" * 40, "40-character lowercase"),
("--expected-config", "g" * 64, "64-character lowercase"),
("--expected-content", "c" * 63, "64-character lowercase"),
("--expected-snapshot", "D" * 64, "64-character lowercase"),
("--expected-release", "e" * 65, "64-character lowercase"),
("--expected-artifact", "f" * 64, "AWS-style base64"),
("--expected-function-version", "0", "numeric published version"),
],
)
def test_strict_public_smoke_rejects_malformed_expected_release_fields(
tmp_path: Path,
option: str,
invalid: str,
message: str,
) -> None:
arguments = _strict_args()
arguments[arguments.index(option) + 1] = invalid
result, state = _run_smoke(tmp_path, mode_args=arguments)
assert result.returncode != 0
assert message in result.stderr
assert state["requests"] == []
@pytest.mark.parametrize(
"field",
[
"identity_status",
"source_revision",
"config_version",
"content_version",
"snapshot_version",
"release_version",
"artifact_code_sha256",
"function_version",
],
)
def test_strict_public_smoke_rejects_runtime_release_drift_before_paid_health(
tmp_path: Path,
field: str,
) -> None:
body = _version_body()
body[field] = "wrong"
result, state = _run_smoke(
tmp_path,
mode_args=_strict_args(),
version_body=body,
)
assert result.returncode != 0
assert "invalid verified release identity" in result.stderr
assert not any(url.endswith("/api/ask") for url in state["requests"])
def test_strict_public_smoke_preserves_disabled_document_containment_check(
tmp_path: Path,
) -> None:
body = _version_body(disabled_documents=[])
result, state = _run_smoke(
tmp_path,
mode_args=_strict_args(),
version_body=body,
)
assert result.returncode != 0
assert "invalid verified release identity" in result.stderr
assert not any(url.endswith("/api/ask") for url in state["requests"])
def test_explicit_legacy_smoke_allows_content_identity_without_release_tuple(
tmp_path: Path,
) -> None:
legacy_body = {
"corpus_version": "test-corpus",
"content_version": CONTENT,
"as_of": "2026-07-29",
"agencies": ["MST"],
"matches_pin": True,
"disabled_documents": ["yolobus-fares"],
}
result, state = _run_smoke(
tmp_path,
mode_args=["--allow-legacy-release-identity"],
version_body=legacy_body,
)
assert result.returncode == 0, result.stderr
assert result.stdout.rstrip().endswith("smoke: PASS")
assert sum(url.endswith("/api/ask") for url in state["requests"]) == 3
@pytest.mark.parametrize(
"forbidden_field",
[
"source_revision",
"config_version",
"snapshot_version",
"release_version",
"artifact_code_sha256",
],
)
def test_legacy_smoke_rejects_identity_bearing_runtime_fields(
tmp_path: Path,
forbidden_field: str,
) -> None:
legacy_body = {
"corpus_version": "test-corpus",
"content_version": CONTENT,
"as_of": "2026-07-29",
"agencies": ["MST"],
"matches_pin": True,
"disabled_documents": ["yolobus-fares"],
forbidden_field: "",
}
result, state = _run_smoke(
tmp_path,
mode_args=["--allow-legacy-release-identity"],
version_body=legacy_body,
)
assert result.returncode != 0
assert "invalid explicit legacy release identity" in result.stderr
assert not any(url.endswith("/api/ask") for url in state["requests"])
def test_legacy_smoke_rejects_verified_identity_status(tmp_path: Path) -> None:
legacy_body = {
"corpus_version": "test-corpus",
"content_version": CONTENT,
"as_of": "2026-07-29",
"agencies": ["MST"],
"matches_pin": True,
"disabled_documents": ["yolobus-fares"],
"identity_status": "verified",
}
result, state = _run_smoke(
tmp_path,
mode_args=["--allow-legacy-release-identity"],
version_body=legacy_body,
)
assert result.returncode != 0
assert "invalid explicit legacy release identity" in result.stderr
assert not any(url.endswith("/api/ask") for url in state["requests"])
def test_legacy_smoke_rejects_expected_release_arguments(tmp_path: Path) -> None:
result, state = _run_smoke(
tmp_path,
mode_args=[
"--allow-legacy-release-identity",
"--expected-content",
CONTENT,
],
)
assert result.returncode != 0
assert "does not accept expected release identity arguments" in result.stderr
assert state["requests"] == []