Files
mtusi2026lab4/student-tests/test_direct_upload_ok.sh
T

59 lines
2.3 KiB
Bash
Executable File

#!/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"
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"
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)
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
rm -f "$out"
echo "PASS test_direct_upload_ok"