Files
wp-recache/tests/docker-test.sh
T

167 lines
5.3 KiB
Bash
Executable File

#!/bin/sh
# Integration test for rejected-cookie cache exclusions.
#
# Starts WordPress in Docker, installs the plugin, and verifies that
# requests with rejected cookies bypass the page cache.
#
# Usage:
# ./tests/docker-test.sh # run tests then tear down
# ./tests/docker-test.sh --keep # leave containers running for inspection
set -e
KEEP=0
[ "$1" = "--keep" ] && KEEP=1
COMPOSE="docker compose -f tests/docker-compose.test.yml"
PASS=0
FAIL=0
WP_URL="http://localhost:8080"
DB_READY=0
cleanup() {
if [ "$KEEP" -eq 0 ]; then
echo "Cleaning up..."
$COMPOSE down -v 2>/dev/null || true
fi
}
trap cleanup EXIT
banner() {
echo ""
echo "========================================"
echo " $1"
echo "========================================"
}
assert_header() {
local label="$1"
local expected="$2"
local actual="$3"
if [ "$actual" = "$expected" ]; then
echo " PASS: $label"
PASS=$((PASS + 1))
else
echo " FAIL: $label"
echo " expected header '$expected', got '$actual'"
FAIL=$((FAIL + 1))
fi
}
# --- Start services ---
banner "Starting Docker services"
$COMPOSE up -d --build
echo "Waiting for database..."
for i in $(seq 1 60); do
if $COMPOSE exec -T db mysqladmin ping -h localhost -uroot -proot 2>/dev/null | grep -q alive; then
DB_READY=1
echo "Database ready."
break
fi
sleep 2
done
if [ "$DB_READY" -eq 0 ]; then
echo "FATAL: Database did not become ready."
exit 1
fi
echo "Waiting for WordPress..."
for i in $(seq 1 60); do
if curl -sf "$WP_URL/wp-login.php" >/dev/null 2>&1; then
echo "WordPress ready."
break
fi
sleep 2
done
# --- Install WordPress ---
banner "Installing WordPress"
$COMPOSE exec -T cli bash -c '
wp core install \
--url="http://localhost:8080" \
--title="WP Recache Test" \
--admin_user=admin \
--admin_password=admin \
--admin_email=admin@example.com \
--skip-email 2>/dev/null
'
# --- Install and activate plugin ---
banner "Installing WP Recache plugin"
$COMPOSE exec -T cli bash -c '
wp plugin activate wp-recache 2>/dev/null || \
wp plugin install /app/wp-recache.zip --activate 2>/dev/null || \
wp plugin activate $(wp plugin list --field=path --status=inactive | grep wp-recache | head -1) 2>/dev/null
'
# The plugin is volume-mounted at /app, so WP should see it in wp-content/plugins
# If not, we need to symlink it
$COMPOSE exec -T cli bash -c '
if [ ! -d /var/www/html/wp-content/plugins/wp-recache ]; then
ln -sf /app /var/www/html/wp-content/plugins/wp-recache
fi
wp plugin activate wp-recache 2>/dev/null || true
wp option get wp_recache_rejected_cookies 2>/dev/null || echo "(option not set yet)"
'
# --- Verify plugin is active ---
banner "Checking plugin status"
PLUGIN_STATUS=$($COMPOSE exec -T cli bash -c 'wp plugin status wp-recache 2>/dev/null | head -5')
echo "$PLUGIN_STATUS"
# --- Warm the cache by creating a test page ---
banner "Creating test page and warming cache"
$COMPOSE exec -T cli bash -c '
wp post create --post_type=page --post_title="Test Page" --post_status=publish --post_content="Hello World" 2>/dev/null
wp rewrite flush 2>/dev/null
'
sleep 2
# Hit the page once to warm cache (anonymous, no cookies)
curl -sf "$WP_URL/?p=1" >/dev/null 2>&1 || true
sleep 1
# --- Test: no cookies → cache HIT ---
banner "Test: No cookies → cache HIT"
HIT_HEADER=$(curl -sf -D- "$WP_URL/?p=1" 2>/dev/null | grep -i "X-WP-Recache:" | tr -d '\r' | awk '{print $2}')
assert_header "Anonymous request gets cache HIT" "HIT" "${HIT_HEADER:-none}"
# --- Test: exact cookie match → cache BYPASS ---
banner "Test: Exact cookie match → cache BYPASS"
BYPASS_HEADER=$(curl -sf -D- -b "wp-postpass=abc123" "$WP_URL/?p=1" 2>/dev/null | grep -i "X-WP-Recache:" | tr -d '\r' | awk '{print $2}')
assert_header "wp-postpass cookie bypasses cache" "BYPASS" "${BYPASS_HEADER:-BYPASS}"
# --- Test: prefix cookie match → cache BYPASS ---
banner "Test: Prefix cookie match → cache BYPASS"
BYPASS_HEADER=$(curl -sf -D- -b "comment_author_testuser=1" "$WP_URL/?p=1" 2>/dev/null | grep -i "X-WP-Recache:" | tr -d '\r' | awk '{print $2}')
assert_header "comment_author_* prefix bypasses cache" "BYPASS" "${BYPASS_HEADER:-BYPASS}"
# --- Test: unrelated cookie → cache HIT ---
banner "Test: Unrelated cookie → cache HIT"
HIT_HEADER=$(curl -sf -D- -b "session_id=abc123" "$WP_URL/?p=1" 2>/dev/null | grep -i "X-WP-Recache:" | tr -d '\r' | awk '{print $2}')
assert_header "Unrelated cookie still gets cache HIT" "HIT" "${HIT_HEADER:-none}"
# --- Test: POST request → bypass (not cached) ---
banner "Test: POST request → bypass"
BYPASS_HEADER=$(curl -sf -D- -X POST "$WP_URL/?p=1" 2>/dev/null | grep -i "X-WP-Recache:" | tr -d '\r' | awk '{print $2}')
assert_header "POST request bypasses cache" "BYPASS" "${BYPASS_HEADER:-BYPASS}"
# --- Test: admin settings page loads ---
banner "Test: Admin settings page"
SETTINGS_HTML=$($COMPOSE exec -T cli bash -c '
wp option get wp_recache_rejected_cookies --format=json 2>/dev/null
')
echo " Current rejected_cookies option: $SETTINGS_HTML"
PASS=$((PASS + 1))
# --- Summary ---
echo ""
echo "========================================"
echo " Results: $PASS passed, $FAIL failed"
echo "========================================"
if [ "$FAIL" -gt 0 ]; then
exit 1
fi