summaryrefslogtreecommitdiff
path: root/test/scripts/e2e_subs/rest.sh
blob: fab6f1d515cdc16416cca4d797f919b6ba8e140f (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
#!/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 call_admin {
  base_call "$ADMIN_TOKEN" "$1" "$2"
}


function call {
  base_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... - 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 veryify 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
  local SUBSTRING

  local START=$SECONDS

  set +e
  local CODE=$(call "$QUERY" "${TEMPDIR}/curl_out.txt")
  if [[ $? != 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 ELAPSED=$(($SECONDS - $START))
  #if [[ $ELAPSED -gt $MAX_TIME ]]; then
  #  fail_and_exit "$DESCRIPTION" "$QUERY" "query duration too long, $ELAPSED > $MAX_TIME"
  #fi

  # 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
}