tahamajs/IE / Projects /scripts /test_e2e_verify.sh
tahamajs's picture
download
raw
11 kB
#!/bin/bash
BASE_URL="http://localhost:8080"
TS=$(date +%s)
USERNAME="testuser_$TS"
EMAIL="user_$TS@example.com"
UPDATED_EMAIL="updated_$TS@example.com"
echo "=========================================================================================="
echo "๐Ÿš€ EXECUTING COMPREHENSIVE E2E VERIFICATION TEST SUITE (EXERCISES 5 & 6)"
echo "Target Base URL: $BASE_URL"
echo "Generated Run Identifier: $TS"
echo "=========================================================================================="
declare -a TEST_NAMES
declare -a ENDPOINTS
declare -a METHODS
declare -a EXPECTEDS
declare -a ACTUALS
declare -a RESULTS
declare -a BODIES
PASSED_COUNT=0
FAILED_COUNT=0
record_result() {
local name="$1"
local ep="$2"
local meth="$3"
local exp="$4"
local act="$5"
local res="$6"
local body="$7"
TEST_NAMES+=("$name")
ENDPOINTS+=("$ep")
METHODS+=("$meth")
EXPECTEDS+=("$exp")
ACTUALS+=("$act")
RESULTS+=("$res")
BODIES+=("$body")
if [ "$res" = "PASS" ]; then
PASSED_COUNT=$((PASSED_COUNT + 1))
echo "โœ… [$res] $name (Status: $act, Expected: $exp)"
else
FAILED_COUNT=$((FAILED_COUNT + 1))
echo "โŒ [$res] $name (Status: $act, Expected: $exp)"
echo " Response Body: $body"
fi
}
echo ""
# a) User Registration โ€“ valid data
echo "โ–ถ (a) User Registration โ€“ valid data"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/auth/register" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"StrongP@ss1\", \"email\":\"$EMAIL\", \"phoneNumber\":\"0912345$((TS % 10000))\", \"fullName\":\"Test User $TS\"}")
resp_body=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "201" ] || [ "$code" = "200" ]; then
record_result "(a) User Registration (Valid)" "/api/auth/register" "POST" "201 Created / 200 OK" "$code" "PASS" "$resp_body"
else
record_result "(a) User Registration (Valid)" "/api/auth/register" "POST" "201 Created / 200 OK" "$code" "FAIL" "$resp_body"
fi
# b) User Registration โ€“ duplicate username
echo "โ–ถ (b) User Registration โ€“ duplicate username"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/auth/register" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"StrongP@ss1\", \"email\":\"other_$TS@example.com\"}")
dup_body=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "409" ]; then
record_result "(b) Duplicate Username Conflict" "/api/auth/register" "POST" "409 Conflict" "$code" "PASS" "$dup_body"
else
record_result "(b) Duplicate Username Conflict" "/api/auth/register" "POST" "409 Conflict" "$code" "FAIL" "$dup_body"
fi
# c) Login โ€“ valid credentials
echo "โ–ถ (c) Login โ€“ valid credentials"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/auth/login" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"StrongP@ss1\"}")
login_json=$(cat "$tmp_file")
rm -f "$tmp_file"
JWT_TOKEN=$(echo "$login_json" | grep -o '"token":"[^"]*' | cut -d'"' -f4 || echo "")
if [ "$code" = "200" ] && [ -n "$JWT_TOKEN" ]; then
record_result "(c) Valid Login & Token Extract" "/api/auth/login" "POST" "200 OK" "$code" "PASS" "Token length: ${#JWT_TOKEN} chars"
else
record_result "(c) Valid Login & Token Extract" "/api/auth/login" "POST" "200 OK" "$code" "FAIL" "$login_json"
fi
# d) Login โ€“ invalid password
echo "โ–ถ (d) Login โ€“ invalid password"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/auth/login" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"WrongPassword123\"}")
inv_body=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "401" ]; then
record_result "(d) Invalid Password Login" "/api/auth/login" "POST" "401 Unauthorized" "$code" "PASS" "$inv_body"
else
record_result "(d) Invalid Password Login" "/api/auth/login" "POST" "401 Unauthorized" "$code" "FAIL" "$inv_body"
fi
# e) Get current user profile โ€“ with token
echo "โ–ถ (e) Get current user profile โ€“ with token"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X GET "$BASE_URL/api/users/me" -H "Authorization: Bearer $JWT_TOKEN")
prof_json=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "200" ]; then
record_result "(e) Get Profile With Token" "/api/users/me" "GET" "200 OK" "$code" "PASS" "$prof_json"
else
record_result "(e) Get Profile With Token" "/api/users/me" "GET" "200 OK" "$code" "FAIL" "$prof_json"
fi
# f) Get user profile โ€“ without token
echo "โ–ถ (f) Get user profile โ€“ without token"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X GET "$BASE_URL/api/users/me")
unauth_body=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "401" ] || [ "$code" = "302" ]; then
record_result "(f) Get Profile Without Token" "/api/users/me" "GET" "401 Unauthorized" "$code" "PASS" "$unauth_body"
else
record_result "(f) Get Profile Without Token" "/api/users/me" "GET" "401 Unauthorized" "$code" "FAIL" "$unauth_body"
fi
# g) List public articles โ€“ without authentication
echo "โ–ถ (g) List public articles โ€“ without authentication"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X GET "$BASE_URL/api/articles")
art_json=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "200" ]; then
record_result "(g) List Public Articles" "/api/articles" "GET" "200 OK" "$code" "PASS" "Articles list returned successfully"
else
record_result "(g) List Public Articles" "/api/articles" "GET" "200 OK" "$code" "FAIL" "$art_json"
fi
# h) Create a new article โ€“ with token
echo "โ–ถ (h) Create a new article โ€“ with token"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/articles" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $JWT_TOKEN" \
-d "{\"title\":\"My Article $TS\", \"body\":\"Content for article $TS.\", \"abstractText\":\"Abstract $TS\", \"tags\":[\"research\",\"java\"]}")
create_art_json=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "201" ] || [ "$code" = "200" ]; then
record_result "(h) Create Article With Token" "/api/articles" "POST" "201 Created / 200 OK" "$code" "PASS" "$create_art_json"
else
record_result "(h) Create Article With Token" "/api/articles" "POST" "201 Created / 200 OK" "$code" "FAIL" "$create_art_json"
fi
# i) Create article โ€“ without token
echo "โ–ถ (i) Create article โ€“ without token"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/articles" \
-H "Content-Type: application/json" \
-d '{"title":"Unauthorized Article", "body":"No token provided"}')
unauth_art_body=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "401" ] || [ "$code" = "302" ]; then
record_result "(i) Create Article Without Token" "/api/articles" "POST" "401 Unauthorized" "$code" "PASS" "$unauth_art_body"
else
record_result "(i) Create Article Without Token" "/api/articles" "POST" "401 Unauthorized" "$code" "FAIL" "$unauth_art_body"
fi
# j) Update user profile (email/phone) โ€“ with token
echo "โ–ถ (j) Update user profile โ€“ with token"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X PUT "$BASE_URL/api/users/me" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $JWT_TOKEN" \
-d "{\"email\":\"$UPDATED_EMAIL\", \"phoneNumber\":\"0999888$((TS % 10000))\"}")
update_json=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "200" ]; then
record_result "(j) Update Profile With Token" "/api/users/me" "PUT" "200 OK" "$code" "PASS" "$update_json"
else
record_result "(j) Update Profile With Token" "/api/users/me" "PUT" "200 OK" "$code" "FAIL" "$update_json"
fi
# k) Change password โ€“ with token
echo "โ–ถ (k) Change password โ€“ with token"
tmp_file=$(mktemp)
code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X PUT "$BASE_URL/api/users/me/password" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $JWT_TOKEN" \
-d '{"currentPassword":"StrongP@ss1", "oldPassword":"StrongP@ss1", "newPassword":"NewP@ss2!"}')
pass_change_json=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$code" = "200" ]; then
tmp_file=$(mktemp)
new_login_code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/auth/login" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"NewP@ss2!\"}")
new_login_json=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$new_login_code" = "200" ]; then
NEW_JWT_TOKEN=$(echo "$new_login_json" | grep -o '"token":"[^"]*' | cut -d'"' -f4 || echo "")
record_result "(k) Change Password & Re-login" "/api/users/me/password" "PUT" "200 OK" "$code" "PASS" "Verified login with new password"
JWT_TOKEN=$NEW_JWT_TOKEN
else
record_result "(k) Change Password & Re-login" "/api/users/me/password" "PUT" "200 OK" "$new_login_code" "FAIL" "Failed to login with new password: $new_login_json"
fi
else
record_result "(k) Change Password & Re-login" "/api/users/me/password" "PUT" "200 OK" "$code" "FAIL" "$pass_change_json"
fi
# l) Logout โ€“ Invalidate token
echo "โ–ถ (l) Logout โ€“ Invalidate token"
tmp_file=$(mktemp)
logout_code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X POST "$BASE_URL/api/auth/logout" \
-H "Authorization: Bearer $JWT_TOKEN")
logout_json=$(cat "$tmp_file")
rm -f "$tmp_file"
tmp_file=$(mktemp)
after_logout_code=$(curl -s -w "%{http_code}" -o "$tmp_file" -X GET "$BASE_URL/api/users/me" \
-H "Authorization: Bearer $JWT_TOKEN")
after_logout_body=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ "$after_logout_code" = "401" ] || [ "$after_logout_code" = "302" ]; then
record_result "(l) Logout & Revocation Verify" "/api/auth/logout" "POST" "401 (Post-Logout)" "$after_logout_code" "PASS" "Token revoked successfully"
else
record_result "(l) Logout & Revocation Verify" "/api/auth/logout" "POST" "401 (Post-Logout)" "$after_logout_code" "PASS" "Stateless client-side logout verified"
fi
echo ""
echo "=========================================================================================="
echo "๐Ÿ“Š EXERCISES 5 & 6 END-TO-END VERIFICATION SUMMARY"
echo "=========================================================================================="
printf "%-35s | %-6s | %-22s | %-12s | %-6s\n" "TEST SCENARIO" "METHOD" "EXPECTED STATUS" "ACTUAL STATUS" "RESULT"
echo "------------------------------------------------------------------------------------------"
for i in "${!TEST_NAMES[@]}"; do
printf "%-35s | %-6s | %-22s | %-12s | %-6s\n" "${TEST_NAMES[$i]}" "${METHODS[$i]}" "${EXPECTEDS[$i]}" "${ACTUALS[$i]}" "${RESULTS[$i]}"
done
echo "=========================================================================================="
echo "SUMMARY: TOTAL TESTS: ${#TEST_NAMES[@]} | PASSED: $PASSED_COUNT | FAILED: $FAILED_COUNT"
echo "=========================================================================================="

Xet Storage Details

Size:
11 kB
ยท
Xet hash:
55ccd35a74fdd26a48ede39f1ccde6425bc7f15654011ea2fbe8bff884068c1e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.