-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_tests.sh
More file actions
executable file
·287 lines (234 loc) · 8.46 KB
/
run_tests.sh
File metadata and controls
executable file
·287 lines (234 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
echo "=== TokenShield Test Suite Runner ==="
echo "Running all tests..."
echo
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Test results
TOTAL_TESTS=0
TOTAL_PASS=0
TOTAL_FAIL=0
# Helper functions
pass() {
echo -e "${GREEN}✓ PASS${NC}: $1"
((TOTAL_PASS++))
}
fail() {
echo -e "${RED}✗ FAIL${NC}: $1"
((TOTAL_FAIL++))
}
info() {
echo -e "${BLUE}ℹ INFO${NC}: $1"
}
# Check if system is running
check_system() {
echo "=== System Check ==="
if ! docker ps | grep -q tokenshield; then
echo -e "${RED}❌ TokenShield containers not running!${NC}"
echo "Please start the system first:"
echo " cd /path/to/TokenShield"
echo " docker-compose up -d"
exit 1
fi
# Wait for services to be ready
info "Waiting for services to be ready..."
sleep 5
if curl -s http://localhost > /dev/null && curl -s http://localhost:8090/api/v1/health > /dev/null; then
pass "System is running and ready"
else
fail "System is not responding properly"
exit 1
fi
}
# Run Go unit tests
run_unit_tests() {
echo
echo "=== Running Go Unit Tests ==="
cd unified-tokenizer
# Run tests with verbose output
if go test -v -timeout 30s ./... 2>&1 | tee test_results.log; then
unit_pass=$(grep -c "--- PASS:" test_results.log 2>/dev/null | head -n1)
unit_fail=$(grep -c "--- FAIL:" test_results.log 2>/dev/null | head -n1)
unit_skip=$(grep -c "--- SKIP:" test_results.log 2>/dev/null | head -n1)
# Set defaults if empty
unit_pass=${unit_pass:-0}
unit_fail=${unit_fail:-0}
unit_skip=${unit_skip:-0}
if [ "$unit_fail" -eq 0 ]; then
pass "All Go unit tests passed ($unit_pass passed, $unit_skip skipped)"
else
fail "$unit_fail Go unit tests failed"
fi
TOTAL_PASS=$((TOTAL_PASS + unit_pass))
TOTAL_FAIL=$((TOTAL_FAIL + unit_fail))
else
fail "Go unit tests failed to run"
TOTAL_FAIL=$((TOTAL_FAIL + 1))
fi
cd ..
}
# Run integration tests
run_integration_tests() {
echo
echo "=== Running Integration Tests ==="
if [ -f test_integration.sh ]; then
chmod +x test_integration.sh
if ./test_integration.sh; then
pass "Integration tests completed successfully"
else
fail "Integration tests failed"
fi
else
fail "Integration test script not found"
fi
}
# Test database consistency
test_database_consistency() {
echo
echo "=== Testing Database Consistency ==="
# Check if we can connect to database
if docker exec tokenshield-mysql mysql -u pciproxy -ppciproxy123 tokenshield -e "SELECT 1;" > /dev/null 2>&1; then
pass "Database connection working"
# Check table structure
tables=$(docker exec tokenshield-mysql mysql -u pciproxy -ppciproxy123 tokenshield -e "SHOW TABLES;" 2>/dev/null | grep -v Tables_in_tokenshield | wc -l)
if [ "$tables" -ge 8 ]; then
pass "Database schema complete ($tables tables)"
else
fail "Database schema incomplete ($tables tables)"
fi
# Check for any data integrity issues
card_count=$(docker exec tokenshield-mysql mysql -u pciproxy -ppciproxy123 tokenshield -e "SELECT COUNT(*) FROM credit_cards;" 2>/dev/null | grep -v COUNT || echo "0")
info "Found $card_count stored tokens"
else
fail "Cannot connect to database"
fi
}
# Test API functionality
test_api_functionality() {
echo
echo "=== Testing API Functionality ==="
info "Enabling test mode to bypass rate limiting"
# Get admin session (latest password)
admin_password=$(docker logs tokenshield-unified 2>&1 | grep "Password:" | tail -1 | awk '{print $NF}')
if [ -n "$admin_password" ]; then
info "Found admin password: ${admin_password:0:5}***"
# Create JSON payload with proper escaping
json_payload=$(jq -n --arg username "admin" --arg password "$admin_password" '{username: $username, password: $password}')
login_response=$(curl -s -X POST http://localhost:8090/api/v1/auth/login \
-H "Content-Type: application/json" \
-d "$json_payload")
info "Login response: $(echo "$login_response" | head -c 100)..."
session_id=$(echo "$login_response" | jq -r '.session_id // empty')
if [ -n "$session_id" ]; then
info "Successfully authenticated as admin"
# Test health endpoint (should work without auth)
if curl -s http://localhost:8090/health | grep -q "healthy"; then
pass "Health endpoint working"
else
fail "Health endpoint not working"
fi
# Test version endpoint
if curl -s http://localhost:8090/api/v1/version | grep -q "version"; then
pass "Version endpoint working"
else
fail "Version endpoint not working"
fi
# Test statistics endpoint with session
if curl -s -H "Authorization: Bearer $session_id" http://localhost:8090/api/v1/stats | grep -q "active_tokens"; then
pass "Statistics endpoint working"
else
fail "Statistics endpoint not working"
fi
else
fail "Could not authenticate for API tests"
fi
else
fail "Could not find admin password in logs"
fi
info "Disabling test mode"
}
# Test tokenization pipeline
test_tokenization_pipeline() {
echo
echo "=== Testing Complete Tokenization Pipeline ==="
# Test with a known card number
test_card="4532015112830366"
info "Testing end-to-end tokenization with card: ${test_card:0:4}****${test_card: -4}"
# Make a test payment
response=$(curl -s -X POST http://localhost/api/checkout \
-H "Content-Type: application/json" \
-d "{\"card_holder\":\"Test User\",\"card_number\":\"$test_card\",\"expiry\":\"12/25\",\"cvv\":\"123\",\"amount\":99.99}")
# Check if we got a token back
if echo "$response" | jq -e '.token_used' | grep -q "tok_"; then
token=$(echo "$response" | jq -r '.token_used')
pass "Tokenization successful: ${token:0:20}..."
# Check if payment was processed
if echo "$response" | jq -e '.gateway_response.status' | grep -q "success"; then
pass "Payment processing successful"
# Verify detokenization worked (payment gateway got real card)
last_four=$(echo "$response" | jq -r '.gateway_response.card_last_four')
if [ "$last_four" = "${test_card: -4}" ]; then
pass "Detokenization successful (last 4: $last_four)"
else
fail "Detokenization failed (expected: ${test_card: -4}, got: $last_four)"
fi
else
fail "Payment processing failed"
fi
else
fail "Tokenization failed"
echo "Response: $response"
fi
}
# Generate test report
generate_report() {
echo
echo "=== Test Report ==="
echo "Timestamp: $(date)"
echo "Total Tests: $((TOTAL_PASS + TOTAL_FAIL))"
echo -e "Passed: ${GREEN}$TOTAL_PASS${NC}"
echo -e "Failed: ${RED}$TOTAL_FAIL${NC}"
if [ $TOTAL_FAIL -eq 0 ]; then
echo -e "${GREEN}🎉 All tests passed!${NC}"
return 0
else
echo -e "${RED}❌ $TOTAL_FAIL test(s) failed.${NC}"
return 1
fi
}
# Save test results
save_results() {
cat > test_results.json << EOF
{
"timestamp": "$(date -Iseconds)",
"total_tests": $((TOTAL_PASS + TOTAL_FAIL)),
"passed": $TOTAL_PASS,
"failed": $TOTAL_FAIL,
"success": $([ $TOTAL_FAIL -eq 0 ] && echo "true" || echo "false"),
}
EOF
info "Test results saved to test_results.json"
}
# Main execution
main() {
echo "Starting comprehensive test suite at $(date)"
echo
# Run all test categories
check_system
test_database_consistency
test_api_functionality
test_tokenization_pipeline
run_unit_tests
run_integration_tests
# Generate final report
generate_report
save_results
# Return appropriate exit code
[ $TOTAL_FAIL -eq 0 ] && exit 0 || exit 1
}
# Run main function
main "$@"