1+ #! /bin/bash
2+
3+ # Test script for telegram_notify.sh
4+ # Tests the validation logic and message formatting
5+
6+ set -e
7+
8+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
9+ TELEGRAM_SCRIPT=" ${SCRIPT_DIR} /../scripts/telegram_notify.sh"
10+
11+ echo " Testing Telegram notification script..."
12+
13+ # Test 1: No arguments
14+ echo " Test 1: No arguments"
15+ if $TELEGRAM_SCRIPT 2> /dev/null; then
16+ echo " ❌ FAILED: Should have failed with no arguments"
17+ exit 1
18+ else
19+ echo " ✅ PASSED: Correctly failed with no arguments"
20+ fi
21+
22+ # Test 2: Invalid version format
23+ echo " Test 2: Invalid version format"
24+ if $TELEGRAM_SCRIPT " 0.2.51" " https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2> /dev/null; then
25+ echo " ❌ FAILED: Should have failed with invalid version format"
26+ exit 1
27+ else
28+ echo " ✅ PASSED: Correctly failed with invalid version format"
29+ fi
30+
31+ # Test 3: Invalid URL format
32+ echo " Test 3: Invalid URL format"
33+ if $TELEGRAM_SCRIPT " v0.2.51" " https://invalid-url.com" 2> /dev/null; then
34+ echo " ❌ FAILED: Should have failed with invalid URL format"
35+ exit 1
36+ else
37+ echo " ✅ PASSED: Correctly failed with invalid URL format"
38+ fi
39+
40+ # Test 4: Valid arguments but missing environment variables
41+ echo " Test 4: Valid arguments but missing environment variables"
42+ if $TELEGRAM_SCRIPT " v0.2.51" " https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2> /dev/null; then
43+ echo " ❌ FAILED: Should have failed with missing environment variables"
44+ exit 1
45+ else
46+ echo " ✅ PASSED: Correctly failed with missing environment variables"
47+ fi
48+
49+ # Test 5: Valid arguments with mock environment variables (dry run)
50+ echo " Test 5: Valid arguments with test environment variables"
51+ export TELEGRAM_BOT_TOKEN=" test_token_123456"
52+ export TELEGRAM_CHAT_ID=" test_chat_id"
53+
54+ # Create a mock curl command that doesn't actually send anything
55+ create_mock_curl () {
56+ cat > /tmp/mock_curl.sh << 'EOF '
57+ #!/bin/bash
58+ # Mock curl command for testing
59+ if [[ "$*" == *"sendMessage"* ]]; then
60+ echo "Mock: Telegram message would be sent"
61+ exit 0
62+ fi
63+ exec /usr/bin/curl "$@"
64+ EOF
65+ chmod +x /tmp/mock_curl.sh
66+ }
67+
68+ # Test the script with a dry run by temporarily replacing curl
69+ create_mock_curl
70+ export PATH=" /tmp:$PATH "
71+
72+ # Temporarily modify the telegram script to use our mock curl
73+ sed ' s|curl -s|/tmp/mock_curl.sh -s|g' $TELEGRAM_SCRIPT > /tmp/telegram_notify_test.sh
74+ chmod +x /tmp/telegram_notify_test.sh
75+
76+ if /tmp/telegram_notify_test.sh " v0.2.51" " https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2> /dev/null; then
77+ echo " ✅ PASSED: Script executed successfully with valid arguments"
78+ else
79+ echo " ❌ FAILED: Script should have succeeded with valid arguments and environment variables"
80+ exit 1
81+ fi
82+
83+ # Clean up
84+ rm -f /tmp/mock_curl.sh /tmp/telegram_notify_test.sh
85+ unset TELEGRAM_BOT_TOKEN TELEGRAM_CHAT_ID
86+
87+ echo " "
88+ echo " 🎉 All tests passed! The Telegram notification script is working correctly."
0 commit comments