forked from ratnakar-asara/Fabric_SampleWebApp
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_rest_api.sh
executable file
·79 lines (55 loc) · 3.75 KB
/
test_rest_api.sh
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
#!/bin/bash -x
# This causes this script to exit with error if any command within this script fails with error.
# https://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs
set -e
TEMP_DIR=test_rest_api_temporary_files
ACTUAL_OUTPUT=${TEMP_DIR}/actual
EXPECTED_OUTPUT=${TEMP_DIR}/expected
PROTOCOL=http
rm ${TEMP_DIR} -rf
mkdir -p ${TEMP_DIR}
function set_expected_output {
output=$1
echo -n "${output}" > ${EXPECTED_OUTPUT}
}
function post {
url=$1
curl -X POST "${url}" > ${ACTUAL_OUTPUT}
}
function get {
url=$1
curl -X GET "${url}" > ${ACTUAL_OUTPUT}
}
function check_results {
diff ${ACTUAL_OUTPUT} ${EXPECTED_OUTPUT}
}
function post_and_check_results {
url=$1
output=$2
set_expected_output "${output}"
post "${url}"
check_results
echo -e "\n\n\n"
}
function get_and_check_results {
url=$1
output=$2
set_expected_output "${output}"
get "${url}"
check_results
}
# TODO: Make a more complete sequence of tests, testing all transactions, transaction permissions checks, and transaction errors.
get_and_check_results "${PROTOCOL}://localhost:3000/query_balance?invoking_user_name=Admin&account_name=Alice" '{"message":"channel.sendTransactionProposal failed; error(s): chaincode error (status: 500, message: Could not query_balance for account \"Alice\"; error was Could not retrieve account named \"Alice\"; error was GetTableRow failed because row with keys [Alice] does not exist); chaincode error (status: 500, message: Could not query_balance for account \"Alice\"; error was Could not retrieve account named \"Alice\"; error was GetTableRow failed because row with keys [Alice] does not exist); "}'
get_and_check_results "${PROTOCOL}://localhost:3000/query_balance?invoking_user_name=Admin&account_name=Bob" '{"message":"channel.sendTransactionProposal failed; error(s): chaincode error (status: 500, message: Could not query_balance for account \"Bob\"; error was Could not retrieve account named \"Bob\"; error was GetTableRow failed because row with keys [Bob] does not exist); chaincode error (status: 500, message: Could not query_balance for account \"Bob\"; error was Could not retrieve account named \"Bob\"; error was GetTableRow failed because row with keys [Bob] does not exist); "}'
post_and_check_results "${PROTOCOL}://localhost:3000/create_account?invoking_user_name=Admin&account_name=Bob&initial_balance=123" '{"status":"VALID"}'
get_and_check_results "${PROTOCOL}://localhost:3000/query_balance?invoking_user_name=Admin&account_name=Bob" '{"Name":"Bob","Balance":123}'
post_and_check_results "${PROTOCOL}://localhost:3000/create_account?invoking_user_name=Admin&account_name=Alice&initial_balance=456" '{"status":"VALID"}'
get_and_check_results "${PROTOCOL}://localhost:3000/query_balance?invoking_user_name=Admin&account_name=Alice" '{"Name":"Alice","Balance":456}'
post_and_check_results "${PROTOCOL}://localhost:3000/create_account?invoking_user_name=Admin&account_name=Alice&initial_balance=789" '{"message":"Error registering or enrolling \"Alice\""}'
get_and_check_results "${PROTOCOL}://localhost:3000/query_balance?invoking_user_name=Admin&account_name=Alice" '{"Name":"Alice","Balance":456}'
post_and_check_results "${PROTOCOL}://localhost:3000/transfer?invoking_user_name=Admin&from_account_name=Alice&to_account_name=Bob&amount=400" '{"status":"VALID"}'
get_and_check_results "${PROTOCOL}://localhost:3000/query_balance?invoking_user_name=Admin&account_name=Alice" '{"Name":"Alice","Balance":56}'
get_and_check_results "${PROTOCOL}://localhost:3000/query_balance?invoking_user_name=Admin&account_name=Bob" '{"Name":"Bob","Balance":523}'
rm ${TEMP_DIR} -rf
echo "....................................."
echo "all transactions behaved as expected."