Files
ctms/docs/setup-config-curl-smoke.sh
T
2026-02-24 16:53:22 +08:00

98 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE_URL="${BASE_URL:-http://localhost}"
EMAIL="${EMAIL:-admin@example.com}"
PASSWORD="${PASSWORD:-admin123}"
STUDY_ID="${STUDY_ID:-aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa}"
echo "[1/6] login: $EMAIL"
TOKEN=$(curl -sS -X POST "$BASE_URL/api/v1/auth/login" \
-H 'Content-Type: application/json' \
-d "{\"email\":\"$EMAIL\",\"password\":\"$PASSWORD\"}" | \
python3 -c 'import json,sys; print(json.load(sys.stdin).get("access_token",""))')
if [[ -z "$TOKEN" ]]; then
echo "login failed: access_token empty"
exit 1
fi
AUTH=(-H "Authorization: Bearer $TOKEN")
echo "[2/6] get setup-config"
GET_RESP=$(curl -sS "$BASE_URL/api/v1/studies/$STUDY_ID/setup-config" "${AUTH[@]}")
VERSION=$(echo "$GET_RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("version",""))')
if [[ -z "$VERSION" ]]; then
echo "get setup-config failed"
echo "$GET_RESP"
exit 1
fi
echo "version=$VERSION"
echo "[3/6] save draft"
PUT_PAYLOAD=$(cat <<JSON
{
"expected_version": $VERSION,
"data": {
"projectMilestones": [],
"enrollmentPlan": {
"totalTarget": 123,
"startDate": "2026-02-01",
"endDate": "2026-12-31",
"monthlyGoalNote": "curl smoke",
"stageBreakdown": "phase-1"
},
"siteMilestones": [],
"siteEnrollmentPlans": [],
"monitoringStrategies": [],
"centerConfirm": []
}
}
JSON
)
PUT_RESP=$(curl -sS -X PUT "$BASE_URL/api/v1/studies/$STUDY_ID/setup-config" \
"${AUTH[@]}" -H 'Content-Type: application/json' -d "$PUT_PAYLOAD")
VERSION=$(echo "$PUT_RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("version",""))')
if [[ -z "$VERSION" ]]; then
echo "save draft failed"
echo "$PUT_RESP"
exit 1
fi
echo "saved version=$VERSION"
echo "[4/6] publish"
PUB_RESP=$(curl -sS -X POST "$BASE_URL/api/v1/studies/$STUDY_ID/setup-config/publish" \
"${AUTH[@]}" -H 'Content-Type: application/json' -d "{\"expected_version\":$VERSION}")
PUB_STATUS=$(echo "$PUB_RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("publish_status",""))')
VERSION=$(echo "$PUB_RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("version",""))')
if [[ "$PUB_STATUS" != "PUBLISHED" ]]; then
echo "publish failed"
echo "$PUB_RESP"
exit 1
fi
echo "publish_status=$PUB_STATUS version=$VERSION"
echo "[5/6] export excel"
EXCEL_FILE="/tmp/setup-config-export.xlsx"
curl -sS "$BASE_URL/api/v1/studies/$STUDY_ID/setup-config/export-excel" "${AUTH[@]}" -o "$EXCEL_FILE"
if [[ ! -s "$EXCEL_FILE" ]]; then
echo "export excel failed"
exit 1
fi
echo "export saved: $EXCEL_FILE"
echo "[6/6] import excel (re-import export file)"
IMP_RESP=$(curl -sS -X POST "$BASE_URL/api/v1/studies/$STUDY_ID/setup-config/import-excel" \
"${AUTH[@]}" \
-F "expected_version=$VERSION" \
-F "file=@$EXCEL_FILE;type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
IMP_VER=$(echo "$IMP_RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("version",""))')
if [[ -z "$IMP_VER" ]]; then
echo "import failed"
echo "$IMP_RESP"
exit 1
fi
echo "import version=$IMP_VER"
echo "setup-config curl smoke passed"