Skip to content

Instantly share code, notes, and snippets.

@olawanlejoel
Created December 16, 2025 18:19
Show Gist options
  • Select an option

  • Save olawanlejoel/96e4be7df6d7a06ae2f5dea2048b7f37 to your computer and use it in GitHub Desktop.

Select an option

Save olawanlejoel/96e4be7df6d7a06ae2f5dea2048b7f37 to your computer and use it in GitHub Desktop.
Automate local API testing with Bash
#!/bin/bash
set -e
EMAIL="testuser_$(date +%s)@example.com"
PASSWORD="securePassword123!"
NEW_PASSWORD="newSecurePassword456!"
NAME="Test User"
echo "================================"
echo "Complete Authentication Workflow"
echo "================================"
echo ""
# 1. Register
echo "1. Registering user: $EMAIL"
REGISTER_RESPONSE=$(curl -s -X POST http://localhost:8080/register \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\",
\"password\": \"$PASSWORD\",
\"name\": \"$NAME\"
}")
echo "$REGISTER_RESPONSE" | jq '.'
USER_ID=$(echo "$REGISTER_RESPONSE" | jq -r '.id')
VERIFICATION_TOKEN=$(echo "$REGISTER_RESPONSE" | jq -r '.verification_token')
if [ "$USER_ID" = "null" ]; then
echo "Registration failed"
exit 1
fi
echo "User registered: $USER_ID"
echo ""
# 2. Verify email
echo "2. Verifying email with token: $VERIFICATION_TOKEN"
VERIFY_RESPONSE=$(curl -s -X POST http://localhost:8080/verify \
-H "Content-Type: application/json" \
-d "{
\"token\": \"$VERIFICATION_TOKEN\"
}")
echo "$VERIFY_RESPONSE" | jq '.'
if echo "$VERIFY_RESPONSE" | jq -e '.status == "verified"' > /dev/null; then
echo "Email verified"
else
echo "Email verification failed"
exit 1
fi
echo ""
# 3. Login
echo "3. Logging in with credentials"
LOGIN_RESPONSE=$(curl -s -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\",
\"password\": \"$PASSWORD\"
}")
echo "$LOGIN_RESPONSE" | jq '.'
JWT_TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.token')
if [ "$JWT_TOKEN" = "null" ]; then
echo "Login failed"
exit 1
fi
echo "Logged in, JWT token received"
echo ""
# 4. Get profile
echo "4. Fetching user profile"
ME_RESPONSE=$(curl -s http://localhost:8080/me \
-H "Authorization: Bearer $JWT_TOKEN")
echo "$ME_RESPONSE" | jq '.'
if echo "$ME_RESPONSE" | jq -e '.id' > /dev/null; then
echo "Profile retrieved successfully"
else
echo "Failed to get profile"
exit 1
fi
echo ""
# 5. Request password reset
echo "5. Requesting password reset"
FORGOT_RESPONSE=$(curl -s -X POST http://localhost:8080/password/forgot \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\"
}")
echo "$FORGOT_RESPONSE" | jq '.'
RESET_TOKEN=$(echo "$FORGOT_RESPONSE" | jq -r '.reset_token')
if [ "$RESET_TOKEN" = "null" ]; then
echo "Password reset request failed"
exit 1
fi
echo "Password reset token received"
echo ""
# 6. Reset password
echo "6. Resetting password"
RESET_RESPONSE=$(curl -s -X POST http://localhost:8080/password/reset \
-H "Content-Type: application/json" \
-d "{
\"token\": \"$RESET_TOKEN\",
\"password\": \"$NEW_PASSWORD\"
}")
echo "$RESET_RESPONSE" | jq '.'
if echo "$RESET_RESPONSE" | jq -e '.status == "password updated"' > /dev/null; then
echo "Password reset successfully"
else
echo "Password reset failed"
exit 1
fi
echo ""
# 7. Login with new password
echo "7. Logging in with new password"
NEW_LOGIN_RESPONSE=$(curl -s -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\",
\"password\": \"$NEW_PASSWORD\"
}")
echo "$NEW_LOGIN_RESPONSE" | jq '.'
NEW_JWT_TOKEN=$(echo "$NEW_LOGIN_RESPONSE" | jq -r '.token')
if [ "$NEW_JWT_TOKEN" = "null" ]; then
echo "Login with new password failed"
exit 1
fi
echo "Logged in with new password"
echo ""
# 8. Verify old token is invalid
echo "8. Verifying old JWT token is invalidated"
OLD_TOKEN_RESPONSE=$(curl -s http://localhost:8080/me \
-H "Authorization: Bearer $JWT_TOKEN")
if echo "$OLD_TOKEN_RESPONSE" | jq -e '.error' > /dev/null; then
echo "Old token correctly invalidated"
else
echo "Warning: Old token still works (unexpected)"
fi
echo ""
# 9. Access profile with new token
echo "9. Accessing profile with new JWT token"
FINAL_ME_RESPONSE=$(curl -s http://localhost:8080/me \
-H "Authorization: Bearer $NEW_JWT_TOKEN")
echo "$FINAL_ME_RESPONSE" | jq '.'
if echo "$FINAL_ME_RESPONSE" | jq -e '.id' > /dev/null; then
echo "Profile accessed with new token"
else
echo "Failed to access profile with new token"
exit 1
fi
echo ""
echo "================================"
echo "All tests passed successfully!"
echo "================================"
echo ""
echo "Summary:"
echo " Email: $EMAIL"
echo " User ID: $USER_ID"
echo " Final JWT: ${NEW_JWT_TOKEN:0:50}..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment