-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.bash
367 lines (304 loc) · 9.65 KB
/
helpers.bash
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
REPO_ROOT=$(git rev-parse --show-toplevel)
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-${REPO_ROOT##*/}}"
CACHE_DIR=${BATS_TMPDIR:-tmp/bats}/galoy-bats-cache
mkdir -p "$CACHE_DIR"
OATHKEEPER_PROXY="http://localhost:4455"
MAILHOG_ENDPOINT="http://localhost:8025"
GQL_APP_ENDPOINT="${OATHKEEPER_PROXY}/app/graphql"
GQL_ADMIN_ENDPOINT="${OATHKEEPER_PROXY}/admin/graphql"
LANA_HOME="${LANA_HOME:-.lana}"
export LANA_CONFIG="${REPO_ROOT}/bats/lana-sim-time.yml"
SERVER_PID_FILE="${LANA_HOME}/server-pid"
LOG_FILE=".e2e-logs"
reset_pg() {
docker exec "${COMPOSE_PROJECT_NAME}-core-pg-1" psql $PG_CON -c "DROP SCHEMA public CASCADE"
docker exec "${COMPOSE_PROJECT_NAME}-core-pg-1" psql $PG_CON -c "CREATE SCHEMA public"
docker exec "${COMPOSE_PROJECT_NAME}-cala-pg-1" psql $PG_CON -c "DROP SCHEMA public CASCADE"
docker exec "${COMPOSE_PROJECT_NAME}-cala-pg-1" psql $PG_CON -c "CREATE SCHEMA public"
}
server_cmd() {
server_location="${REPO_ROOT}/target/debug/lana-cli"
if [[ ! -z ${CARGO_TARGET_DIR} ]]; then
server_location="${CARGO_TARGET_DIR}/debug/lana-cli"
fi
bash -c ${server_location} $@
}
start_server() {
# Check for running server
if [ -n "$BASH_VERSION" ]; then
server_process_and_status=$(
ps a | grep 'target/debug/lana-cli' | grep -v grep
echo ${PIPESTATUS[2]}
)
elif [ -n "$ZSH_VERSION" ]; then
server_process_and_status=$(
ps a | grep 'target/debug/lana-cli' | grep -v grep
echo ${pipestatus[3]}
)
else
echo "Unsupported shell."
exit 1
fi
exit_status=$(echo "$server_process_and_status" | tail -n 1)
if [ "$exit_status" -eq 0 ]; then
rm -f "$SERVER_PID_FILE"
return 0
fi
# Start server if not already running
background server_cmd > "$LOG_FILE" 2>&1
for i in {1..20}; do
if head "$LOG_FILE" | grep -q 'Starting graphql server on port'; then
break
elif head "$LOG_FILE" | grep -q 'Connection reset by peer'; then
stop_server
sleep 1
background server_cmd > "$LOG_FILE" 2>&1
else
sleep 1
fi
done
}
stop_server() {
if [[ -f "$SERVER_PID_FILE" ]]; then
kill -9 $(cat "$SERVER_PID_FILE") || true
fi
}
gql_query() {
cat "$(gql_file $1)" | tr '\n' ' ' | sed 's/"/\\"/g'
}
gql_file() {
echo "${REPO_ROOT}/bats/customer-gql/$1.gql"
}
gql_admin_query() {
cat "$(gql_admin_file $1)" | tr '\n' ' ' | sed 's/"/\\"/g'
}
gql_admin_file() {
echo "${REPO_ROOT}/bats/admin-gql/$1.gql"
}
graphql_output() {
echo $output | jq -r "$@"
}
login_customer() {
local email=$1
flowId=$(curl -s -X GET -H "Accept: application/json" "${OATHKEEPER_PROXY}/app/self-service/login/api" | jq -r '.id')
variables=$(jq -n --arg email "$email" '{ identifier: $email, method: "code" }' )
curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/app/self-service/login?flow=$flowId"
sleep 1
code=$(getEmailCode $email)
variables=$(jq -n --arg email "$email" --arg code "$code" '{ identifier: $email, method: "code", code: $code }' )
session=$(curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/app/self-service/login?flow=$flowId")
token=$(echo $session | jq -r '.session_token')
cache_value "$email" $token
}
exec_customer_graphql() {
local token_name=$1
local query_name=$2
local variables=${3:-"{}"}
AUTH_HEADER="Authorization: Bearer $(read_value "$token_name")"
if [[ "${BATS_TEST_DIRNAME}" != "" ]]; then
run_cmd="run"
else
run_cmd=""
fi
${run_cmd} curl -s \
-X POST \
${AUTH_HEADER:+ -H "$AUTH_HEADER"} \
-H "Content-Type: application/json" \
-d "{\"query\": \"$(gql_query $query_name)\", \"variables\": $variables}" \
"${GQL_APP_ENDPOINT}"
}
login_superadmin() {
local email="admin@galoy.io"
flowId=$(curl -s -X GET -H "Accept: application/json" "${OATHKEEPER_PROXY}/admin/self-service/login/api" | jq -r '.id')
variables=$(jq -n --arg email "$email" '{ identifier: $email, method: "code" }' )
curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/admin/self-service/login?flow=$flowId"
sleep 1
code=$(getEmailCode $email)
variables=$(jq -n --arg email "$email" --arg code "$code" '{ identifier: $email, method: "code", code: $code }' )
session=$(curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/admin/self-service/login?flow=$flowId")
token=$(echo $session | jq -r '.session_token')
cache_value "superadmin" $token
}
exec_admin_graphql() {
local query_name=$1
local variables=${2:-"{}"}
AUTH_HEADER="Authorization: Bearer $(read_value "superadmin")"
if [[ "${BATS_TEST_DIRNAME}" != "" ]]; then
run_cmd="run"
else
run_cmd=""
fi
${run_cmd} curl -s \
-X POST \
${AUTH_HEADER:+ -H "$AUTH_HEADER"} \
-H "Content-Type: application/json" \
-d "{\"query\": \"$(gql_admin_query $query_name)\", \"variables\": $variables}" \
"${GQL_ADMIN_ENDPOINT}"
}
exec_admin_graphql_upload() {
local query_name=$1
local variables=$2
local file_path=$3
local file_var_name=${4:-"file"}
AUTH_HEADER="Authorization: Bearer $(read_value "superadmin")"
curl -s -X POST \
${AUTH_HEADER:+ -H "$AUTH_HEADER"} \
-H "Content-Type: multipart/form-data" \
-F "operations={\"query\": \"$(gql_admin_query $query_name)\", \"variables\": $variables}" \
-F "map={\"0\":[\"variables.$file_var_name\"]}" \
-F "0=@$file_path" \
"${GQL_ADMIN_ENDPOINT}"
}
# Run the given command in the background. Useful for starting a
# node and then moving on with commands that exercise it for the
# test.
#
# Ensures that BATS' handling of file handles is taken into account;
# see
# https://github.com/bats-core/bats-core#printing-to-the-terminal
# https://github.com/sstephenson/bats/issues/80#issuecomment-174101686
# for details.
background() {
"$@" 3>- &
echo $!
}
# Taken from https://github.com/docker/swarm/blob/master/test/integration/helpers.bash
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
retry() {
local attempts=$1
shift
local delay=$1
shift
local i
for ((i = 0; i < attempts; i++)); do
run "$@"
if [[ "$status" -eq 0 ]]; then
return 0
fi
sleep "$delay"
done
echo "Command \"$*\" failed $attempts times. Output: $output"
false
}
random_uuid() {
if [[ -e /proc/sys/kernel/random/uuid ]]; then
cat /proc/sys/kernel/random/uuid
else
uuidgen
fi
}
cache_value() {
echo $2 >${CACHE_DIR}/$1
}
read_value() {
cat ${CACHE_DIR}/$1
}
cat_logs() {
cat "$LOG_FILE"
}
reset_log_files() {
for file in "$@"; do
rm "$file" &> /dev/null || true && touch "$file"
done
}
getEmailCode() {
local email="$1"
local emails=$(curl -s -X GET "${MAILHOG_ENDPOINT}/api/v2/search?kind=to&query=${email}")
if [[ $(echo "$emails" | jq '.total') -eq 0 ]]; then
echo "No message for email ${email}"
exit 1
fi
local email_content=$(echo "$emails" | jq '.items[0].MIME.Parts[0].Body' | tr -d '"')
local code=$(echo "$email_content" | grep -Eo '[0-9]{6}' | head -n1)
echo "$code"
}
generate_email() {
echo "user$(date +%s%N)@example.com" | tr '[:upper:]' '[:lower:]'
}
create_customer() {
customer_email=$(generate_email)
telegramId=$(generate_email)
variables=$(
jq -n \
--arg email "$customer_email" \
--arg telegramId "$telegramId" \
'{
input: {
email: $email,
telegramId: $telegramId
}
}'
)
exec_admin_graphql 'customer-create' "$variables"
customer_id=$(graphql_output .data.customerCreate.customer.customerId)
[[ "$customer_id" != "null" ]] || exit 1
echo $customer_id
}
add() {
sum=0
for num in "$@"; do
sum=$(echo "scale=2; $sum + $num" | bc)
done
echo $sum
}
sub() {
diff=$1
shift
for num in "$@"; do
diff=$(echo "scale=2; $diff - $num" | bc)
done
echo $diff
}
assert_balance_sheet_balanced() {
variables=$(
jq -n \
--arg from "$(from_utc)" \
'{ from: $from }'
)
exec_admin_graphql 'balance-sheet' "$variables"
echo $(graphql_output)
balance_usd=$(graphql_output '.data.balanceSheet.balance.usd.balancesByLayer.settled.netDebit')
balance=${balance_usd}
echo "Balance Sheet USD Balance (should be 0): $balance"
[[ "$balance" == "0" ]] || exit 1
debit_usd=$(graphql_output '.data.balanceSheet.balance.usd.balancesByLayer.settled.debit')
debit=${debit_usd}
echo "Balance Sheet USD Debit (should be >0): $debit"
[[ "$debit" -gt "0" ]] || exit 1
credit_usd=$(graphql_output '.data.balanceSheet.balance.usd.balancesByLayer.settled.credit')
credit=${credit_usd}
echo "Balance Sheet USD Credit (should be == debit): $credit"
[[ "$credit" == "$debit" ]] || exit 1
}
assert_trial_balance() {
variables=$(
jq -n \
--arg from "$(from_utc)" \
'{ from: $from }'
)
exec_admin_graphql 'trial-balance' "$variables"
echo $(graphql_output)
all_btc=$(graphql_output '.data.trialBalance.total.btc.balancesByLayer.all.netDebit')
echo "Trial Balance BTC (should be zero): $all_btc"
[[ "$all_btc" == "0" ]] || exit 1
all_usd=$(graphql_output '.data.trialBalance.total.usd.balancesByLayer.all.netDebit')
echo "Trial Balance USD (should be zero): $all_usd"
[[ "$all_usd" == "0" ]] || exit 1
}
assert_accounts_balanced() {
assert_balance_sheet_balanced
assert_trial_balance
}
net_usd_revenue() {
variables=$(
jq -n \
--arg from "$(from_utc)" \
'{ from: $from }'
)
exec_admin_graphql 'profit-and-loss' "$variables"
revenue_usd=$(graphql_output '.data.profitAndLossStatement.net.usd.balancesByLayer.all.netCredit')
echo $revenue_usd
}
from_utc() {
date -u -d @0 +"%Y-%m-%dT%H:%M:%S.%3NZ"
}