forked from jsnbuchanan/crowd-funder-for-time-pwa
- Add DID generation and management for testing - Create .generated directory for test artifacts - Add environment variable support for test configuration - Improve deep link test script with better URL handling - Add print/execute modes for testing with/without device The changes improve the testing workflow by: 1. Generating and managing test DIDs automatically 2. Storing test artifacts in .generated directory (gitignored) 3. Adding proper URL encoding for deep links 4. Supporting both print mode for debugging and execute mode for device testing 5. Adding better error handling and validation
144 lines
4.5 KiB
Bash
Executable File
144 lines
4.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Parse command line arguments
|
|
GENERATE_ONLY=false
|
|
ALL_TESTS=false
|
|
while getopts "ga" opt; do
|
|
case $opt in
|
|
g) GENERATE_ONLY=true ;;
|
|
a) ALL_TESTS=true ;;
|
|
\?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
# Directory for storing generated DIDs
|
|
mkdir -p .generated
|
|
|
|
# Function to validate environment variables
|
|
validate_env() {
|
|
local missing=false
|
|
for var in CONTACT1_DID CONTACT1_KEY CONTACT2_DID CONTACT2_KEY ISSUER_DID ISSUER_KEY; do
|
|
if [ -z "${!var}" ]; then
|
|
echo "Error: $var is not set" >&2
|
|
missing=true
|
|
fi
|
|
done
|
|
if [ "$missing" = true ]; then
|
|
rm -f .generated/test-env.sh # Remove invalid env file
|
|
echo "Please regenerate DIDs by running:" >&2
|
|
echo "./test-scripts/run-deeplink-tests.sh -g" >&2
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# Check if we already have generated DIDs
|
|
if [ -f .generated/test-env.sh ] && [ "$GENERATE_ONLY" = false ]; then
|
|
echo "Using existing DIDs from .generated/test-env.sh"
|
|
source .generated/test-env.sh
|
|
validate_env || exit 1
|
|
else
|
|
# Function to extract DID info from did_generator.sh output
|
|
extract_did_info() {
|
|
local output
|
|
# Read all input into output variable
|
|
output=$(cat)
|
|
|
|
# Debug the input
|
|
echo "Parsing output:" >&2
|
|
echo "$output" >&2
|
|
|
|
# Extract values using more robust patterns
|
|
local did=$(echo "$output" | grep "^DID: " | cut -d' ' -f2-)
|
|
local private_key=$(echo "$output" | grep "^Private Key: " | cut -d' ' -f3-)
|
|
|
|
# Debug the extracted values
|
|
echo "Found DID: $did" >&2
|
|
echo "Found Key: $private_key" >&2
|
|
|
|
# Output JSON only if both values were found
|
|
if [ -n "$did" ] && [ -n "$private_key" ]; then
|
|
printf '%s\n' "{\"did\":\"$did\",\"privateKey\":\"$private_key\"}"
|
|
else
|
|
echo "Error: Could not extract DID info from output" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
echo "Generating first contact DID..."
|
|
CONTACT1_INFO=$(./test-scripts/did_generator.sh | tee /dev/stderr | extract_did_info)
|
|
if [ $? -ne 0 ]; then
|
|
echo "Failed to generate first contact DID"
|
|
exit 1
|
|
fi
|
|
CONTACT1_DID=$(echo "$CONTACT1_INFO" | jq -r .did)
|
|
CONTACT1_KEY=$(echo "$CONTACT1_INFO" | jq -r .privateKey)
|
|
echo "Extracted DID: $CONTACT1_DID"
|
|
echo "Extracted Key: $CONTACT1_KEY"
|
|
|
|
echo "Generating second contact DID (Jordan)..."
|
|
CONTACT2_INFO=$(./test-scripts/did_generator.sh 2>&1 | tee /dev/stderr | extract_did_info)
|
|
CONTACT2_DID=$(echo "$CONTACT2_INFO" | jq -r .did)
|
|
CONTACT2_KEY=$(echo "$CONTACT2_INFO" | jq -r .privateKey)
|
|
|
|
echo "Generating issuer DID..."
|
|
ISSUER_INFO=$(./test-scripts/did_generator.sh 2>&1 | tee /dev/stderr | extract_did_info)
|
|
ISSUER_DID=$(echo "$ISSUER_INFO" | jq -r .did)
|
|
ISSUER_KEY=$(echo "$ISSUER_INFO" | jq -r .privateKey)
|
|
|
|
# Create a temporary env file with the generated DIDs
|
|
echo "Creating test-env.sh with generated DIDs..."
|
|
cat > .generated/test-env.sh << EOF
|
|
#!/bin/bash
|
|
# Generated $(date)
|
|
export CONTACT1_DID="$CONTACT1_DID"
|
|
export CONTACT1_KEY="$CONTACT1_KEY"
|
|
export CONTACT2_DID="$CONTACT2_DID"
|
|
export CONTACT2_KEY="$CONTACT2_KEY"
|
|
export ISSUER_DID="$ISSUER_DID"
|
|
export ISSUER_KEY="$ISSUER_KEY"
|
|
EOF
|
|
|
|
# Debug output
|
|
echo "Contents of generated test-env.sh:"
|
|
cat .generated/test-env.sh
|
|
|
|
# Make sure file is executable
|
|
chmod +x .generated/test-env.sh
|
|
|
|
# Source and validate the newly created env file
|
|
echo "Sourcing generated test-env.sh..."
|
|
source .generated/test-env.sh
|
|
validate_env || exit 1
|
|
fi
|
|
|
|
if [ "$GENERATE_ONLY" = true ]; then
|
|
echo "Generated DIDs and created environment file at .generated/test-env.sh"
|
|
echo "To use these DIDs, run:"
|
|
echo "source .generated/test-env.sh"
|
|
exit 0
|
|
fi
|
|
|
|
# Create contacts JSON for deep links
|
|
CONTACTS_JSON=$(jq -n \
|
|
--arg did1 "$CONTACT1_DID" \
|
|
--arg did2 "$CONTACT2_DID" \
|
|
'[
|
|
{"did": $did1},
|
|
{
|
|
"did": $did2,
|
|
"name": "Jordan",
|
|
"nextPubKeyHashB64": "IBfRZfwdzeKOzqCx8b+WlLpMJHOAT9ZknIDJo7F3rZE=",
|
|
"publicKeyBase64": "A1eIndfaxgMpVwyD5dYe74DgjuIo5SwPZFCcLdOemjf"
|
|
}
|
|
]')
|
|
|
|
export CONTACTS_JSON
|
|
export TEST_MODE=${TEST_MODE:-print} # Use TEST_MODE from environment or default to print
|
|
|
|
# Run deep link tests in order
|
|
if [ "$ALL_TESTS" = true ]; then
|
|
./test-scripts/test-deeplinks.sh -t 5 -a
|
|
else
|
|
./test-scripts/test-deeplinks.sh -t 5
|
|
fi |