-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.bash
208 lines (182 loc) · 4.04 KB
/
common.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
#!/bin/bash
load "../bats-assert/load.bash"
load "../bats-support/load.bash"
load "../bats-file/load.bash"
export readonly ROCKETCHAT_HOST=${ROCKETCHAT_HOST:-"bats.rocket.chat"}
readonly HOST="${ROCKETCHAT_URL:-http://localhost:3000}"
readonly EMAIL="dummy+$(date +%s)@nonexistent.email"
readonly REALNAME="Dummy User $(date +%s)"
readonly USERNAME="dummy.user.$(date +%s)"
readonly PASSWORD="dummypassword1234"
api() {
printf "$HOST/api/v1/%s" "${1?path required}"
}
curl() {
local host_args=()
if [[ -n "${ROCKETCHAT_HOST:-}" ]]; then
host_args=(-H "HOST: $ROCKETCHAT_HOST")
fi
command curl "${host_args[@]}" "$@"
}
wait_for_server() {
local max_attempts="${ROCKETCHAT_MAX_ATTEMPTS:-50}"
local counter=0
until curl --connect-timeout 5 --output /dev/null --silent --head --fail $HOST; do
((counter >= max_attempts)) && fail "timeout reached, couldn't connect to Rocket.Chat"
counter=$((counter + 1))
sleep 1
done
}
_json_array() {
# @description generate json arg array
# shellcheck disable=2206
local args=(${1//=/ }) # [0]=key [1]=values
local _IFS=$IFS
IFS=,
local members=()
for member in ${args[1]}; do
members+=("$member")
done
IFS=$_IFS
printf "%s=%s" "${args[0]}" "$(jo -a "${members[@]}")"
}
_is_simple_assignment() {
[[ $1 =~ .+=[^,]+$ ]]
}
_is_nested_assignment() {
[[ $1 =~ .+=$ ]]
}
_is_array_assignment() {
[[ $1 =~ , ]]
}
_build_json_from_args() {
local jo_args=()
while [[ -n "$1" ]]; do
if _is_simple_assignment "$1"; then
jo_args+=("$1")
shift
fi
if _is_array_assignment "$1"; then
jo_args+=("$(_json_array "$1")")
shift
fi
# more than two levels of nesting doesn't work with this
# because of shell quoting issues
# FIXME
if _is_nested_assignment "$1"; then
# shellcheck disable=2086
jo_args+=("${1}$(_build_json_from_args $2)")
shift 2
fi
done
#((${#jo_args[@]} == 1)) && echo "${jo_args[@]}" || jo "${jo_args[@]}"
jo "${jo_args[@]}"
}
_query() {
local query="?"
for param in "$@"; do
query+="${param}&"
done
echo "${query:0:$((${#query} - 1))}"
}
get() {
local curl_args=(
--silent
-H
"x-auth-token: $AUTH_TOKEN"
-H
"x-user-id: $USER_ID"
-H
"content-type: application/json"
-H
"Host: $ROCKETCHAT_HOST"
)
local endpoint
endpoint="$(api "${1?endpoint required}")"
shift
local query
query="$(_query "$@")"
curl_args+=(
"${endpoint}${query}"
)
run curl "${curl_args[@]}"
assert_success
assert_api_success
assert_equal "$?" 0
}
post() {
local curl_args=(
--silent
-H
"x-auth-token: $AUTH_TOKEN"
-H
"x-user-id: $USER_ID"
-H
"content-type: application/json"
-H
"Host: $ROCKETCHAT_HOST"
)
local endpoint
endpoint="$(api "${1?endpoint required}")"
curl_args+=("$endpoint")
shift
local body
((${#@} > 0)) && body="$(_build_json_from_args "$@")"
curl_args+=(
-d
"$body"
)
run curl "${curl_args[@]}"
assert_success
assert_api_success
assert_equal "$?" 0
}
register() {
echo "# username=\"$USERNAME\" email=\"$EMAIL\" pass=\"$PASSWORD\" name=\"$REALNAME\"" >&3
post users.register username="$USERNAME" email="$EMAIL" pass="$PASSWORD" name="$REALNAME"
}
login() {
post login user="$USERNAME" password="$PASSWORD"
export AUTH_TOKEN="$(jq .data.authToken -r <<<"$output")"
export USER_ID="$(jq .data.userId -r <<<"$output")"
}
logout() {
post logout
}
_is_number() {
[[ "$1" =~ ^[0-9]+$ ]]
}
_needs_square_brackets() {
! [[ "$1" =~ ^[a-zA-Z_-]+$ ]]
}
assert_field_equal() {
local to_check="${@: -1}"
local check_string=
set -- "${@:1:$(($# - 1))}"
for arg in "$@"; do
if _is_number "$arg"; then
check_string+="[$arg]"
continue
fi
if _needs_square_brackets "$arg"; then
check_string+="[\"$arg\"]"
continue
fi
check_string+=".${arg}"
done
assert_equal "$(jq "${check_string}" -r <<<"$output")" "$to_check"
}
assert_field_equal_raw() {
assert_equal "$(jq "${1}" -r <<<"$output")" "$2"
}
assert_api_success() {
assert [ "$(jq .success -r <<<"$output")" = 'true' -o "$(jq .status -r <<<"$output")" = 'success' ]
}
run_and_assert_success() {
run "$@"
assert_success
}
run_and_assert_failure() {
run "$@"
assert_failure
}