Add frontend test environment with real backend integration

This commit introduces a proper integration test environment for the frontend:

- Creates a test environment option in the frontend installer
- Uses the real backend in Docker containers
- Binds frontend to 127.0.0.1 for local testing only
- Adds automatic verification testing of backend-frontend communication
- Provides scripts to easily start and stop the test environment

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-03-28 09:33:59 +01:00
parent 8c3c80fb5c
commit 3e08a09d87
5 changed files with 873 additions and 3 deletions

View File

@ -0,0 +1,45 @@
version: '3.8'
services:
# Backend for testing
backend:
build:
context: ../../backend
dockerfile: Dockerfile
container_name: myp-backend-test
ports:
- "5000:5000"
environment:
- SECRET_KEY=testsecretkey123456789
- DATABASE_URL=sqlite:///myp.db
- FLASK_ENV=development
- TESTING=true
volumes:
- backend-test-data:/app/instance
networks:
- test-network
# Frontend for testing - bound to loopback address
frontend-test:
build:
context: ..
dockerfile: Dockerfile
container_name: myp-frontend-test
environment:
- NODE_ENV=development
- AUTH_TRUST_HOST=true
- AUTH_SECRET=test-secret-key-for-testing-only-do-not-use-in-production
- NEXT_PUBLIC_BACKEND_URL=http://backend:5000
ports:
- "127.0.0.1:3000:3000"
depends_on:
- backend
networks:
- test-network
networks:
test-network:
driver: bridge
volumes:
backend-test-data:

View File

@ -0,0 +1,164 @@
#!/bin/bash
# Test integration script - validates if the frontend and backend work together
# Specifically designed to test if the production environment setup will work
# Get the directory containing this script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PARENT_DIR="$(dirname "$SCRIPT_DIR")"
TEST_LOG="$SCRIPT_DIR/test-integration.log"
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Create or clear log file
> "$TEST_LOG"
echo -e "${YELLOW}Starting MYP integration test environment...${NC}" | tee -a "$TEST_LOG"
echo "Test directory: $SCRIPT_DIR" | tee -a "$TEST_LOG"
echo "Parent directory: $PARENT_DIR" | tee -a "$TEST_LOG"
echo "Log file: $TEST_LOG" | tee -a "$TEST_LOG"
# Check if Docker is available
if ! command -v docker &> /dev/null; then
echo -e "${RED}Error: Docker is not installed or not in PATH${NC}" | tee -a "$TEST_LOG"
exit 1
fi
# Function to run docker compose (handles both docker-compose and docker compose syntax)
run_docker_compose() {
if command -v docker-compose &> /dev/null; then
docker-compose "$@"
else
docker compose "$@"
fi
}
# Stop any existing test environment (cleanup)
echo -e "${YELLOW}Cleaning up any existing test environment...${NC}" | tee -a "$TEST_LOG"
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1
# Start the test environment
echo -e "${YELLOW}Starting test environment with docker-compose...${NC}" | tee -a "$TEST_LOG"
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" up -d >> "$TEST_LOG" 2>&1
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to start test environment.${NC}" | tee -a "$TEST_LOG"
echo "Check the log file for details: $TEST_LOG" | tee -a "$TEST_LOG"
exit 1
fi
# Wait for backend to be ready
echo -e "${YELLOW}Waiting for backend to be ready...${NC}" | tee -a "$TEST_LOG"
max_attempts=30
attempt=1
backend_ready=false
while [ $attempt -le $max_attempts ] && [ "$backend_ready" = "false" ]; do
echo "Checking backend readiness (attempt $attempt/$max_attempts)..." | tee -a "$TEST_LOG"
if curl -s http://localhost:5000/api/health &> /dev/null; then
backend_ready=true
echo -e "${GREEN}Backend is ready!${NC}" | tee -a "$TEST_LOG"
else
echo "Backend not ready yet, waiting..." | tee -a "$TEST_LOG"
sleep 2
attempt=$((attempt+1))
fi
done
if [ "$backend_ready" = "false" ]; then
echo -e "${RED}Backend failed to start properly after $max_attempts attempts${NC}" | tee -a "$TEST_LOG"
echo "Logs from backend container:" | tee -a "$TEST_LOG"
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" logs backend >> "$TEST_LOG" 2>&1
# Cleanup
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1
exit 1
fi
# Wait for frontend to be ready
echo -e "${YELLOW}Waiting for frontend to be ready...${NC}" | tee -a "$TEST_LOG"
max_attempts=30
attempt=1
frontend_ready=false
while [ $attempt -le $max_attempts ] && [ "$frontend_ready" = "false" ]; do
echo "Checking frontend readiness (attempt $attempt/$max_attempts)..." | tee -a "$TEST_LOG"
if curl -s http://127.0.0.1:3000 &> /dev/null; then
frontend_ready=true
echo -e "${GREEN}Frontend is ready!${NC}" | tee -a "$TEST_LOG"
else
echo "Frontend not ready yet, waiting..." | tee -a "$TEST_LOG"
sleep 2
attempt=$((attempt+1))
fi
done
if [ "$frontend_ready" = "false" ]; then
echo -e "${RED}Frontend failed to start properly after $max_attempts attempts${NC}" | tee -a "$TEST_LOG"
echo "Logs from frontend container:" | tee -a "$TEST_LOG"
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" logs frontend-test >> "$TEST_LOG" 2>&1
# Cleanup
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1
exit 1
fi
# Perform basic integration tests
echo -e "${YELLOW}Performing basic integration tests...${NC}" | tee -a "$TEST_LOG"
# Test 1: Frontend can fetch API data (printers endpoint)
echo "Test 1: Frontend can fetch data from backend API..." | tee -a "$TEST_LOG"
frontend_container_id=$(docker ps -qf "name=myp-frontend-test")
if [ -z "$frontend_container_id" ]; then
echo -e "${RED}Failed to find frontend container${NC}" | tee -a "$TEST_LOG"
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1
exit 1
fi
# Run a simple test inside the frontend container to check API connectivity
api_test_result=$(docker exec $frontend_container_id curl -s http://backend:5000/api/health)
if [[ "$api_test_result" == *"healthy"* ]]; then
echo -e "${GREEN}Test 1 PASSED: Frontend can connect to backend API${NC}" | tee -a "$TEST_LOG"
else
echo -e "${RED}Test 1 FAILED: Frontend cannot connect to backend API${NC}" | tee -a "$TEST_LOG"
echo "API response: $api_test_result" | tee -a "$TEST_LOG"
# Don't exit, continue with other tests
fi
# Test 2: Frontend serves HTML content
echo "Test 2: Frontend serves valid HTML content..." | tee -a "$TEST_LOG"
frontend_html=$(curl -s http://127.0.0.1:3000)
if [[ "$frontend_html" == *"<!DOCTYPE html>"* ]]; then
echo -e "${GREEN}Test 2 PASSED: Frontend serves valid HTML${NC}" | tee -a "$TEST_LOG"
else
echo -e "${RED}Test 2 FAILED: Frontend does not serve valid HTML${NC}" | tee -a "$TEST_LOG"
# Don't exit, continue with other tests
fi
# All tests completed
echo -e "${GREEN}Integration tests completed${NC}" | tee -a "$TEST_LOG"
# Ask if the environment should be kept running or shutdown
echo -e "${YELLOW}Test environment is running at:${NC}" | tee -a "$TEST_LOG"
echo "Frontend: http://127.0.0.1:3000" | tee -a "$TEST_LOG"
echo "Backend: http://localhost:5000" | tee -a "$TEST_LOG"
echo "" | tee -a "$TEST_LOG"
read -p "Do you want to keep the test environment running? (y/n): " keep_running
if [[ "$keep_running" != "y" && "$keep_running" != "Y" ]]; then
echo -e "${YELLOW}Shutting down test environment...${NC}" | tee -a "$TEST_LOG"
run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1
echo -e "${GREEN}Test environment has been shut down${NC}" | tee -a "$TEST_LOG"
else
echo -e "${GREEN}Test environment is still running${NC}" | tee -a "$TEST_LOG"
echo "To stop it later, run: docker-compose -f $SCRIPT_DIR/test-env.yml down" | tee -a "$TEST_LOG"
fi
echo -e "${GREEN}Integration testing completed. See log for details: $TEST_LOG${NC}"