From 0d7803079c61235abfd891aac0ebcb7f406c449f Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 16 Jul 2026 13:37:22 +0200 Subject: [PATCH] ci: add advisory functional-E2E curl harness (fixes #299) Codify the manual live-E2E curl flows sessions have been re-running by hand into a CI regression net: a new `functional-e2e` job boots the app from source (via scripts/e2e-local.sh, parameterized for Release) and drives scripts/ e2e-functional.sh against it. First-cut contracts (all curl-only, deterministic, no seeded media/ffmpeg/browser): - legacy->SPA redirect sweep + the /api,/artwork never-redirect exemption - auth/CSRF/security-stamp flow (setup-claim, read-gate, CSRF, login, logout+revoke) - library-scan status contract (404/202/scan-status) - optimistic-concurrency If-Match/412 round-trip Advisory by design (separate job, not a `build` dependency, not a required check) so a functional-E2E flake can't block the unit-test gate; promote once proven, mirroring the migrations-job rollout. SQLite default -> no DB service. Deferred to #299 follow-ups (need scanner+seeded media or a browser to be deterministic): the racy 409 re-trigger, playout-build lock 409, Playwright UI flows. Assertions verified 30/30 green against a real Release-built instance; caught /artwork/* returning 400 (not the 404 a static read suggested). Docs updated same PR: docs/ci-cd.md (new job), docs/e2e-local.md (harness), docs/decisions.md (append-only). Co-Authored-By: Claude Opus 4.8 (1M context) --- .gitea/workflows/docker-build.yml | 66 +++++++++ docs/ci-cd.md | 27 ++++ docs/decisions.md | 30 +++++ docs/e2e-local.md | 42 +++++- scripts/e2e-functional.sh | 217 ++++++++++++++++++++++++++++++ scripts/e2e-local.sh | 7 +- 6 files changed, 386 insertions(+), 3 deletions(-) create mode 100755 scripts/e2e-functional.sh diff --git a/.gitea/workflows/docker-build.yml b/.gitea/workflows/docker-build.yml index 665d19fc3..427b1e401 100644 --- a/.gitea/workflows/docker-build.yml +++ b/.gitea/workflows/docker-build.yml @@ -202,6 +202,72 @@ jobs: done echo "::endgroup::" + functional-e2e: + name: Functional E2E (curl contracts) + runs-on: ubuntu-latest + # Advisory gate (ersatztv#299): boots the app from source and drives the manual live-E2E + # curl flows (legacy->SPA redirects, auth/CSRF/security-stamp, library-scan status contract, + # If-Match/412) that sessions have been re-running by hand. Deliberately NOT a `needs:` of + # `build` and not (yet) a required check, so a functional-E2E flake can't block image builds or + # the unit-test gate — promote it to a required check / build dependency once it's proven + # reliable (same rollout the `migrations` job used). SQLite default provider -> no DB service. + # Runs on PRs and on main (regression net); skipped for v* tag builds. + if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main' + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + + - name: Cache NuGet packages + uses: actions/cache@v4 + with: + path: ~/.nuget/packages + key: nuget-${{ runner.os }}-${{ hashFiles('Directory.Packages.props', 'global.json') }} + restore-keys: nuget-${{ runner.os }}- + + - name: Restore + run: dotnet restore + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: '22.x' + cache: npm + cache-dependency-path: web/package-lock.json + + - name: Install SPA dependencies + working-directory: web + run: npm ci + + - name: Build SPA + working-directory: web + run: npm run build + + - name: Build (Release) + run: dotnet build ErsatzTV.sln --configuration Release --no-restore + + - name: Ensure ffmpeg is available + run: command -v ffmpeg >/dev/null 2>&1 || (sudo apt-get update && sudo apt-get install -y ffmpeg) + + - name: Boot instance and run functional-E2E harness + run: | + set -euo pipefail + export ETV_BUILD_CONFIG=Release ETV_UI_PORT=8409 + CFG="$(mktemp -d)" + # e2e-local.sh copies wwwroot, launches the DLL in the background (logging to a file, so + # this command substitution returns as soon as the app is ready), and prints PID/CONFIG_DIR. + OUT="$(scripts/e2e-local.sh "$CFG")" + printf '%s\n' "$OUT" + PID="$(printf '%s\n' "$OUT" | awk -F= '/^PID=/{print $2}')" + trap 'kill "$PID" 2>/dev/null || true' EXIT + scripts/e2e-functional.sh "http://localhost:${ETV_UI_PORT}" "$CFG" + build: name: Build & push image (amd64) # `small` = the dedicated small-jobs runner lane (server-management#574). diff --git a/docs/ci-cd.md b/docs/ci-cd.md index 5cc248ba3..2e0e233b0 100644 --- a/docs/ci-cd.md +++ b/docs/ci-cd.md @@ -123,6 +123,33 @@ the image build. cleanup; dumps container logs on failure. Catches routing / base-URL (#1) / migration regressions that leave the app "up" but serving broken output. +### `functional-e2e` job (advisory; PR + main) + +Boots the app **from source** and drives the manual live-E2E curl flows sessions have historically +re-run by hand, turning them into a CI regression net (ersatztv#299). It is the automatable half of +`docs/e2e-local.md`; the two scripts it chains run identically locally and in CI: + +1. `npm ci` + `npm run build` (SPA), `dotnet build ErsatzTV.sln -c Release`, ensure `ffmpeg` is on PATH. +2. `ETV_BUILD_CONFIG=Release scripts/e2e-local.sh ` — copies `wwwroot`, launches + `dotnet ErsatzTV.dll` in the background (logging to a file so the launch step returns once the app + is ready), prints `PID`/`CONFIG_DIR`. +3. `scripts/e2e-functional.sh http://localhost:8409 ` — asserts, all curl-only and + deterministic (no seeded media, no browser): the **legacy→SPA redirect sweep** (+ the `/api`, + `/artwork` never-redirect exemption), the **auth/CSRF/security-stamp** flow (setup-claim → + read-gate 401/200 → re-claim 409 → CSRF 403 → login 401/200 → logout 403/204 → post-logout + stamp-revocation 401), the **library-scan status contract** (404 unknown / 202 queued / + `scan-status` 200), and the **If-Match/412** round-trip on `rerun-collections`. A `trap` kills the + instance on step exit. + +**Advisory, by design** (the issue's "keep it a separate job so a functional-E2E flake can't block the +unit-test gate"): it is **not** a `needs:` of `build` and **not (yet) a required check**, so a flake +blocks nothing. Promote it to a required check / `build` dependency once it's proven reliable — the +same staged rollout the `migrations` job used. SQLite is the default provider, so unlike `migrations` +it needs **no** DB service container. Runs on PRs and on `main` (regression net); skipped for `v*` tag +builds. Out of scope for this first cut (need the scanner subprocess + seeded media, or a browser, to +be deterministic — tracked as ersatztv#299 follow-ups): the racy 409 "already-scanning" re-trigger, +the playout-build lock 409, and the genuinely UI-interactive Playwright flows. + ### `docs-reminder` job (non-blocking, PR-only) A lightweight nudge that enforces the CLAUDE.md "docs-update is part of done" rule for the diff --git a/docs/decisions.md b/docs/decisions.md index 40ea73f75..65c14e086 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -89,6 +89,7 @@ keep append-only from accreting stale, contradictory, or unreadably-large histor - [2026-07-13 — API versioning: the whole `/api` surface is mounted at `/api/v1`, additive-only after freeze (#286)](#2026-07-13--api-versioning-the-whole-api-surface-is-mounted-at-apiv1-additive-only-after-freeze-286) - [2026-07-13 — Scheduling API hardening: null-name 500s, duplicate template items, unreachable 404 (#172)](#2026-07-13--scheduling-api-hardening-null-name-500s-duplicate-template-items-unreachable-404-172) - [2026-07-13 — Release promotion: floating `:prod`, exact-image scan before manual deploy (#335)](#2026-07-13--release-promotion-floating-prod-exact-image-scan-before-manual-deploy-335) +- [2026-07-16 — Functional-E2E CI harness: advisory curl-contract job over an app booted from source (#299)](#2026-07-16--functional-e2e-ci-harness-advisory-curl-contract-job-over-an-app-booted-from-source-299) --- @@ -1844,3 +1845,32 @@ Tests pin stable route-object identity (including sibling sub-paths and query-on sub-path rendering and dirty-popstate restoration, and primary-action matching/latest-handler/route-change/ cleanup/stale-owner behavior. Detailed Builder and Settings behavior is colocated with those screens while `App.test.tsx` remains shell/routing/composition coverage. Refs #247 #243 #238 #230. + +## 2026-07-16 — Functional-E2E CI harness: advisory curl-contract job over an app booted from source (#299) + +The manual live-E2E curl flows sessions had been re-running by hand (and leaving only as PR/issue +comments) are now a CI regression net. Two decisions shaped it: + +**Boot from source + `dotnet run`, not the built image.** The only "E2E" in CI before this was the +smoke test in the `build` job, which runs against the *pushed* image — so it exists only on `main`/`v*` +(the image isn't built on PRs) and would test a stale image, not the PR's code. To gate PRs on the PR's +own code, the `functional-e2e` job builds the SPA + solution and launches `dotnet ErsatzTV.dll` via the +same `scripts/e2e-local.sh` used locally (parameterized with `ETV_BUILD_CONFIG=Release`). The assertions +live in `scripts/e2e-functional.sh`, so the identical harness runs by hand and in CI — which is the +point of the issue (stop re-deriving the flows each session). + +**Advisory, not blocking — separate job, not a `build` dependency, not a required check.** Per the +issue's "a functional-E2E flake must not block the unit-test gate." A boot-the-app job has more moving +parts (background process, port, readiness wait) than a pure unit test, so it starts advisory and gets +promoted to a required check / `build` dependency once proven reliable — the same staged rollout the +`migrations` job used. SQLite is the default provider, so it needs no DB service container. + +**Scope is curl-only and deterministic; the racy/interactive flows are explicitly deferred.** The first +cut asserts the legacy→SPA redirect sweep (+ `/api`/`/artwork` never-redirect exemption), the +auth/CSRF/security-stamp flow, the library-scan status contract (404/202/`scan-status`), and the +`If-Match`/412 round-trip — all exercisable without seeded media, ffmpeg-transcode, or a browser (an +empty local library still enqueues `202`; an empty collection drives the concurrency editor). The 409 +"already-scanning" re-trigger (needs a long-running scan to be non-racy), the playout-build lock 409, +and the genuinely UI-interactive Playwright flows are deferred as #299 follow-ups rather than shipped +flaky. Assertions were written against a real running instance, not the source — which caught that +`/artwork/*` returns `400` (not the `404` a static read suggested); extend the harness the same way. diff --git a/docs/e2e-local.md b/docs/e2e-local.md index f38571fad..923c394ba 100644 --- a/docs/e2e-local.md +++ b/docs/e2e-local.md @@ -120,11 +120,51 @@ Usage: scripts/e2e-local.sh [CONFIG_DIR] ``` - `CONFIG_DIR` defaults to a fresh `mktemp -d` if omitted. -- Copies `ErsatzTV/wwwroot` → `ErsatzTV/bin/Debug/net10.0/wwwroot`. +- Copies `ErsatzTV/wwwroot` → `ErsatzTV/bin//net10.0/wwwroot`. - Launches `dotnet ErsatzTV.dll` in the background with `ETV_CONFIG_FOLDER` set. - Waits (up to 120s) for the `Done migrating search index` log line. - Prints the PID and port, then **exits leaving the server running** — the caller is responsible for killing the PID when done (`kill `). +- **`ETV_BUILD_CONFIG`** selects which build output to launch (`Debug` default for local dev; the + CI `functional-e2e` job sets `Release`). It must match the `dotnet build --configuration` you ran + first — the script only copies `wwwroot` + launches; it does not build. + +## Functional-E2E harness: `scripts/e2e-functional.sh` + +The ad-hoc curl scenarios sessions run against a live instance (redirect sweeps, auth flows, the scan +status contract, `If-Match`/412) are codified into a single harness so they run identically by hand +and in CI (the advisory `functional-e2e` job — `docs/ci-cd.md`). It does **not** boot the app; pair it +with `scripts/e2e-local.sh` above (or point it at any running instance): + +```bash +CFG=$(mktemp -d) +eval "$(scripts/e2e-local.sh "$CFG" | sed -n 's/^\(PID\|PORT\)=/\1=/p')" # launch, capture PID/PORT +scripts/e2e-functional.sh "http://localhost:${PORT}" "$CFG" # assert; exit 1 on any failure +kill "$PID" +``` + +`CONFIG_DIR` (arg 2) is **required** — the harness reads the instance's machine key from +`$CONFIG_DIR/api.key` and sends `X-Api-Key` on every `/api` call (the surface is fail-closed). It runs +each assertion even after a failure and prints a pass/fail summary, exiting non-zero if any failed. + +What it covers (all curl-only, deterministic, no seeded media / ffmpeg-transcode / browser needed): +- **Legacy→SPA redirects**: a sweep of representative `LegacyUiRedirects` routes 302→`/app/*`, plus + the `/api` + `/artwork` never-redirect exemption (asserted as "did not 302 to `/app`", since those + 4xx from their own handlers — `/api/*` 404s, `/artwork/*` 400s). +- **Auth/CSRF/security-stamp** (mirrors the #295 manual set): fresh `auth/config` `setupRequired:true` + → read-gate 401-without-key / 200-with-key → setup-claim 200 → re-claim 409 → session mutation + 403-without-`X-CSRF` → login 401-then-200 → logout 403-without-CSRF / 204-with → post-logout the + same cookie is 401 (rotated security stamp). +- **Library-scan status contract**: create an empty local library → scan `202`, unknown-library scan + `404`, `scan-status` `200`. +- **Optimistic concurrency**: create a collection + a `rerun-collections` targeting it → `GET` emits + an `ETag` → `PUT` with a stale `If-Match` `412`, current `200`, malformed `400`. + +**Deliberately deferred** (need the scanner subprocess + seeded media, or a browser, to be +deterministic — ersatztv#299 follow-ups): the 409 "already-scanning" re-trigger, the playout-build +lock 409, and the genuinely UI-interactive Playwright flows. When you extend the harness, add the +observed contract here and keep the assertions deterministic (probe the real instance first — the +initial cut caught that `/artwork/*` 400s where a guess said 404). ## Seeding a local TV library for E2E diff --git a/scripts/e2e-functional.sh b/scripts/e2e-functional.sh new file mode 100755 index 000000000..515c80434 --- /dev/null +++ b/scripts/e2e-functional.sh @@ -0,0 +1,217 @@ +#!/usr/bin/env bash +# scripts/e2e-functional.sh — drive the manual live-E2E functional flows against a RUNNING +# ErsatzTV instance and assert their HTTP contracts. This is the codified, automatable half of the +# ad-hoc curl scenarios sessions have been running by hand (ersatztv#299); it runs both locally and +# in CI (the `functional-e2e` job in .gitea/workflows/docker-build.yml). +# +# It does NOT boot the app — pair it with scripts/e2e-local.sh (which builds/launches and prints the +# CONFIG_DIR), or point it at any already-running instance: +# +# scripts/e2e-functional.sh [BASE_URL] [CONFIG_DIR] +# +# BASE_URL default http://localhost:8409 (the app's UI+API port; ETV_UI_PORT) +# CONFIG_DIR the instance's config folder — REQUIRED, so we can read its machine api key +# ($CONFIG_DIR/api.key). The /api surface is fail-closed (Api:RequireKeyForReads +# defaults true), so every /api call sends X-Api-Key. +# +# Scope (all curl-only, deterministic, no seeded media / ffmpeg / browser needed): +# 1. Legacy -> SPA redirect sweep (+ the /api,/artwork never-redirect exemption). +# 2. Auth / CSRF / security-stamp flow (setup-claim, read-gate, CSRF gate, login, logout+revoke). +# 3. Library scan lifecycle status-code contract (404 / 202 / scan-status). +# 4. Optimistic-concurrency If-Match / 412 round-trip. +# +# Deliberately OUT of scope for this first cut (need seeded media + the scanner subprocess, or a +# browser, to be deterministic — tracked as ersatztv#299 follow-ups): +# - the 409 "already scanning" re-trigger (racy without a long-running scan), +# - the playout-build lock 409, +# - the genuinely UI-interactive Playwright flows. +# +# Exit status: 0 if every assertion passed, 1 if any failed. Assertions keep running after a +# failure so one run reports the full picture. + +set -uo pipefail + +BASE_URL="${1:-http://localhost:8409}" +CONFIG_DIR="${2:-}" + +if [ -z "$CONFIG_DIR" ]; then + echo "error: CONFIG_DIR (arg 2) is required — needed to read the machine api key ($CONFIG_DIR/api.key)." >&2 + echo "usage: scripts/e2e-functional.sh [BASE_URL] CONFIG_DIR" >&2 + exit 2 +fi + +KEY_FILE="$CONFIG_DIR/api.key" +if [ ! -f "$KEY_FILE" ]; then + echo "error: $KEY_FILE not found. Is the instance running with ETV_CONFIG_FOLDER=$CONFIG_DIR?" >&2 + exit 2 +fi +API_KEY="$(cat "$KEY_FILE")" + +PASS=0 +FAIL=0 +FAILURES=() + +# ---- assertion helpers ------------------------------------------------------------------------- + +# ok/bad: record a single assertion result. +ok() { PASS=$((PASS + 1)); printf ' \033[32mPASS\033[0m %s\n' "$1"; } +bad() { FAIL=$((FAIL + 1)); FAILURES+=("$1"); printf ' \033[31mFAIL\033[0m %s\n' "$1"; } + +# status_of METHOD URL [extra curl args...] -> prints the HTTP status code. +status_of() { + local method="$1" url="$2"; shift 2 + curl -s -o /dev/null -w '%{http_code}' -X "$method" "$@" "$url" +} + +# expect_status "desc" EXPECTED METHOD URL [extra curl args...] +expect_status() { + local desc="$1" expected="$2" method="$3" url="$4"; shift 4 + local got; got="$(status_of "$method" "$url" "$@")" + if [ "$got" = "$expected" ]; then ok "$desc (=$expected)"; else bad "$desc — expected $expected, got $got [$method $url]"; fi +} + +# api(): curl against the /api surface with the machine key + JSON content type. +api() { curl -s -H "X-Api-Key: $API_KEY" -H 'Content-Type: application/json' "$@"; } + +# json_field FILE FIELD -> value of a top-level JSON field (via python3, always present here). +json_field() { python3 -c "import json,sys; print(json.load(open('$1')).get('$2',''))"; } + +section() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; } + +echo "Functional E2E against $BASE_URL (config: $CONFIG_DIR)" + +# ---- 1. Legacy -> SPA redirect sweep ------------------------------------------------------------ +# The legacy branch 302s GET requests to their /app equivalent; /api, /artwork, /docs, /openapi are +# NEVER redirected (they 4xx from their own handlers/fallback). See ErsatzTV/LegacyUiRedirects.cs + +# Startup.cs MapFallback, and docs/blazor-route-parity.md. +section "Legacy -> SPA redirects" + +# redirects_to "src" "expected /app location suffix" +redirects_to() { + local src="$1" want="$2" + local code loc + code="$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL$src")" + loc="$(curl -s -o /dev/null -w '%{redirect_url}' "$BASE_URL$src")" + if [ "$code" = "302" ] && [ "$loc" = "$BASE_URL$want" ]; then + ok "$src -> 302 $want" + else + bad "$src — expected 302 -> $BASE_URL$want, got $code -> ${loc:-}" + fi +} +redirects_to "/" "/app" +redirects_to "/channels" "/app/channels" +redirects_to "/channels/5" "/app/edit-channel/5" +redirects_to "/settings/ffmpeg" "/app/settings/streaming" +redirects_to "/media/movies" "/app/media?kind=movies" +redirects_to "/media/sources/plex/3/libraries" "/app/libraries/plex/3/sync" + +# The exempt prefixes must NOT redirect to /app — they 4xx from their own handlers (an unknown +# /api path 404s; /artwork rejects a non-artwork path 400). Assert only "did not 302 to /app". +never_redirects() { + local src="$1" + local code loc + code="$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL$src")" + loc="$(curl -s -o /dev/null -w '%{redirect_url}' "$BASE_URL$src")" + if [ "$code" != "302" ] && [ -z "$loc" ] && [ "${code:0:1}" = "4" ]; then + ok "$src -> not redirected (${code})" + else + bad "$src — expected a non-redirect 4xx, got $code -> ${loc:-}" + fi +} +never_redirects "/api/does-not-exist" +never_redirects "/artwork/does-not-exist" + +# ---- 2. Machine-key data-plane contracts (scan lifecycle + If-Match/412) ------------------------ +# These use the machine key (X-Api-Key), which is independent of the session and does NOT claim the +# local admin — so the fresh-config setup-claim flow in section 3 still sees setupRequired:true. +section "Read gate (Api:RequireKeyForReads = true)" +expect_status "GET /api/v1/channels without a key -> 401" 401 GET "$BASE_URL/api/v1/channels" +expect_status "GET /api/v1/channels with the machine key -> 200" 200 GET "$BASE_URL/api/v1/channels" \ + -H "X-Api-Key: $API_KEY" + +section "Library scan lifecycle contract" +# Create an empty local library (no LibraryPath) — enough to exercise the enqueue/status contract +# without any media on disk (QueueLibraryScanByLibraryIdHandler never inspects paths). +LIB_JSON="$(mktemp)" +api -X POST "$BASE_URL/api/v1/libraries/local" -d '{"name":"E2E Functional Library"}' -o "$LIB_JSON" -w '' +LIB_ID="$(json_field "$LIB_JSON" id)" +if [ -n "$LIB_ID" ]; then ok "created local library id=$LIB_ID"; else bad "could not create local library (body: $(cat "$LIB_JSON"))"; fi +expect_status "POST /api/v1/libraries/$LIB_ID/scan -> 202 (queued)" 202 POST "$BASE_URL/api/v1/libraries/$LIB_ID/scan" \ + -H "X-Api-Key: $API_KEY" -H 'Content-Type: application/json' -d '{}' +expect_status "POST /api/v1/libraries/99999/scan -> 404 (unknown library)" 404 POST "$BASE_URL/api/v1/libraries/99999/scan" \ + -H "X-Api-Key: $API_KEY" -H 'Content-Type: application/json' -d '{}' +expect_status "GET /api/v1/libraries/scan-status -> 200" 200 GET "$BASE_URL/api/v1/libraries/scan-status" \ + -H "X-Api-Key: $API_KEY" + +section "Optimistic concurrency (If-Match / 412)" +# rerun-collections is the reference concurrency-aware editor: GET emits an ETag, PUT enforces +# If-Match. It needs an existing collection to target, so create one first. +COLL_JSON="$(mktemp)" +api -X POST "$BASE_URL/api/v1/collections" -d '{"name":"E2E Functional Collection"}' -o "$COLL_JSON" -w '' +COLL_ID="$(json_field "$COLL_JSON" id)" +RC_JSON="$(mktemp)" +api -X POST "$BASE_URL/api/v1/rerun-collections" -o "$RC_JSON" -w '' -d "{ + \"name\":\"E2E Functional Rerun\",\"collectionType\":\"Collection\",\"selectedId\":$COLL_ID, + \"firstRunPlaybackOrder\":\"Chronological\",\"rerunPlaybackOrder\":\"Chronological\"}" +RC_ID="$(json_field "$RC_JSON" id)" +if [ -n "$RC_ID" ]; then ok "created rerun-collection id=$RC_ID (targets collection $COLL_ID)"; else bad "could not create rerun-collection (body: $(cat "$RC_JSON"))"; fi + +# GET must surface the current version as a quoted-integer ETag. +ETAG="$(api -D - -o /dev/null "$BASE_URL/api/v1/rerun-collections/$RC_ID" | awk 'tolower($1)=="etag:"{print $2}' | tr -d '\r')" +if [ -n "$ETAG" ]; then ok "GET rerun-collection $RC_ID surfaces ETag $ETAG"; else bad "GET rerun-collection $RC_ID has no ETag header"; fi + +RC_BODY="{\"name\":\"E2E Functional Rerun v2\",\"selectedId\":$COLL_ID,\"firstRunPlaybackOrder\":\"Chronological\",\"rerunPlaybackOrder\":\"Chronological\"}" +# A well-formed but non-current version must be rejected 412 (999999 can never be the live version here). +expect_status "PUT with a stale If-Match -> 412" 412 PUT "$BASE_URL/api/v1/rerun-collections/$RC_ID" \ + -H "X-Api-Key: $API_KEY" -H 'Content-Type: application/json' -H 'If-Match: "999999"' -d "$RC_BODY" +# The current ETag must be accepted. +expect_status "PUT with the current If-Match -> 200" 200 PUT "$BASE_URL/api/v1/rerun-collections/$RC_ID" \ + -H "X-Api-Key: $API_KEY" -H 'Content-Type: application/json' -H "If-Match: $ETAG" -d "$RC_BODY" +# A malformed (non-integer) If-Match is a client error. +expect_status "PUT with a malformed If-Match -> 400" 400 PUT "$BASE_URL/api/v1/rerun-collections/$RC_ID" \ + -H "X-Api-Key: $API_KEY" -H 'Content-Type: application/json' -H 'If-Match: not-a-version' -d "$RC_BODY" + +# ---- 3. Auth / CSRF / security-stamp flow ------------------------------------------------------- +# Runs last: setup-claim is a one-shot on a fresh config and logout revokes the session. All prior +# sections used the machine key, which does not claim the admin, so setupRequired is still true here. +section "Auth: setup-claim, CSRF gate, login, logout + stamp revocation" + +CFG_JSON="$(mktemp)" +curl -s "$BASE_URL/api/v1/auth/config" -o "$CFG_JSON" +if [ "$(json_field "$CFG_JSON" setupRequired)" = "True" ]; then ok "fresh config: setupRequired=true"; else bad "expected setupRequired=true on a fresh config (body: $(cat "$CFG_JSON"))"; fi + +JAR="$(mktemp)" +# Claim the local admin (setup is CSRF-gated like every mutation). +expect_status "POST /api/v1/auth/setup -> 200 (claims admin, issues session)" 200 POST "$BASE_URL/api/v1/auth/setup" \ + -c "$JAR" -H 'Content-Type: application/json' -H 'X-CSRF: 1' -d '{"username":"admin","password":"e2e-pass-1234"}' +curl -s "$BASE_URL/api/v1/auth/config" -o "$CFG_JSON" +if [ "$(json_field "$CFG_JSON" setupRequired)" = "False" ]; then ok "after claim: setupRequired=false"; else bad "expected setupRequired=false after claim (body: $(cat "$CFG_JSON"))"; fi +expect_status "POST /api/v1/auth/setup again -> 409 (already configured)" 409 POST "$BASE_URL/api/v1/auth/setup" \ + -H 'Content-Type: application/json' -H 'X-CSRF: 1' -d '{"username":"admin2","password":"e2e-pass-1234"}' + +# CSRF gate on a general /api session mutation: a POST with the session cookie but no X-CSRF is 403. +expect_status "session mutation without X-CSRF -> 403" 403 POST "$BASE_URL/api/v1/collections" \ + -b "$JAR" -H 'Content-Type: application/json' -d '{"name":"should-be-csrf-blocked"}' + +# Login: wrong password 401, correct 200 (fresh cookie jar). +expect_status "login with wrong password -> 401" 401 POST "$BASE_URL/api/v1/auth/login" \ + -H 'Content-Type: application/json' -H 'X-CSRF: 1' -d '{"username":"admin","password":"nope"}' +JAR2="$(mktemp)" +expect_status "login with correct password -> 200" 200 POST "$BASE_URL/api/v1/auth/login" \ + -c "$JAR2" -H 'Content-Type: application/json' -H 'X-CSRF: 1' -d '{"username":"admin","password":"e2e-pass-1234"}' + +# Logout is CSRF-gated; and after it, the same cookie is invalidated by the rotated security stamp. +expect_status "authed GET before logout -> 200" 200 GET "$BASE_URL/api/v1/auth/machine-key" -b "$JAR2" +expect_status "logout without X-CSRF -> 403" 403 POST "$BASE_URL/api/v1/auth/logout" -b "$JAR2" +expect_status "logout with X-CSRF -> 204" 204 POST "$BASE_URL/api/v1/auth/logout" -b "$JAR2" -H 'X-CSRF: 1' +expect_status "authed GET after logout (stamp revoked) -> 401" 401 GET "$BASE_URL/api/v1/auth/machine-key" -b "$JAR2" + +# ---- summary ------------------------------------------------------------------------------------ +printf '\n\033[1m== Summary ==\033[0m\n' +printf 'passed: %d failed: %d\n' "$PASS" "$FAIL" +if [ "$FAIL" -ne 0 ]; then + printf '\033[31mfailed assertions:\033[0m\n' + for f in "${FAILURES[@]}"; do printf ' - %s\n' "$f"; done + exit 1 +fi +echo "all functional-E2E assertions passed." diff --git a/scripts/e2e-local.sh b/scripts/e2e-local.sh index f2877bc82..2afbb7215 100755 --- a/scripts/e2e-local.sh +++ b/scripts/e2e-local.sh @@ -22,7 +22,10 @@ set -euo pipefail REPO_ROOT="$(git rev-parse --show-toplevel)" -BUILD_DIR="$REPO_ROOT/ErsatzTV/bin/Debug/net10.0" +# ETV_BUILD_CONFIG selects which build output to launch (Debug for local dev; the CI functional-E2E +# job builds Release). Must match the `dotnet build --configuration` you ran beforehand. +BUILD_CONFIG="${ETV_BUILD_CONFIG:-Debug}" +BUILD_DIR="$REPO_ROOT/ErsatzTV/bin/$BUILD_CONFIG/net10.0" PORT="${ETV_UI_PORT:-8409}" READY_LINE="Done migrating search index" TIMEOUT_SECS=120 @@ -31,7 +34,7 @@ CONFIG_DIR="${1:-$(mktemp -d)}" mkdir -p "$CONFIG_DIR" if [ ! -d "$BUILD_DIR" ]; then - echo "error: $BUILD_DIR does not exist — run 'dotnet build ErsatzTV.sln' first" >&2 + echo "error: $BUILD_DIR does not exist — run 'dotnet build ErsatzTV.sln${BUILD_CONFIG:+ --configuration $BUILD_CONFIG}' first" >&2 exit 1 fi -- 2.47.3