forked from mtusi2026/mtusi2026lab4
lab4
This commit is contained in:
Executable
+100
@@ -0,0 +1,100 @@
|
||||
#!/bin/sh
|
||||
# Shared helpers for student-tests/*.sh (curl + jq only).
|
||||
# Usage: . "$(dirname "$0")/lib.sh"
|
||||
|
||||
set -eu
|
||||
|
||||
BASE_URL="${BASE_URL:-http://127.0.0.1:8080}"
|
||||
PASS="${STUDENT_TEST_PASSWORD:-Student-test-password-1}"
|
||||
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
|
||||
FIXTURES="$ROOT/tools/fixtures"
|
||||
|
||||
die() {
|
||||
printf '%s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
need() {
|
||||
command -v "$1" >/dev/null 2>&1 || die "missing dependency: $1"
|
||||
}
|
||||
|
||||
need curl
|
||||
need jq
|
||||
need sha256sum
|
||||
need python3
|
||||
|
||||
uniq_name() {
|
||||
printf '%s_%s' "$1" "$(python3 -c 'import uuid; print(uuid.uuid4().hex[:12])')"
|
||||
}
|
||||
|
||||
# http_json METHOD PATH [curl args...]
|
||||
# Sets: HTTP_CODE, HTTP_BODY, HTTP_HDRS
|
||||
http_json() {
|
||||
method=$1
|
||||
path=$2
|
||||
shift 2
|
||||
hdr=$(mktemp)
|
||||
body=$(mktemp)
|
||||
code=$(curl -sS -m 60 -o "$body" -D "$hdr" -w '%{http_code}' \
|
||||
-X "$method" "$BASE_URL$path" "$@") || {
|
||||
rm -f "$hdr" "$body"
|
||||
die "curl failed: $method $path"
|
||||
}
|
||||
HTTP_CODE=$code
|
||||
HTTP_BODY=$(cat "$body")
|
||||
HTTP_HDRS=$(cat "$hdr")
|
||||
rm -f "$hdr" "$body"
|
||||
}
|
||||
|
||||
expect_code() {
|
||||
want=$1
|
||||
[ "$HTTP_CODE" = "$want" ] || die "expected HTTP $want got $HTTP_CODE body=$HTTP_BODY"
|
||||
}
|
||||
|
||||
register_user() {
|
||||
label=$1
|
||||
user=$(uniq_name "$label")
|
||||
http_json POST /api/v1/users \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"$user\",\"display_name\":\"$label\",\"password\":\"$PASS\"}"
|
||||
expect_code 201
|
||||
USER_ID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
USERNAME=$user
|
||||
cred=$(printf '%s' "$USERNAME:$PASS" | python3 -c 'import sys,base64; print(base64.b64encode(sys.stdin.buffer.read()).decode())')
|
||||
http_json POST /api/v1/auth/sessions -H "Authorization: Basic $cred"
|
||||
expect_code 201
|
||||
TOKEN=$(printf '%s' "$HTTP_BODY" | jq -r .token)
|
||||
[ -n "$TOKEN" ] && [ "$TOKEN" != null ] || die "empty token"
|
||||
}
|
||||
|
||||
create_chat() {
|
||||
title=${1:-test}
|
||||
member_json=$2
|
||||
http_json POST /api/v1/chats \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"title\":\"$title\",\"member_ids\":$member_json}"
|
||||
expect_code 201
|
||||
CHAT_ID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
}
|
||||
|
||||
upload_file() {
|
||||
chat_id=$1
|
||||
file=$2
|
||||
filename=$3
|
||||
ctype=$4
|
||||
http_json POST "/api/v1/chats/$chat_id/attachments?filename=$filename" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: $ctype" \
|
||||
--data-binary @"$file"
|
||||
}
|
||||
|
||||
sha256_file() {
|
||||
sha256sum "$1" | awk '{print $1}'
|
||||
}
|
||||
|
||||
assert_eq() {
|
||||
[ "$1" = "$2" ] || die "assert_eq failed: '$1' != '$2'${3:+ ($3)}"
|
||||
}
|
||||
|
||||
printf 'lib.sh: BASE_URL=%s\n' "$BASE_URL" >&2
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
# Run grade-3 attachment student tests (+ drain / compensation dry-run).
|
||||
set -eu
|
||||
DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
|
||||
ROOT=$(CDPATH= cd -- "$DIR/.." && pwd)
|
||||
|
||||
for t in \
|
||||
test_attachment_upload_ok.sh \
|
||||
test_attachment_too_large.sh \
|
||||
test_attachment_corrupted.sh \
|
||||
test_attachment_mime_spoof.sh \
|
||||
test_attachment_foreign.sh \
|
||||
test_attachment_wrong_chat.sh \
|
||||
test_graceful_drain.sh
|
||||
do
|
||||
echo "=== $t ==="
|
||||
sh "$DIR/$t"
|
||||
done
|
||||
|
||||
echo "=== tools/gc_orphans.sh (dry-run) ==="
|
||||
sh "$ROOT/tools/gc_orphans.sh"
|
||||
|
||||
echo "ALL grade3 student-tests PASS"
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
|
||||
ROOT=$(CDPATH= cd -- "$DIR/.." && pwd)
|
||||
|
||||
echo "=== tests/check_bucket_private.sh ==="
|
||||
sh "$ROOT/tests/check_bucket_private.sh"
|
||||
|
||||
echo "=== test_attachment_limited_creds.sh ==="
|
||||
sh "$DIR/test_attachment_limited_creds.sh"
|
||||
|
||||
# Load before persistence: restart cools cgroup RSS accounting and makes
|
||||
# docker-stats delta noisy right after recreate.
|
||||
echo "=== scripts/load_uploads.sh ==="
|
||||
sh "$ROOT/scripts/load_uploads.sh"
|
||||
|
||||
echo "=== test_attachment_persistence.sh ==="
|
||||
sh "$DIR/test_attachment_persistence.sh"
|
||||
|
||||
echo "ALL grade4 student-tests PASS"
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
# Grade 5 / Track 1: direct (presigned) upload + proxy still green.
|
||||
set -eu
|
||||
DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
|
||||
|
||||
echo "=== test_direct_upload_ok.sh ==="
|
||||
sh "$DIR/test_direct_upload_ok.sh"
|
||||
|
||||
echo "=== test_direct_upload_negatives.sh ==="
|
||||
sh "$DIR/test_direct_upload_negatives.sh"
|
||||
|
||||
echo "=== tools/gc_direct_uploads.sh (dry-run) ==="
|
||||
sh "$DIR/../tools/gc_direct_uploads.sh"
|
||||
|
||||
echo "ALL grade5 track1 student-tests PASS"
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/bin/sh
|
||||
# Truncated / corrupt PNG → 415 (not 500).
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
register_user alice
|
||||
create_chat 'corrupt' "[\"$USER_ID\"]"
|
||||
|
||||
bad=$(mktemp)
|
||||
# PNG magic + IHDR-looking garbage, truncated — fails stbi decode.
|
||||
printf '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\x00\x00\x00\x01\x00\x08\x02\x00\x00\x00' > "$bad"
|
||||
|
||||
upload_file "$CHAT_ID" "$bad" 'broken.png' 'image/png'
|
||||
rm -f "$bad"
|
||||
expect_code 415
|
||||
code=$(printf '%s' "$HTTP_BODY" | jq -r .error.code)
|
||||
assert_eq "$code" unsupported_media_type error_code
|
||||
|
||||
printf 'PASS test_attachment_corrupted\n'
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/bin/sh
|
||||
# Non-member → 404 on GET metadata and /content (no leak).
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
png="$FIXTURES/tiny.png"
|
||||
[ -f "$png" ] || die "missing fixture $png"
|
||||
|
||||
register_user alice
|
||||
ALICE_ID=$USER_ID
|
||||
ALICE_TOK=$TOKEN
|
||||
register_user eve
|
||||
EVE_TOK=$TOKEN
|
||||
|
||||
TOKEN=$ALICE_TOK
|
||||
create_chat 'foreign' "[\"$ALICE_ID\"]"
|
||||
upload_file "$CHAT_ID" "$png" 'tiny.png' 'image/png'
|
||||
expect_code 201
|
||||
AID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
|
||||
http_json GET "/api/v1/attachments/$AID" -H "Authorization: Bearer $EVE_TOK"
|
||||
expect_code 404
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .error.code)" not_found meta
|
||||
|
||||
http_json GET "/api/v1/attachments/$AID/content" -H "Authorization: Bearer $EVE_TOK"
|
||||
expect_code 404
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .error.code)" not_found content
|
||||
|
||||
http_json GET "/api/v1/attachments/$AID"
|
||||
expect_code 401
|
||||
|
||||
printf 'PASS test_attachment_foreign\n'
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#!/bin/sh
|
||||
# Grade 4: API uses limited S3 user via *_FILE secrets, not MinIO root.
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
||||
|
||||
# API process env must expose limited-key secret paths, never root password vars.
|
||||
env_dump=$($COMPOSE exec -T api sh -c 'env' | sort)
|
||||
|
||||
printf '%s\n' "$env_dump" | grep -q '^S3_ACCESS_KEY_ID_FILE=' \
|
||||
|| die "API missing S3_ACCESS_KEY_ID_FILE"
|
||||
printf '%s\n' "$env_dump" | grep -q '^S3_SECRET_ACCESS_KEY_FILE=' \
|
||||
|| die "API missing S3_SECRET_ACCESS_KEY_FILE"
|
||||
|
||||
# Root MinIO credentials must not be injected into the API container.
|
||||
if printf '%s\n' "$env_dump" | grep -E '^(MINIO_ROOT_USER|MINIO_ROOT_PASSWORD)=' >/dev/null; then
|
||||
die "API container has MinIO root credentials in env"
|
||||
fi
|
||||
|
||||
# Secret file contents must differ from root user (compare access key id only).
|
||||
api_key=$($COMPOSE exec -T api sh -c 'cat "$S3_ACCESS_KEY_ID_FILE"' | tr -d '\r\n')
|
||||
root_user=$(grep -E '^MINIO_ROOT_USER=' "$ROOT/.env" | head -n1 | cut -d= -f2- | tr -d '\r\n"')
|
||||
[ -n "$api_key" ] || die "empty limited access key"
|
||||
[ -n "$root_user" ] || die "MINIO_ROOT_USER missing in .env"
|
||||
[ "$api_key" != "$root_user" ] || die "API S3 access key equals MinIO root user"
|
||||
|
||||
# Limited user cannot delete the bucket (admin action) — policy is object-scoped.
|
||||
# Probe via mc with the limited credentials from secrets (same values as API).
|
||||
lim_key=$api_key
|
||||
lim_secret=$($COMPOSE exec -T api sh -c 'cat "$S3_SECRET_ACCESS_KEY_FILE"' | tr -d '\r\n')
|
||||
bucket=$($COMPOSE exec -T api sh -c 'printf %s "$S3_BUCKET"')
|
||||
|
||||
out=$($COMPOSE run --rm --no-deps \
|
||||
-e LIM_KEY="$lim_key" -e LIM_SECRET="$lim_secret" -e S3_BUCKET="$bucket" \
|
||||
--entrypoint /bin/sh minio-init -c '
|
||||
set +e
|
||||
mc alias set lim "$S3_ENDPOINT" "$LIM_KEY" "$LIM_SECRET" >/dev/null
|
||||
mc rb --force "lim/$S3_BUCKET" 2>/tmp/rb.err
|
||||
rc=$?
|
||||
cat /tmp/rb.err
|
||||
exit $rc
|
||||
' 2>&1) || true
|
||||
|
||||
printf '%s\n' "$out" | grep -qiE 'AccessDenied|denied|not allowed|InvalidAccessKeyId|error' \
|
||||
|| die "expected AccessDenied on bucket delete by limited user; out=$out"
|
||||
|
||||
# Positive: limited user can still PutObject (smoke via API upload).
|
||||
png="$FIXTURES/tiny.png"
|
||||
register_user alice
|
||||
create_chat 'creds' "[\"$USER_ID\"]"
|
||||
upload_file "$CHAT_ID" "$png" 'tiny.png' 'image/png'
|
||||
expect_code 201
|
||||
|
||||
printf 'PASS test_attachment_limited_creds\n'
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
# JPEG bytes with Content-Type: image/png → 415 (content wins over header).
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
jpg="$FIXTURES/tiny.jpg"
|
||||
[ -f "$jpg" ] || die "missing fixture $jpg"
|
||||
|
||||
register_user alice
|
||||
create_chat 'mime-spoof' "[\"$USER_ID\"]"
|
||||
|
||||
upload_file "$CHAT_ID" "$jpg" 'spoof.png' 'image/png'
|
||||
expect_code 415
|
||||
code=$(printf '%s' "$HTTP_BODY" | jq -r .error.code)
|
||||
assert_eq "$code" unsupported_media_type error_code
|
||||
|
||||
printf 'PASS test_attachment_mime_spoof\n'
|
||||
Executable
+46
@@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
# Grade 4: upload → restart API replicas (volumes kept) → download same bytes.
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
||||
png="$FIXTURES/320x200.png"
|
||||
[ -f "$png" ] || die "missing $png"
|
||||
want_sha=$(sha256_file "$png")
|
||||
|
||||
register_user alice
|
||||
ALICE_ID=$USER_ID
|
||||
create_chat 'persist' "[\"$ALICE_ID\"]"
|
||||
upload_file "$CHAT_ID" "$png" 'sample.png' 'image/png'
|
||||
expect_code 201
|
||||
AID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .sha256)" "$want_sha" upload_sha
|
||||
|
||||
# Restart only API (and LB); keep pgdata + miniodata.
|
||||
$COMPOSE restart api
|
||||
# Wait for both replicas healthy + LB ready.
|
||||
i=0
|
||||
while [ "$i" -lt 60 ]; do
|
||||
http_json GET /health/ready || true
|
||||
if [ "${HTTP_CODE:-}" = "200" ]; then
|
||||
break
|
||||
fi
|
||||
i=$((i + 1))
|
||||
sleep 1
|
||||
done
|
||||
[ "${HTTP_CODE:-}" = "200" ] || die "stack not ready after api restart"
|
||||
|
||||
# Session may still be in Redis (AOF); reuse token.
|
||||
http_json GET "/api/v1/attachments/$AID" -H "Authorization: Bearer $TOKEN"
|
||||
expect_code 200
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .sha256)" "$want_sha" meta_sha
|
||||
|
||||
out=$(mktemp)
|
||||
code=$(curl -sS -m 60 -o "$out" -w '%{http_code}' \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
"$BASE_URL/api/v1/attachments/$AID/content")
|
||||
assert_eq "$code" 200 content_status
|
||||
assert_eq "$(sha256_file "$out")" "$want_sha" content_sha
|
||||
rm -f "$out"
|
||||
|
||||
printf 'PASS test_attachment_persistence\n'
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/bin/sh
|
||||
# Grade 4 criteria (negative): 11 MiB body → 413 payload_too_large (not 500).
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
register_user alice
|
||||
create_chat 'too-large' "[\"$USER_ID\"]"
|
||||
|
||||
big=$(mktemp)
|
||||
# Valid PNG magic + padding to 11 MiB so Content-Length triggers early reject.
|
||||
dd if=/dev/zero of="$big" bs=1M count=11 status=none
|
||||
# Prepend PNG signature so it's not rejected as empty before size check.
|
||||
printf '\x89PNG\r\n\x1a\n' | dd of="$big" conv=notrunc status=none 2>/dev/null || true
|
||||
|
||||
upload_file "$CHAT_ID" "$big" 'huge.png' 'image/png'
|
||||
rm -f "$big"
|
||||
expect_code 413
|
||||
code=$(printf '%s' "$HTTP_BODY" | jq -r .error.code)
|
||||
assert_eq "$code" payload_too_large error_code
|
||||
|
||||
printf 'PASS test_attachment_too_large\n'
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/bin/sh
|
||||
# Grade 3: PNG 320×200 → 201 ready → download bytes match sha256; message with attachment_ids.
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
png="$FIXTURES/320x200.png"
|
||||
[ -f "$png" ] || die "missing fixture $png"
|
||||
want_sha=$(sha256_file "$png")
|
||||
want_size=$(wc -c < "$png" | tr -d ' ')
|
||||
|
||||
register_user alice
|
||||
ALICE_ID=$USER_ID
|
||||
ALICE_TOK=$TOKEN
|
||||
register_user bob
|
||||
BOB_ID=$USER_ID
|
||||
BOB_TOK=$TOKEN
|
||||
|
||||
TOKEN=$ALICE_TOK
|
||||
create_chat 'upload-ok' "[\"$ALICE_ID\",\"$BOB_ID\"]"
|
||||
|
||||
upload_file "$CHAT_ID" "$png" 'sample.png' 'image/png'
|
||||
expect_code 201
|
||||
AID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .status)" ready status
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .content_type)" image/png ctype
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .sha256)" "$want_sha" sha256
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .size_bytes)" "$want_size" size
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .error_code)" null error_code
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -c .variants)" '[]' variants
|
||||
|
||||
http_json POST "/api/v1/chats/$CHAT_ID/messages" \
|
||||
-H "Authorization: Bearer $ALICE_TOK" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-H "Idempotency-Key: $(uniq_name idem)" \
|
||||
-d "{\"text\":\"\",\"attachment_ids\":[\"$AID\"]}"
|
||||
expect_code 201
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -c .attachment_ids)" "[\"$AID\"]" msg_atts
|
||||
|
||||
out=$(mktemp)
|
||||
curl -sS -m 60 -o "$out" -w '%{http_code}' \
|
||||
-H "Authorization: Bearer $BOB_TOK" \
|
||||
"$BASE_URL/api/v1/attachments/$AID/content" > /tmp/att_dl.code
|
||||
assert_eq "$(cat /tmp/att_dl.code)" 200 download_status
|
||||
got_sha=$(sha256_file "$out")
|
||||
assert_eq "$got_sha" "$want_sha" download_sha256
|
||||
rm -f "$out" /tmp/att_dl.code
|
||||
|
||||
printf 'PASS test_attachment_upload_ok\n'
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
# attachment_id from another chat in POST messages → 404.
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
png="$FIXTURES/tiny.png"
|
||||
[ -f "$png" ] || die "missing fixture $png"
|
||||
|
||||
register_user alice
|
||||
ALICE_ID=$USER_ID
|
||||
|
||||
create_chat 'chat-a' "[\"$ALICE_ID\"]"
|
||||
CHAT_A=$CHAT_ID
|
||||
upload_file "$CHAT_A" "$png" 'tiny.png' 'image/png'
|
||||
expect_code 201
|
||||
AID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
|
||||
create_chat 'chat-b' "[\"$ALICE_ID\"]"
|
||||
CHAT_B=$CHAT_ID
|
||||
|
||||
http_json POST "/api/v1/chats/$CHAT_B/messages" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-H "Idempotency-Key: $(uniq_name idem)" \
|
||||
-d "{\"text\":\"hi\",\"attachment_ids\":[\"$AID\"]}"
|
||||
expect_code 404
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .error.code)" not_found
|
||||
|
||||
# Empty/whitespace text without attachments → 400
|
||||
http_json POST "/api/v1/chats/$CHAT_B/messages" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-H "Idempotency-Key: $(uniq_name idem)" \
|
||||
-d '{"text":" "}'
|
||||
expect_code 400
|
||||
|
||||
printf 'PASS test_attachment_wrong_chat\n'
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
#!/bin/sh
|
||||
# Track 1 negatives: bad checksum complete, abort, foreign complete, no PUT.
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
png="$FIXTURES/320x200.png"
|
||||
[ -f "$png" ] || die "missing fixture $png"
|
||||
want_sha=$(sha256_file "$png")
|
||||
want_size=$(wc -c < "$png" | tr -d ' ')
|
||||
bad_sha=$(printf '%s' "$want_sha" | tr 'a-f0-9' '0-9a-f' | head -c 64)
|
||||
# ensure different
|
||||
[ "$bad_sha" != "$want_sha" ] || bad_sha="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
|
||||
register_user alice
|
||||
ALICE_TOKEN=$TOKEN
|
||||
ALICE_ID=$USER_ID
|
||||
create_chat "du-neg" "[\"$ALICE_ID\"]"
|
||||
register_user bob
|
||||
BOB_TOKEN=$TOKEN
|
||||
TOKEN=$ALICE_TOKEN
|
||||
|
||||
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
|
||||
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
||||
|
||||
put_via_net() {
|
||||
url=$1
|
||||
ct=$2
|
||||
file=$3
|
||||
code=$($COMPOSE run --rm --no-deps \
|
||||
-v "$file:/tmp/up.bin:ro" \
|
||||
--entrypoint curl api \
|
||||
-sS -m 60 -o /dev/null -w '%{http_code}' \
|
||||
-X PUT -H "Content-Type: $ct" --data-binary @/tmp/up.bin \
|
||||
"$url")
|
||||
printf '%s' "$code"
|
||||
}
|
||||
|
||||
# --- complete without PUT ---
|
||||
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"filename\":\"a.png\",\"content_type\":\"image/png\",\"size_bytes\":$want_size,\"sha256\":\"$want_sha\"}"
|
||||
expect_code 201
|
||||
ID1=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
http_json POST "/api/v1/direct-uploads/$ID1/complete" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN"
|
||||
expect_code 400
|
||||
|
||||
# --- abort ---
|
||||
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"filename\":\"b.png\",\"content_type\":\"image/png\",\"size_bytes\":$want_size,\"sha256\":\"$want_sha\"}"
|
||||
expect_code 201
|
||||
ID2=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
URL2=$(printf '%s' "$HTTP_BODY" | jq -r .upload_url)
|
||||
CT2=$(printf '%s' "$HTTP_BODY" | jq -r '.headers["Content-Type"]')
|
||||
put_code=$(put_via_net "$URL2" "$CT2" "$png")
|
||||
[ "$put_code" = "200" ] || die "PUT abort-case got $put_code"
|
||||
http_json DELETE "/api/v1/direct-uploads/$ID2" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN"
|
||||
expect_code 204
|
||||
http_json POST "/api/v1/direct-uploads/$ID2/complete" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN"
|
||||
expect_code 409
|
||||
|
||||
# --- foreign owner ---
|
||||
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"filename\":\"c.png\",\"content_type\":\"image/png\",\"size_bytes\":$want_size,\"sha256\":\"$want_sha\"}"
|
||||
expect_code 201
|
||||
ID3=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
URL3=$(printf '%s' "$HTTP_BODY" | jq -r .upload_url)
|
||||
CT3=$(printf '%s' "$HTTP_BODY" | jq -r '.headers["Content-Type"]')
|
||||
put_code=$(put_via_net "$URL3" "$CT3" "$png")
|
||||
[ "$put_code" = "200" ] || die "PUT foreign-case got $put_code"
|
||||
http_json POST "/api/v1/direct-uploads/$ID3/complete" \
|
||||
-H "Authorization: Bearer $BOB_TOKEN"
|
||||
expect_code 404
|
||||
|
||||
# --- checksum mismatch (declared sha ≠ bytes) ---
|
||||
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"filename\":\"d.png\",\"content_type\":\"image/png\",\"size_bytes\":$want_size,\"sha256\":\"$bad_sha\"}"
|
||||
expect_code 201
|
||||
ID4=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
URL4=$(printf '%s' "$HTTP_BODY" | jq -r .upload_url)
|
||||
CT4=$(printf '%s' "$HTTP_BODY" | jq -r '.headers["Content-Type"]')
|
||||
put_code=$(put_via_net "$URL4" "$CT4" "$png")
|
||||
[ "$put_code" = "200" ] || die "PUT checksum-case got $put_code"
|
||||
http_json POST "/api/v1/direct-uploads/$ID4/complete" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN"
|
||||
expect_code 400
|
||||
|
||||
# Proxy path still works
|
||||
upload_file "$CHAT_ID" "$png" "proxy.png" "image/png"
|
||||
expect_code 201
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .status)" "ready" proxy_still_ok
|
||||
|
||||
echo "PASS test_direct_upload_negatives"
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#!/bin/sh
|
||||
# Track 1: initiate → PUT to MinIO (compose network) → complete → download.
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
png="$FIXTURES/320x200.png"
|
||||
[ -f "$png" ] || die "missing fixture $png"
|
||||
want_sha=$(sha256_file "$png")
|
||||
want_size=$(wc -c < "$png" | tr -d ' ')
|
||||
|
||||
register_user alice
|
||||
ALICE_TOKEN=$TOKEN
|
||||
ALICE_ID=$USER_ID
|
||||
create_chat "du-ok" "[\"$ALICE_ID\"]"
|
||||
|
||||
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"filename\":\"photo.png\",\"content_type\":\"image/png\",\"size_bytes\":$want_size,\"sha256\":\"$want_sha\"}"
|
||||
expect_code 201
|
||||
UPLOAD_ID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
UPLOAD_URL=$(printf '%s' "$HTTP_BODY" | jq -r .upload_url)
|
||||
CT=$(printf '%s' "$HTTP_BODY" | jq -r '.headers["Content-Type"]')
|
||||
[ -n "$UPLOAD_ID" ] && [ "$UPLOAD_ID" != null ] || die "empty upload id"
|
||||
[ -n "$UPLOAD_URL" ] && [ "$UPLOAD_URL" != null ] || die "empty upload_url"
|
||||
assert_eq "$CT" "image/png" content_type_header
|
||||
|
||||
# PUT bytes on the compose network (MinIO ports stay unpublished by default).
|
||||
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
|
||||
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
||||
$COMPOSE run --rm --no-deps \
|
||||
-v "$png:/tmp/up.bin:ro" \
|
||||
--entrypoint curl api \
|
||||
-sS -m 60 -o /dev/null -w '%{http_code}' \
|
||||
-X PUT -H "Content-Type: $CT" --data-binary @/tmp/up.bin \
|
||||
"$UPLOAD_URL" > /tmp/du_put_code.$$
|
||||
put_code=$(cat /tmp/du_put_code.$$)
|
||||
rm -f /tmp/du_put_code.$$
|
||||
[ "$put_code" = "200" ] || die "presigned PUT expected 200 got $put_code"
|
||||
|
||||
http_json POST "/api/v1/direct-uploads/$UPLOAD_ID/complete" \
|
||||
-H "Authorization: Bearer $ALICE_TOKEN"
|
||||
expect_code 201
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .status)" "ready" status
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .sha256)" "$want_sha" sha256
|
||||
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .id)" "$UPLOAD_ID" attachment_id
|
||||
|
||||
out=$(mktemp)
|
||||
curl -sS -m 60 -o "$out" -H "Authorization: Bearer $ALICE_TOKEN" \
|
||||
"$BASE_URL/api/v1/attachments/$UPLOAD_ID/content"
|
||||
assert_eq "$(sha256_file "$out")" "$want_sha" download_sha256
|
||||
rm -f "$out"
|
||||
|
||||
echo "PASS test_direct_upload_ok"
|
||||
Executable
+50
@@ -0,0 +1,50 @@
|
||||
#!/bin/sh
|
||||
# Graceful shutdown: SIGTERM → /health/ready becomes 503 while process drains.
|
||||
# Targets one API container; restores it at the end.
|
||||
set -eu
|
||||
. "$(dirname "$0")/lib.sh"
|
||||
|
||||
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
||||
|
||||
cid=$($COMPOSE ps -q api | head -n1)
|
||||
[ -n "$cid" ] || die "no api container"
|
||||
|
||||
# Confirm ready before drain.
|
||||
http_json GET /health/ready
|
||||
expect_code 200
|
||||
|
||||
# docker stop → SIGTERM to PID1 (messenger after entrypoint exec).
|
||||
# Probe the draining instance directly (LB may still hit the other replica).
|
||||
docker stop -t 15 "$cid" >/dev/null &
|
||||
stop_pid=$!
|
||||
|
||||
saw_drain=0
|
||||
i=0
|
||||
while [ "$i" -lt 40 ]; do
|
||||
code=$(docker exec "$cid" curl -sS -m 2 -o /dev/null -w '%{http_code}' \
|
||||
http://127.0.0.1:8080/health/ready 2>/dev/null || printf '000')
|
||||
if [ "$code" = "503" ] || [ "$code" = "000" ]; then
|
||||
saw_drain=1
|
||||
break
|
||||
fi
|
||||
i=$((i + 1))
|
||||
sleep 0.25
|
||||
done
|
||||
wait "$stop_pid" || true
|
||||
[ "$saw_drain" = "1" ] || die "did not observe drain/stop (last code=${code:-none})"
|
||||
|
||||
# Restore replicas.
|
||||
$COMPOSE up -d --no-deps api >/dev/null
|
||||
j=0
|
||||
HTTP_CODE=000
|
||||
while [ "$j" -lt 40 ]; do
|
||||
http_json GET /health/ready || true
|
||||
if [ "${HTTP_CODE:-}" = "200" ]; then
|
||||
break
|
||||
fi
|
||||
j=$((j + 1))
|
||||
sleep 1
|
||||
done
|
||||
[ "${HTTP_CODE:-}" = "200" ] || die "stand not ready after recreate"
|
||||
|
||||
printf 'PASS test_graceful_drain\n'
|
||||
Reference in New Issue
Block a user