fit: complete lab4, fix P1.1-P1.3, add Track 1 presigned

This commit is contained in:
2026-11-06 04:23:59 +03:00
parent 3223dd469a
commit 7741b66fd2
16 changed files with 577 additions and 219 deletions
+3
View File
@@ -6,6 +6,9 @@ DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
echo "=== test_direct_upload_ok.sh ==="
sh "$DIR/test_direct_upload_ok.sh"
echo "=== test_presigned_complete_concurrent.sh ==="
sh "$DIR/test_presigned_complete_concurrent.sh"
echo "=== test_direct_upload_negatives.sh ==="
sh "$DIR/test_direct_upload_negatives.sh"
@@ -43,7 +43,8 @@ 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
expect_code 409
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .error.code)" not_ready
# --- abort ---
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
@@ -91,7 +92,8 @@ 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
expect_code 409
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .error.code)" hash_mismatch
# Proxy path still works
upload_file "$CHAT_ID" "$png" "proxy.png" "image/png"
+9 -4
View File
@@ -27,14 +27,12 @@ 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 \
put_code=$($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.$$
"$UPLOAD_URL")
[ "$put_code" = "200" ] || die "presigned PUT expected 200 got $put_code"
http_json POST "/api/v1/direct-uploads/$UPLOAD_ID/complete" \
@@ -45,6 +43,13 @@ 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)
replay_code=$($COMPOSE run --rm --no-deps \
--entrypoint curl api \
-sS -m 60 -o /dev/null -w '%{http_code}' \
-X PUT -H "Content-Type: $CT" --data-binary 'replay-content-12345' \
"$UPLOAD_URL")
[ "$replay_code" = "200" ] || die "replay PUT expected 200 got $replay_code"
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
@@ -0,0 +1,59 @@
#!/bin/sh
# A missing S3 object must fail before the content response headers are sent.
set -eu
. "$(dirname "$0")/lib.sh"
png="$FIXTURES/320x200.png"
[ -f "$png" ] || die "missing fixture $png"
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
response_body=$(mktemp)
object_deleted=0
restore_object() {
action=$1
$COMPOSE run --rm --no-deps -T \
-e "CHAT_ID=$CHAT_ID" -e "ATTACHMENT_ID=$AID" \
-e "OBJECT_ACTION=$action" \
-v "$png:/tmp/restore.png:ro" \
--entrypoint /bin/sh minio-init -c '
set -eu
mc alias set local "$S3_ENDPOINT" "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null
object="local/$S3_BUCKET/attachments/$CHAT_ID/$ATTACHMENT_ID"
if [ "$OBJECT_ACTION" = delete ]; then
mc rm --force "$object"
else
mc cp /tmp/restore.png "$object"
fi
'
}
cleanup() {
if [ "$object_deleted" = 1 ]; then
restore_object restore || printf 'WARNING: failed to restore test object\n' >&2
fi
rm -f "$response_body"
}
trap cleanup 0 HUP INT TERM
want_sha=$(sha256_file "$png")
TOKEN=""
register_user alice
create_chat 'missing-object' "[\"$USER_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" metadata_sha
restore_object delete
object_deleted=1
code=$(curl -sS -m 60 -o "$response_body" -w '%{http_code}' \
-H "Authorization: Bearer $TOKEN" \
"$BASE_URL/api/v1/attachments/$AID/content")
case "$code" in
404|5??) ;;
*) die "expected 404/5xx for missing object, got HTTP $code body=$(cat "$response_body")" ;;
esac
restore_object restore
object_deleted=0
printf 'PASS test_download_missing_object\n'
@@ -0,0 +1,73 @@
#!/bin/sh
# Track 1: concurrent complete calls publish one intact attachment.
. "$(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 ' ')
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' 0 HUP INT TERM
register_user alice
ALICE_TOKEN=$TOKEN
ALICE_ID=$USER_ID
create_chat "du-concurrent" "[\"$ALICE_ID\"]"
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
-H "Authorization: Bearer $ALICE_TOKEN" \
-H 'Content-Type: application/json' \
-d "{\"filename\":\"concurrent.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"
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
put_code=$($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")
[ "$put_code" = "200" ] || die "presigned PUT expected 200 got $put_code"
pids=
for i in 1 2 3 4; do
curl -sS -m 60 -o "$tmpdir/body.$i" -w '%{http_code}' \
-X POST \
-H "Authorization: Bearer $ALICE_TOKEN" \
"$BASE_URL/api/v1/direct-uploads/$UPLOAD_ID/complete" \
> "$tmpdir/code.$i" &
pids="$pids $!"
done
for pid in $pids; do
wait "$pid" || die "complete request failed"
done
count_201=0
for i in 1 2 3 4; do
code=$(cat "$tmpdir/code.$i")
case "$code" in
201) count_201=$((count_201 + 1)) ;;
200|409) ;;
*) die "unexpected concurrent complete HTTP $code: $(cat "$tmpdir/body.$i")" ;;
esac
done
assert_eq "$count_201" 1 created_count
http_json GET "/api/v1/attachments/$UPLOAD_ID" \
-H "Authorization: Bearer $ALICE_TOKEN"
expect_code 200
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .status)" ready metadata_status
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .sha256)" "$want_sha" metadata_sha
code=$(curl -sS -m 60 -o "$tmpdir/content" -w '%{http_code}' \
-H "Authorization: Bearer $ALICE_TOKEN" \
"$BASE_URL/api/v1/attachments/$UPLOAD_ID/content")
assert_eq "$code" 200 content_status
assert_eq "$(sha256_file "$tmpdir/content")" "$want_sha" content_sha
printf 'PASS test_presigned_complete_concurrent\n'