example agent
modified: test_agent.sh
This commit is contained in:
parent
596dbb923d
commit
35268e6884
1 changed files with 108 additions and 33 deletions
141
test_agent.sh
141
test_agent.sh
|
|
@ -1,75 +1,150 @@
|
|||
#!/usr/bin/env bash
|
||||
#
|
||||
# A Bash script to demonstrate and test multiple authenticated routes
|
||||
# in the pyrafiles application (or a similar Pyramid-based app).
|
||||
#
|
||||
# This script:
|
||||
# 1. Checks if cookies.txt is older than a defined threshold (default 1 year).
|
||||
# - If too old, it deletes cookies.txt so we force re-login.
|
||||
# 2. If cookies.txt is still valid or absent, proceeds to the next steps:
|
||||
# - If cookies.txt does not exist or was removed, it does the email + OTP flow.
|
||||
# - If cookies.txt exists and is fresh, it reuses that session.
|
||||
# 3. Fetches and prints the Profile page (GET /auth/profile) as a quick test.
|
||||
# 4. Uploads a sample media file (POST /media/upload), marked public.
|
||||
# 5. Parses the server's redirect "Location" header to extract user_short_id and media_short_id.
|
||||
# 6. Views media details (GET /media/<user_short_id>/<media_short_id>/details).
|
||||
# 7. Edits the newly uploaded media (POST /media/<user_short_id>/<media_short_id>/edit).
|
||||
# 8. Deletes the media (POST /media/<user_short_id>/<media_short_id>/delete).
|
||||
# 9. Lists all public media (GET /media/list).
|
||||
# 10. Logs out (GET /auth/logout).
|
||||
#
|
||||
# The script exits on the first command error (set -e).
|
||||
# Adjust BASE_URL, EMAIL, and file paths as needed.
|
||||
#
|
||||
# Usage:
|
||||
# MAX_COOKIE_AGE=1800 ./agent_upload.sh /path/to/somefile.jpg
|
||||
# MAX_COOKIE_AGE=<seconds> ./test_all_routes.sh /path/to/somefile.jpg
|
||||
# By default, MAX_COOKIE_AGE=31536000 (1 year).
|
||||
# If cookies.txt is younger than that, we skip re-login. Otherwise, we do the OTP flow.
|
||||
#
|
||||
# Description:
|
||||
# Upload a file to the pyrafiles server as an agent, making it public.
|
||||
# - If cookies.txt is newer than MAX_COOKIE_AGE seconds (default 31536000, ~1 year),
|
||||
# we skip re-login.
|
||||
# - Otherwise, we prompt for OTP again.
|
||||
#
|
||||
# Notes:
|
||||
# 1. If not set, MAX_COOKIE_AGE defaults to 31536000 (one year).
|
||||
# 2. We assume the server runs on http://localhost:6544. Adjust BASE_URL if needed.
|
||||
# By default, uses http://localhost:6544 and agent@example.com
|
||||
|
||||
set -e # Exit on first error
|
||||
|
||||
BASE_URL="${BASE_URL:-http://localhost:6544}"
|
||||
EMAIL="${EMAIL:-agent@example.com}"
|
||||
MEDIA_FILE="$1"
|
||||
# Default to 1 year in seconds if not provided:
|
||||
MAX_COOKIE_AGE="${MAX_COOKIE_AGE:-31536000}"
|
||||
MAX_COOKIE_AGE="${MAX_COOKIE_AGE:-31536000}" # default ~1 year in seconds
|
||||
|
||||
if [[ -z "$MEDIA_FILE" ]]; then
|
||||
echo "Usage: MAX_COOKIE_AGE=<seconds> $0 /path/to/mediafile"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if cookies.txt is 'fresh enough':
|
||||
echo "=== Checking cookies.txt freshness ==="
|
||||
|
||||
COOKIE_FRESH=0
|
||||
if [[ -f cookies.txt ]]; then
|
||||
# 'stat -c %Y' returns the file's mod time on Linux; 'stat -f %m' on macOS/BSD
|
||||
# 'stat -c %Y' returns the file modification time on Linux
|
||||
# 'stat -f %m' does the same on BSD/macOS
|
||||
MOD_TIME=$(stat -c %Y cookies.txt 2>/dev/null || stat -f %m cookies.txt 2>/dev/null)
|
||||
NOW=$(date +%s)
|
||||
AGE=$((NOW - MOD_TIME))
|
||||
|
||||
echo "cookies.txt is $AGE seconds old, threshold is $MAX_COOKIE_AGE."
|
||||
if (( AGE < MAX_COOKIE_AGE )); then
|
||||
COOKIE_FRESH=1
|
||||
echo "Reusing existing cookies.txt (fresh enough)."
|
||||
else
|
||||
echo "cookies.txt is too old. Removing it to force re-login."
|
||||
rm -f cookies.txt
|
||||
fi
|
||||
else
|
||||
echo "No cookies.txt found. A new session will be created."
|
||||
fi
|
||||
|
||||
if [[ "$COOKIE_FRESH" -eq 1 ]]; then
|
||||
echo "Reusing existing cookies (file age < $MAX_COOKIE_AGE seconds)."
|
||||
echo "Skipping OTP prompt."
|
||||
else
|
||||
echo "Starting new session (cookie missing or stale)..."
|
||||
curl -s -c cookies.txt -b cookies.txt "$BASE_URL/" >/dev/null
|
||||
if [[ "$COOKIE_FRESH" -eq 0 ]]; then
|
||||
echo ""
|
||||
echo "=== Starting new session and logging in with OTP flow ==="
|
||||
curl -s -i -c cookies.txt -b cookies.txt "$BASE_URL/" >/dev/null
|
||||
|
||||
echo "Logging in with email: $EMAIL"
|
||||
curl -s -c cookies.txt -b cookies.txt \
|
||||
echo "Logging in with email: $EMAIL (POST /auth/login)"
|
||||
curl -s -i -c cookies.txt -b cookies.txt \
|
||||
-X POST \
|
||||
-F "email=$EMAIL" \
|
||||
"$BASE_URL/auth/login"
|
||||
|
||||
echo ""
|
||||
echo "Check your console or email for the 6-digit verification code."
|
||||
echo "Check your server logs or email for the 6-digit verification code."
|
||||
read -p "Enter the 6-digit code: " VERIFICATION_CODE
|
||||
|
||||
echo "Verifying code..."
|
||||
curl -s -c cookies.txt -b cookies.txt \
|
||||
echo "Verifying code (POST /auth/verify)"
|
||||
curl -s -i -c cookies.txt -b cookies.txt \
|
||||
-X POST \
|
||||
-F "code=$VERIFICATION_CODE" \
|
||||
"$BASE_URL/auth/verify"
|
||||
|
||||
echo "Cookies refreshed."
|
||||
echo "Login/OTP flow complete. cookies.txt saved."
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Uploading media: $MEDIA_FILE (public)"
|
||||
curl -s -c cookies.txt -b cookies.txt \
|
||||
-X POST \
|
||||
-F "media_file=@$MEDIA_FILE" \
|
||||
-F "is_public=on" \
|
||||
"$BASE_URL/media/upload"
|
||||
echo "=== Quick check: Fetching Profile page (GET /auth/profile) ==="
|
||||
curl -i -c cookies.txt -b cookies.txt "$BASE_URL/auth/profile"
|
||||
|
||||
echo ""
|
||||
echo "Upload complete. File is public."
|
||||
echo "=== Uploading media (POST /media/upload) as public ==="
|
||||
UPLOAD_RESPONSE=$(curl -i -c cookies.txt -b cookies.txt \
|
||||
-X POST \
|
||||
-F "title=TestUploadByAgent" \
|
||||
-F "media_file=@${MEDIA_FILE}" \
|
||||
-F "is_public=on" \
|
||||
"$BASE_URL/media/upload")
|
||||
|
||||
echo "$UPLOAD_RESPONSE"
|
||||
echo ""
|
||||
echo "Parsing 'Location' header from upload response..."
|
||||
|
||||
LOCATION_HEADER=$(echo "$UPLOAD_RESPONSE" | grep -i "^Location:" | head -n1 | sed 's/\r//g')
|
||||
LOCATION_URL="${LOCATION_HEADER#Location: }"
|
||||
LOCATION_URL=$(echo "$LOCATION_URL" | tr -d '[:space:]')
|
||||
LOCATION_PATH="${LOCATION_URL#*//*/}"
|
||||
|
||||
USER_SHORT_ID=$(echo "$LOCATION_PATH" | cut -d'/' -f2)
|
||||
MEDIA_SHORT_ID=$(echo "$LOCATION_PATH" | cut -d'/' -f3)
|
||||
|
||||
echo "user_short_id=$USER_SHORT_ID"
|
||||
echo "media_short_id=$MEDIA_SHORT_ID"
|
||||
|
||||
if [[ -z "$USER_SHORT_ID" || -z "$MEDIA_SHORT_ID" ]]; then
|
||||
echo "Failed to parse user or media short ID. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=== Viewing media details (GET /media/$USER_SHORT_ID/$MEDIA_SHORT_ID/details) ==="
|
||||
curl -i -c cookies.txt -b cookies.txt \
|
||||
"$BASE_URL/media/$USER_SHORT_ID/$MEDIA_SHORT_ID/details"
|
||||
|
||||
echo ""
|
||||
echo "=== Editing media title to 'RenamedByAgent' (POST /media/$USER_SHORT_ID/$MEDIA_SHORT_ID/edit) ==="
|
||||
curl -i -c cookies.txt -b cookies.txt \
|
||||
-X POST \
|
||||
-F "title=RenamedByAgent" \
|
||||
"$BASE_URL/media/$USER_SHORT_ID/$MEDIA_SHORT_ID/edit"
|
||||
|
||||
echo ""
|
||||
echo "=== Deleting the media (POST /media/$USER_SHORT_ID/$MEDIA_SHORT_ID/delete) ==="
|
||||
curl -i -c cookies.txt -b cookies.txt \
|
||||
-X POST \
|
||||
"$BASE_URL/media/$USER_SHORT_ID/$MEDIA_SHORT_ID/delete"
|
||||
|
||||
echo ""
|
||||
echo "=== Listing all public media (GET /media/list) ==="
|
||||
curl -i -c cookies.txt -b cookies.txt "$BASE_URL/media/list"
|
||||
|
||||
echo ""
|
||||
echo "=== Logging out (GET /auth/logout) ==="
|
||||
curl -i -c cookies.txt -b cookies.txt "$BASE_URL/auth/logout"
|
||||
|
||||
echo ""
|
||||
echo "Test complete. We exercised multiple routes, including login/verify, profile, "
|
||||
echo "upload/delete/edit media, and logout. If no errors appeared, all requests succeeded!"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue