|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Set default API URL from argument if provided |
| 4 | +export API_URL=${1:-"http://localhost:8787"} |
| 5 | + |
| 6 | +source "$(dirname "$0")/utils.sh" |
| 7 | + |
| 8 | +test_chainhooks() { |
| 9 | + echo "====================" |
| 10 | + echo "ChainhooksDO Tests" |
| 11 | + echo "====================" |
| 12 | + |
| 13 | + # Test base endpoint |
| 14 | + test_cors "/chainhooks" "Base endpoint CORS" |
| 15 | + test_endpoint "/chainhooks" 200 "Base endpoint" |
| 16 | + |
| 17 | + # Test events endpoint (GET all events) |
| 18 | + test_cors "/chainhooks/events" "Events endpoint CORS" |
| 19 | + test_endpoint "/chainhooks/events" 200 "Get all events" |
| 20 | + |
| 21 | + # Test post-event endpoint without auth (should fail with 401) |
| 22 | + echo "Testing post-event without auth (should fail)..." |
| 23 | + local post_url="${API_URL}/chainhooks/post-event" |
| 24 | + local payload='{"test":"data"}' |
| 25 | + |
| 26 | + # Test CORS for post-event endpoint |
| 27 | + test_cors "/chainhooks/post-event" "Post event CORS" |
| 28 | + |
| 29 | + # Test unauthorized post (should return 401) |
| 30 | + local unauth_response=$(curl -s -X POST -H "Content-Type: application/json" -d "$payload" "$post_url") |
| 31 | + local unauth_status=$(echo "$unauth_response" | jq -r '.success // false') |
| 32 | + |
| 33 | + if [ "$unauth_status" == "false" ]; then |
| 34 | + echo -e "${GREEN}✓${NC} Unauthorized post correctly rejected" |
| 35 | + ((TOTAL_TESTS++)) |
| 36 | + else |
| 37 | + echo -e "${RED}✗${NC} Unauthorized post should have been rejected: $unauth_response" |
| 38 | + ((TOTAL_TESTS++)) |
| 39 | + ((FAILED_TESTS++)) |
| 40 | + fi |
| 41 | + |
| 42 | + # Test post-event with auth token (if available in environment) |
| 43 | + if [ -n "$CHAINHOOKS_AUTH_TOKEN" ]; then |
| 44 | + echo "Testing post-event with auth..." |
| 45 | + local auth_response=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $CHAINHOOKS_AUTH_TOKEN" -d "$payload" "$post_url") |
| 46 | + local auth_status=$(echo "$auth_response" | jq -r '.success // false') |
| 47 | + local event_id=$(echo "$auth_response" | jq -r '.data.eventId // ""') |
| 48 | + |
| 49 | + if [ "$auth_status" == "true" ] && [ -n "$event_id" ]; then |
| 50 | + echo -e "${GREEN}✓${NC} Authorized post successful" |
| 51 | + ((TOTAL_TESTS++)) |
| 52 | + |
| 53 | + # Now test retrieving the specific event we just created |
| 54 | + echo "Testing get specific event..." |
| 55 | + test_endpoint "/chainhooks/events/$event_id" 200 "Get specific event" |
| 56 | + else |
| 57 | + echo -e "${RED}✗${NC} Authorized post failed: $auth_response" |
| 58 | + ((TOTAL_TESTS++)) |
| 59 | + ((FAILED_TESTS++)) |
| 60 | + fi |
| 61 | + else |
| 62 | + echo "Skipping authorized post test (CHAINHOOKS_AUTH_TOKEN not set)" |
| 63 | + fi |
| 64 | + |
| 65 | + # Test invalid event ID |
| 66 | + test_endpoint "/chainhooks/events/invalid-id" 404 "Invalid event ID" |
| 67 | + |
| 68 | + # Test invalid endpoints |
| 69 | + test_cors "/chainhooks/invalid" "Invalid endpoint CORS" |
| 70 | + test_endpoint "/chainhooks/invalid" 404 "Invalid endpoint" |
| 71 | +} |
| 72 | + |
| 73 | +# Allow running just this test file |
| 74 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 75 | + export FAILED_TESTS=0 |
| 76 | + export TOTAL_TESTS=0 |
| 77 | + |
| 78 | + echo -e "\nTesting Chainhooks API at: $API_URL" |
| 79 | + test_chainhooks |
| 80 | + |
| 81 | + echo "====================" |
| 82 | + echo "Test Summary" |
| 83 | + echo "====================" |
| 84 | + echo "Passed tests: $((TOTAL_TESTS - FAILED_TESTS))" |
| 85 | + echo "Failed tests: $FAILED_TESTS" |
| 86 | + echo "Total tests: $TOTAL_TESTS" |
| 87 | + |
| 88 | + if [ $FAILED_TESTS -eq 0 ]; then |
| 89 | + echo -e "${GREEN}All tests passed!${NC}" |
| 90 | + exit 0 |
| 91 | + else |
| 92 | + echo -e "${RED}Some tests failed!${NC}" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +fi |
0 commit comments