-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.sh
More file actions
executable file
·135 lines (114 loc) · 3.9 KB
/
test_api.sh
File metadata and controls
executable file
·135 lines (114 loc) · 3.9 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
#!/bin/bash
# Simple test script to validate the API locally
# Usage: bash test_api.sh
# or: chmod +x test_api.sh && ./test_api.sh
set -e
BASE_URL="http://localhost:5000"
PASSED=0
FAILED=0
echo "Testing Transaction Validator API..."
echo "Base URL: $BASE_URL"
echo ""
# Helper function to make requests and check responses
test_endpoint() {
local method=$1
local endpoint=$2
local data=$3
local expected_status=$4
local test_name=$5
echo -n "Testing: $test_name ... "
if [ -z "$data" ]; then
response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
-H "Content-Type: application/json")
else
response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
-H "Content-Type: application/json" \
-d "$data")
fi
# Extract status code (last line) and body (everything else)
http_code=$(echo "$response" | tail -n 1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "$expected_status" ]; then
echo "[PASS] HTTP $http_code"
((PASSED++))
else
echo "[FAIL] Expected $expected_status, got $http_code"
echo "Response: $body"
((FAILED++))
fi
}
# Test 1: Health check
test_endpoint "GET" "/health" "" "200" "GET /health"
# Test 2: Valid transaction
valid_txn='{
"amount": 1234.56,
"transaction_type": "TRANSFER",
"account_id": 54321,
"timestamp": "2024-05-18T14:30:00Z",
"description": "Test transaction"
}'
test_endpoint "POST" "/validate/transaction" "$valid_txn" "200" "POST /validate/transaction (valid)"
# Test 3: Missing required field
missing_field='{
"amount": 100.00,
"transaction_type": "TRANSFER"
}'
test_endpoint "POST" "/validate/transaction" "$missing_field" "400" "POST /validate/transaction (missing field)"
# Test 4: Invalid amount (negative)
invalid_amount='{
"amount": -50.00,
"transaction_type": "TRANSFER",
"account_id": 123,
"timestamp": "2024-05-18T14:30:00Z"
}'
test_endpoint "POST" "/validate/transaction" "$invalid_amount" "400" "POST /validate/transaction (negative amount)"
# Test 5: Invalid transaction type
invalid_type='{
"amount": 100.00,
"transaction_type": "INVALID_TYPE",
"account_id": 123,
"timestamp": "2024-05-18T14:30:00Z"
}'
test_endpoint "POST" "/validate/transaction" "$invalid_type" "400" "POST /validate/transaction (invalid type)"
# Test 6: High-value transaction (should flag fraud signal)
high_value='{
"amount": 75000.00,
"transaction_type": "WITHDRAWAL",
"account_id": 99999,
"timestamp": "2024-05-18T14:30:00Z"
}'
test_endpoint "POST" "/validate/transaction" "$high_value" "200" "POST /validate/transaction (high-value, with fraud signal)"
# Test 7: Invalid timestamp format
invalid_ts='{
"amount": 100.00,
"transaction_type": "TRANSFER",
"account_id": 123,
"timestamp": "not-a-valid-timestamp"
}'
test_endpoint "POST" "/validate/transaction" "$invalid_ts" "400" "POST /validate/transaction (invalid timestamp)"
# Test 8: API docs
test_endpoint "GET" "/docs" "" "200" "GET /docs"
# Test 9: 404 error
test_endpoint "GET" "/nonexistent" "" "404" "GET /nonexistent (404 test)"
# Test 10: Wrong Content-Type (no header)
echo -n "Testing: POST without Content-Type ... "
response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/validate/transaction" \
-d '{"amount": 100}' 2>/dev/null | tail -n 1)
if [ "$response" = "415" ]; then
echo "[PASS] HTTP 415"
((PASSED++))
else
echo "[FAIL] Expected 415, got $response"
((FAILED++))
fi
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Test Results: $PASSED passed, $FAILED failed"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ $FAILED -eq 0 ]; then
echo "[SUCCESS] All tests passed"
exit 0
else
echo "[FAILURE] Some tests failed"
exit 1
fi