3 # Test the XOAUTH2 support in send
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 test_send_only_fakehttp
() {
15 test_send_no_servers
"$@"
24 XOAUTH
='dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
27 start_test
'access token ready, smtp server accepts message'
32 access-nobody@example.com: test-access
33 refresh-nobody@example.com: test-refresh
34 expire-nobody@example.com: 2000000000
38 run_test
"send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com"
41 start_test
'expired access token, refresh works, smtp server accepts message'
46 access-nobody@example.com: old-access
47 refresh-nobody@example.com: test-refresh
48 expire-nobody@example.com: 1414303986
51 expect_http_post_refresh
53 fake_json_response
<<EOF
55 "access_token": "test-access",
56 "token_type": "Bearer",
62 access-nobody@example.com: test-access
63 refresh-nobody@example.com: test-refresh
64 expire-nobody@example.com:
73 start_test
'expired access token, refresh works and gets updated, smtp server accepts message'
78 access-nobody@example.com: old-access
79 refresh-nobody@example.com: old-refresh
80 expire-nobody@example.com: 1414303986
83 expect_http_post_old_refresh
85 fake_json_response
<<EOF
87 "access_token": "test-access",
88 "refresh_token": "test-refresh",
89 "token_type": "Bearer"
94 access-nobody@example.com: test-access
95 refresh-nobody@example.com: test-refresh
103 start_test
'access token has no expiration, refresh works, smtp server accepts message'
108 access-nobody@example.com: old-access
109 refresh-nobody@example.com: test-refresh
112 expect_http_post_refresh
114 fake_json_response
<<EOF
116 "access_token": "test-access",
117 "token_type": "Bearer"
122 access-nobody@example.com: test-access
123 refresh-nobody@example.com: test-refresh
131 start_test
'no access token, refresh works, smtp server accepts message'
136 refresh-nobody@example.com: test-refresh
139 expect_http_post_refresh
141 fake_json_response
<<EOF
143 "access_token": "test-access",
144 "token_type": "Bearer"
149 access-nobody@example.com: test-access
150 refresh-nobody@example.com: test-refresh
164 start_test
'no service definition'
166 run_test
"send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice bogus -user nobody@example.com" 'send: Unable to retrieve oauth profile entries: scope is missing'
169 start_test
'no creds file -- should tell user to mhlogin'
171 rm -f "${MHTMPDIR}/oauth-test"
173 test_send_only_fakesmtp
'post: no credentials -- run mhlogin -saslmech xoauth2 -authservice test
174 send: message not delivered to anyone'
177 start_test
'empty creds file -- should tell user to mhlogin'
179 fake_creds
< /dev
/null
181 test_send_only_fakesmtp
'post: user not found in cred file: nobody@example.com
182 send: message not delivered to anyone'
185 start_test
'garbage creds file'
187 echo bork
| fake_creds
189 test_send_only_fakesmtp
'post: eof encountered in field "bork"
190 post: error loading cred file
191 send: message not delivered to anyone'
194 start_test
'unexpected field in creds file'
198 access-nobody@example.com: test-access
201 test_send_only_fakesmtp
'post: error loading cred file: unexpected field
202 send: message not delivered to anyone'
205 start_test
'garbage expiration time'
208 access-nobody@example.com: test-access
209 expire-nobody@example.com: 99999999999999999999999999999999
212 test_send_only_fakesmtp
'post: error loading cred file: invalid expiration time
213 send: message not delivered to anyone'
216 start_test
'refresh response has no access token'
219 refresh-nobody@example.com: test-refresh
222 expect_http_post_refresh
224 fake_json_response
<<EOF
226 "refresh_token": "refresh-token",
227 "token_type": "Bearer"
231 test_send
'post: error refreshing OAuth2 token
232 post: invalid response: no access token
233 send: message not delivered to anyone'
236 start_test
'expired access token, no refresh token -- tell user to mhlogin'
239 access-nobody@example.com: test-access
240 expire-nobody@example.com: 1414303986
243 test_send_only_fakesmtp
'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
244 send: message not delivered to anyone'
247 start_test
'access token has no expiration, no refresh token -- tell user to mhlogin'
250 access-nobody@example.com: test-access
253 test_send_only_fakesmtp
'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
254 send: message not delivered to anyone'
257 start_test
'refresh finds no http server'
260 access-nobody@example.com: test-access
261 refresh-nobody@example.com: test-refresh
264 cat > "${testname}.expected-send-output" <<EOF
265 post: error refreshing OAuth2 token
266 post: error making HTTP request to OAuth2 authorization endpoint: [details]
267 send: message not delivered to anyone
271 run_prog send
-draft -server 127.0.0.1 -port ${smtp_port} \
272 -sasl -saslmech xoauth2
-authservice test -user nobody@example.com
> "${testname}.send-output" 2>&1 || true
273 # Clear out an error message we get from libcurl on some systems (seen at least
274 # 3 different versions of this error message, on FreeBSD 10.1, Ubuntu 12.04, and
276 f
="${testname}.send-output"
277 sed 's/\(post: error making HTTP request to OAuth2 authorization endpoint:\).*/\1 [details]/' "$f" > "$f".clean
278 check
"$f".clean
"${testname}.expected-send-output"
282 start_test
'refresh gets bogus 200 response from http server'
284 expect_http_post_refresh
286 fake_http_response
'200 OK' <<EOF
287 Content-Type: text/html
292 test_send
'post: error refreshing OAuth2 token
293 post: invalid response
294 send: message not delivered to anyone'
297 start_test
'refresh gets 500 response from http server'
299 expect_http_post_refresh
301 fake_http_response
'500 Server Error' <<EOF
302 Content-Type: text/html
307 test_send
'post: error refreshing OAuth2 token
308 post: invalid response
309 send: message not delivered to anyone'
312 start_test
'refresh gets proper error from http'
314 expect_http_post_refresh
316 fake_http_response
'400 Bad Request' <<EOF
317 Content-Type: application/json
320 "error": "invalid_grant"
324 test_send
'post: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test
325 send: message not delivered to anyone'
328 start_test
'refresh gets response too big'
331 refresh-nobody@example.com: test-refresh
334 expect_http_post_refresh
336 fake_json_response
<<EOF
338 "access_token": "test-access",
339 "token_type": "Bearer",
344 awk 'BEGIN { for (i = 0; i < 8192; i++) { print "." } }' \
345 >> "${testname}.http-res"
347 test_send
'post: error refreshing OAuth2 token
348 post: refusing to process response body larger than 8192 bytes
349 send: message not delivered to anyone'
352 start_test
'smtp server rejects token'
354 XOAUTH
='not-that-one'
357 access-nobody@example.com: test-access
358 expire-nobody@example.com: 2000000000
361 test_send_only_fakesmtp
'post: problem initializing server; [BHST] Authentication failed: Not no way, not no how!
362 send: message not delivered to anyone'
365 start_test
'botched json response'
368 refresh-nobody@example.com: test-refresh
371 expect_http_post_refresh
373 fake_json_response
<<EOF
375 "refresh_token": "refresh-toke
379 test_send
'post: error refreshing OAuth2 token
380 post: invalid response
381 send: message not delivered to anyone'
384 start_test
"smtp server doesn't support oauth"
388 test_send_only_fakesmtp
'post: problem initializing server; [BHST] SMTP server does not support SASL
389 send: message not delivered to anyone'