1 # Common routines for OAuth tests
3 .
"${MH_OBJ_DIR}/test/common.sh"
7 if [ "${OAUTH_SUPPORT}" -eq 0 ]; then
8 test_skip
'no oauth support'
11 testname
="${MH_TEST_DIR}/$$"
13 arith_eval
64001 + `id -u` % 1000
14 http_port
=${arith_val}
16 arith_eval
64000 + `id -u` % 1000
19 arith_eval
64002 + `id -u` % 1000
20 smtp_port
=${arith_val}
23 oauth-test-scope: test-scope
24 oauth-test-client_id: test-id
25 oauth-test-client_secret: test-secret
26 oauth-test-auth_endpoint: http://127.0.0.1:${http_port}/oauth/auth
27 oauth-test-token_endpoint: http://127.0.0.1:${http_port}/oauth/token
28 oauth-test-redirect_uri: urn:ietf:wg:oauth:2.0:oob
32 pop_message
=${MH_TEST_DIR}/testmessage
33 cat > "${pop_message}" <<EOM
34 Received: From somewhere
35 From: No Such User <nosuch@example.com>
36 To: Some Other User <someother@example.com>
38 Date: Sun, 17 Dec 2006 12:13:14 -0500
45 cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
46 From: Mr Nobody <nobody@example.com>
47 To: Somebody Else <somebody@example.com>
50 Content-Type: text/plain; charset="us-ascii"
57 "${MH_OBJ_DIR}/test/fakehttp" "${testname}.http-req" ${http_port} \
58 "${testname}.http-res" > /dev
/null
62 "${MH_OBJ_DIR}/test/fakepop" "${pop_port}" "$1" "$2" "${pop_message}" \
68 'dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
72 "${MH_OBJ_DIR}/test/fakesmtp" "${testname}.smtp-req" ${smtp_port} \
77 cat > "${MHTMPDIR}/oauth-test"
80 fake_http_response
() {
81 echo "HTTP/1.1 $1" > "${testname}.http-res"
82 cat >> "${testname}.http-res"
85 fake_json_response
() {
86 (echo 'Content-Type: application/json';
88 cat) | fake_http_response
'200 OK'
91 # The format of the POST request is mostly dependent on curl, and could possibly
92 # change with newer or older curl versions, or by configuration. curl 7.39.0
93 # makes POST requests like this on FreeBSD 10 and Ubuntu 14.04. If you find
94 # this failing, you'll need to make this a smarter comparison.
96 cat > "${testname}.expected-http-req" <<EOF
97 POST /oauth/token HTTP/1.1
98 User-Agent: nmh/${MH_VERSION} ${CURL_USER_AGENT}
99 Host: 127.0.0.1:${http_port}
102 Content-Type: application/x-www-form-urlencoded
108 expect_http_post_code
() {
109 expect_http_post
132 'code=code&grant_type=authorization_code&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&client_id=test-id&client_secret=test-secret'
112 expect_http_post_refresh
() {
113 expect_http_post
95 'grant_type=refresh_token&refresh_token=test-refresh&client_id=test-id&client_secret=test-secret'
116 expect_http_post_old_refresh
() {
117 expect_http_post
94 'grant_type=refresh_token&refresh_token=old-refresh&client_id=test-id&client_secret=test-secret'
121 cat > "${testname}.expected-creds"
125 run_test
"inc -host 127.0.0.1 -port ${pop_port} -oauth test -user nobody@example.com -width 80" "$@"
129 test_inc
'Incorporating new mail into inbox...
131 11+ 12/17 No Such User Hello<<Hey man >>'
132 check
"${pop_message}" "`mhpath +inbox 11`" 'keep first'
135 test_send_no_servers
() {
136 run_test
"send -draft -server 127.0.0.1 -port ${smtp_port} -oauth test -user nobody@example.com" "$@"
139 test_send_only_fakesmtp
() {
141 test_send_no_servers
"$@"
146 test_send_only_fakesmtp
"$@"
147 check
"${testname}.http-req" "${testname}.expected-http-req"
151 check
"${testname}.http-req" "${testname}.expected-http-req"
154 check_creds_private
() {
155 f
="${MHTMPDIR}/oauth-test"
156 if ls -dl "$f" | grep '^-rw-------' > /dev
/null
2>&1; then
159 echo "$f permissions not private"
160 failed
=`expr ${failed:-0} + 1`
165 # It's hard to calculate the exact expiration time mhlogin is going to use,
166 # so we'll just use sed to remove the actual time so we can easily compare
167 # it against our "correct" output.
168 f
="${MHTMPDIR}/oauth-test"
170 sed 's/^expire:.*/expire:/' "$f" > "$f".notime
171 check
"$f".notime
"${testname}.expected-creds"