-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuser_service_flow.sh
More file actions
executable file
·175 lines (159 loc) · 5.79 KB
/
Copy pathuser_service_flow.sh
File metadata and controls
executable file
·175 lines (159 loc) · 5.79 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
#!/usr/bin/env bash
# User Service flow using curl based on postman/collection.microservices.json
# Endpoints covered:
# - POST /login (get JWT)
# - POST /users (create user)
# - POST /users/{id}/addresses (create default address)
# - GET /users/{id}/addresses (list addresses)
# - GET /users/{id} (get user)
# - DELETE /users/{id} (delete user)
#
# Requirements: curl, jq
#
# Env overrides (with sensible defaults):
# USER_BASE - default http://localhost:8082/api/v1
# USERNAME - default alice
# EMAIL - default alice@example.com
# PASSWORD - default p@ssw0rd
# ADDRESS_* vars - override address fields if needed
# SKIP_DELETE=1 - if set, skip deleting the created user
#
set -euo pipefail
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required" >&2; exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq is required (e.g. sudo apt-get install jq)" >&2; exit 1
fi
USER_BASE=${USER_BASE:-"http://localhost:8082/api/v1"}
USERNAME=${USERNAME:-"alice"}
EMAIL=${EMAIL:-"alice@example.com"}
PASSWORD=${PASSWORD:-"p@ssw0rd"}
# Address defaults (matches Postman example)
ADDRESS_LINE1=${ADDRESS_LINE1:-"1 Main St"}
ADDRESS_CITY=${ADDRESS_CITY:-"NYC"}
ADDRESS_STATE=${ADDRESS_STATE:-"NY"}
ADDRESS_POSTAL=${ADDRESS_POSTAL:-"10001"}
ADDRESS_COUNTRY=${ADDRESS_COUNTRY:-"US"}
ADDRESS_PHONE=${ADDRESS_PHONE:-"+1-555-0000"}
ADDRESS_IS_DEFAULT=${ADDRESS_IS_DEFAULT:-true}
# Slight randomness to avoid collisions when the backend enforces unique usernames/emails
RAND_SUFFIX=$(printf "%04d" $((RANDOM % 10000)))
RAND_EMAIL_TAG=$(date +%s)
USERNAME_UNIQ=${USERNAME}-${RAND_SUFFIX}
EMAIL_UNIQ=${EMAIL/@@/@} # noop if not placeholder
if [[ "$EMAIL" == *"@"* ]]; then
EMAIL_UNIQ="${EMAIL%%@*}+${RAND_EMAIL_TAG}@${EMAIL##*@}"
fi
SEP="__HTTP_STATUS__SEPARATOR__"
req() {
# Usage: req METHOD URL DATA_JSON [AUTH_TOKEN]
local method="$1" url="$2" data="${3:-}" token="${4:-}"
local headers=("-H" "Accept: application/json")
if [[ -n "$data" ]]; then
headers+=("-H" "Content-Type: application/json" "--data" "$data")
fi
if [[ -n "$token" ]]; then
headers+=("-H" "Authorization: Bearer $token")
fi
# -sS silent but show errors, include http code with delimiter
curl -sS -X "$method" "$url" "${headers[@]}" -w "${SEP}%{http_code}"
}
handle_response() {
# Split body and status using our delimiter
local resp="$1"
local http_code body
http_code="${resp##*${SEP}}"
body="${resp%${SEP}*}"
echo "$http_code" "$body"
}
say() { printf "\n==> %s\n" "$*"; }
say "User base: $USER_BASE"
# 1) Login to get JWT
say "Logging in to get JWT"
LOGIN_PAYLOAD=$(jq -n --arg u "$USERNAME" --arg p "$PASSWORD" '{username: $u, password: $p}')
resp=$(req POST "$USER_BASE/login" "$LOGIN_PAYLOAD")
read -r code body < <(handle_response "$resp")
echo "HTTP $code"
echo "$body" | jq '.' || true
if [[ "$code" != "200" && "$code" != "201" ]]; then
echo "Error: login failed (HTTP $code)" >&2; exit 1
fi
JWT=$(echo "$body" | jq -r '.token // empty')
if [[ -z "${JWT}" || "$JWT" == "null" ]]; then
echo "Error: token not found in login response" >&2; exit 1
fi
say "JWT acquired"
# 2) Create user (authorized)
say "Creating user: $USERNAME_UNIQ ($EMAIL_UNIQ)"
CREATE_USER_PAYLOAD=$(jq -n \
--arg u "$USERNAME_UNIQ" \
--arg e "$EMAIL_UNIQ" \
--arg p "$PASSWORD" \
'{username: $u, email: $e, password: $p}')
resp=$(req POST "$USER_BASE/users" "$CREATE_USER_PAYLOAD" "$JWT")
read -r code body < <(handle_response "$resp")
echo "HTTP $code"
echo "$body" | jq '.' || true
if [[ "$code" != "200" && "$code" != "201" ]]; then
echo "Warning: create user returned HTTP $code; continuing" >&2
fi
USER_ID=$(echo "$body" | jq -r '.id // empty')
if [[ -z "$USER_ID" || "$USER_ID" == "null" ]]; then
# Try to login again with the unique username to fetch a user profile if backend supports it
USER_ID=$(echo "$body" | jq -r '.user.id // empty')
fi
if [[ -z "$USER_ID" || "$USER_ID" == "null" ]]; then
echo "Warning: user id not returned; attempting to fetch via GET /users (may not be supported)" >&2
fi
# 3) Add default address if we have a user id
if [[ -n "${USER_ID:-}" ]]; then
say "Adding default address for user $USER_ID"
ADDR_PAYLOAD=$(jq -n \
--arg l1 "$ADDRESS_LINE1" \
--arg city "$ADDRESS_CITY" \
--arg st "$ADDRESS_STATE" \
--arg pc "$ADDRESS_POSTAL" \
--arg ctry "$ADDRESS_COUNTRY" \
--arg ph "$ADDRESS_PHONE" \
--argjson def "$ADDRESS_IS_DEFAULT" \
'{line1: $l1, city: $city, state: $st, postal_code: $pc, country: $ctry, phone: $ph, is_default: $def}')
resp=$(req POST "$USER_BASE/users/$USER_ID/addresses" "$ADDR_PAYLOAD" "$JWT")
read -r code body < <(handle_response "$resp")
echo "HTTP $code"
echo "$body" | jq '.' || true
ADDRESS_ID=$(echo "$body" | jq -r '.id // empty')
else
echo "Skipping address creation (no USER_ID)" >&2
fi
# 4) List addresses (if we have a user id)
if [[ -n "${USER_ID:-}" ]]; then
say "Listing addresses for user $USER_ID"
resp=$(req GET "$USER_BASE/users/$USER_ID/addresses" "" "$JWT")
read -r code body < <(handle_response "$resp")
echo "HTTP $code"
echo "$body" | jq '.' || true
fi
# 5) Get user (if we have a user id)
if [[ -n "${USER_ID:-}" ]]; then
say "Fetching user $USER_ID"
resp=$(req GET "$USER_BASE/users/$USER_ID" "" "$JWT")
read -r code body < <(handle_response "$resp")
echo "HTTP $code"
echo "$body" | jq '.' || true
fi
# 6) Optionally delete the user
if [[ -z "${SKIP_DELETE:-}" && -n "${USER_ID:-}" ]]; then
say "Deleting user $USER_ID (set SKIP_DELETE=1 to keep)"
resp=$(req DELETE "$USER_BASE/users/$USER_ID" "" "$JWT")
read -r code body < <(handle_response "$resp")
echo "HTTP $code"
if [[ "$code" == "200" || "$code" == "404" ]]; then
echo "Delete completed (ok or not found)"
else
echo "$body" | jq '.' || true
fi
else
say "Skip delete (SKIP_DELETE=1 or missing USER_ID)"
fi
say "Done."