#!/bin/bash # Minimal positive-flow test for LeCoffre Anchor API # Usage: ./test-api-ok.sh [API_URL] [API_KEY] # Load environment variables when .env exists if [ -f ".env" ]; then set -o allexport # shellcheck disable=SC1091 source ".env" set +o allexport fi API_URL=${1:-"${ANCHORE_API_URL:-}"} # No fallback: must be provided API_KEY=${2:-"${ANCHORE_API_KEY:-}"} # No fallback: must be provided if [ -z "$API_URL" ]; then echo "❌ ANCHORE_API_URL non défini (fournir la variable ou passer l'URL en argument)." exit 1 fi if [ -z "$API_KEY" ]; then echo "❌ ANCHORE_API_KEY non défini (fournir la variable ou passer la clé en argument)." exit 1 fi echo "🧪 Test OK - Anchor API" echo "📍 URL: $API_URL" echo "🔑 API Key: ${API_KEY:0:8}..." echo "" set -euo pipefail # Health check (expected 200) echo "1. Health check" curl -s -o /tmp/anchor_health.json -w "%{http_code}" "$API_URL/health" > /tmp/anchor_health_code.txt health_code=$(cat /tmp/anchor_health_code.txt) if [ "$health_code" != "200" ]; then echo "❌ Health check a retourné HTTP $health_code" cat /tmp/anchor_health.json exit 1 fi echo " ✅ Health OK" # Positive anchor document (expected 200) echo "2. Anchor document (OK case)" payload='{"documentUid":"ok-test","hash":"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"}' response=$(curl -s -w "\n%{http_code}" -X POST "$API_URL/api/anchor/document" \ -H "Content-Type: application/json" \ -H "x-api-key: $API_KEY" \ -d "$payload") http_code=$(echo "$response" | tail -n1) body=$(echo "$response" | head -n -1) if [ "$http_code" != "200" ]; then echo "❌ Anchor document a retourné HTTP $http_code (attendu 200)" echo "$body" exit 1 fi transaction_id=$(echo "$body" | jq -r '.transaction_id') if [ -z "$transaction_id" ] || [ "$transaction_id" = "null" ]; then echo "❌ transaction_id invalide dans la réponse" echo "$body" exit 1 fi # Vérifier que transaction_id est un txid Bitcoin (64 hex) if ! echo "$transaction_id" | grep -qE '^[a-f0-9]{64}$'; then echo "❌ transaction_id n'est pas un txid Bitcoin valide (64 hex): $transaction_id" echo "$body" exit 1 fi echo " ✅ Anchor OK (transaction_id/txid: $transaction_id)" # Confirm status endpoint reachable (expected 200) echo "3. Status (OK case)" status_response=$(curl -s -w "\n%{http_code}" "$API_URL/api/anchor/status/$transaction_id" \ -H "x-api-key: $API_KEY") status_code=$(echo "$status_response" | tail -n1) status_body=$(echo "$status_response" | head -n -1) if [ "$status_code" != "200" ]; then echo "❌ Status a retourné HTTP $status_code" echo "$status_body" exit 1 fi echo " ✅ Status OK" echo "" echo "🎉 Test OK terminé avec succès."