]> diplodocus.org Git - nmh/blob - test/oauth/common.sh
5deebfd567b0118a950c8ce41410d6749a7fb4e9
[nmh] / test / oauth / common.sh
1 # Common routines for OAuth tests
2
3 . "${MH_OBJ_DIR}/test/common.sh"
4
5 setup_test
6
7 if [ "${OAUTH_SUPPORT}" -eq 0 ]; then
8 test_skip 'no oauth support'
9 fi
10
11 testname="${MH_TEST_DIR}/$$"
12
13 arith_eval 64001 + `id -u` % 1000
14 http_port=${arith_val}
15
16 arith_eval 64000 + `id -u` % 1000
17 pop_port=${arith_val}
18
19 arith_eval 64002 + `id -u` % 1000
20 smtp_port=${arith_val}
21
22 cat >> ${MH} <<EOF
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
29 EOF
30
31 setup_pop() {
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>
37 Subject: Hello
38 Date: Sun, 17 Dec 2006 12:13:14 -0500
39
40 Hey man
41 EOM
42 }
43
44 setup_draft() {
45 cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
46 From: Mr Nobody <nobody@example.com>
47 To: Somebody Else <somebody@example.com>
48 Subject: Test
49 MIME-Version: 1.0
50 Content-Type: text/plain; charset="us-ascii"
51
52 This is a test
53 EOF
54 }
55
56 start_fakehttp() {
57 "${MH_OBJ_DIR}/test/fakehttp" "${testname}.http-req" ${http_port} \
58 "${testname}.http-res" > /dev/null
59 }
60
61 start_pop() {
62 "${MH_OBJ_DIR}/test/fakepop" "${pop_port}" "$1" "$2" "${pop_message}" \
63 > /dev/null
64 }
65
66 start_pop_xoauth() {
67 start_pop XOAUTH \
68 'dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
69 }
70
71 start_fakesmtp() {
72 "${MH_OBJ_DIR}/test/fakesmtp" "${testname}.smtp-req" ${smtp_port} \
73 > /dev/null
74 }
75
76 fake_creds() {
77 cat > "${MHTMPDIR}/oauth-test"
78 }
79
80 fake_http_response() {
81 echo "HTTP/1.1 $1" > "${testname}.http-res"
82 cat >> "${testname}.http-res"
83 }
84
85 fake_json_response() {
86 (echo 'Content-Type: application/json';
87 echo;
88 cat) | fake_http_response '200 OK'
89 }
90
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.
95 expect_http_post() {
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}
100 Accept: */*
101 Content-Length: $1
102 Content-Type: application/x-www-form-urlencoded
103
104 $2
105 EOF
106 }
107
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'
110 }
111
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'
114 }
115
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'
118 }
119
120 expect_creds() {
121 cat > "${testname}.expected-creds"
122 }
123
124 test_inc() {
125 run_test "inc -host 127.0.0.1 -port ${pop_port} -oauth test -user nobody@example.com -width 80" "$@"
126 }
127
128 test_inc_success() {
129 test_inc 'Incorporating new mail into inbox...
130
131 11+ 12/17 No Such User Hello<<Hey man >>'
132 check "${pop_message}" "`mhpath +inbox 11`" 'keep first'
133 }
134
135 test_send_no_servers() {
136 run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -oauth test -user nobody@example.com" "$@"
137 }
138
139 test_send_only_fakesmtp() {
140 start_fakesmtp
141 test_send_no_servers "$@"
142 }
143
144 test_send() {
145 start_fakehttp
146 test_send_only_fakesmtp "$@"
147 check "${testname}.http-req" "${testname}.expected-http-req"
148 }
149
150 check_http_req() {
151 check "${testname}.http-req" "${testname}.expected-http-req"
152 }
153
154 check_creds_private() {
155 f="${MHTMPDIR}/oauth-test"
156 if ls -dl "$f" | grep '^-rw-------' > /dev/null 2>&1; then
157 :
158 else
159 echo "$f permissions not private"
160 failed=`expr ${failed:-0} + 1`
161 fi
162 }
163
164 check_creds() {
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"
169
170 sed 's/^expire:.*/expire:/' "$f" > "$f".notime
171 check "$f".notime "${testname}.expected-creds"
172 rm "$f"
173 }