209 lines
6.7 KiB
Bash
209 lines
6.7 KiB
Bash
#!/usr/bin/env bash
|
|
# ============================================================
|
|
# Copyparty Content Tag — API Sanity Tests
|
|
# Run against localhost:8086 (user:12345)
|
|
# Usage: bash tests/api-tests.sh
|
|
# ============================================================
|
|
|
|
set -euo pipefail
|
|
|
|
BASE="http://localhost:8086"
|
|
PW="12345"
|
|
PASS=0
|
|
FAIL=0
|
|
|
|
pass() { PASS=$((PASS+1)); echo " PASS $1"; }
|
|
fail() { FAIL=$((FAIL+1)); echo " FAIL $1"; }
|
|
|
|
cleanup() {
|
|
# Remove test artifacts if they exist
|
|
curl -sf -X POST "${BASE}/private/user/tests_api_tmp?delete&j" \
|
|
-H "PW: ${PW}" >/dev/null 2>&1 || true
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
echo ""
|
|
echo "=== Copyparty API Sanity Tests ==="
|
|
echo ""
|
|
|
|
# ------------------------------------------------------------------
|
|
# 1. Authentication
|
|
# ------------------------------------------------------------------
|
|
echo "--- Auth ---"
|
|
|
|
# Anonymous listing should succeed (read-only)
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "${BASE}/?ls")
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
pass "Anonymous GET /?ls → 200"
|
|
else
|
|
fail "Anonymous GET /?ls → $HTTP_CODE (expected 200)"
|
|
fi
|
|
|
|
# Authenticated listing with PW header
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "${BASE}/?ls" -H "PW: ${PW}")
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
pass "Authenticated GET /?ls + PW header → 200"
|
|
else
|
|
fail "Authenticated GET /?ls + PW header → $HTTP_CODE (expected 200)"
|
|
fi
|
|
|
|
# Wrong password → 403 on write endpoints
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "${BASE}/private/user/?ls" -H "PW: wrongpassword")
|
|
if [ "$HTTP_CODE" = "403" ]; then
|
|
pass "Wrong PW → 403"
|
|
else
|
|
fail "Wrong PW → $HTTP_CODE (expected 403)"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# 2. Directory listing
|
|
# ------------------------------------------------------------------
|
|
echo "--- Directory ---"
|
|
|
|
RESP=$(curl -sf "${BASE}/private/user/?ls" -H "PW: ${PW}")
|
|
if echo "$RESP" | jq -e '.dirs' >/dev/null 2>&1 || echo "$RESP" | jq -e '.files' >/dev/null 2>&1; then
|
|
pass "Directory listing returns JSON with dirs/files"
|
|
else
|
|
fail "Directory listing missing dirs or files keys"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# 3. Create directory
|
|
# ------------------------------------------------------------------
|
|
echo "--- Mkdir ---"
|
|
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
-X POST "${BASE}/private/user/?replace" \
|
|
-H "PW: ${PW}" \
|
|
-d "act=mkdir&name=tests_api_tmp")
|
|
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "302" ]; then
|
|
pass "Mkdir → $HTTP_CODE"
|
|
else
|
|
fail "Mkdir → $HTTP_CODE (expected 200/302)"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# 4. Write file (PUT + Replace header)
|
|
# ------------------------------------------------------------------
|
|
echo "--- Write ---"
|
|
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
-X PUT "${BASE}/private/user/tests_api_tmp/testfile.txt?j" \
|
|
-H "PW: ${PW}" \
|
|
-H "Replace: 1" \
|
|
--data-raw "hello world")
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
pass "PUT write (Replace:1) → 200"
|
|
else
|
|
fail "PUT write (Replace:1) → $HTTP_CODE (expected 200)"
|
|
fi
|
|
|
|
# Read back the file
|
|
CONTENT=$(curl -sf "${BASE}/private/user/tests_api_tmp/testfile.txt?txt" -H "PW: ${PW}")
|
|
if [ "$CONTENT" = "hello world" ]; then
|
|
pass "Read text returns correct content"
|
|
else
|
|
fail "Read text returned: $CONTENT (expected 'hello world')"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# 5. Append to file
|
|
# ------------------------------------------------------------------
|
|
echo "--- Append ---"
|
|
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
-X PUT "${BASE}/private/user/tests_api_tmp/testfile.txt?apnd&j" \
|
|
-H "PW: ${PW}" \
|
|
--data-raw " appended")
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
pass "Append → 200"
|
|
else
|
|
fail "Append → $HTTP_CODE (expected 200)"
|
|
fi
|
|
|
|
CONTENT=$(curl -sf "${BASE}/private/user/tests_api_tmp/testfile.txt?txt" -H "PW: ${PW}")
|
|
if [ "$CONTENT" = "hello world appended" ]; then
|
|
pass "Appended content correct"
|
|
else
|
|
fail "Appended content: '$CONTENT' (expected 'hello world appended')"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# 6. Move file
|
|
# ------------------------------------------------------------------
|
|
echo "--- Move ---"
|
|
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
-X POST "${BASE}/private/user/tests_api_tmp/testfile.txt?move=/private/user/tests_api_tmp/moved_file.txt" \
|
|
-H "PW: ${PW}")
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
pass "Move → 200"
|
|
else
|
|
fail "Move → $HTTP_CODE (expected 200)"
|
|
fi
|
|
|
|
# Verify source gone, dest exists
|
|
MOVED=$(curl -sf "${BASE}/private/user/tests_api_tmp/moved_file.txt?txt" -H "PW: ${PW}" 2>/dev/null || echo "")
|
|
if [ "$MOVED" = "hello world appended" ]; then
|
|
pass "Moved file has correct content"
|
|
else
|
|
fail "Moved file content: $MOVED"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# 7. Copy file
|
|
# ------------------------------------------------------------------
|
|
echo "--- Copy ---"
|
|
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
-X POST "${BASE}/private/user/tests_api_tmp/moved_file.txt?copy=/private/user/tests_api_tmp/copied_file.txt" \
|
|
-H "PW: ${PW}")
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
pass "Copy → 200"
|
|
else
|
|
fail "Copy → $HTTP_CODE (expected 200)"
|
|
fi
|
|
|
|
COPIED=$(curl -sf "${BASE}/private/user/tests_api_tmp/copied_file.txt?txt" -H "PW: ${PW}" 2>/dev/null || echo "")
|
|
if [ "$COPIED" = "hello world appended" ]; then
|
|
pass "Copied file has correct content"
|
|
else
|
|
fail "Copied file content: $COPIED"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# 8. Delete file
|
|
# ------------------------------------------------------------------
|
|
echo "--- Delete ---"
|
|
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
-X POST "${BASE}/private/user/tests_api_tmp/copied_file.txt?delete&j" \
|
|
-H "PW: ${PW}")
|
|
if [ "$HTTP_CODE" = "200" ]; then
|
|
pass "Delete → 200"
|
|
else
|
|
fail "Delete → $HTTP_CODE (expected 200)"
|
|
fi
|
|
|
|
# Verify file gone
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
"${BASE}/private/user/tests_api_tmp/copied_file.txt?txt" -H "PW: ${PW}")
|
|
if [ "$HTTP_CODE" = "404" ]; then
|
|
pass "Deleted file returns 404"
|
|
else
|
|
fail "Deleted file still accessible (HTTP $HTTP_CODE)"
|
|
fi
|
|
|
|
# ------------------------------------------------------------------
|
|
# Cleanup: delete test directory
|
|
# ------------------------------------------------------------------
|
|
curl -sf -X POST "${BASE}/private/user/tests_api_tmp?delete&j" \
|
|
-H "PW: ${PW}" >/dev/null 2>&1 || true
|
|
|
|
# ------------------------------------------------------------------
|
|
# Summary
|
|
# ------------------------------------------------------------------
|
|
echo ""
|
|
echo "=== Results: ${PASS} passed, ${FAIL} failed ==="
|
|
[ "$FAIL" -eq 0 ] && exit 0 || exit 1
|