Files
SluijsensandClaude Sonnet 5.5 9f07934f9e Smoke: controle op inline scripts zonder grep -P, JSON-blokken en importmaps tellen niet; test erbij
De controle staat in scripts/lib-inline-script.sh (alleen POSIX-tools), met een test (scripts/test-inline-script-check.sh) en een lokale proef van smoke-site.sh tegen een admin-pagina met en zonder inline script. De melding noemt nu wat er gevonden is.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 22:41:57 +01:00

117 lines
5.8 KiB
Bash

#!/usr/bin/env bash
# Smoke test of a deployed site: anonymous GET requests only, no secrets.
#
# Usage: scripts/smoke-site.sh <url-on-the-host> <profile>
# <url-on-the-host> any URL of the host, for example its /health URL; only scheme and host are used
# <profile> sls-test | asf-test
#
# A site project is a thin composition (modules + appsettings.json), so what can still go wrong at deploy time is
# the composition itself: a module missing, the wrong website mode, a missing setting. The CI builds and tests the
# master CMS only; this script is the check that the deployed composition of a site is the one that was intended.
# A failing check makes the CI run fail.
set -u
# shellcheck source=lib-inline-script.sh
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib-inline-script.sh"
base="$(printf '%s' "${1:?usage: smoke-site.sh <url-on-the-host> <profile>}" | sed -E 's#^(https?://[^/]+).*#\1#')"
profile="${2:?profile (sls-test | asf-test) is required}"
failures=0
curl_get() { curl -sS --max-time 20 --retry 4 --retry-delay 3 --retry-all-errors "$@"; }
status_of() { curl_get -o /dev/null -w '%{http_code}' "$base$1"; }
pass() { echo "ok $1"; }
fail() { echo "FAIL $1"; failures=$((failures + 1)); }
expect_status() { # path expected description
local got; got="$(status_of "$1")"
if [ "$got" = "$2" ]; then pass "$3 ($1 -> $got)"; else fail "$3 ($1 -> $got, expected $2)"; fi
}
expect_header() { # path header-name expected-substring description
local got; got="$(curl_get -D - -o /dev/null "$base$1" | tr -d '\r' | grep -i "^$2:" | head -1)"
if printf '%s' "$got" | grep -qi "$3"; then pass "$4 ($1 $2)"; else fail "$4 ($1 $2: '${got:-<missing>}')"; fi
}
expect_body() { # path expected-substring description
if curl_get "$base$1" | grep -qF "$2"; then pass "$3 ($1)"; else fail "$3 ($1 does not contain '$2')"; fi
}
echo "Smoke test '$profile' on $base"
expect_status /health 200 "health"
caps="$(curl_get "$base/api/v1/System/capabilities" || true)"
module_listed() { printf '%s' "$caps" | grep -q "\"$1\""; }
expect_module() { if module_listed "$1"; then pass "module $1 is loaded"; else fail "module $1 is NOT loaded (capabilities: ${caps:-<empty>})"; fi; }
expect_no_module() { if module_listed "$1"; then fail "module $1 is loaded but must not be"; else pass "module $1 is not loaded"; fi; }
expect_status /api/v1/offerings 200 "public offerings"
# 204 = no active banner, 200 = there is one; both are a healthy answer.
banner="$(status_of '/api/v1/offerings/banner')"
if [ "$banner" = "200" ] || [ "$banner" = "204" ]; then pass "public banner (/api/v1/offerings/banner -> $banner)"; else fail "public banner (-> $banner, expected 200 or 204)"; fi
expect_status /api/v1/offerings/all 401 "the administrator list is protected"
# The hero: 204 = no hero to show (the website keeps its own), 200 = there is one; the management routes are protected and
# the media endpoint answers a plain 404 for something that is not stored.
hero="$(status_of '/api/v1/hero')"
if [ "$hero" = "200" ] || [ "$hero" = "204" ]; then pass "public hero (/api/v1/hero -> $hero)"; else fail "public hero (-> $hero, expected 200 or 204)"; fi
expect_status /api/v1/hero/all 401 "the hero management read is protected"
expect_status /media/hero/0123456789abcdef/wide-960.webp 404 "media that is not stored is a plain 404"
# The admin page must have no inline script: the host only allows scripts from its own origin (script-src 'self').
# The check lives in lib-inline-script.sh (and is tested by test-inline-script-check.sh); it prints what it found.
admin_html="$(curl_get "$base/admin/" || true)"
if printf '%s' "$admin_html" | grep -q 'id="root"'; then
inline_found="$(printf '%s' "$admin_html" | inline_scripts)"
if [ -n "$inline_found" ]; then fail "the admin page has an inline script (blocked by the Content-Security-Policy): $(printf '%s' "$inline_found" | head -3 | tr '
' ' ')"; else pass "the admin page has no inline script"; fi
expect_status /admin/theme-init.js 200 "the theme script is a file of its own"
fi
# A test host is never indexed.
expect_header / X-Robots-Tag noindex "noindex on the website"
expect_body /robots.txt "Disallow: /" "robots.txt disallows everything"
# The home page is served by the host. When the website build left CMS markers in it, the host must have processed
# them: an unprocessed <!--CMS:DATA--> or JSON-LD marker means the page does not go through the injector (a proxy or
# IIS serving files itself, or a host without the injection), and a deployed data block must carry this host's address.
home="$(curl_get "$base/" || true)"
if printf '%s' "$home" | grep -qE '<!--CMS:(DATA|OFFER-JSON-LD)'; then
fail "CMS markers on the home page were NOT processed by the host"
else
pass "no unprocessed CMS markers on the home page"
fi
if printf '%s' "$home" | grep -q 'id="cms-data"'; then
if printf '%s' "$home" | grep -q "\"siteUrl\":\"$base\""; then
pass "the data block carries this host's address (siteUrl $base)"
else
fail "the data block's siteUrl is not $base (is Website__PublicBaseUrl set on this host?)"
fi
expect_header / ETag '"' "the injected home page has an ETag"
expect_header / Cache-Control no-cache "the injected home page is no-cache"
fi
case "$profile" in
sls-test)
for m in Identity Availability Offerings Contact Hero Master; do expect_module "$m"; done
expect_status /sitemap.xml 404 "no sitemap on a test host"
;;
asf-test)
for m in Identity Availability Offerings Contact Hero; do expect_module "$m"; done
expect_no_module Master
# Prerendered website: an unknown path is a real 404 (an SPA would answer 200).
expect_status /smoke-test-path-that-does-not-exist 404 "unknown website path is a 404"
;;
*)
echo "Unknown profile '$profile' (use sls-test or asf-test)"
exit 2
;;
esac
if [ "$failures" -gt 0 ]; then
echo "Smoke test FAILED: $failures check(s)."
exit 1
fi
echo "Smoke test passed."