mirror of
https://github.com/Sea-Haven-Industries/procurement-ingest.git
synced 2026-09-30 16:33:14 +00:00
Some checks are pending
Deploy / deploy (push) Waiting to run
* feat(api): Add @redocly/cli as a dev dependency
Signed-off-by: Adam Moussa <adam@seahavenind.com>
* feat(api): Add Redocly configuration file with custom rules
Signed-off-by: Adam Moussa <adam@seahavenind.com>
* chore(api): Redocly lint config + bring openapi.json into compliance
redocly.yaml from the Redocly guidelines builder, with three generated
rules corrected: response-contains-property had the status codes as the
required body fields (intent was the Error schema's top-level 'error';
403 exempt since API Gateway emits AWS's {message} shape, 501 not 503);
operation-4xx-problem-details-rfc7807 off (adopting RFC 7807 would be a
runtime + SHOC-contract change, decided against); the two inert casing
rules (parameter names, schema properties) removed because both name
sets are contract-pinned (gateway resource paths, DynamoDB items).
Spec changes, no runtime impact: operationIds renamed to method-prefixed
kebab-case (get-work-orders, post-work-order-comment, ...); tags added to
all 15 operations + root tags object (groups the Redoc sidebar); examples
on all six parameters; license field; server description punctuation; two
descriptions reworded to start capitalized. Real linter catches fixed:
the two x-planned ops were missing their {workOrderId} path parameter
and any 4xx response (403 added - true today, gateway rejects unsigned).
.redocly.lint-ignore.yaml pins the six deliberate exceptions: webhook
keys are the shipped SHOC contract event names (not renameable), and the
x-planned ops answer only 501 (no 2xx to document).
package.json: npm run lint:api. Verified: lint 0 errors, 675 pytest,
headless-Chrome render of the tagged docs page.
* feat(api): SHOC design-system theme for /docs (vendored fonts)
Themes the Redoc page with the canonical SHOC token set: Montserrat 600
headings / DM Sans body / JetBrains Mono code, primary #1c75bc, navy
#262262 sidebar text + right panel, #f9fafb background, 244px sidebar.
sortRequiredPropsFirst on; 200 responses pre-expanded.
Fonts ship as lambdas/api/fonts.css (latin woff2 subsets from
@fontsource 5.3.0, embedded as data URIs, ~90KB) and inline via a new
__FONTS_CSS__ placeholder with the same </style breakout guard --
the offline single-response invariant holds, nothing fetches Google
Fonts (test-pinned). Bundling cp + bundle-consistency pin + spec-drift
asset checks extended.
Verified: headless-Chrome render (theme + fonts applied), ruff, 675
pytest, cdk synth + staged-asset check.
* feat(api): SHOC gradient topbar on /docs
64px fixed header with the SHOC shell gradient token (#1b1f52 ->
#1c4f8f -> #1c75bc), Sea Haven wordmark in Montserrat 600, page name
right-aligned in DM Sans. Redoc's scrollYOffset: 64 keeps the sticky
sidebar and anchor scrolling clear of the fixed bar. Verified via
headless-Chrome render.
* style(api): normalize /docs header and right-panel blues
The right panel's #262262 is a purple-leaning navy that clashed with
the cyan-leaning gradient, and the bar's brightest point sat directly
over the dark panel. Right panel now uses #1b1f52 (the gradient's own
dark endpoint) and the gradient runs bright-to-dark so its dark end
lands flush on the panel -- no seam, one blue family. Verified via
headless-Chrome render.
* style(api): right-panel gradient on /docs via bundle-pinned override
Redoc's theme only takes solid colors (it derives shades from
rightPanel.backgroundColor), so the gradient (#1b3d79 -> #1b3068 ->
#1b1f52, continuing the topbar blend) rides as a CSS override on the
styled-components classes of the per-section right-panel divs
(.sc-iGgWBj.sc-gsFSXq + the .sc-dExYaf stub). Those names are
deterministic for the vendored 2.5.3 bundle (verified across loads) but
change on any Redoc bump: re-derive via headless probe (find elements
whose computed background equals the rightPanel color). If they stop
matching, the panel falls back to the solid #1b1f52 theme color --
cosmetic only. Verified via headless-Chrome render.
* feat(api): collapsible samples column on /docs
Redoc CE has no built-in panel toggle, so the topbar gains a Hide/Show
samples button that flips .samples-collapsed on <html>: the right-panel
divs hide (same bundle-pinned styled-components classes as the gradient
override) and each section's content half takes the full width. Choice
persists in localStorage; aria-pressed tracks state. If the pinned
classes stop matching after a Redoc bump the toggle goes inert --
cosmetic only. Both states verified via headless-Chrome render.
* ci(api): spec-lint CI gate + npm Dependabot coverage
New spec-lint job mirrors the local npm run lint:api so openapi.json
cannot drift from redocly.yaml with green CI. Dependabot gains the npm
ecosystem (package.json is new; nothing watched @redocly/cli).
* feat(api): docs finishing touches - x-tagGroups, favicon, docs:preview
x-tagGroups sections the Redoc sidebar (Read API / Meta / SHOC Feed);
inline data-URI SVG favicon (SHOC blue) stops the browser's follow-up
/favicon.ico request 403ing at the gateway; npm run docs:preview wraps
the real-handler local render (scripts/preview_docs.py); README gains a
docs-page architecture section covering the inline pattern, theme,
pinned-selector caveat, and tooling. Lint 0 errors, 675 pytest,
headless render verified.
---------
Signed-off-by: Adam Moussa <adam@seahavenind.com>
280 lines
8.7 KiB
Python
280 lines
8.7 KiB
Python
"""procurement-api handler tests (offline, fake Dynamo).
|
|
|
|
Pins the auth seam both ways: docs routes fail closed on the shared token
|
|
(including the browser ?token= shim), while data routes must NOT consult the
|
|
token at all -- SigV4 authorization is API Gateway's job upstream, and a
|
|
handler-side token check there would break every legitimate SigV4 caller.
|
|
Also pins routing, 404/501/400 mapping, Decimal-safe serialization, and limit
|
|
clamping.
|
|
"""
|
|
|
|
import json
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
|
|
from tests.support import load_lambda_module
|
|
|
|
|
|
class _FakeTable:
|
|
def __init__(self, items):
|
|
self._items = items
|
|
self.last_kwargs = None
|
|
|
|
def scan(self, **kwargs):
|
|
self.last_kwargs = kwargs
|
|
return {"Items": list(self._items)}
|
|
|
|
def query(self, **kwargs):
|
|
self.last_kwargs = kwargs
|
|
return {"Items": list(self._items)}
|
|
|
|
def get_item(self, Key): # noqa: N803 (boto3 kwarg name)
|
|
pk_attr, pk_val = next(iter(Key.items()))
|
|
for item in self._items:
|
|
if item.get(pk_attr) == pk_val:
|
|
return {"Item": item}
|
|
return {}
|
|
|
|
|
|
class _FakeDynamo:
|
|
def __init__(self, tables):
|
|
self.tables = tables
|
|
|
|
def Table(self, name): # noqa: N802 (boto3 method name)
|
|
return self.tables[name]
|
|
|
|
|
|
def _event(method, resource, path_params=None, qs=None, headers=None):
|
|
return {
|
|
"httpMethod": method,
|
|
"resource": resource,
|
|
"pathParameters": path_params or {},
|
|
"queryStringParameters": qs or {},
|
|
"headers": headers or {},
|
|
}
|
|
|
|
|
|
@pytest.fixture()
|
|
def api(monkeypatch):
|
|
mod = load_lambda_module("api", "handler")
|
|
wo_items = [
|
|
{
|
|
"work_order_id": "11144580730",
|
|
"wo_status": "assigned",
|
|
"description": "Dock door 14",
|
|
}
|
|
]
|
|
comment_items = [
|
|
{
|
|
"work_order_id": "11144580730",
|
|
"comment_id": "11144580730#2026-01-01T00:00:00#abc123def456",
|
|
"text": "Vendor dispatched",
|
|
}
|
|
]
|
|
po_items = [
|
|
{
|
|
"po_number": "2D-22030794",
|
|
"total_amount": Decimal("123.45"),
|
|
"line_items": [{"quantity": Decimal("5"), "price": Decimal("24.69")}],
|
|
}
|
|
]
|
|
site_items = [{"siteCode": "JFK8", "state": "NY", "poCount": Decimal("7")}]
|
|
|
|
tables = {
|
|
mod.wo_repo.WORK_ORDERS_TABLE: _FakeTable(wo_items),
|
|
mod.wo_repo.COMMENTS_TABLE: _FakeTable(comment_items),
|
|
mod.po_repo.PO_TABLE: _FakeTable(po_items),
|
|
mod.po_repo.VERIFIED_SITES_TABLE: _FakeTable(site_items),
|
|
}
|
|
fake = _FakeDynamo(tables)
|
|
monkeypatch.setattr(mod.wo_repo, "dynamodb", fake)
|
|
monkeypatch.setattr(mod.po_repo, "dynamodb", fake)
|
|
return mod, fake
|
|
|
|
|
|
def test_healthcheck_short_circuits(api):
|
|
mod, _ = api
|
|
assert mod.handler({"healthcheck": True}, None) == {"healthcheck": "ok"}
|
|
|
|
|
|
def test_docs_fail_closed_without_token(api):
|
|
# No WEB_UI_AUTH_TOKEN_SECRET_ARN configured -> web_ui_auth returns None
|
|
# -> the docs routes must 401, not serve the spec.
|
|
mod, _ = api
|
|
for resource in ("/docs", "/openapi.json"):
|
|
response = mod.handler(_event("GET", resource), None)
|
|
assert response["statusCode"] == 401
|
|
|
|
|
|
def test_docs_served_when_authenticated(api, monkeypatch):
|
|
mod, _ = api
|
|
monkeypatch.setattr(mod, "is_authenticated", lambda event: True)
|
|
docs = mod.handler(_event("GET", "/docs"), None)
|
|
assert docs["statusCode"] == 200
|
|
assert docs["headers"]["Content-Type"].startswith("text/html")
|
|
# The vendored Redoc bundle is inlined and the spec (carrying the title)
|
|
# with it.
|
|
assert "Redoc.init" in docs["body"]
|
|
assert "Procurement Ingest API" in docs["body"]
|
|
# Every placeholder must be substituted, or the page renders broken.
|
|
for placeholder in (
|
|
mod._SPEC_PLACEHOLDER,
|
|
mod._JS_PLACEHOLDER,
|
|
mod._FONTS_PLACEHOLDER,
|
|
):
|
|
assert placeholder not in docs["body"]
|
|
|
|
spec = mod.handler(_event("GET", "/openapi.json"), None)
|
|
assert spec["statusCode"] == 200
|
|
assert json.loads(spec["body"])["openapi"] == "3.1.0"
|
|
|
|
|
|
def test_token_query_shim_synthesizes_header(api, monkeypatch):
|
|
mod, _ = api
|
|
seen = {}
|
|
|
|
def capture(event):
|
|
seen.update(event.get("headers") or {})
|
|
return False
|
|
|
|
monkeypatch.setattr(mod, "is_authenticated", capture)
|
|
original = _event("GET", "/docs", qs={"token": "sekrit"})
|
|
mod.handler(original, None)
|
|
assert seen.get("x-auth-token") == "sekrit"
|
|
# The shim must not mutate the caller's event in place.
|
|
assert "x-auth-token" not in original["headers"]
|
|
|
|
|
|
def test_non_ascii_token_fails_closed_not_500(api):
|
|
# A non-ASCII presented token must 401 (fail closed), not crash
|
|
# hmac.compare_digest into a 500 that pages the 5xx alarm. Reach the
|
|
# web_ui_auth module namespace through the imported is_authenticated's
|
|
# globals and seed a valid cached token.
|
|
mod, _ = api
|
|
auth_globals = mod.is_authenticated.__globals__
|
|
auth_globals["_auth_token_cache"] = "correct-token"
|
|
auth_globals["_auth_token_cached_at"] = float("inf")
|
|
try:
|
|
response = mod.handler(
|
|
_event("GET", "/docs", headers={"x-auth-token": "é"}), None
|
|
)
|
|
assert response["statusCode"] == 401
|
|
finally:
|
|
auth_globals["_auth_token_cache"] = None
|
|
auth_globals["_auth_token_cached_at"] = 0.0
|
|
|
|
|
|
def test_data_routes_never_consult_docs_token(api, monkeypatch):
|
|
mod, _ = api
|
|
|
|
def explode(event):
|
|
raise AssertionError("data routes must not consult the docs token")
|
|
|
|
monkeypatch.setattr(mod, "is_authenticated", explode)
|
|
response = mod.handler(_event("GET", "/work-orders"), None)
|
|
assert response["statusCode"] == 200
|
|
|
|
|
|
def test_list_and_get_routing(api):
|
|
mod, _ = api
|
|
listing = mod.handler(_event("GET", "/work-orders"), None)
|
|
body = json.loads(listing["body"])
|
|
assert body["items"][0]["work_order_id"] == "11144580730"
|
|
assert body["next_cursor"] is None
|
|
|
|
hit = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/work-orders/{workOrderId}",
|
|
path_params={"workOrderId": "11144580730"},
|
|
),
|
|
None,
|
|
)
|
|
assert json.loads(hit["body"])["wo_status"] == "assigned"
|
|
|
|
miss = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/work-orders/{workOrderId}",
|
|
path_params={"workOrderId": "999"},
|
|
),
|
|
None,
|
|
)
|
|
assert miss["statusCode"] == 404
|
|
|
|
comments = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/work-orders/{workOrderId}/comments",
|
|
path_params={"workOrderId": "11144580730"},
|
|
),
|
|
None,
|
|
)
|
|
assert json.loads(comments["body"])["items"][0]["text"] == "Vendor dispatched"
|
|
|
|
sites = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/verified-sites/{siteCode}",
|
|
path_params={"siteCode": "JFK8"},
|
|
),
|
|
None,
|
|
)
|
|
assert json.loads(sites["body"])["state"] == "NY"
|
|
|
|
|
|
def test_decimal_serialization_round_trips(api):
|
|
mod, _ = api
|
|
response = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/purchase-orders/{poNumber}",
|
|
path_params={"poNumber": "2D-22030794"},
|
|
),
|
|
None,
|
|
)
|
|
body = json.loads(response["body"])
|
|
assert body["total_amount"] == 123.45
|
|
assert body["line_items"][0]["quantity"] == 5
|
|
assert isinstance(body["line_items"][0]["quantity"], int)
|
|
assert "Decimal" not in response["body"]
|
|
|
|
|
|
def test_limit_clamped_to_bounds(api):
|
|
mod, fake = api
|
|
mod.handler(_event("GET", "/work-orders", qs={"limit": "9999"}), None)
|
|
assert fake.tables[mod.wo_repo.WORK_ORDERS_TABLE].last_kwargs["Limit"] == 500
|
|
mod.handler(_event("GET", "/work-orders", qs={"limit": "0"}), None)
|
|
assert fake.tables[mod.wo_repo.WORK_ORDERS_TABLE].last_kwargs["Limit"] == 1
|
|
|
|
|
|
def test_non_integer_limit_is_400(api):
|
|
mod, _ = api
|
|
response = mod.handler(_event("GET", "/work-orders", qs={"limit": "abc"}), None)
|
|
assert response["statusCode"] == 400
|
|
|
|
|
|
def test_planned_routes_return_501(api):
|
|
mod, _ = api
|
|
for method, resource in mod.PLANNED_ROUTES:
|
|
response = mod.handler(_event(method, resource), None)
|
|
assert response["statusCode"] == 501, (method, resource)
|
|
|
|
|
|
def test_unknown_route_is_404(api):
|
|
mod, _ = api
|
|
response = mod.handler(_event("GET", "/nope"), None)
|
|
assert response["statusCode"] == 404
|
|
|
|
|
|
def test_unexpected_error_maps_to_clean_500(api, monkeypatch):
|
|
mod, fake = api
|
|
|
|
def boom(**kwargs):
|
|
raise RuntimeError("dynamo fell over")
|
|
|
|
monkeypatch.setattr(fake.tables[mod.wo_repo.WORK_ORDERS_TABLE], "scan", boom)
|
|
response = mod.handler(_event("GET", "/work-orders"), None)
|
|
assert response["statusCode"] == 500
|
|
assert json.loads(response["body"]) == {"error": "internal error"}
|