summaryrefslogtreecommitdiff
path: root/test/scripts/e2e_subs/rest.sh
blob: 4613a7ba9fafaa71a497fa14a59d1ae8925fd015 (plain)
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
# TIMEOUT=50

# Helpers for REST API tests.
# Use the following boilerplate code at the top of new REST tests:

#    #!/usr/bin/env bash
#    # TIMEOUT=300
#    
#    my_dir="$(dirname "$0")"
#    #"$my_dir/rest.sh" "$@"
#    source "$my_dir/rest.sh" "$@"
#    
#    date "+$0 start %Y%m%d_%H%M%S"

set -ex
set -o pipefail
export SHELLOPTS

WALLET=$1
gcmd="goal -w ${WALLET}"
ACCOUNT=$(${gcmd} account list|awk '{ print $3 }')

# REST Parameters
PUB_TOKEN=$(cat "$ALGORAND_DATA"/algod.token)
ADMIN_TOKEN=$(cat "$ALGORAND_DATA"/algod.admin.token)
NET=$(cat "$ALGORAND_DATA"/algod.net)

PRIMARY_NET=$(cat "$ALGORAND_DATA2"/algod.net)
PRIMARY_ADMIN_TOKEN=$(cat "$ALGORAND_DATA2"/algod.admin.token)


function base_call {
  curl -o "$3" -w "%{http_code}" -q -s -H "Authorization: Bearer $1" "$NET$2"
}


function base_post_call {
  curl -X POST --data-binary @/${TEMPDIR}/$4 -o "$3" -w "%{http_code}" -q -s -H "Authorization: Bearer $1" "$NET$2"
}


function base_delete_call {
  curl -X DELETE -o "$3" -w "%{http_code}" -q -s -H "Authorization: Bearer $1" "$NET$2"
}

function call_admin {
  base_call "$ADMIN_TOKEN" "$1" "$2"
}

function call_post_admin {
  base_post_call "$ADMIN_TOKEN" "$1" "$2" "$3"
}

function call_delete_admin {
  base_delete_call "$ADMIN_TOKEN" "$1" "$2" "$3"
}

function call {
  base_call "$PUB_TOKEN" "$1" "$2"
}

function call_post {
  base_post_call "$PUB_TOKEN" "$1" "$2"
}

function call_delete {
  base_delete_call "$PUB_TOKEN" "$1" "$2"
}


function fail_and_exit {
  printf "\n\nFailed test - $1 ($2): $3\n\n"
  exit 1
}

# $1 - test description.
# $2 - query
# $3 - expected status code
# $4 - the file to upload
# $5... - substring that should be in the response
function call_post_and_verify {
   local DESCRIPTION="$1"
   shift
   local QUERY="$1"
   shift
   local EXPECTED_CODE="$1"
   shift
   local FILENAME_TO_UPLOAD="$1"
   shift

   echo "MATCHING $@"
   curl_post_test "$DESCRIPTION" "$QUERY" "$EXPECTED_CODE" true "$FILENAME_TO_UPLOAD" "$@"
}


# CURL POST Test - POST query and verify results
# $1 - test description.
# $2 - query
# $3 - expected status code
# $4 - match result
# $5 - the file to upload
# $6... - substring(s) that should be in the response
function curl_post_test {
  local DESCRIPTION="$1"
  shift
  local QUERY="$1"
  shift
  local EXPECTED_CODE="$1"
  shift
  local MATCH_RESULT="$1"
  shift
  local FILENAME_TO_UPLOAD="$1"
  shift

  set +e
  local CODE
  if [[ "$USE_ADMIN" = true ]]; then
    CODE=$(call_post_admin "$QUERY" "${TEMPDIR}/curl_out.txt" "$FILENAME_TO_UPLOAD")
  else
    CODE=$(call_post "$QUERY" "${TEMPDIR}/curl_out.txt" "$FILENAME_TO_UPLOAD")
  fi

  verify $? "$CODE" "$DESCRIPTION" "$QUERY" "$EXPECTED_CODE" "$MATCH_RESULT" "$@"

}

# CURL Test - query and verify results
# $1 - test description.
# $2 - query
# $3 - expected status code
# $4 - match result
function call_delete_and_verify {
  local DESCRIPTION="$1"
  shift
  local QUERY="$1"
  shift
  local EXPECTED_CODE="$1"
  shift

  local MATCH_RESULT="$1"
  shift

  set +e

  local CODE
  if [[ "$USE_ADMIN" = true ]]; then
    CODE=$(call_delete_admin "$QUERY" "${TEMPDIR}/curl_out.txt")
  else
    CODE=$(call_delete "$QUERY" "${TEMPDIR}/curl_out.txt" )
  fi

  verify $? "$CODE" "$DESCRIPTION" "$QUERY" "$EXPECTED_CODE" "$MATCH_RESULT" "$@"

}


# CURL Test - query and verify results
# $1 - test description.
# $2 - query
# $3 - expected status code
# $4 - match result
# $5... - substring(s) that should be in the response
function curl_test {
  local DESCRIPTION="$1"
  shift
  local QUERY="$1"
  shift
  local EXPECTED_CODE="$1"
  shift
  local MATCH_RESULT="$1"
  shift

  set +e

  local CODE
  if [[ "$USE_ADMIN" = true ]]; then
    CODE=$(call_admin "$QUERY" "${TEMPDIR}/curl_out.txt")
  else
    CODE=$(call "$QUERY" "${TEMPDIR}/curl_out.txt" )
  fi

  verify $? "$CODE" "$DESCRIPTION" "$QUERY" "$EXPECTED_CODE" "$MATCH_RESULT" "$@"

}


# $1 - test description.
# $2 - query
# $3 - expected status code
# $4... - substring that should be in the response
function call_and_verify {
  local DESCRIPTION="$1"
  shift
  local QUERY="$1"
  shift
  local EXPECTED_CODE="$1"
  shift

  echo "MATCHING $@"
  curl_test "$DESCRIPTION" "$QUERY" "$EXPECTED_CODE" true "$@"
}

# CURL Test - query and verify results
# $1 - test description.
# $2 - query
# $3 - expected status code
# $4 - match result
# $5... - substring(s) that should be in the response
function curl_test {
  local DESCRIPTION="$1"
  shift
  local QUERY="$1"
  shift
  local EXPECTED_CODE="$1"
  shift
  local MATCH_RESULT="$1"
  shift

  set +e

  local CODE
  if [[ "$USE_ADMIN" = true ]]; then
    CODE=$(call_admin "$QUERY" "${TEMPDIR}/curl_out.txt")
  else
    CODE=$(call "$QUERY" "${TEMPDIR}/curl_out.txt" )
  fi

  if [[ $? != 0 ]]; then
    cat $CURL_TEMPFILE
    fail_and_exit "$DESCRIPTION" "$QUERY" "curl had a non-zero exit code."
  fi

  verify $? "$CODE" "$DESCRIPTION" "$QUERY" "$EXPECTED_CODE" "$MATCH_RESULT" "$@"

}

# verify - Common verification code
# $1 - return code of CURL sub-shell command
# $2 - HTTP status code
# $3 - description of test
# $4 - query to execute
# $5 - expected HTTP status code to check
# $6 - match result
# $7... - substring(s) that should be in the response
function verify {
  local SUCCESS=$1
  shift
  local CODE=$1
  shift
  local DESCRIPTION="$1"
  shift
  local QUERY="$1"
  shift
  local EXPECTED_CODE="$1"
  shift
  local MATCH_RESULT="$1"
  shift

  if [[ $SUCCESS != 0 ]]; then
    cat $CURL_TEMPFILE
    fail_and_exit "$DESCRIPTION" "$QUERY" "curl had a non-zero exit code."
  fi

  set -e

  RES=$(cat "${TEMPDIR}/curl_out.txt")
  if [[ "$CODE" != "$EXPECTED_CODE" ]]; then
    fail_and_exit "$DESCRIPTION" "$QUERY" "unexpected HTTP status code expected $EXPECTED_CODE (actual $CODE): $RES"
  fi

  local SUBSTRING

  # Check result substrings
  for SUBSTRING in "$@"; do
    echo "CHECKING '$SUBSTRING'"
    if [[ $MATCH_RESULT = true ]]; then
      if [[ "$RES" != *"$SUBSTRING"* ]]; then
        fail_and_exit "$DESCRIPTION" "$QUERY" "unexpected response. should contain '$SUBSTRING', actual: $RES"
      fi
    else
      if [[ "$RES" == *"$SUBSTRING"* ]]; then
        fail_and_exit "$DESCRIPTION" "$QUERY" "unexpected response. should NOT contain '$SUBSTRING', actual: $RES"
      fi
    fi
  done
}