]> diplodocus.org Git - nmh/blob - test/oauth/test-send
Reverted commit 9a4b4a3d3b27fe4a7ff6d0b8724ce1c06b5917eb.
[nmh] / test / oauth / test-send
1 #!/bin/sh
2 #
3 # Test the XOAUTH2 support in send
4 #
5
6 if test -z "${MH_OBJ_DIR}"; then
7 srcdir=`dirname "$0"`/../..
8 MH_OBJ_DIR=`cd "${srcdir}" && pwd`; export MH_OBJ_DIR
9 fi
10
11 . "${srcdir}/test/oauth/common.sh"
12
13 check_exit '-eq 1' send -
14
15 test_send_only_fakehttp() {
16 start_fakehttp
17 test_send_no_servers "$@"
18 check_http_req
19 }
20
21 #
22 # success cases
23 #
24
25 export XOAUTH
26 XOAUTH='dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
27
28 # TEST
29 start_test 'access token ready, smtp server accepts message'
30
31 setup_draft
32
33 fake_creds <<EOF
34 access-nobody@example.com: test-access
35 refresh-nobody@example.com: test-refresh
36 expire-nobody@example.com: 2000000000
37 EOF
38
39 start_fakesmtp
40 run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com"
41
42 # TEST
43 start_test 'expired access token, refresh works, smtp server accepts message'
44
45 setup_draft
46
47 fake_creds <<EOF
48 access-nobody@example.com: old-access
49 refresh-nobody@example.com: test-refresh
50 expire-nobody@example.com: 1414303986
51 EOF
52
53 expect_http_post_refresh
54
55 fake_json_response <<EOF
56 {
57 "access_token": "test-access",
58 "token_type": "Bearer",
59 "expires_in": 3600
60 }
61 EOF
62
63 expect_creds <<EOF
64 access-nobody@example.com: test-access
65 refresh-nobody@example.com: test-refresh
66 expire-nobody@example.com:
67 EOF
68
69 test_send
70
71 check_creds_private
72 check_creds
73
74 # TEST
75 start_test 'expired access token, refresh works and gets updated, smtp server accepts message'
76
77 setup_draft
78
79 fake_creds <<EOF
80 access-nobody@example.com: old-access
81 refresh-nobody@example.com: old-refresh
82 expire-nobody@example.com: 1414303986
83 EOF
84
85 expect_http_post_old_refresh
86
87 fake_json_response <<EOF
88 {
89 "access_token": "test-access",
90 "refresh_token": "test-refresh",
91 "token_type": "Bearer"
92 }
93 EOF
94
95 expect_creds <<EOF
96 access-nobody@example.com: test-access
97 refresh-nobody@example.com: test-refresh
98 EOF
99
100 test_send
101
102 check_creds
103
104 # TEST
105 start_test 'access token has no expiration, refresh works, smtp server accepts message'
106
107 setup_draft
108
109 fake_creds <<EOF
110 access-nobody@example.com: old-access
111 refresh-nobody@example.com: test-refresh
112 EOF
113
114 expect_http_post_refresh
115
116 fake_json_response <<EOF
117 {
118 "access_token": "test-access",
119 "token_type": "Bearer"
120 }
121 EOF
122
123 expect_creds <<EOF
124 access-nobody@example.com: test-access
125 refresh-nobody@example.com: test-refresh
126 EOF
127
128 test_send
129
130 check_creds
131
132 # TEST
133 start_test 'no access token, refresh works, smtp server accepts message'
134
135 setup_draft
136
137 fake_creds <<EOF
138 refresh-nobody@example.com: test-refresh
139 EOF
140
141 expect_http_post_refresh
142
143 fake_json_response <<EOF
144 {
145 "access_token": "test-access",
146 "token_type": "Bearer"
147 }
148 EOF
149
150 expect_creds <<EOF
151 access-nobody@example.com: test-access
152 refresh-nobody@example.com: test-refresh
153 EOF
154
155 test_send
156
157 check_creds
158
159 #
160 # fail cases
161 #
162
163 setup_draft
164
165 # TEST
166 start_test 'no service definition'
167
168 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
170 # TEST
171 start_test 'no creds file -- should tell user to mhlogin'
172
173 rm -f "${MHTMPDIR}/oauth-test"
174
175 test_send_only_fakesmtp 'post: no credentials -- run mhlogin -saslmech xoauth2 -authservice test
176 send: message not delivered to anyone'
177
178 # TEST
179 start_test 'empty creds file -- should tell user to mhlogin'
180
181 fake_creds < /dev/null
182
183 test_send_only_fakesmtp 'post: user not found in cred file: nobody@example.com
184 send: message not delivered to anyone'
185
186 # TEST
187 start_test 'garbage creds file'
188
189 echo bork | fake_creds
190
191 test_send_only_fakesmtp 'post: eof encountered in field "bork"
192 post: error loading cred file
193 send: message not delivered to anyone'
194
195 # TEST
196 start_test 'unexpected field in creds file'
197
198 fake_creds <<EOF
199 bork: bork
200 access-nobody@example.com: test-access
201 EOF
202
203 test_send_only_fakesmtp 'post: error loading cred file: unexpected field
204 send: message not delivered to anyone'
205
206 # TEST
207 start_test 'garbage expiration time'
208
209 fake_creds <<EOF
210 access-nobody@example.com: test-access
211 expire-nobody@example.com: 99999999999999999999999999999999
212 EOF
213
214 test_send_only_fakesmtp 'post: error loading cred file: invalid expiration time
215 send: message not delivered to anyone'
216
217 # TEST
218 start_test 'refresh response has no access token'
219
220 fake_creds <<EOF
221 refresh-nobody@example.com: test-refresh
222 EOF
223
224 expect_http_post_refresh
225
226 fake_json_response <<EOF
227 {
228 "refresh_token": "refresh-token",
229 "token_type": "Bearer"
230 }
231 EOF
232
233 test_send 'post: error refreshing OAuth2 token
234 post: invalid response: no access token
235 send: message not delivered to anyone'
236
237 # TEST
238 start_test 'expired access token, no refresh token -- tell user to mhlogin'
239
240 fake_creds <<EOF
241 access-nobody@example.com: test-access
242 expire-nobody@example.com: 1414303986
243 EOF
244
245 test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
246 send: message not delivered to anyone'
247
248 # TEST
249 start_test 'access token has no expiration, no refresh token -- tell user to mhlogin'
250
251 fake_creds <<EOF
252 access-nobody@example.com: test-access
253 EOF
254
255 test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
256 send: message not delivered to anyone'
257
258 # TEST
259 start_test 'refresh finds no http server'
260
261 fake_creds <<EOF
262 access-nobody@example.com: test-access
263 refresh-nobody@example.com: test-refresh
264 EOF
265
266 cat > "${testname}.expected-send-output" <<EOF
267 post: error refreshing OAuth2 token
268 post: error making HTTP request to OAuth2 authorization endpoint: [details]
269 send: message not delivered to anyone
270 EOF
271
272 start_fakesmtp
273 run_prog send -draft -server 127.0.0.1 -port ${smtp_port} \
274 -sasl -saslmech xoauth2 -authservice test -user nobody@example.com > "${testname}.send-output" 2>&1 || true
275 # Clear out an error message we get from libcurl on some systems (seen at least
276 # 3 different versions of this error message, on FreeBSD 10.1, Ubuntu 12.04, and
277 # Ubuntu 14.04).
278 f="${testname}.send-output"
279 sed 's/\(post: error making HTTP request to OAuth2 authorization endpoint:\).*/\1 [details]/' "$f" > "$f".clean
280 check "$f".clean "${testname}.expected-send-output"
281 rm "$f"
282
283 # TEST
284 start_test 'refresh gets bogus 200 response from http server'
285
286 expect_http_post_refresh
287
288 fake_http_response '200 OK' <<EOF
289 Content-Type: text/html
290
291 <html>doh!</htmxl>
292 EOF
293
294 test_send 'post: error refreshing OAuth2 token
295 post: invalid response
296 send: message not delivered to anyone'
297
298 # TEST
299 start_test 'refresh gets 500 response from http server'
300
301 expect_http_post_refresh
302
303 fake_http_response '500 Server Error' <<EOF
304 Content-Type: text/html
305
306 <html>doh!</html>
307 EOF
308
309 test_send 'post: error refreshing OAuth2 token
310 post: invalid response
311 send: message not delivered to anyone'
312
313 # TEST
314 start_test 'refresh gets proper error from http'
315
316 expect_http_post_refresh
317
318 fake_http_response '400 Bad Request' <<EOF
319 Content-Type: application/json
320
321 {
322 "error": "invalid_grant"
323 }
324 EOF
325
326 test_send 'post: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test
327 send: message not delivered to anyone'
328
329 # TEST
330 start_test 'refresh gets response too big'
331
332 fake_creds <<EOF
333 refresh-nobody@example.com: test-refresh
334 EOF
335
336 expect_http_post_refresh
337
338 fake_json_response <<EOF
339 {
340 "access_token": "test-access",
341 "token_type": "Bearer",
342 "expires_in": 3600
343 }
344 EOF
345
346 awk 'BEGIN { for (i = 0; i < 8192; i++) { print "." } }' \
347 >> "${testname}.http-res"
348
349 test_send 'post: error refreshing OAuth2 token
350 post: refusing to process response body larger than 8192 bytes
351 send: message not delivered to anyone'
352
353 # TEST
354 start_test 'smtp server rejects token'
355
356 XOAUTH='not-that-one'
357
358 fake_creds <<EOF
359 access-nobody@example.com: test-access
360 expire-nobody@example.com: 2000000000
361 EOF
362
363 test_send_only_fakesmtp 'post: problem initializing server; [BHST] Authentication failed: Not no way, not no how!
364 send: message not delivered to anyone'
365
366 # TEST
367 start_test 'botched json response'
368
369 fake_creds <<EOF
370 refresh-nobody@example.com: test-refresh
371 EOF
372
373 expect_http_post_refresh
374
375 fake_json_response <<EOF
376 {
377 "refresh_token": "refresh-toke
378 }
379 EOF
380
381 test_send 'post: error refreshing OAuth2 token
382 post: invalid response
383 send: message not delivered to anyone'
384
385 # TEST
386 start_test "smtp server doesn't support oauth"
387
388 unset XOAUTH
389
390 test_send_only_fakesmtp 'post: problem initializing server; [BHST] SMTP server does not support SASL
391 send: message not delivered to anyone'
392
393 clean_fakesmtp
394 clean_fakehttp
395 finish_test
396
397 exit ${failed:-0}