Repository navigation
Expand file tree
/
Copy pathtest_integration.sh
More file actions
executable file
·312 lines (257 loc) · 9.17 KB
/
Copy pathtest_integration.sh
File metadata and controls
executable file
·312 lines (257 loc) · 9.17 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
echo "=== TokenShield Integration Test Suite ==="
echo "Testing all flows..."
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
PASS=0
FAIL=0
# Test configuration
BASE_URL="http://localhost"
API_URL="http://localhost:8090"
ADMIN_SECRET="change-this-admin-secret"
TEST_CARDS=(
"4532015112830366" # Visa
"5425233430109903" # Mastercard
"378282246310005" # Amex
"6011111111111117" # Discover
)
# Helper functions
pass() {
echo -e "${GREEN}✓ PASS${NC}: $1"
((PASS++))
}
fail() {
echo -e "${RED}✗ FAIL${NC}: $1"
((FAIL++))
}
warn() {
echo -e "${YELLOW}⚠ WARN${NC}: $1"
}
info() {
echo -e "${BLUE}ℹ INFO${NC}: $1"
}
# Check prerequisites
check_prerequisites() {
echo "=== Prerequisites Check ==="
# Check if services are running
if ! curl -s $BASE_URL > /dev/null; then
fail "HAProxy not responding on $BASE_URL"
return 1
fi
if ! curl -s $API_URL/api/v1/health > /dev/null; then
fail "API server not responding on $API_URL"
return 1
fi
if ! docker ps | grep -q tokenshield; then
fail "TokenShield containers not running"
return 1
fi
pass "All services are running"
return 0
}
# Test tokenization flow (HAProxy -> HTTP Tokenizer -> App)
test_tokenization_flow() {
echo
echo "=== Testing Tokenization Flow ==="
for card in "${TEST_CARDS[@]}"; do
info "Testing tokenization with card: ${card:0:4}****${card: -4}"
response=$(curl -s -X POST $BASE_URL/api/checkout \
-H "Content-Type: application/json" \
-d "{\"card_holder\":\"Test User\",\"card_number\":\"$card\",\"expiry\":\"12/25\",\"cvv\":\"123\",\"amount\":100.50}")
# Check if response contains a token
if echo "$response" | jq -e '.token_used' | grep -q "tok_"; then
pass "Card $card was tokenized successfully"
else
fail "Card $card was not tokenized"
echo " Response: $response"
fi
# Verify payment went through
if echo "$response" | jq -e '.gateway_response.status' | grep -q "success"; then
pass "Payment processed successfully for card $card"
else
fail "Payment failed for card $card"
fi
done
}
# Test detokenization flow (Squid -> ICAP -> Payment Gateway)
test_detokenization_flow() {
echo
echo "=== Testing Detokenization Flow ==="
# First, make a payment to create a token
response=$(curl -s -X POST $BASE_URL/api/checkout \
-H "Content-Type: application/json" \
-d '{"card_holder":"Test User","card_number":"4532015112830366","expiry":"12/25","cvv":"123","amount":100.50}')
# Check if the payment gateway received correct card details
last_four=$(echo "$response" | jq -r '.gateway_response.card_last_four')
if [ "$last_four" = "0366" ]; then
pass "Payment gateway received detokenized card (last 4: $last_four)"
else
fail "Payment gateway did not receive correct detokenized card"
echo " Expected last 4: 0366, got: $last_four"
fi
}
# Test card storage and retrieval
test_card_storage() {
echo
echo "=== Testing Card Storage and Retrieval ==="
# Make test payment and capture the token
info "Making test payment to store a card"
checkout_response=$(curl -s -X POST $BASE_URL/api/checkout \
-H "Content-Type: application/json" \
-d '{"card_holder":"Test User","card_number":"4532015112830366","expiry":"12/25","cvv":"123","amount":50.00}')
# Extract the token from checkout response
token_used=$(echo "$checkout_response" | jq -r '.token_used')
# Check if card was stored
cards_response=$(curl -s $BASE_URL/api/cards)
card_count=$(echo "$cards_response" | jq '.cards | length')
if [ "$card_count" -gt 0 ]; then
pass "Card was stored successfully ($card_count cards found)"
# Find the most recent card by cardholder and verify it was detokenized
displayed_card=$(echo "$cards_response" | jq -r '.cards[] | select(.card_holder == "Test User") | {id, card_number}' | jq -sr 'sort_by(.id) | reverse | .[0].card_number')
if [ "$displayed_card" = "4532015112830366" ]; then
pass "Card was properly detokenized for display"
else
fail "Card was not detokenized for display: $displayed_card"
fi
else
fail "No cards were stored"
fi
}
# Test token formats
test_token_formats() {
echo
echo "=== Testing Token Formats ==="
# Test current token format (should be prefix)
response=$(curl -s -X POST $BASE_URL/api/checkout \
-H "Content-Type: application/json" \
-d '{"card_holder":"Test User","card_number":"5425233430109903","expiry":"12/25","cvv":"123","amount":25.00}')
token=$(echo "$response" | jq -r '.token_used')
if echo "$token" | grep -q "^tok_"; then
pass "Prefix token format working: ${token:0:20}..."
else
fail "Token format incorrect: $token"
fi
}
# Test API endpoints
test_api_endpoints() {
echo
echo "=== Testing API Endpoints ==="
# Test health endpoint
health_response=$(curl -s $API_URL/health)
if echo "$health_response" | jq -e '.status' | grep -q "healthy"; then
pass "Health endpoint working"
else
fail "Health endpoint not working"
fi
# Test version endpoint
version_response=$(curl -s $API_URL/api/v1/version)
if echo "$version_response" | jq -e '.version' > /dev/null; then
pass "Version endpoint working"
else
fail "Version endpoint not working"
fi
}
# Test card distributor integration
test_card_distributor() {
echo
echo "=== Testing Card Distributor Integration ==="
# Import cards from distributor
import_response=$(curl -s -X POST $BASE_URL/api/import-cards \
-H "Content-Type: application/json" \
-d '{"count": 2}')
if echo "$import_response" | jq -e '.status' | grep -q "success"; then
imported_count=$(echo "$import_response" | jq -r '.imported_count')
pass "Imported $imported_count cards from distributor"
# Verify cards are visible
cards_response=$(curl -s $BASE_URL/api/cards)
visible_count=$(echo "$cards_response" | jq '.cards | length')
if [ "$visible_count" -ge "$imported_count" ]; then
pass "Imported cards are visible ($visible_count total cards)"
else
fail "Imported cards not visible"
fi
else
fail "Card import failed"
echo " Response: $import_response"
fi
}
# Test error handling
test_error_handling() {
echo
echo "=== Testing Error Handling ==="
# Test invalid card number
response=$(curl -s -X POST $BASE_URL/api/checkout \
-H "Content-Type: application/json" \
-d '{"card_holder":"Test User","card_number":"invalid","expiry":"12/25","cvv":"123","amount":100.50}')
# Should still process (tokenizer doesn't validate card numbers)
if echo "$response" | jq -e '.status' > /dev/null; then
pass "Invalid card number handled gracefully"
else
fail "Invalid card number not handled properly"
fi
# Test malformed JSON
response=$(curl -s -X POST $BASE_URL/api/checkout \
-H "Content-Type: application/json" \
-d '{invalid json}')
# Should return error
if echo "$response" | grep -q "error\|400\|Bad Request"; then
pass "Malformed JSON handled properly"
else
warn "Malformed JSON handling could be improved"
fi
}
# Test performance
test_performance() {
echo
echo "=== Testing Performance ==="
info "Testing 10 concurrent tokenization requests..."
start_time=$(date +%s%N)
for i in {1..10}; do
(curl -s -X POST $BASE_URL/api/checkout \
-H "Content-Type: application/json" \
-d "{\"card_holder\":\"User$i\",\"card_number\":\"4532015112830366\",\"expiry\":\"12/25\",\"cvv\":\"123\",\"amount\":$((i * 10)).00}" > /dev/null) &
done
wait
end_time=$(date +%s%N)
duration=$(( (end_time - start_time) / 1000000 )) # Convert to milliseconds
if [ $duration -lt 5000 ]; then # Less than 5 seconds
pass "Performance test completed in ${duration}ms"
else
warn "Performance test took ${duration}ms (might be slow)"
fi
}
# Main test execution
main() {
echo "Starting TokenShield Integration Tests at $(date)"
echo
# Run all tests
check_prerequisites || exit 1
test_tokenization_flow
test_detokenization_flow
test_card_storage
test_token_formats
test_api_endpoints
test_card_distributor
test_error_handling
test_performance
echo
echo "=== Test Summary ==="
echo "Tests run: $((PASS + FAIL))"
echo -e "${GREEN}Passed: $PASS${NC}"
echo -e "${RED}Failed: $FAIL${NC}"
if [ $FAIL -eq 0 ]; then
echo -e "${GREEN}🎉 All tests passed!${NC}"
exit 0
else
echo -e "${RED}❌ $FAIL test(s) failed.${NC}"
exit 1
fi
}
# Run main function
main "$@"