Files
mtusi2026lab4/student-tests/test_download_missing_object.sh
T

59 lines
1.8 KiB
Bash

#!/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'