🧪 test: add JWT secret rotation BDD scenarios and step implementations (#12)
✨ merge: implement JWT secret rotation with BDD scenario isolation - Implement JWT secret rotation mechanism (closes #8) - Add per-scenario state isolation for BDD tests (closes #14) - Validate password reset workflow via BDD tests (closes #7) - Fix port conflicts in test validation - Add state tracer for debugging test execution - Document BDD isolation strategies in ADR 0025 - Fix PostgreSQL configuration environment variables Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Co-authored-by: Gabriel Radureau <arcodange@gmail.com> Co-committed-by: Gabriel Radureau <arcodange@gmail.com>
This commit was merged in pull request #12.
This commit is contained in:
168
scripts/test-feature.sh
Executable file
168
scripts/test-feature.sh
Executable file
@@ -0,0 +1,168 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Feature-Specific Test Runner Script
|
||||
# Runs BDD tests for a specific feature with proper isolation
|
||||
|
||||
set -e
|
||||
|
||||
# Check if feature name is provided
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "❌ Usage: $0 <feature-name>"
|
||||
echo "Available features: auth, config, greet, health, jwt"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
FEATURE=$1
|
||||
SCRIPTS_DIR=$(dirname `realpath ${BASH_SOURCE[0]}`)
|
||||
cd $SCRIPTS_DIR/..
|
||||
|
||||
# Validate feature name
|
||||
case $FEATURE in
|
||||
auth|config|greet|health|jwt)
|
||||
echo "🧪 Setting up ${FEATURE} feature tests..."
|
||||
;;
|
||||
*)
|
||||
echo "❌ Invalid feature: $FEATURE"
|
||||
echo "Available features: auth, config, greet, health, jwt"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Feature-specific configuration
|
||||
DATABASE="dance_lessons_coach_${FEATURE}_test"
|
||||
CONFIG="features/${FEATURE}/${FEATURE}-test-config.yaml"
|
||||
PORT=$(grep "port:" "$CONFIG" | awk '{print $2}')
|
||||
|
||||
# Setup function
|
||||
setup_feature_environment() {
|
||||
echo "🧪 Setting up ${FEATURE} feature tests..."
|
||||
|
||||
# Check if we're in CI environment
|
||||
if [ -n "$GITHUB_ACTIONS" ] || [ -n "$GITEA_ACTIONS" ]; then
|
||||
# CI environment - PostgreSQL is already running as a service
|
||||
echo "🏗️ CI environment detected"
|
||||
|
||||
# Create database if it doesn't exist
|
||||
if ! psql -h postgres -p 5432 -U postgres -lqt | cut -d \| -f 1 | grep -qw "${DATABASE}"; then
|
||||
echo "📦 Creating ${FEATURE} test database..."
|
||||
createdb -h postgres -p 5432 -U postgres "${DATABASE}"
|
||||
echo "✅ ${FEATURE} test database created successfully!"
|
||||
else
|
||||
echo "✅ ${FEATURE} test database already exists"
|
||||
fi
|
||||
else
|
||||
# Local environment - use docker compose
|
||||
echo "💻 Local environment detected"
|
||||
|
||||
# Check if PostgreSQL container is running, start it if not
|
||||
if ! docker ps --format '{{.Names}}' | grep -q "^dance-lessons-coach-postgres$"; then
|
||||
echo "🐋 Starting PostgreSQL container..."
|
||||
docker compose up -d postgres
|
||||
|
||||
# Wait for PostgreSQL to be ready
|
||||
echo "⏳ Waiting for PostgreSQL to be ready..."
|
||||
max_attempts=30
|
||||
attempt=0
|
||||
while [ $attempt -lt $max_attempts ]; do
|
||||
if docker exec dance-lessons-coach-postgres pg_isready -U postgres 2>/dev/null; then
|
||||
echo "✅ PostgreSQL is ready!"
|
||||
break
|
||||
fi
|
||||
attempt=$((attempt + 1))
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if [ $attempt -eq $max_attempts ]; then
|
||||
echo "❌ PostgreSQL failed to start"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "✅ PostgreSQL container is already running"
|
||||
fi
|
||||
|
||||
# Create feature-specific database
|
||||
if docker exec dance-lessons-coach-postgres psql -U postgres -lqt | cut -d \| -f 1 | grep -qw "${DATABASE}"; then
|
||||
echo "✅ ${FEATURE} test database already exists"
|
||||
else
|
||||
echo "📦 Creating ${FEATURE} test database..."
|
||||
if docker exec dance-lessons-coach-postgres createdb -U postgres "${DATABASE}"; then
|
||||
echo "✅ ${FEATURE} test database created successfully!"
|
||||
else
|
||||
echo "❌ Failed to create ${FEATURE} test database"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Run tests function
|
||||
run_feature_tests() {
|
||||
echo "🚀 Running ${FEATURE} feature tests..."
|
||||
|
||||
# Set feature-specific environment variables
|
||||
export DLC_DATABASE_HOST="localhost"
|
||||
export DLC_DATABASE_PORT="5432"
|
||||
export DLC_DATABASE_USER="postgres"
|
||||
export DLC_DATABASE_PASSWORD="postgres"
|
||||
export DLC_DATABASE_NAME="${DATABASE}"
|
||||
export DLC_DATABASE_SSL_MODE="disable"
|
||||
export DLC_CONFIG_FILE="${CONFIG}"
|
||||
|
||||
# Run tests with proper coverage measurement and tag exclusion
|
||||
set +e
|
||||
test_output=$(go test ./features/${FEATURE}/... -tags=~@flaky,~@todo,~@skip -v -cover -coverpkg=./... -coverprofile=coverage-${FEATURE}.out 2>&1)
|
||||
test_exit_code=$?
|
||||
set -e
|
||||
|
||||
echo "$test_output"
|
||||
|
||||
# Check for undefined steps
|
||||
if echo "$test_output" | grep -q "undefined"; then
|
||||
echo "❌ FAILED: Found undefined steps in ${FEATURE} tests"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for pending steps
|
||||
if echo "$test_output" | grep -q "pending"; then
|
||||
echo "❌ FAILED: Found pending steps in ${FEATURE} tests"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for skipped steps
|
||||
if echo "$test_output" | grep -q "skipped"; then
|
||||
echo "❌ FAILED: Found skipped steps in ${FEATURE} tests"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if tests passed
|
||||
if [ $test_exit_code -eq 0 ]; then
|
||||
echo "✅ All ${FEATURE} feature tests passed successfully!"
|
||||
return 0
|
||||
else
|
||||
echo "❌ ${FEATURE} feature tests failed"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Cleanup function
|
||||
cleanup_feature_environment() {
|
||||
echo "🧹 Cleaning up ${FEATURE} feature tests..."
|
||||
|
||||
# Check if we're in CI environment
|
||||
if [ -n "$GITHUB_ACTIONS" ] || [ -n "$GITEA_ACTIONS" ]; then
|
||||
# CI environment - drop database
|
||||
echo "🗑️ Dropping ${FEATURE} test database..."
|
||||
dropdb -h postgres -p 5432 -U postgres "${DATABASE}" 2>/dev/null || true
|
||||
echo "✅ ${FEATURE} test database cleaned up"
|
||||
else
|
||||
# Local environment - drop database
|
||||
echo "🗑️ Dropping ${FEATURE} test database..."
|
||||
docker exec dance-lessons-coach-postgres dropdb -U postgres "${DATABASE}" 2>/dev/null || true
|
||||
echo "✅ ${FEATURE} test database cleaned up"
|
||||
fi
|
||||
}
|
||||
|
||||
# Main execution
|
||||
setup_feature_environment
|
||||
run_feature_tests
|
||||
cleanup_feature_environment
|
||||
Reference in New Issue
Block a user