#!/usr/bin/env bash # ci/tests/approval-override.test.sh — proves ci/approval-override.py selects the override it # claims to select, and REJECTS the ones it claims to reject. # # This parser decides whether a human review requirement gets waived. Every rule in it exists # because the obvious implementation ("does the body contain the directive?") is wrong in a way # that is invisible until abused: a quoted directive would re-arm an override, a bare directive # would waive review with no stated reason, and an override posted against commit A would go on # covering commit B forever. A guard is worth nothing until somebody has watched it refuse, which # is the same reasoning as ci/tests/decisions-log.test.sh one layer up. # # The parser is pure and offline BY DESIGN — authorisation (the live permission lookup) lives in # ci/approval-policy.sh, which cannot be tested without Gitea. That split is what makes this file # possible, and it is also why a green run here does NOT prove the stage authorises correctly. set -uo pipefail CI_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" SUT="${CI_DIR}/approval-override.py" PASS=0 FAIL=0 ok() { printf ' ok %s\n' "$*"; PASS=$((PASS+1)); } bad() { printf ' FAIL %s\n' "$*"; FAIL=$((FAIL+1)); } HEAD_TIME="2026-10-01T12:00:00Z" AFTER="2026-10-01T12:30:00Z" BEFORE="2026-10-01T11:30:00Z" # run -> stdout of the parser (stderr is diagnostic only) run() { COMMENTS_JSON="$1" REVIEWS_JSON="" HEAD_TIME="${2:-$HEAD_TIME}" DIRECTIVE="${3:-/approve-override}" \ python3 "$SUT" 2>/dev/null } # run_rev — the same, but through the REVIEWS source. run_rev() { COMMENTS_JSON="" REVIEWS_JSON="$1" HEAD_TIME="${2:-$HEAD_TIME}" DIRECTIVE="${3:-/approve-override}" \ python3 "$SUT" 2>/dev/null } # comment comment() { python3 -c ' import json, sys print(json.dumps([{"user": {"login": sys.argv[1]}, "created_at": sys.argv[2], "body": sys.argv[3]}])) ' "$1" "$2" "$3" } expect_none() { local label="$1" out out="$(run "$2")" if [ -z "$out" ]; then ok "$label"; else bad "$label (got: ${out})"; fi } expect_user() { local label="$1" json="$2" want="$3" out got out="$(run "$json")" got="$(cut -f1 <<<"$out")" if [ "$got" = "$want" ]; then ok "$label"; else bad "$label (wanted ${want}, got '${got}')"; fi } expect_reason() { local label="$1" json="$2" want="$3" out got out="$(run "$json")" got="$(cut -f4- <<<"$out")" if [ "$got" = "$want" ]; then ok "$label"; else bad "$label (wanted '${want}', got '${got}')"; fi } # check