#!/bin/bash set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color COMPOSE_FILE="docker-compose.yml" TEST_TIMEOUT=60 CONTAINER_NAME="dyn-ddns-test" echo -e "${YELLOW}=== DDNS Container Integration Test ===${NC}" # Detect compose command if command -v podman-compose &> /dev/null; then COMPOSE_CMD="podman-compose" echo -e "${GREEN}Using podman-compose${NC}" elif command -v docker-compose &> /dev/null; then COMPOSE_CMD="docker-compose" echo -e "${GREEN}Using docker-compose${NC}" else echo -e "${RED}Error: Neither podman-compose nor docker-compose found${NC}" exit 1 fi # Cleanup function cleanup() { echo -e "${YELLOW}Cleaning up...${NC}" $COMPOSE_CMD -f $COMPOSE_FILE -p $CONTAINER_NAME down -v 2>/dev/null || true rm -f .env.test } # Set trap for cleanup on exit trap cleanup EXIT # Create test environment file cat > .env.test << EOF SERVER_PORT=8080 DATABASE_PATH=/data/dyn.db TECHNITIUM_URL=http://mock-dns:8080 TECHNITIUM_TOKEN=test-token BASE_DOMAIN=test.rip SPACE_SUBDOMAIN=space RATE_LIMIT_PER_IP=100 RATE_LIMIT_PER_TOKEN=100 EOF echo -e "${YELLOW}Starting services...${NC}" $COMPOSE_CMD -f $COMPOSE_FILE -p $CONTAINER_NAME --env-file .env.test up -d --build # Wait for services to be ready echo -e "${YELLOW}Waiting for services to start (timeout: ${TEST_TIMEOUT}s)...${NC}" for i in $(seq 1 $TEST_TIMEOUT); do if curl -s http://localhost:8080/ > /dev/null 2>&1; then echo -e "${GREEN}Services are ready!${NC}" break fi if [ $i -eq $TEST_TIMEOUT ]; then echo -e "${RED}Timeout waiting for services${NC}" exit 1 fi sleep 1 done echo -e "${YELLOW}Running integration tests...${NC}" # Test 1: Health check echo -n "Test 1: Health check... " if curl -s http://localhost:8080/ | grep -q "DWS Dynamic DNS"; then echo -e "${GREEN}PASS${NC}" else echo -e "${RED}FAIL${NC}" exit 1 fi # Test 2: Check subdomain availability echo -n "Test 2: Check subdomain availability... " RESPONSE=$(curl -s "http://localhost:8080/api/check?subdomain=testspace") if echo "$RESPONSE" | grep -q '"available":true'; then echo -e "${GREEN}PASS${NC}" else echo -e "${RED}FAIL${NC} - Response: $RESPONSE" exit 1 fi # Test 3: Claim space echo -n "Test 3: Claim space... " RESPONSE=$(curl -s -X POST http://localhost:8080/api/claim \ -H "Content-Type: application/json" \ -d '{"subdomain":"mytest"}') if echo "$RESPONSE" | grep -q '"token"'; then TOKEN=$(echo "$RESPONSE" | grep -o '"token":"[^"]*"' | cut -d'"' -f4) echo -e "${GREEN}PASS${NC} (token: ${TOKEN:0:20}...)" else echo -e "${RED}FAIL${NC} - Response: $RESPONSE" exit 1 fi # Test 4: Check claimed subdomain is not available echo -n "Test 4: Check claimed subdomain unavailable... " RESPONSE=$(curl -s "http://localhost:8080/api/check?subdomain=mytest") if echo "$RESPONSE" | grep -q '"available":false'; then echo -e "${GREEN}PASS${NC}" else echo -e "${RED}FAIL${NC} - Response: $RESPONSE" exit 1 fi # Test 5: DynDNS update (mocked - will fail DNS but test auth) echo -n "Test 5: DynDNS update endpoint... " RESPONSE=$(curl -s -u "none:$TOKEN" \ "http://localhost:8080/api/nic/update?hostname=mytest.space.test.rip&myip=192.168.1.100") # We expect 911 since there's no real DNS server, but auth should work if [ $? -eq 0 ]; then echo -e "${GREEN}PASS${NC} (endpoint reachable)" else echo -e "${RED}FAIL${NC}" exit 1 fi # Test 6: Profanity filter echo -n "Test 6: Profanity filter... " RESPONSE=$(curl -s -X POST http://localhost:8080/api/claim \ -H "Content-Type: application/json" \ -d '{"subdomain":"fuck"}') if echo "$RESPONSE" | grep -q 'inappropriate'; then echo -e "${GREEN}PASS${NC}" else echo -e "${RED}FAIL${NC} - Response: $RESPONSE" exit 1 fi # Test 7: Custom filter (DWS terms) echo -n "Test 7: Custom filter (DWS terms)... " RESPONSE=$(curl -s -X POST http://localhost:8080/api/claim \ -H "Content-Type: application/json" \ -d '{"subdomain":"dws"}') if echo "$RESPONSE" | grep -q 'reserved'; then echo -e "${GREEN}PASS${NC}" else echo -e "${RED}FAIL${NC} - Response: $RESPONSE" exit 1 fi # Test 8: Invalid subdomain format echo -n "Test 8: Invalid subdomain format... " RESPONSE=$(curl -s -X POST http://localhost:8080/api/claim \ -H "Content-Type: application/json" \ -d '{"subdomain":"ab"}') if echo "$RESPONSE" | grep -q 'Invalid'; then echo -e "${GREEN}PASS${NC}" else echo -e "${RED}FAIL${NC} - Response: $RESPONSE" exit 1 fi echo -e "${GREEN}=== All tests passed! ===${NC}"