6 if test -z "${MH_OBJ_DIR}"; then
7 srcdir
=`dirname "$0"`/..
/..
8 MH_OBJ_DIR
=`cd "${srcdir}" && pwd`; export MH_OBJ_DIR
11 .
"${srcdir}/test/oauth/common.sh"
13 check_exit
'-eq 1' mhlogin
- # gcov
16 cat /dev
/null
> "${MHTMPDIR}/$$.expected-creds"
17 cat /dev
/null
> "${MHTMPDIR}/oauth-test"
18 chmod 600 "${MHTMPDIR}/oauth-test"
23 run_test
'eval echo code | mhlogin -saslmech xoauth2 -authservice test -user nobody@example.com' \
24 "Load the following URL in your browser and authorize nmh to access test:
26 http://127.0.0.1:${http_port}/oauth/auth?response_type=code&client_id=test-id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=test-scope
28 Enter the authorization code: $1"
34 test_mhlogin_invalid_response
() {
35 test_mhlogin
'mhlogin: error exchanging code for OAuth2 token
36 mhlogin: invalid response'
44 start_test
'mhlogin receives access and expiration'
48 fake_json_response
<<EOF
50 "access_token": "test-access",
51 "token_type": "Bearer",
57 access-nobody@example.com: test-access
58 expire-nobody@example.com:
64 start_test
'mhlogin receives access and refresh'
68 fake_json_response
<<EOF
70 "access_token": "test-access",
71 "token_type": "Bearer"
76 access-nobody@example.com: test-access
82 start_test
'mhlogin receives access, expiration, and refresh'
86 fake_json_response
<<EOF
88 "access_token": "test-access",
89 "refresh_token": "refresh-token",
91 "token_type": "Bearer"
96 access-nobody@example.com: test-access
97 refresh-nobody@example.com: refresh-token
98 expire-nobody@example.com:
104 start_test
'mhlogin receives refresh only'
106 expect_http_post_code
108 fake_json_response
<<EOF
110 "refresh_token": "refresh-token",
111 "token_type": "Bearer"
116 refresh-nobody@example.com: refresh-token
122 start_test
'mhlogin receives token_type only'
124 expect_http_post_code
126 fake_json_response
<<EOF
128 "token_type": "Bearer"
134 test_mhlogin_invalid_response
137 start_test
'mhlogin ignores extra bits in successful response JSON'
139 expect_http_post_code
141 fake_json_response
<<EOF
143 "access_token": "test-access",
144 "refresh_token": "refresh-token",
148 "c": [{}, {"foo": "bar"}]
152 "token_type": "Bearer"
157 access-nobody@example.com: test-access
158 refresh-nobody@example.com: refresh-token
159 expire-nobody@example.com:
165 start_test
'mhlogin multiple users'
167 expect_http_post_code
169 fake_json_response
<<EOF
171 "access_token": "user3-access",
172 "refresh_token": "user3-refresh",
174 "token_type": "Bearer"
179 access-nobody@example.com: user1-access
180 refresh-nobody@example.com: user1-refresh
181 expire-nobody@example.com:
182 access-nobody2@example.com: user2-access
183 refresh-nobody2@example.com: user2-refresh
184 expire-nobody2@example.com:
185 access-nobody3@example.com: user3-access
186 refresh-nobody3@example.com: user3-refresh
187 expire-nobody3@example.com:
191 access-nobody@example.com: user1-access
192 refresh-nobody@example.com: user1-refresh
193 expire-nobody@example.com: 100
194 access-nobody2@example.com: user2-access
195 refresh-nobody2@example.com: user2-refresh
196 expire-nobody2@example.com: 100
200 run_test
'eval echo code | mhlogin -saslmech xoauth2 -authservice test -user nobody3@example.com' \
201 "Load the following URL in your browser and authorize nmh to access test:
203 http://127.0.0.1:${http_port}/oauth/auth?response_type=code&client_id=test-id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=test-scope
205 Enter the authorization code: $1"
215 start_test
'mhlogin user enters bad code'
217 expect_http_post_code
219 fake_http_response
'400 Bad Request' <<EOF
220 Content-Type: application/json
223 "error": "invalid_grant"
229 test_mhlogin
'Code rejected; try again? '
232 start_test
'mhlogin response has no content-type'
234 expect_http_post_code
236 fake_http_response
'200 OK' <<EOF
239 "access_token": "test-access",
240 "token_type": "Bearer",
247 test_mhlogin_invalid_response
250 start_test
'mhlogin JSON array'
252 expect_http_post_code
254 fake_json_response
<<EOF
260 test_mhlogin_invalid_response
263 start_test
'mhlogin JSON empty object'
265 expect_http_post_code
267 fake_json_response
<<EOF
273 test_mhlogin_invalid_response
276 start_test
'mhlogin empty response body'
278 expect_http_post_code
280 fake_json_response
<<EOF
285 test_mhlogin_invalid_response
288 start_test
'mhlogin gets proper error from http'
290 expect_http_post_code
292 fake_http_response
'400 Bad Request' <<EOF
293 Content-Type: application/json
296 "error": "invalid_request"
302 test_mhlogin
'mhlogin: error exchanging code for OAuth2 token
303 mhlogin: bad OAuth request; re-run with -snoop and send REDACTED output to nmh-workers'
306 start_test
'mhlogin -browser'
308 #### Strip off the error string because it can vary, e.g.,
309 #### "Failed to connect to 127.0.0.1 port 64546: Connection refused", or
310 #### "Connection timed out after 1004 milliseconds"
311 echo code
| mhlogin
-saslmech xoauth2
-authservice test -user nobody@example.com \
312 -browser "echo \$@ > ${MHTMPDIR}/$$.browser" 2>&1 1>/dev
/null
| \
313 sed 's/\( endpoint:\) .*/\1/' > "${MHTMPDIR}/$$.mhlogin.err"
314 cat > "${MHTMPDIR}/$$.mhlogin.err.expected" <<EOF
315 mhlogin: error exchanging code for OAuth2 token
316 mhlogin: error making HTTP request to OAuth2 authorization endpoint:
318 check
"${MHTMPDIR}/$$.mhlogin.err" "${MHTMPDIR}/$$.mhlogin.err.expected"
320 cat > "${MHTMPDIR}/$$.browser.expected" <<EOF
321 http://127.0.0.1:${http_port}/oauth/auth?response_type=code&client_id=test-id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=test-scope
323 check
"${MHTMPDIR}/$$.browser" "${MHTMPDIR}/$$.browser.expected"
326 start_test
'empty authorization code'
327 run_test
'eval echo '' | mhlogin -saslmech xoauth2 -authservice test -user nobody@example.com' \
328 "Load the following URL in your browser and authorize nmh to access test:
330 http://127.0.0.1:${http_port}/oauth/auth?response_type=code&client_id=test-id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=test-scope
332 Enter the authorization code: Empty code; try again? "