-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.sh
208 lines (183 loc) · 5.18 KB
/
test.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
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
#!/bin/bash
set -e
set -u
#set -o pipefail
# Pre-requisites:
# - jq: https://webinstall.dev/jq
# - keypairs: https://webinstall.dev/jq
source .env
echo ''
echo 'Expecting hello'
curl -fsSL http://localhost:"${PORT}"/hello
echo ''
echo ''
echo 'Logout: Expecting success even when not logged in:'
curl -fsSL -X DELETE http://localhost:"${PORT}"/api/authn/session
echo ''
echo ''
echo 'User Credentials: Expecting cookies and id_token'
curl -fsSL -X POST http://localhost:"${PORT}"/api/authn/session \
-b cookies.jar -c cookies.jar \
-H "Content-Type: application/json" \
-d '{ "user": "coolaj86@gmail.com", "pass": "secret123" }'
echo ''
# Should give error
echo ''
echo 'No Cookies: Expecting Error:'
if ! curl -sSL -X POST http://localhost:"${PORT}"/api/authn/refresh |
grep 'SESSION_INVALID'; then
echo "Expected auth error"
exit 1
fi
echo ''
echo ''
echo 'Recent Cookies: Expecting New Token:'
my_id_token="$(
curl -fsSL -X POST http://localhost:"${PORT}"/api/authn/refresh \
-b cookies.jar -c cookies.jar |
jq -r '.id_token'
)"
echo "${my_id_token}"
echo ''
echo 'Exchange: Expecting new user access_token'
my_access_token="$(
curl -fsSL -X POST http://localhost:"${PORT}"/api/authn/exchange \
-H "Authorization: Bearer ${my_id_token}" \
-H "Content-Type: application/json" \
-d '{ "account_id": "Polo" }' |
jq -r '.access_token'
)"
#echo "'${my_access_token}'"
# TODO add issue for keypairs exiting cleanly on error
keypairs inspect "${my_access_token}" > /dev/null
echo ''
echo ''
echo 'Logout: Expecting success when logged in:'
curl -fsSL -X DELETE http://localhost:"${PORT}"/api/authn/session \
-b cookies.jar -c cookies.jar
echo ''
echo ''
echo 'Refresh: Expecting Error (logged out)'
if ! curl -sSL -X POST http://localhost:"${PORT}"/api/authn/refresh \
-b cookies.jar -c cookies.jar |
grep 'SESSION_INVALID'; then
echo "Expected auth error"
exit 1
fi
echo ''
if [[ -z ${GOOGLE_TEST_TOKEN:-} ]]; then
echo >&2 ''
echo >&2 '[SKIP] Google ID Token Test SKIPPED'
echo >&2 ''
else
echo ''
echo 'Expecting to exchange Google Token'
my_access_token="$(
curl -fsSL -X POST http://localhost:"${PORT}"/api/authn/session/oidc/google.com \
-H "Authorization: Bearer ${GOOGLE_TEST_TOKEN}" |
jq -r '.id_token'
)"
echo "'${my_access_token}'"
keypairs inspect "${my_access_token}" > /dev/null
echo ''
fi
echo ''
echo 'Use Token: Inspect'
curl -fsSL http://localhost:"${PORT}"/api/debug/inspect \
-H "Authorization: Bearer ${my_access_token}"
echo ''
echo ''
echo 'Use Token: List Dummies: Expecting Error'
if ! curl -sSL http://localhost:"${PORT}"/api/dummy \
-H "Authorization: Bearer ${my_access_token}" | grep 'Unauthorized'; then
echo "Expected auth error"
exit 1
fi
echo ''
echo ''
echo 'Exchange: Expecting new admin access_token'
my_access_token="$(
curl -fsSL -X POST http://localhost:"${PORT}"/api/authn/exchange \
-H "Authorization: Bearer ${my_id_token}" \
-H "Content-Type: application/json" \
-d '{ "account_id": "Marko" }' |
jq -r '.access_token'
)"
###################
# #
# Admin Dummies #
# #
###################
echo ''
echo 'Use Token: List Dummies'
curl -fsSL http://localhost:"${PORT}"/api/dummy \
-H "Authorization: Bearer ${my_access_token}"
echo ''
echo ''
echo 'Use Token: Add New Dummies'
echo '['
curl -fsSL -X POST http://localhost:"${PORT}"/api/dummy \
-H "Authorization: Bearer ${my_access_token}" \
-H "Content-Type: application/json" \
-d '{
"name": "AJ",
"age": "30"
}'
echo ','
my_result="$(
curl -fsSL -X POST http://localhost:"${PORT}"/api/dummy \
-H "Authorization: Bearer ${my_access_token}" \
-H "Content-Type: application/json" \
-d '{
"name": "Aggie",
"age": "24"
}'
)"
echo "${my_result}"
my_id="$(echo "${my_result}" | jq -r '.id')"
echo ']'
echo ''
echo 'Use Token: List Dummies'
curl -fsSL http://localhost:"${PORT}"/api/dummy \
-H "Authorization: Bearer ${my_access_token}"
echo ''
echo ''
echo "Use Token: Get Dummy ${my_id}"
curl -fsSL "http://localhost:${PORT}/api/dummy/${my_id}" \
-H "Authorization: Bearer ${my_access_token}"
echo ''
##################
# #
# User Dummies #
# #
##################
echo ''
echo 'Exchange: Expecting new user access_token'
my_access_token="$(
curl -fsSL -X POST http://localhost:"${PORT}"/api/authn/exchange \
-H "Authorization: Bearer ${my_id_token}" \
-H "Content-Type: application/json" \
-d '{ "account_id": "Polo" }' |
jq -r '.access_token'
)"
echo ''
echo ''
echo 'Expect Error: Add New Dummy'
if ! curl -sSL -X POST http://localhost:"${PORT}"/api/dummy \
-H "Authorization: Bearer ${my_access_token}" \
-H "Content-Type: application/json" \
-d '{
"name": "Ben",
"age": "35"
}' | grep Unauthorized; then
echo "Expected admin error"
exit 1
fi
echo ''
echo ''
echo "Use Token: Get Dummy ${my_id}"
curl -fsSL "http://localhost:${PORT}/api/dummy/${my_id}" \
-H "Authorization: Bearer ${my_access_token}"
echo ''
echo ''
echo 'PASS'