Files
mtusi2026lab4/student-tests/test_direct_upload_negatives.sh
T

104 lines
3.9 KiB
Bash
Executable File

#!/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 409
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .error.code)" not_ready
# --- 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 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"
expect_code 201
assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .status)" "ready" proxy_still_ok
echo "PASS test_direct_upload_negatives"