From d322e88f23e7400245b5f769d324228b771375c4 Mon Sep 17 00:00:00 2001 From: Willem Jiang Date: Sun, 22 Nov 2026 15:52:23 +0800 Subject: [PATCH] fix(smoke-test): sign in with an existing test account (#8481) * fix(smoke-test): sign in with an existing test account * fix(smoke-test): read session cookies from stdin --- .agent/skills/smoke-test/SKILL.md | 6 +- .agent/skills/smoke-test/references/SOP.md | 16 +- .../smoke-test/scripts/frontend_check.sh | 137 +++++++----------- backend/tests/test_frontend_smoke_check.py | 114 +++++++++++++++ 4 files changed, 175 insertions(+), 98 deletions(-) create mode 100644 backend/tests/test_frontend_smoke_check.py diff --git a/.agent/skills/smoke-test/SKILL.md b/.agent/skills/smoke-test/SKILL.md index 2796d870bc..a58575bf7a 100644 --- a/.agent/skills/smoke-test/SKILL.md +++ b/.agent/skills/smoke-test/SKILL.md @@ -97,14 +97,14 @@ smoke-test/ 2. **Check frontend service** - Visit `http://localhost:2026` and verify that the page loads 3. **Check API Gateway** - Verify the `http://localhost:2026/health` endpoint 4. **Check LangGraph-compatible API** - Verify the `/api/langgraph/*` route exposed by Gateway -5. **Frontend route smoke check** - Run `bash .agent/skills/smoke-test/scripts/frontend_check.sh` to verify key routes under `/workspace`. The script auto-detects whether authentication is enabled and, if so, registers / logs in a smoke-test user so the real pages are verified rather than the login redirect. +5. **Frontend route smoke check** - Run `bash .agent/skills/smoke-test/scripts/frontend_check.sh` to verify key routes under `/workspace`. When authentication is enabled, first finish setup and supply credentials for an existing test account as described below. **Docker mode health check** (when using Docker): 1. **Check container status** - Run `docker ps` and confirm that all containers are running 2. **Check frontend service** - Visit `http://localhost:2026` and verify that the page loads 3. **Check API Gateway** - Verify the `http://localhost:2026/health` endpoint 4. **Check LangGraph-compatible API** - Verify the `/api/langgraph/*` route exposed by Gateway -5. **Frontend route smoke check** - Run `bash .agent/skills/smoke-test/scripts/frontend_check.sh` to verify key routes under `/workspace`. The script auto-detects whether authentication is enabled and, if so, registers / logs in a smoke-test user so the real pages are verified rather than the login redirect. +5. **Frontend route smoke check** - Run `bash .agent/skills/smoke-test/scripts/frontend_check.sh` to verify key routes under `/workspace`. When authentication is enabled, first finish setup and supply credentials for an existing test account as described below. ### Optional Functional Verification @@ -136,7 +136,7 @@ smoke-test/ The following warnings can appear during smoke testing and do not block a successful result: - Feishu/Lark SSL errors in Gateway logs (certificate verification failure) can be ignored if that channel is not enabled - Warnings in Gateway logs about missing methods in the custom checkpointer, such as `adelete_for_runs` or `aprune`, do not affect the core functionality -- The `frontend_check.sh` script automatically handles authentication. When auth is enabled it registers / logs in a smoke-test user (`smoke-test@deerflow.dev` by default) to verify the real `/workspace/*` pages. The registration may produce a log entry from the auth provider, which is expected and harmless. +- With authentication enabled, set `SMOKE_TEST_EMAIL` and `SMOKE_TEST_PASSWORD` for an existing test account with a local DeerFlow password and workspace access. The script uses local sign-in and removes its temporary cookie jar on exit. With authentication disabled, it checks the routes anonymously. ## Key Tools diff --git a/.agent/skills/smoke-test/references/SOP.md b/.agent/skills/smoke-test/references/SOP.md index e55b0ecfcb..bfe58fc31d 100644 --- a/.agent/skills/smoke-test/references/SOP.md +++ b/.agent/skills/smoke-test/references/SOP.md @@ -368,12 +368,8 @@ curl http://localhost:2026/health **Steps**: 1. Run `bash .agent/skills/smoke-test/scripts/frontend_check.sh`. 2. The script auto-detects whether authentication (`DEER_FLOW_AUTH_DISABLED`) is enabled. -3. When auth is on, the script registers / logs in a smoke-test user (`smoke-test@deerflow.dev` by default) and passes the session cookie so the real `/workspace/*` pages are verified — not the login redirect. -4. When auth is off, the routes are checked anonymously as before. - -**Customisation**: -- `SMOKE_TEST_EMAIL` — email for the test account (default: `smoke-test@deerflow.dev`). -- `SMOKE_TEST_PASSWORD` — password for the test account (default: `SmokeTest123!`). +3. When auth is on, finish setup before running the check. Set `SMOKE_TEST_EMAIL` and `SMOKE_TEST_PASSWORD` for an existing test account with a local DeerFlow password and workspace access. The script signs in and uses a temporary cookie jar that it removes on exit. +4. When auth is off, the script checks the routes anonymously. --- @@ -434,12 +430,8 @@ curl http://localhost:2026/health **Steps**: 1. Run `bash .agent/skills/smoke-test/scripts/frontend_check.sh`. 2. The script auto-detects whether authentication (`DEER_FLOW_AUTH_DISABLED`) is enabled. -3. When auth is on, the script registers / logs in a smoke-test user (`smoke-test@deerflow.dev` by default) and passes the session cookie so the real `/workspace/*` pages are verified — not the login redirect. -4. When auth is off, the routes are checked anonymously as before. - -**Customisation**: -- `SMOKE_TEST_EMAIL` — email for the test account (default: `smoke-test@deerflow.dev`). -- `SMOKE_TEST_PASSWORD` — password for the test account (default: `SmokeTest123!`). +3. When auth is on, finish setup before running the check. Set `SMOKE_TEST_EMAIL` and `SMOKE_TEST_PASSWORD` for an existing test account with a local DeerFlow password and workspace access. The script signs in and uses a temporary cookie jar that it removes on exit. +4. When auth is off, the script checks the routes anonymously. --- diff --git a/.agent/skills/smoke-test/scripts/frontend_check.sh b/.agent/skills/smoke-test/scripts/frontend_check.sh index 024ce32f38..cf27fd35e8 100644 --- a/.agent/skills/smoke-test/scripts/frontend_check.sh +++ b/.agent/skills/smoke-test/scripts/frontend_check.sh @@ -9,97 +9,68 @@ echo "" BASE_URL="${BASE_URL:-http://localhost:2026}" DOC_PATH="${DOC_PATH:-/en/docs}" -# When the gateway has authentication enabled (DEER_FLOW_AUTH_DISABLED != 1), -# protected /workspace/* routes redirect anonymous requests to /login. -# We detect auth, register / log in a smoke-test user, and pass the session -# cookie to all curl calls so the real pages are verified, not the login form. -SMOKE_TEST_EMAIL="${SMOKE_TEST_EMAIL:-smoke-test@deerflow.dev}" -SMOKE_TEST_PASSWORD="${SMOKE_TEST_PASSWORD:-SmokeTest123!}" -COOKIE_JAR=$(mktemp /tmp/deerflow-smoke-cookies.XXXXXX) -trap 'rm -f "$COOKIE_JAR"' EXIT -CURL_AUTH_OPTS="" +# On auth-enabled deployments, verify protected routes with a session for an +# existing account. Account provisioning belongs to the operator. +COOKIE_JAR="" +trap 'if [ -n "$COOKIE_JAR" ]; then rm -f -- "$COOKIE_JAR"; fi' EXIT +trap 'exit 130' INT +trap 'exit 143' TERM + +curl_get() { + if [ -n "$COOKIE_JAR" ]; then + # curl treats a cookie argument containing '=' as literal cookie data. + curl -q --cookie - "$@" < "$COOKIE_JAR" + else + curl -q "$@" + fi +} authenticate() { - # Make sure the service is reachable before detecting auth - local health - health=$(curl -s -o /dev/null -w "%{http_code}" "${BASE_URL}/" 2>/dev/null) - if [ "$health" = "000" ]; then - echo "✗ Cannot reach ${BASE_URL} — is the service running?" + local auth_check + if ! auth_check=$(curl -q -s -o /dev/null -w "%{http_code}" "${BASE_URL}/api/models"); then + echo "✗ Cannot reach ${BASE_URL} — is the service running?" >&2 + return 1 + fi + case "$auth_check" in + 200) + echo "ℹ Auth is disabled — no login needed" + return 0 + ;; + 401) ;; + *) + echo "✗ Could not check authentication (HTTP $auth_check)" >&2 + return 1 + ;; + esac + + if [ -z "${SMOKE_TEST_EMAIL:-}" ] || [ -z "${SMOKE_TEST_PASSWORD:-}" ]; then + echo "✗ Set SMOKE_TEST_EMAIL and SMOKE_TEST_PASSWORD for an existing test account." >&2 + return 1 + fi + if ! COOKIE_JAR=$(mktemp "${TMPDIR:-/tmp}/deerflow-smoke-cookies.XXXXXX"); then + echo "✗ Could not create a temporary cookie jar" >&2 return 1 fi - # A protected endpoint returns 401 when auth is on - local auth_check - auth_check=$(curl -s -o /dev/null -w "%{http_code}" "${BASE_URL}/api/models" 2>/dev/null) - if [ "$auth_check" != "401" ]; then - echo "ℹ Auth is disabled — no login needed" - return 0 + echo "🔐 Auth is enabled — signing in to the existing test account..." + local login_code + if ! login_code=$(printf '%s' "$SMOKE_TEST_PASSWORD" | curl -q -s -o /dev/null -w "%{http_code}" \ + "${BASE_URL}/api/v1/auth/login/local" \ + --data-urlencode "username=${SMOKE_TEST_EMAIL}" \ + --data-urlencode 'password@-' \ + --data-urlencode 'remember_me=false' \ + -c "$COOKIE_JAR") || [ "$login_code" != "200" ]; then + echo "✗ Login failed (HTTP ${login_code:-unavailable}). Check the existing test account and finish setup first." >&2 + return 1 fi - echo "🔐 Auth is enabled — setting up smoke test session..." - - # Check whether the system needs first-boot initialization - local needs_setup - needs_setup=$(curl -s "${BASE_URL}/api/v1/auth/setup-status" \ - | grep -o '"needs_setup":[^,}]*' | grep -o 'true\|false') - if [ "$needs_setup" = "true" ]; then - echo " Initializing system with smoke test admin..." - local init_code - init_code=$(curl -s -o /dev/null -w "%{http_code}" \ - -X POST "${BASE_URL}/api/v1/auth/initialize" \ - -H "Content-Type: application/json" \ - -d "{\"email\":\"${SMOKE_TEST_EMAIL}\",\"password\":\"${SMOKE_TEST_PASSWORD}\"}" \ - -c "$COOKIE_JAR" 2>/dev/null) - if [ "$init_code" != "201" ]; then - echo "✗ Initialize failed (HTTP $init_code)" - return 1 - fi - echo "✓ Admin initialized & logged in" - elif [ -z "$needs_setup" ]; then - echo "⚠ Could not determine setup status — skipping initialize, trying register/login" - else - # Register first — on success (201) it also auto-logs-in via the cookie. - # This avoids a wasted login attempt that counts toward rate-limiting. - local auth_code - auth_code=$(curl -s -o /dev/null -w "%{http_code}" \ - -X POST "${BASE_URL}/api/v1/auth/register" \ - -H "Content-Type: application/json" \ - -d "{\"email\":\"${SMOKE_TEST_EMAIL}\",\"password\":\"${SMOKE_TEST_PASSWORD}\"}" \ - -c "$COOKIE_JAR" 2>/dev/null) - - if [ "$auth_code" = "201" ]; then - echo "✓ Registered as ${SMOKE_TEST_EMAIL}" - else - # User already exists — clear stale cookies from register attempt, then log in - : > "$COOKIE_JAR" - local login_code - login_code=$(curl -s -o /dev/null -w "%{http_code}" \ - -X POST "${BASE_URL}/api/v1/auth/login/local" \ - --data-urlencode "username=${SMOKE_TEST_EMAIL}" \ - --data-urlencode "password=${SMOKE_TEST_PASSWORD}" \ - -c "$COOKIE_JAR" 2>/dev/null) - if [ "$login_code" != "200" ]; then - echo "✗ Login failed (HTTP $login_code)" - return 1 - fi - echo "✓ Logged in as ${SMOKE_TEST_EMAIL}" - fi - fi - - # Verify the session cookie works across all branches local me_code - me_code=$(curl -s -o /dev/null -w "%{http_code}" \ - -b "$COOKIE_JAR" "${BASE_URL}/api/v1/auth/me" 2>/dev/null) - - if [ "$me_code" = "200" ]; then - echo "✓ Session verified for ${SMOKE_TEST_EMAIL}" - CURL_AUTH_OPTS="-b $COOKIE_JAR" - return 0 + if ! me_code=$(curl_get -s -o /dev/null -w "%{http_code}" "${BASE_URL}/api/v1/auth/me") || [ "$me_code" != "200" ]; then + echo "✗ Auth failed — session cookie not accepted (HTTP ${me_code:-unavailable})" >&2 + return 1 fi - echo "✗ Auth failed — session cookie not accepted (HTTP $me_code)" - echo " Set SMOKE_TEST_EMAIL / SMOKE_TEST_PASSWORD or check the existing account" - return 1 + echo "✓ Session verified" } all_passed=true @@ -110,7 +81,7 @@ check_status() { local expected_re="$3" local status - status="$(curl -s -o /dev/null -w "%{http_code}" -L ${CURL_AUTH_OPTS} "$url")" + status="$(curl_get -s -o /dev/null -w "%{http_code}" -L "$url")" if echo "$status" | grep -Eq "$expected_re"; then echo "✓ $name ($url) -> $status" else @@ -125,7 +96,7 @@ check_final_url() { local expected_path_re="$3" local effective - effective="$(curl -s -o /dev/null -w "%{url_effective}" -L ${CURL_AUTH_OPTS} "$url")" + effective="$(curl_get -s -o /dev/null -w "%{url_effective}" -L "$url")" if echo "$effective" | grep -Eq "$expected_path_re"; then echo "✓ $name redirect target -> $effective" else diff --git a/backend/tests/test_frontend_smoke_check.py b/backend/tests/test_frontend_smoke_check.py new file mode 100644 index 0000000000..77f77f0fc0 --- /dev/null +++ b/backend/tests/test_frontend_smoke_check.py @@ -0,0 +1,114 @@ +"""Exercise the frontend smoke check without contacting a deployment.""" + +import os +import shutil +import subprocess +from pathlib import Path + +import pytest +from support.shell import require_script_bash + +_SCRIPT = Path(__file__).resolve().parents[2] / ".agent/skills/smoke-test/scripts/frontend_check.sh" +_PRELUDE = r""" +has_session_cookie() { + local cookie_source="$1" + [ -f logged-in ] || return 1 + # curl treats a cookie argument containing '=' as literal cookie data. + case "$cookie_source" in + ""|*=*) return 1 ;; + -) [ "$(cat)" = 'temporary session' ] ;; + *) [ -f "$cookie_source" ] && [ "$(cat < "$cookie_source")" = 'temporary session' ] ;; + esac +} +curl() { + local arg endpoint write_format body cookie_output cookie_input i + local sent_username=false + local -a args=("$@") + for (( i=0; i < ${#args[@]}; i++ )); do + arg=${args[$i]} + case "$arg" in + http://fixture.invalid/*) endpoint=${arg#http://fixture.invalid} ;; + -w) write_format=${args[$((i+1))]} ;; + -c) cookie_output=${args[$((i+1))]} ;; + -b|--cookie) cookie_input=${args[$((i+1))]} ;; + --data-urlencode) + if [ "${args[$((i+1))]}" = "username=$SMOKE_TEST_EMAIL" ]; then + sent_username=true + fi + ;; + esac + done + printf '%s\n' "$endpoint" >> requests.log + case "$endpoint" in + /api/models) printf '%s' "$FAKE_AUTH_STATUS" ;; + /api/v1/auth/login/local) + body=$(cat) + case " $* " in + *" --data-urlencode password@- --data-urlencode remember_me=false "*) ;; + *) return 1 ;; + esac + [ "$sent_username" = true ] && [ "$body" = "$SMOKE_TEST_PASSWORD" ] && [ -n "$cookie_output" ] || return 1 + printf 'temporary session' > "$cookie_output" + if [ "$FAKE_LOGIN_STATUS" = 200 ]; then + touch logged-in + fi + printf '%s' "$FAKE_LOGIN_STATUS" + ;; + /api/v1/auth/me) + if has_session_cookie "$cookie_input"; then + printf '200' + else + printf '401' + fi + ;; + /workspace*) + if [ "$FAKE_AUTH_STATUS" = 401 ]; then + has_session_cookie "$cookie_input" || return 1 + fi + if [ "$write_format" = '%{url_effective}' ]; then + if [ "$endpoint" = /workspace ]; then + printf 'http://fixture.invalid/workspace/chats/new' + else + printf 'http://fixture.invalid%s' "$endpoint" + fi + else + printf '200' + fi + ;; + /|/en/docs) printf '200' ;; + *) return 1 ;; + esac +} +export -f curl has_session_cookie +"$1" frontend_check.sh +""" + + +@pytest.mark.parametrize( + ("auth_status", "login_status", "credentials", "exit_code", "attempts"), + [ + ("200", "200", False, 0, []), + ("401", "200", False, 1, []), + ("401", "200", True, 0, ["/api/v1/auth/login/local", "/api/v1/auth/me"]), + ("401", "401", True, 1, ["/api/v1/auth/login/local"]), + ("503", "200", True, 1, []), + ], + ids=["anonymous", "missing-credentials", "existing-account", "rejected-login", "auth-probe-unavailable"], +) +def test_smoke_check_only_signs_in_to_existing_accounts(tmp_path, auth_status, login_status, credentials, exit_code, attempts): + bash = require_script_bash() + shutil.copy2(_SCRIPT, tmp_path / "frontend_check.sh") + scratch = tmp_path / "cookie scratch=local" + scratch.mkdir() + env = {key: value for key, value in os.environ.items() if key not in {"BASH_ENV", "ENV", "SMOKE_TEST_EMAIL", "SMOKE_TEST_PASSWORD"}} + env.update(BASE_URL="http://fixture.invalid", DOC_PATH="/en/docs", TMPDIR=f"./{scratch.name}", FAKE_AUTH_STATUS=auth_status, FAKE_LOGIN_STATUS=login_status) + if credentials: + env.update(SMOKE_TEST_EMAIL="tester@example.invalid", SMOKE_TEST_PASSWORD="fixture only: q+%") + result = subprocess.run([bash, "-c", _PRELUDE, "smoke-check-test", bash], cwd=tmp_path, env=env, capture_output=True, encoding="utf-8", check=False, timeout=20) + calls = (tmp_path / "requests.log").read_text(encoding="utf-8").splitlines() + assert [path for path in calls if path.startswith("/api/v1/auth/")] == attempts + assert result.returncode == exit_code, result.stdout + result.stderr + assert ("/workspace/chats/new" in calls) == (exit_code == 0) + assert not list(scratch.iterdir()) + if credentials: + assert env["SMOKE_TEST_PASSWORD"] not in result.stdout + result.stderr