|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Execute from the "test" directory so that the test files are available.
|
|
|
|
#
|
|
|
|
# We recommend you have the pkgx.dev tools installed.
|
|
|
|
# If you want to use your installed curl & jq & node, you can comment out the two "pkgx" commands.
|
|
|
|
|
|
|
|
HOST=http://localhost:3002
|
|
|
|
|
|
|
|
if ! [[ "$PWD" == */test ]]; then
|
|
|
|
echo "Error: Run this script in the 'test' directory."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# load the tools: curl, jq, node
|
|
|
|
eval "$(pkgx --shellcode)"
|
|
|
|
env +curl +jq +node
|
|
|
|
|
|
|
|
JWT_CODE_USER_0='OWNER_DID="did:ethr:0x0000694B58C2cC69658993A90D3840C560f2F51F"; OWNER_PRIVATE_KEY_HEX="2b6472c026ec2aa2c4235c994a63868fc9212d18b58f6cbfe861b52e71330f5b"; didJwt = require("did-jwt"); didJwt.createJWT({ exp: Math.floor(Date.now() / 1000) + 60, iat: Math.floor(Date.now() / 1000), iss: OWNER_DID }, { issuer: OWNER_DID, signer: didJwt.SimpleSigner(OWNER_PRIVATE_KEY_HEX) }).then(console.log)'
|
|
|
|
JWT_CODE_USER_1='OWNER_DID="did:ethr:0x111d15564f824D56C7a07b913aA7aDd03382aA39"; OWNER_PRIVATE_KEY_HEX="be64d297e1c6f3545971cd0bc24c3bf32656f8639a2ae32cb84a1e3c75ad69cd"; didJwt = require("did-jwt"); didJwt.createJWT({ exp: Math.floor(Date.now() / 1000) + 60, iat: Math.floor(Date.now() / 1000), iss: OWNER_DID }, { issuer: OWNER_DID, signer: didJwt.SimpleSigner(OWNER_PRIVATE_KEY_HEX) }).then(console.log)'
|
|
|
|
|
|
|
|
# exit as soon as anything fails
|
|
|
|
set -e
|
|
|
|
|
|
|
|
echo "Upload test0.png by user #0"
|
|
|
|
JWT=$(node -e "$JWT_CODE_USER_0")
|
|
|
|
echo JWT: $JWT
|
|
|
|
RESULT=$(curl -X POST -H "Authorization: Bearer $JWT" -F "image=@test0.png" "$HOST/image")
|
|
|
|
echo curl result: $RESULT
|
|
|
|
|
|
|
|
echo "Download from the URL supplied"
|
|
|
|
URL0=$(echo $RESULT | jq -r '.url')
|
|
|
|
# -L to follow redirect because the returned URL is a timesafari.app URL
|
|
|
|
STATUS_CODE=$(curl -o test0-back.png -w "%{http_code}" -L $URL0);
|
|
|
|
if [ $STATUS_CODE -ne 200 ]; then
|
|
|
|
echo "File is not accessible, received status code: $STATUS_CODE";
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Check that downloaded file is the same as the original"
|
|
|
|
if diff "test0.png" "test0-back.png" >/dev/null; then
|
|
|
|
echo "Got the same file."
|
|
|
|
else
|
|
|
|
echo "Did not get the same file."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Upload test1.png by user #1"
|
|
|
|
JWT=$(node -e "$JWT_CODE_USER_1")
|
|
|
|
echo JWT: $JWT
|
|
|
|
RESULT=$(curl -X POST -H "Authorization: Bearer $JWT" -F "image=@test1.png" "$HOST/image")
|
|
|
|
echo curl result: $RESULT
|
|
|
|
URL2=$(echo $RESULT | jq -r '.url')
|
|
|
|
if [ "$URL0" != "$URL2" ]; then
|
|
|
|
echo "URLs 0 & 1 are different."
|
|
|
|
else
|
|
|
|
echo "URLs 0 & 1 are not different."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Now unsuccessfully upload a change to the image by user 1"
|
|
|
|
FILENAME0=$(basename $URL0)
|
|
|
|
JWT=$(node -e "$JWT_CODE_USER_1")
|
|
|
|
echo JWT: $JWT
|
|
|
|
RESULT=$(curl -X POST -H "Authorization: Bearer $JWT" -F "image=@test1.png" -F "fileName=$FILENAME0" "$HOST/image")
|
|
|
|
echo curl result: $RESULT
|
|
|
|
SUCCESS=$(echo $RESULT | jq -r '.success')
|
|
|
|
if [ $SUCCESS = "false" ]; then
|
|
|
|
echo "User #1 could not replace existing file."
|
|
|
|
else
|
|
|
|
echo "File may have been replaced wrongly.";
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Now successfully upload a change to the image by user 0"
|
|
|
|
JWT=$(node -e "$JWT_CODE_USER_0")
|
|
|
|
echo JWT: $JWT
|
|
|
|
RESULT=$(curl -X POST -H "Authorization: Bearer $JWT" -F "image=@test1.png" -F "fileName=$FILENAME0" "$HOST/image")
|
|
|
|
echo curl result: $RESULT
|
|
|
|
SUCCESS=$(echo $RESULT | jq -r '.success')
|
|
|
|
if [ $SUCCESS = "true" ]; then
|
|
|
|
echo "User #0 did replace file.";
|
|
|
|
else
|
|
|
|
echo "User #0 couldn't replace file.";
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Fail to remove test file 0 from the service"
|
|
|
|
TEST_URL="https%3A%2F%2Ftest-image.timesafari.app%2F4599145c3a8792a678f458747f2d8512c680e8680bf5563c35b06cd770051ed2.png"
|
|
|
|
JWT=$(node -e "$JWT_CODE_USER_1")
|
|
|
|
echo JWT: $JWT
|
|
|
|
RESULT=$(curl -X DELETE -H "Authorization: Bearer $JWT" "$HOST/image/$TEST_URL")
|
|
|
|
echo curl result: $RESULT
|
|
|
|
SUCCESS=$(echo $RESULT | jq -r '.success')
|
|
|
|
if [ "$SUCCESS" = "false" ]; then
|
|
|
|
echo "Test file 0 was not cleaned off server."
|
|
|
|
else
|
|
|
|
echo "Test file 0 was cleaned off server.";
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Remove test file 0 from the service"
|
|
|
|
TEST_URL="https%3A%2F%2Ftest-image.timesafari.app%2F4599145c3a8792a678f458747f2d8512c680e8680bf5563c35b06cd770051ed2.png"
|
|
|
|
JWT=$(node -e "$JWT_CODE_USER_0")
|
|
|
|
echo JWT: $JWT
|
|
|
|
RESULT=$(curl -X DELETE -H "Authorization: Bearer $JWT" "$HOST/image/$TEST_URL")
|
|
|
|
echo curl result: $RESULT
|
|
|
|
SUCCESS=$(echo $RESULT | jq -r '.success')
|
|
|
|
if [[ -z "$RESULT" ]] || [[ "$SUCCESS" = "true" ]]; then
|
|
|
|
echo "Test file 0 was cleaned off server."
|
|
|
|
else
|
|
|
|
echo "Test file 0 was not cleaned off server.";
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Remove test file 1 from the service"
|
|
|
|
TEST_URL="https%3A%2F%2Ftest-image.timesafari.app%2F83801e59789f962ddd19dbf99abd65b416e4c6560c28bdb3e663cea045561b07.png"
|
|
|
|
JWT=$(node -e "$JWT_CODE_USER_1")
|
|
|
|
echo JWT: $JWT
|
|
|
|
RESULT=$(curl -X DELETE -H "Authorization: Bearer $JWT" "$HOST/image/$TEST_URL")
|
|
|
|
echo curl result: $RESULT
|
|
|
|
SUCCESS=$(echo $RESULT | jq -r '.success')
|
|
|
|
if [[ -z "$RESULT" ]] || [[ "$SUCCESS" = "true" ]]; then
|
|
|
|
echo "Test file 1 was cleaned off server."
|
|
|
|
else
|
|
|
|
echo "Test file 1 was not cleaned off server.";
|
|
|
|
exit 1
|
|
|
|
fi
|