Browse Source
- Add .generated directory for test artifacts - Save key derivation data to key_derivation.json - Save account initialization to account_init.json - Save registration data to registration.json - Save claim details to claim_details.json - Save test environment to test-env.sh - Save contacts data to contacts.json - Add proper error handling for file operations - Improve deeplink test flow with JSON-based data - Add color output and better status messages - Add ADB device detection and fallback to print mode Technical Changes: - Add file system operations with proper error handling - Standardize JSON output format across Python/TypeScript - Update test flow to use generated JSON files - Add proper typing for registration response - Improve error reporting and debug output This improves the test workflow by saving all intermediate data as JSON files that can be used by other test scripts. The deeplink testing now uses this data instead of environment variables for better reliability.pull/127/head
3 changed files with 398 additions and 174 deletions
@ -1,175 +1,138 @@ |
|||
#!/bin/bash |
|||
|
|||
# File header documentation |
|||
# @file run-deeplink-tests.sh |
|||
# @brief Automated deeplink testing script using generated JSON files and ADB |
|||
# @author Matthew Raymer |
|||
# @date $(date +%Y-%m-%d) |
|||
|
|||
# Color definitions |
|||
BLUE='\033[0;34m' |
|||
GREEN='\033[0;32m' |
|||
RED='\033[0;31m' |
|||
YELLOW='\033[1;33m' |
|||
NC='\033[0m' |
|||
|
|||
# Parse command line arguments |
|||
GENERATE_ONLY=false |
|||
ALL_TESTS=false |
|||
while getopts "ga" opt; do |
|||
TIMEOUT=5 |
|||
TEST_MODE="execute" # Default to execute mode |
|||
|
|||
while getopts "t:p" opt; do |
|||
case $opt in |
|||
g) GENERATE_ONLY=true ;; |
|||
a) ALL_TESTS=true ;; |
|||
t) TIMEOUT=$OPTARG ;; |
|||
p) TEST_MODE="print" ;; |
|||
\?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; |
|||
esac |
|||
done |
|||
|
|||
# Directory for storing generated DIDs |
|||
mkdir -p .generated |
|||
# Verify .generated directory exists |
|||
if [ ! -d .generated ]; then |
|||
echo -e "${RED}Error: .generated directory not found${NC}" |
|||
echo "Please run the DID generation script first" |
|||
exit 1 |
|||
fi |
|||
|
|||
# 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 |
|||
# Function to read and validate JSON files |
|||
validate_json_files() { |
|||
local required_files=("test-env.sh" "claim_details.json" "contacts.json") |
|||
for file in "${required_files[@]}"; do |
|||
if [ ! -f ".generated/$file" ]; then |
|||
echo -e "${RED}Error: Missing required file .generated/$file${NC}" |
|||
return 1 |
|||
fi |
|||
done |
|||
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="$1" |
|||
|
|||
# Debug: Show what we received |
|||
echo "DEBUG: extract_did_info received: '$output'" >&2 |
|||
|
|||
# Parse and validate JSON |
|||
if ! printf '%s' "$output" | jq -e 'if type == "object" and .status == "success" then true else false end' >/dev/null 2>&1; then |
|||
echo "Error: DID generation failed" >&2 |
|||
echo "Error details:" >&2 |
|||
if ! printf '%s' "$output" | jq -e . >/dev/null 2>&1; then |
|||
echo "Invalid JSON output: $output" >&2 |
|||
# Function to URL encode string |
|||
urlencode() { |
|||
local string="${1}" |
|||
local strlen=${#string} |
|||
local encoded="" |
|||
local pos c o |
|||
|
|||
for (( pos=0 ; pos<strlen ; pos++ )); do |
|||
c=${string:$pos:1} |
|||
case "$c" in |
|||
[-_.~a-zA-Z0-9] ) o="${c}" ;; |
|||
* ) printf -v o '%%%02x' "'$c" |
|||
esac |
|||
encoded+="${o}" |
|||
done |
|||
echo "${encoded}" |
|||
} |
|||
|
|||
# Function to execute or print deeplink |
|||
execute_deeplink() { |
|||
local url="$1" |
|||
local description="$2" |
|||
local encoded_url=$(urlencode "$url") |
|||
|
|||
echo -e "\n${BLUE}Testing: $description${NC}" |
|||
|
|||
if [ "$TEST_MODE" = "print" ]; then |
|||
echo -e "${YELLOW}Deep Link URL:${NC}" |
|||
echo "$url" |
|||
echo -e "${YELLOW}Encoded URL:${NC}" |
|||
echo "$encoded_url" |
|||
echo "---" |
|||
else |
|||
printf '%s' "$output" | jq -r '"\(.stage): \(.error // "Unknown error")"' >&2 |
|||
fi |
|||
echo "URL: $url" |
|||
if adb shell am start -a android.intent.action.VIEW -d "$encoded_url"; then |
|||
echo -e "${GREEN}Success: Deeplink executed${NC}" |
|||
sleep "$TIMEOUT" |
|||
else |
|||
echo -e "${RED}Error: Failed to execute deeplink${NC}" |
|||
return 1 |
|||
fi |
|||
fi |
|||
} |
|||
|
|||
# Return the successful JSON |
|||
printf '%s' "$output" |
|||
} |
|||
|
|||
echo "Generating first contact DID..." |
|||
# Debug: Show raw command output |
|||
DEBUG_OUTPUT=$(DEBUG=0 ./test-scripts/did_generator.sh) |
|||
GEN_STATUS=$? |
|||
echo "DEBUG: Raw did_generator.sh output: '$DEBUG_OUTPUT'" >&2 |
|||
|
|||
if [ $GEN_STATUS -ne 0 ]; then |
|||
echo "Error: did_generator.sh failed with status $GEN_STATUS" >&2 |
|||
# Main test sequence |
|||
main() { |
|||
# Validate environment and files |
|||
if ! validate_json_files; then |
|||
exit 1 |
|||
fi |
|||
|
|||
CONTACT1_OUTPUT=$(printf '%s' "$DEBUG_OUTPUT" | tr -d '\r') |
|||
echo "DEBUG: After tr command: '$CONTACT1_OUTPUT'" >&2 |
|||
# Source environment variables |
|||
source .generated/test-env.sh |
|||
|
|||
# Debug: Show what we're passing to extract_did_info |
|||
echo "DEBUG: Calling extract_did_info with: '$CONTACT1_OUTPUT'" >&2 |
|||
CONTACT1_INFO=$(extract_did_info "$CONTACT1_OUTPUT") |
|||
EXTRACT_STATUS=$? |
|||
echo "DEBUG: extract_did_info returned status: $EXTRACT_STATUS" >&2 |
|||
echo "DEBUG: CONTACT1_INFO: '$CONTACT1_INFO'" >&2 |
|||
# Load JSON data |
|||
CLAIM_DETAILS=$(cat .generated/claim_details.json) |
|||
CONTACTS=$(cat .generated/contacts.json) |
|||
|
|||
if [ $EXTRACT_STATUS -ne 0 ]; then |
|||
echo "DEBUG: extract_did_info failed" >&2 |
|||
exit 1 |
|||
fi |
|||
CONTACT1_DID=$(printf '%s' "$CONTACT1_INFO" | jq -r .did) |
|||
CONTACT1_KEY=$(printf '%s' "$CONTACT1_INFO" | jq -r .privateKey) |
|||
|
|||
echo "Generating second contact DID (Jordan)..." |
|||
DEBUG_OUTPUT=$(DEBUG=0 ./test-scripts/did_generator.sh) |
|||
CONTACT2_OUTPUT=$(printf '%s' "$DEBUG_OUTPUT" | tr -d '\r') |
|||
CONTACT2_INFO=$(extract_did_info "$CONTACT2_OUTPUT") |
|||
if [ $? -ne 0 ]; then |
|||
exit 1 |
|||
fi |
|||
CONTACT2_DID=$(printf '%s' "$CONTACT2_INFO" | jq -r .did) |
|||
CONTACT2_KEY=$(printf '%s' "$CONTACT2_INFO" | jq -r .privateKey) |
|||
|
|||
echo "Generating issuer DID..." |
|||
DEBUG_OUTPUT=$(DEBUG=0 ./test-scripts/did_generator.sh) |
|||
ISSUER_OUTPUT=$(printf '%s' "$DEBUG_OUTPUT" | tr -d '\r') |
|||
ISSUER_INFO=$(extract_did_info "$ISSUER_OUTPUT") |
|||
if [ $? -ne 0 ]; then |
|||
exit 1 |
|||
fi |
|||
ISSUER_DID=$(printf '%s' "$ISSUER_INFO" | jq -r .did) |
|||
ISSUER_KEY=$(printf '%s' "$ISSUER_INFO" | jq -r .privateKey) |
|||
|
|||
# Add some visual feedback about the generated DIDs |
|||
echo |
|||
echo "Generated DIDs:" |
|||
echo " Contact 1: ${CONTACT1_DID:0:20}..." |
|||
echo " Contact 2: ${CONTACT2_DID:0:20}..." |
|||
echo " Issuer: ${ISSUER_DID:0:20}..." |
|||
echo |
|||
|
|||
# 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 |
|||
# 1. Claim-based deeplinks |
|||
execute_deeplink "timesafari://claim-cert/$(jq -r .claim_id <<< "$CLAIM_DETAILS")" \ |
|||
"Testing claim certificate view" |
|||
|
|||
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 |
|||
execute_deeplink "timesafari://claim-add-raw/$(jq -r .claim_id <<< "$CLAIM_DETAILS")" \ |
|||
"Testing raw claim addition" |
|||
|
|||
# 2. DID-based deeplinks |
|||
execute_deeplink "timesafari://did/$CONTACT1_DID" \ |
|||
"Testing DID view" |
|||
|
|||
execute_deeplink "timesafari://contact-edit/$CONTACT1_DID" \ |
|||
"Testing contact editing" |
|||
|
|||
# 3. JSON-based deeplinks |
|||
execute_deeplink "timesafari://contacts/import?contacts=$(jq -r @uri <<< "$CONTACTS")" \ |
|||
"Testing contacts import" |
|||
|
|||
# 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 |
|||
echo -e "\n${GREEN}All deeplink tests completed${NC}" |
|||
} |
|||
|
|||
# Check for adb if not in print mode |
|||
if [ "$TEST_MODE" = "execute" ]; then |
|||
if ! command -v adb >/dev/null 2>&1; then |
|||
echo -e "${YELLOW}Warning: adb not found, switching to print mode${NC}" |
|||
TEST_MODE="print" |
|||
elif [ -z "$(adb devices | grep -v List | grep device)" ]; then |
|||
echo -e "${YELLOW}Warning: no devices/emulators found, switching to print mode${NC}" |
|||
TEST_MODE="print" |
|||
fi |
|||
fi |
|||
|
|||
# Execute main test sequence |
|||
main |
Loading…
Reference in new issue