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"
14 cat /dev
/null
> "${MHTMPDIR}/$$.expected-creds"
15 cat /dev
/null
> "${MHTMPDIR}/oauth-test"
16 chmod 600 "${MHTMPDIR}/oauth-test"
21 run_test
'eval echo code | mhlogin -saslmech xoauth2 -authservice test -user nobody@example.com' \
22 "Load the following URL in your browser and authorize nmh to access test:
24 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
26 Enter the authorization code: $1"
32 test_mhlogin_invalid_response
() {
33 test_mhlogin
'mhlogin: error exchanging code for OAuth2 token
34 mhlogin: invalid response'
42 start_test
'mhlogin receives access and expiration'
46 fake_json_response
<<EOF
48 "access_token": "test-access",
49 "token_type": "Bearer",
55 access-nobody@example.com: test-access
56 expire-nobody@example.com:
62 start_test
'mhlogin receives access and refresh'
66 fake_json_response
<<EOF
68 "access_token": "test-access",
69 "token_type": "Bearer"
74 access-nobody@example.com: test-access
80 start_test
'mhlogin receives access, expiration, and refresh'
84 fake_json_response
<<EOF
86 "access_token": "test-access",
87 "refresh_token": "refresh-token",
89 "token_type": "Bearer"
94 access-nobody@example.com: test-access
95 refresh-nobody@example.com: refresh-token
96 expire-nobody@example.com:
102 start_test
'mhlogin receives refresh only'
104 expect_http_post_code
106 fake_json_response
<<EOF
108 "refresh_token": "refresh-token",
109 "token_type": "Bearer"
114 refresh-nobody@example.com: refresh-token
120 start_test
'mhlogin receives token_type only'
122 expect_http_post_code
124 fake_json_response
<<EOF
126 "token_type": "Bearer"
132 test_mhlogin_invalid_response
135 start_test
'mhlogin ignores extra bits in successful response JSON'
137 expect_http_post_code
139 fake_json_response
<<EOF
141 "access_token": "test-access",
142 "refresh_token": "refresh-token",
146 "c": [{}, {"foo": "bar"}]
150 "token_type": "Bearer"
155 access-nobody@example.com: test-access
156 refresh-nobody@example.com: refresh-token
157 expire-nobody@example.com:
163 start_test
'mhlogin multiple users'
165 expect_http_post_code
167 fake_json_response
<<EOF
169 "access_token": "user3-access",
170 "refresh_token": "user3-refresh",
172 "token_type": "Bearer"
177 access-nobody@example.com: user1-access
178 refresh-nobody@example.com: user1-refresh
179 expire-nobody@example.com:
180 access-nobody2@example.com: user2-access
181 refresh-nobody2@example.com: user2-refresh
182 expire-nobody2@example.com:
183 access-nobody3@example.com: user3-access
184 refresh-nobody3@example.com: user3-refresh
185 expire-nobody3@example.com:
189 access-nobody@example.com: user1-access
190 refresh-nobody@example.com: user1-refresh
191 expire-nobody@example.com: 100
192 access-nobody2@example.com: user2-access
193 refresh-nobody2@example.com: user2-refresh
194 expire-nobody2@example.com: 100
198 run_test
'eval echo code | mhlogin -saslmech xoauth2 -authservice test -user nobody3@example.com' \
199 "Load the following URL in your browser and authorize nmh to access test:
201 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
203 Enter the authorization code: $1"
213 start_test
'mhlogin user enters bad code'
215 expect_http_post_code
217 fake_http_response
'400 Bad Request' <<EOF
218 Content-Type: application/json
221 "error": "invalid_grant"
227 test_mhlogin
'Code rejected; try again? '
230 start_test
'mhlogin response has no content-type'
232 expect_http_post_code
234 fake_http_response
'200 OK' <<EOF
237 "access_token": "test-access",
238 "token_type": "Bearer",
245 test_mhlogin_invalid_response
248 start_test
'mhlogin JSON array'
250 expect_http_post_code
252 fake_json_response
<<EOF
258 test_mhlogin_invalid_response
261 start_test
'mhlogin JSON empty object'
263 expect_http_post_code
265 fake_json_response
<<EOF
271 test_mhlogin_invalid_response
274 start_test
'mhlogin empty response body'
276 expect_http_post_code
278 fake_json_response
<<EOF
283 test_mhlogin_invalid_response
286 start_test
'mhlogin gets proper error from http'
288 expect_http_post_code
290 fake_http_response
'400 Bad Request' <<EOF
291 Content-Type: application/json
294 "error": "invalid_request"
300 test_mhlogin
'mhlogin: error exchanging code for OAuth2 token
301 mhlogin: bad OAuth request; re-run with -snoop and send REDACTED output to nmh-workers'
304 start_test
'mhlogin -browser'
306 run_test
"eval echo code | mhlogin -saslmech xoauth2 -authservice test\
307 -user nobody@example.com -browser 'echo \$@ > ${MHTMPDIR}/$$.browser'" \
308 "Follow the prompts in your browser to authorize nmh to access test.
309 Enter the authorization code: mhlogin: error exchanging code for OAuth2 token
310 mhlogin: error making HTTP request to OAuth2 authorization endpoint: Failed to connect to 127.0.0.1 port ${http_port}: Connection refused"
312 cat > "${MHTMPDIR}/$$.browser.expected" <<EOF
313 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
315 check
"${MHTMPDIR}/$$.browser" "${MHTMPDIR}/$$.browser.expected"
318 start_test
'empty authorization code'
319 run_test
'eval echo '' | mhlogin -saslmech xoauth2 -authservice test -user nobody@example.com' \
320 "Load the following URL in your browser and authorize nmh to access test:
322 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
324 Enter the authorization code: Empty code; try again? "