-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cases.sh
More file actions
executable file
·287 lines (240 loc) · 7.62 KB
/
Copy pathtest_cases.sh
File metadata and controls
executable file
·287 lines (240 loc) · 7.62 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
#!/usr/bin/env bash
set -euo pipefail
BASE_URL="${BASE_URL:-http://127.0.0.1:8000}"
EMAIL="${EMAIL:-admin@test.com}"
PASSWORD="${PASSWORD:-tonmotdepasse}"
# --- helpers ---------------------------------------------------------------
PASS_COUNT=0
FAIL_COUNT=0
hr() { echo "----------------------------------------------------------------------"; }
have_cmd() { command -v "$1" >/dev/null 2>&1; }
json_get() {
# json_get '<json>' 'key'
python3 - <<'PY' "$1" "$2"
import json,sys
data=json.loads(sys.argv[1])
key=sys.argv[2]
print(data.get(key, ""))
PY
}
pretty() {
# Pretty print JSON if possible, else raw.
python3 - <<'PY'
import sys, json
raw = sys.stdin.read()
raw = raw.strip()
if not raw:
sys.exit(0)
try:
obj=json.loads(raw)
print(json.dumps(obj, indent=2, ensure_ascii=False))
except Exception:
print(raw)
PY
}
request() {
# request METHOD PATH BODY(optional) AUTH(optional true/false)
local method="$1"
local path="$2"
local body="${3:-}"
local auth="${4:-true}"
local url="${BASE_URL}${path}"
local tmp_body
tmp_body="$(mktemp)"
local -a args
args=(-s -S -X "$method" "$url" -H "Content-Type: application/json")
if [[ "$auth" == "true" ]]; then
args+=(-H "Authorization: Bearer ${TOKEN:-}")
fi
if [[ -n "$body" ]]; then
args+=(-d "$body")
fi
# capture http code separately
local http_code
http_code="$(curl "${args[@]}" -o "$tmp_body" -w "%{http_code}")"
local resp
resp="$(cat "$tmp_body")"
rm -f "$tmp_body"
echo "$http_code" $'\n' "$resp"
}
run_test() {
# run_test "NAME" METHOD PATH BODY AUTH expected_http_regex
local name="$1"
local method="$2"
local path="$3"
local body="${4:-}"
local auth="${5:-true}"
local expect="${6:-^2}"
echo
echo "🧪 $name"
hr
local out http body_out
out="$(request "$method" "$path" "$body" "$auth")"
http="$(echo "$out" | head -n 1 | tr -d '\r')"
body_out="$(echo "$out" | tail -n +2)"
if [[ "$http" =~ $expect ]]; then
echo "✅ OK (HTTP $http)"
echo "$body_out" | pretty
PASS_COUNT=$((PASS_COUNT+1))
else
echo "❌ FAIL (HTTP $http) — attendu $expect"
echo "$body_out" | pretty
FAIL_COUNT=$((FAIL_COUNT+1))
fi
}
extract_json_field() {
# extract_json_field '<json>' 'path' (simple: top-level key only)
python3 - <<'PY' "$1" "$2"
import json,sys
data=json.loads(sys.argv[1])
key=sys.argv[2]
print(data.get(key, ""))
PY
}
extract_nested() {
# extract_nested '<json>' 'a.b.c' (best-effort)
python3 - <<'PY' "$1" "$2"
import json,sys
data=json.loads(sys.argv[1])
path=sys.argv[2].split(".")
cur=data
for p in path:
if isinstance(cur, dict) and p in cur:
cur=cur[p]
else:
print("")
sys.exit(0)
print(cur if not isinstance(cur,(dict,list)) else json.dumps(cur))
PY
}
must() {
if [[ -z "${1:-}" ]]; then
echo "❌ Erreur: variable vide: $2"
exit 1
fi
}
# --- start -----------------------------------------------------------------
echo "BASE_URL=$BASE_URL"
echo "EMAIL=$EMAIL"
echo
# 1) LOGIN
echo "🔐 Login..."
LOGIN_OUT="$(request "POST" "/auth/login" "{\"email\":\"$EMAIL\",\"password\":\"$PASSWORD\"}" "false")"
LOGIN_HTTP="$(echo "$LOGIN_OUT" | head -n 1 | tr -d '\r')"
LOGIN_BODY="$(echo "$LOGIN_OUT" | tail -n +2)"
if [[ ! "$LOGIN_HTTP" =~ ^2 ]]; then
echo "❌ Login failed (HTTP $LOGIN_HTTP)"
echo "$LOGIN_BODY" | pretty
exit 1
fi
TOKEN="$(python3 - <<'PY' "$LOGIN_BODY"
import json,sys
print(json.loads(sys.argv[1]).get("access_token",""))
PY
)"
must "$TOKEN" "TOKEN"
echo "✅ Login OK (HTTP $LOGIN_HTTP)"
echo "TOKEN obtenu."
# 2) Create KYB case
echo
echo "➕ Create KYB case..."
CREATE_KYB_OUT="$(request "POST" "/cases" '{"case_type":"KYB"}' "true")"
CREATE_KYB_HTTP="$(echo "$CREATE_KYB_OUT" | head -n 1 | tr -d '\r')"
CREATE_KYB_BODY="$(echo "$CREATE_KYB_OUT" | tail -n +2)"
if [[ ! "$CREATE_KYB_HTTP" =~ ^2 ]]; then
echo "❌ Create KYB case failed (HTTP $CREATE_KYB_HTTP)"
echo "$CREATE_KYB_BODY" | pretty
exit 1
fi
CASE_ID="$(python3 - <<'PY' "$CREATE_KYB_BODY"
import json,sys
print(json.loads(sys.argv[1]).get("id",""))
PY
)"
must "$CASE_ID" "CASE_ID"
echo "✅ KYB created: $CASE_ID"
# 3) Create KYC case (to get a person entity_id)
echo
echo "➕ Create KYC case..."
CREATE_KYC_OUT="$(request "POST" "/cases" '{"case_type":"KYC"}' "true")"
CREATE_KYC_HTTP="$(echo "$CREATE_KYC_OUT" | head -n 1 | tr -d '\r')"
CREATE_KYC_BODY="$(echo "$CREATE_KYC_OUT" | tail -n +2)"
if [[ ! "$CREATE_KYC_HTTP" =~ ^2 ]]; then
echo "❌ Create KYC case failed (HTTP $CREATE_KYC_HTTP)"
echo "$CREATE_KYC_BODY" | pretty
exit 1
fi
KYC_ID="$(python3 - <<'PY' "$CREATE_KYC_BODY"
import json,sys
print(json.loads(sys.argv[1]).get("id",""))
PY
)"
must "$KYC_ID" "KYC_ID"
echo "✅ KYC created: $KYC_ID"
# --- TESTS -----------------------------------------------------------------
# GET /cases list
run_test "List cases" "GET" "/cases" "" "true" "^2"
# GET /cases?status=DRAFT
run_test "List cases filtered by status=DRAFT" "GET" "/cases?status=DRAFT" "" "true" "^2"
# GET /cases?q=<id>
run_test "Search cases by q=<CASE_ID>" "GET" "/cases?q=${CASE_ID}" "" "true" "^2"
# GET /cases/{case_id} detail KYB
run_test "Get KYB case detail" "GET" "/cases/${CASE_ID}" "" "true" "^2"
# PATCH /cases/{case_id}
run_test "Update KYB case (PATCH status/risk/urgent)" "PATCH" "/cases/${CASE_ID}" \
'{"status":"ACTION_REQUIRED","urgent_flag":true,"urgent_reason":"Docs manquants","risk_level":"MEDIUM"}' "true" "^2"
# PUT /cases/{case_id}/company
run_test "Upsert company for KYB case" "PUT" "/cases/${CASE_ID}/company" \
'{"legal_name":"ACME SARL","registration_number":"SN-RCCM-2026-A-0001","country":"SN","incorporation_date":"2023-01-15","address_line1":"Dakar Plateau"}' "true" "^2"
# PUT /cases/{kyc_id}/person
run_test "Upsert person for KYC case" "PUT" "/cases/${KYC_ID}/person" \
'{"first_names":"Moussa","last_name":"Diop","nationality":"SN","dob":"1990-05-12"}' "true" "^2"
# Get KYC case detail to extract person.entity_id
echo
echo "🔎 Extract person_entity_id from KYC detail..."
KYC_DETAIL_OUT="$(request "GET" "/cases/${KYC_ID}" "" "true")"
KYC_DETAIL_HTTP="$(echo "$KYC_DETAIL_OUT" | head -n 1 | tr -d '\r')"
KYC_DETAIL_BODY="$(echo "$KYC_DETAIL_OUT" | tail -n +2)"
if [[ ! "$KYC_DETAIL_HTTP" =~ ^2 ]]; then
echo "❌ Get KYC detail failed (HTTP $KYC_DETAIL_HTTP)"
echo "$KYC_DETAIL_BODY" | pretty
else
PERSON_ENTITY_ID="$(python3 - <<'PY' "$KYC_DETAIL_BODY"
import json,sys
d=json.loads(sys.argv[1])
p=d.get("person") or {}
print(p.get("entity_id",""))
PY
)"
if [[ -z "$PERSON_ENTITY_ID" ]]; then
echo "⚠️ Impossible d'extraire person.entity_id depuis /cases/$KYC_ID"
echo "$KYC_DETAIL_BODY" | pretty
else
echo "✅ person_entity_id=$PERSON_ENTITY_ID"
fi
fi
# POST /cases/{case_id}/company/people (UBO)
if [[ -n "${PERSON_ENTITY_ID:-}" ]]; then
run_test "Add company person (UBO) to KYB case" "POST" "/cases/${CASE_ID}/company/people" \
"{\"person_entity_id\":\"${PERSON_ENTITY_ID}\",\"role_type\":\"UBO\",\"ownership_pct\":25}" "true" "^2"
else
echo
echo "🧪 Add company person (UBO) to KYB case"
hr
echo "❌ SKIP — person_entity_id manquant (voir étape extraction KYC detail)"
FAIL_COUNT=$((FAIL_COUNT+1))
fi
# Re-check KYB detail after company + people
run_test "Get KYB case detail (after updates)" "GET" "/cases/${CASE_ID}" "" "true" "^2"
# Negative test: call /cases without token should fail (expect 401/403)
run_test "NEGATIVE: Create case without token should fail" "POST" "/cases" '{"case_type":"KYB"}' "false" "^(401|403)$"
# --- summary ---------------------------------------------------------------
echo
hr
echo "✅ PASS: $PASS_COUNT"
echo "❌ FAIL: $FAIL_COUNT"
hr
if [[ "$FAIL_COUNT" -gt 0 ]]; then
exit 2
fi
echo "🎉 All tests passed!"