v0.3.11 - Working on admin api
This commit is contained in:
@@ -20,19 +20,18 @@ set -e # Exit on any error
|
||||
# CONFIGURATION
|
||||
# =======================================================================
|
||||
|
||||
# Test mode credentials (provided by user)
|
||||
# Test mode credentials (from current relay startup)
|
||||
ADMIN_PRIVKEY="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
ADMIN_PUBKEY="6a04ab98d9e4774ad806e302dddeb63bea16b5cb5f223ee77478e861bb583eb3"
|
||||
RELAY_PUBKEY="4f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa"
|
||||
|
||||
# Server configuration
|
||||
RELAY_HOST="localhost"
|
||||
RELAY_HOST="127.0.0.1"
|
||||
RELAY_PORT="8888"
|
||||
RELAY_URL="ws://${RELAY_HOST}:${RELAY_PORT}"
|
||||
|
||||
# Test configuration
|
||||
TIMEOUT=5
|
||||
LOG_FILE="whitelist_blacklist_test.log"
|
||||
TEMP_DIR="/tmp/c_relay_test_$$"
|
||||
|
||||
# Color codes for output
|
||||
@@ -53,23 +52,23 @@ TESTS_FAILED=0
|
||||
# =======================================================================
|
||||
|
||||
log() {
|
||||
echo -e "${BLUE}[$(date '+%H:%M:%S')]${RESET} $1" | tee -a "$LOG_FILE"
|
||||
echo -e "${BLUE}[$(date '+%H:%M:%S')]${RESET} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${RESET} $1" | tee -a "$LOG_FILE"
|
||||
echo -e "${GREEN}[SUCCESS]${RESET} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${RESET} $1" | tee -a "$LOG_FILE"
|
||||
echo -e "${RED}[ERROR]${RESET} $1"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${RESET} $1" | tee -a "$LOG_FILE"
|
||||
echo -e "${YELLOW}[WARNING]${RESET} $1"
|
||||
}
|
||||
|
||||
log_info() {
|
||||
echo -e "${BLUE}[INFO]${RESET} $1" | tee -a "$LOG_FILE"
|
||||
echo -e "${BLUE}[INFO]${RESET} $1"
|
||||
}
|
||||
|
||||
increment_test() {
|
||||
@@ -79,11 +78,15 @@ increment_test() {
|
||||
pass_test() {
|
||||
TESTS_PASSED=$((TESTS_PASSED + 1))
|
||||
log_success "Test $TESTS_RUN: PASSED - $1"
|
||||
echo ""
|
||||
echo ""
|
||||
}
|
||||
|
||||
fail_test() {
|
||||
TESTS_FAILED=$((TESTS_FAILED + 1))
|
||||
log_error "Test $TESTS_RUN: FAILED - $1"
|
||||
echo ""
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Generate test keypairs
|
||||
@@ -123,14 +126,21 @@ send_websocket_message() {
|
||||
local expected_response="$2"
|
||||
local timeout="${3:-$TIMEOUT}"
|
||||
|
||||
log_info "Sending WebSocket message: ${message:0:100}..."
|
||||
|
||||
# Use wscat to send message and capture response
|
||||
# Use websocat to send message and capture response (following pattern from tests/1_nip_test.sh)
|
||||
local response=""
|
||||
if command -v wscat &> /dev/null; then
|
||||
response=$(echo "$message" | timeout "$timeout" wscat -c "$RELAY_URL" 2>/dev/null | head -1)
|
||||
if command -v websocat &> /dev/null; then
|
||||
# Capture output from websocat (following working pattern from 1_nip_test.sh)
|
||||
response=$(echo "$message" | timeout "$timeout" websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
|
||||
# Check if connection failed
|
||||
if [[ "$response" == *"Connection failed"* ]]; then
|
||||
log_error "Failed to connect to relay"
|
||||
return 1
|
||||
fi
|
||||
|
||||
else
|
||||
log_error "wscat not found - required for WebSocket testing"
|
||||
log_error "websocat not found - required for WebSocket testing"
|
||||
log_error "Please install websocat for WebSocket communication"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -147,13 +157,12 @@ send_auth_rule_event() {
|
||||
|
||||
log_info "Creating auth rule event: $action $rule_type $pattern_type ${pattern_value:0:16}..."
|
||||
|
||||
# Create the auth rule event using nak - match the working NIP-42 pattern
|
||||
# Create the auth rule event using nak with correct tag format
|
||||
# Server expects proper key=value tags for auth rules
|
||||
# Using Kind 23456 (ephemeral auth rules management) - no d tag needed
|
||||
local event_json
|
||||
event_json=$(nak event -k 33335 --content "{\"action\":\"$action\",\"description\":\"$description\"}" \
|
||||
-t "d=$RELAY_PUBKEY" \
|
||||
-t "$rule_type=$pattern_type" \
|
||||
-t "pattern=$pattern_value" \
|
||||
-t "action=$action" \
|
||||
event_json=$(nak event -k 23456 --content "{\"action\":\"$action\",\"description\":\"$description\"}" \
|
||||
-t "$rule_type=$pattern_type" -t "pattern_value=$pattern_value" \
|
||||
--sec "$ADMIN_PRIVKEY" 2>/dev/null)
|
||||
|
||||
if [ $? -ne 0 ] || [ -z "$event_json" ]; then
|
||||
@@ -161,7 +170,7 @@ send_auth_rule_event() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Send the event using nak directly to relay (more reliable than wscat)
|
||||
# Send the event using nak directly to relay (more reliable than websocat)
|
||||
log_info "Publishing auth rule event to relay..."
|
||||
local result
|
||||
result=$(echo "$event_json" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
@@ -179,6 +188,40 @@ send_auth_rule_event() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Clear all auth rules using the new system command functionality
|
||||
clear_all_auth_rules() {
|
||||
log_info "Clearing all existing auth rules..."
|
||||
|
||||
# Create system command event to clear all auth rules
|
||||
# Using Kind 23456 (ephemeral auth rules management)
|
||||
local event_json
|
||||
event_json=$(nak event -k 23456 --content "{\"action\":\"clear_all\"}" \
|
||||
-t "system_command=clear_all_auth_rules" \
|
||||
--sec "$ADMIN_PRIVKEY" 2>/dev/null)
|
||||
|
||||
if [ $? -ne 0 ] || [ -z "$event_json" ]; then
|
||||
log_error "Failed to create clear auth rules event with nak"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Send the event using nak directly to relay
|
||||
log_info "Sending clear all auth rules command..."
|
||||
local result
|
||||
result=$(echo "$event_json" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
local exit_code=$?
|
||||
|
||||
log_info "Clear auth rules result: $result"
|
||||
|
||||
# Check if response indicates success
|
||||
if [ $exit_code -eq 0 ] && echo "$result" | grep -q -i "success\|OK.*true\|published"; then
|
||||
log_success "All auth rules cleared successfully"
|
||||
return 0
|
||||
else
|
||||
log_error "Failed to clear auth rules: $result (exit code: $exit_code)"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Test event publishing with a specific key
|
||||
test_event_publishing() {
|
||||
local test_privkey="$1"
|
||||
@@ -198,7 +241,7 @@ test_event_publishing() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Send the event using nak directly (more reliable than wscat)
|
||||
# Send the event using nak directly (more reliable than websocat)
|
||||
log_info "Publishing test event to relay..."
|
||||
local result
|
||||
result=$(echo "$test_event" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
@@ -236,9 +279,6 @@ setup_test_environment() {
|
||||
# Create temporary directory
|
||||
mkdir -p "$TEMP_DIR"
|
||||
|
||||
# Clear log file
|
||||
echo "=== C-Relay Whitelist/Blacklist Test Started at $(date) ===" > "$LOG_FILE"
|
||||
|
||||
# Check if required tools are available - like NIP-42 test
|
||||
log_info "Checking dependencies..."
|
||||
|
||||
@@ -258,9 +298,10 @@ setup_test_environment() {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v wscat &> /dev/null; then
|
||||
log_warning "wscat not found. Some WebSocket tests may be limited"
|
||||
log_warning "Install with: npm install -g wscat"
|
||||
if ! command -v websocat &> /dev/null; then
|
||||
log_error "websocat not found - required for WebSocket testing"
|
||||
log_error "Please install websocat for WebSocket communication"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log_success "Dependencies check complete"
|
||||
@@ -283,10 +324,10 @@ test_admin_authentication() {
|
||||
log "Test $TESTS_RUN: Admin Authentication"
|
||||
|
||||
# Create a simple configuration event to test admin authentication
|
||||
local content="Testing admin authentication"
|
||||
# Using Kind 23455 (ephemeral configuration management) - no d tag needed
|
||||
local content="{\"action\":\"set\",\"description\":\"Testing admin authentication\"}"
|
||||
local config_event
|
||||
config_event=$(nak event -k 33334 --content "$content" \
|
||||
-t "d=$RELAY_PUBKEY" \
|
||||
config_event=$(nak event -k 23455 --content "$content" \
|
||||
-t "test_auth=true" \
|
||||
--sec "$ADMIN_PRIVKEY" 2>/dev/null)
|
||||
|
||||
@@ -295,25 +336,11 @@ test_admin_authentication() {
|
||||
return
|
||||
fi
|
||||
|
||||
# DEBUG: Print the full event that will be sent
|
||||
log_info "=== DEBUG: Full admin event being sent ==="
|
||||
echo "$config_event" | jq . 2>/dev/null || echo "$config_event"
|
||||
log_info "=== END DEBUG EVENT ==="
|
||||
|
||||
# Send admin event
|
||||
local message="[\"EVENT\",$config_event]"
|
||||
log_info "=== DEBUG: Full WebSocket message ==="
|
||||
echo "$message"
|
||||
log_info "=== END DEBUG MESSAGE ==="
|
||||
|
||||
local response
|
||||
response=$(send_websocket_message "$message" "OK" 10)
|
||||
|
||||
# DEBUG: Print the full response from server
|
||||
log_info "=== DEBUG: Full server response ==="
|
||||
echo "$response"
|
||||
log_info "=== END DEBUG RESPONSE ==="
|
||||
|
||||
if echo "$response" | grep -q '"OK".*true'; then
|
||||
pass_test "Admin authentication successful"
|
||||
else
|
||||
@@ -321,11 +348,65 @@ test_admin_authentication() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 2: Basic Whitelist Functionality
|
||||
# Test 2: Auth Rules Storage and Query Test
|
||||
test_auth_rules_storage_query() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Auth Rules Storage and Query Test"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Add a simple blacklist rule
|
||||
log_info "Adding test blacklist rule..."
|
||||
if send_auth_rule_event "add" "blacklist" "pubkey" "$TEST1_PUBKEY" "Test storage blacklist entry"; then
|
||||
log_success "Auth rule added successfully"
|
||||
|
||||
# Wait a moment for rule to be processed
|
||||
sleep 1
|
||||
|
||||
# Query all auth rules using admin query
|
||||
log_info "Querying all auth rules..."
|
||||
local query_event
|
||||
query_event=$(nak event -k 23456 --content "{\"action\":\"list_all\"}" \
|
||||
-t "auth_query=list_all" \
|
||||
--sec "$ADMIN_PRIVKEY" 2>/dev/null)
|
||||
|
||||
if [ $? -ne 0 ] || [ -z "$query_event" ]; then
|
||||
fail_test "Failed to create auth query event"
|
||||
return
|
||||
fi
|
||||
|
||||
# Send the query event
|
||||
log_info "Sending auth query to relay..."
|
||||
local query_result
|
||||
query_result=$(echo "$query_event" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
local exit_code=$?
|
||||
|
||||
log_info "Auth query result: $query_result"
|
||||
|
||||
# Check if we got a response and if it contains our test rule
|
||||
if [ $exit_code -eq 0 ]; then
|
||||
if echo "$query_result" | grep -q "$TEST1_PUBKEY"; then
|
||||
pass_test "Auth rule storage and query working - found test rule in query results"
|
||||
else
|
||||
fail_test "Auth rule not found in query results - rule may not have been stored"
|
||||
fi
|
||||
else
|
||||
fail_test "Auth query failed: $query_result"
|
||||
fi
|
||||
else
|
||||
fail_test "Failed to add auth rule for storage test"
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 3: Basic Whitelist Functionality
|
||||
test_basic_whitelist() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Basic Whitelist Functionality"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Add TEST1 pubkey to whitelist
|
||||
if send_auth_rule_event "add" "whitelist" "pubkey" "$TEST1_PUBKEY" "Test whitelist entry"; then
|
||||
# Test that whitelisted pubkey can publish
|
||||
@@ -339,11 +420,14 @@ test_basic_whitelist() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 3: Basic Blacklist Functionality
|
||||
# Test 4: Basic Blacklist Functionality
|
||||
test_basic_blacklist() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Basic Blacklist Functionality"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Add TEST2 pubkey to blacklist
|
||||
if send_auth_rule_event "add" "blacklist" "pubkey" "$TEST2_PUBKEY" "Test blacklist entry"; then
|
||||
# Test that blacklisted pubkey cannot publish
|
||||
@@ -357,11 +441,20 @@ test_basic_blacklist() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 4: Rule Removal
|
||||
# Test 5: Rule Removal
|
||||
test_rule_removal() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Rule Removal"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# First add TEST2 to blacklist to test removal
|
||||
if ! send_auth_rule_event "add" "blacklist" "pubkey" "$TEST2_PUBKEY" "Test blacklist for removal"; then
|
||||
fail_test "Failed to add pubkey to blacklist for removal test"
|
||||
return
|
||||
fi
|
||||
|
||||
# Remove TEST2 from blacklist
|
||||
if send_auth_rule_event "remove" "blacklist" "pubkey" "$TEST2_PUBKEY" "Remove test blacklist entry"; then
|
||||
# Test that previously blacklisted pubkey can now publish
|
||||
@@ -375,11 +468,14 @@ test_rule_removal() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 5: Multiple Users Scenario
|
||||
# Test 6: Multiple Users Scenario
|
||||
test_multiple_users() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Multiple Users Scenario"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Add TEST1 to whitelist and TEST3 to blacklist
|
||||
local success_count=0
|
||||
|
||||
@@ -408,11 +504,14 @@ test_multiple_users() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 6: Priority Testing (Blacklist vs Whitelist)
|
||||
# Test 7: Priority Testing (Blacklist vs Whitelist)
|
||||
test_priority_rules() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Priority Rules Testing"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Add same pubkey to both whitelist and blacklist
|
||||
local setup_success=0
|
||||
|
||||
@@ -438,11 +537,14 @@ test_priority_rules() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 7: Hash-based Blacklist
|
||||
# Test 8: Hash-based Blacklist
|
||||
test_hash_blacklist() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Hash-based Blacklist"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Create a test event to get its hash
|
||||
local test_content="Content to be blacklisted by hash"
|
||||
local test_event
|
||||
@@ -482,11 +584,14 @@ test_hash_blacklist() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 8: WebSocket Connection Behavior
|
||||
# Test 9: WebSocket Connection Behavior
|
||||
test_websocket_behavior() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: WebSocket Connection Behavior"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Test that the WebSocket connection handles multiple rapid requests
|
||||
local rapid_success_count=0
|
||||
|
||||
@@ -516,11 +621,14 @@ test_websocket_behavior() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 9: Rule Persistence Verification
|
||||
# Test 10: Rule Persistence Verification
|
||||
test_rule_persistence() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Rule Persistence Verification"
|
||||
|
||||
# Clear all existing rules to start fresh
|
||||
clear_all_auth_rules
|
||||
|
||||
# Add a rule, then verify it persists by testing enforcement
|
||||
if send_auth_rule_event "add" "blacklist" "pubkey" "$TEST3_PUBKEY" "Persistence test blacklist"; then
|
||||
# Wait a moment for rule to be processed
|
||||
@@ -546,7 +654,7 @@ test_rule_persistence() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Test 10: Cleanup and Final Verification
|
||||
# Test 11: Cleanup and Final Verification
|
||||
test_cleanup_verification() {
|
||||
increment_test
|
||||
log "Test $TESTS_RUN: Cleanup and Final Verification"
|
||||
@@ -589,10 +697,11 @@ run_all_tests() {
|
||||
# Setup
|
||||
setup_test_environment
|
||||
|
||||
# Run only test 1 for debugging admin authentication
|
||||
test_admin_authentication
|
||||
# Clear all auth rules before starting tests
|
||||
clear_all_auth_rules
|
||||
|
||||
# Comment out other tests for now to focus on debugging
|
||||
# test_admin_authentication
|
||||
# test_auth_rules_storage_query
|
||||
# test_basic_whitelist
|
||||
# test_basic_blacklist
|
||||
# test_rule_removal
|
||||
@@ -648,8 +757,8 @@ main() {
|
||||
echo -e "${BLUE}===============================================${RESET}"
|
||||
echo ""
|
||||
|
||||
# Check if relay is running - use the same method we verified manually
|
||||
if ! echo '["REQ","connection_test",{}]' | timeout 5 wscat -c "$RELAY_URL" >/dev/null 2>&1; then
|
||||
# Check if relay is running - using websocat like the working tests
|
||||
if ! echo '["REQ","connection_test",{}]' | timeout 5 websocat "$RELAY_URL" >/dev/null 2>&1; then
|
||||
log_error "Cannot connect to relay at $RELAY_URL"
|
||||
log_error "Please ensure the C-Relay server is running in test mode"
|
||||
exit 1
|
||||
@@ -661,12 +770,10 @@ main() {
|
||||
if run_all_tests; then
|
||||
echo ""
|
||||
log_success "All whitelist/blacklist tests completed successfully!"
|
||||
echo -e "Test log saved to: ${YELLOW}$LOG_FILE${RESET}"
|
||||
exit 0
|
||||
else
|
||||
echo ""
|
||||
log_error "Some tests failed. Check the log for details."
|
||||
echo -e "Test log saved to: ${YELLOW}$LOG_FILE${RESET}"
|
||||
log_error "Some tests failed."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user