]> diplodocus.org Git - nmh/blobdiff - test/oauth/test-mhlogin
config/version.sh: Rewrite. Use uname(1), git-describe(1), and UTC.
[nmh] / test / oauth / test-mhlogin
index 7f2bcc4ab6b69f9ead7731e9ec6c8ffa1d85f68b..c4a7bf2d812f65824441db3753ac6e18bfa8cd11 100755 (executable)
@@ -10,6 +10,8 @@ fi
 
 . "${srcdir}/test/oauth/common.sh"
 
+check_exit '-eq 1' mhlogin -
+
 expect_no_creds() {
     cat /dev/null > "${MHTMPDIR}/$$.expected-creds"
     cat /dev/null > "${MHTMPDIR}/oauth-test"
@@ -18,7 +20,7 @@ expect_no_creds() {
 
 test_mhlogin() {
     start_fakehttp
-    run_test 'eval echo code | mhlogin -saslmech xoauth2 -authservice test' \
+    run_test 'eval echo code | mhlogin -saslmech xoauth2 -authservice test -user nobody@example.com' \
 "Load the following URL in your browser and authorize nmh to access test:
 
 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
@@ -52,8 +54,8 @@ fake_json_response <<EOF
 EOF
 
 expect_creds <<EOF
-access: test-access
-expire:
+access-nobody@example.com: test-access
+expire-nobody@example.com:
 EOF
 
 test_mhlogin
@@ -71,7 +73,7 @@ fake_json_response <<EOF
 EOF
 
 expect_creds <<EOF
-access: test-access
+access-nobody@example.com: test-access
 EOF
 
 test_mhlogin
@@ -91,9 +93,9 @@ fake_json_response <<EOF
 EOF
 
 expect_creds <<EOF
-access: test-access
-refresh: refresh-token
-expire:
+access-nobody@example.com: test-access
+refresh-nobody@example.com: refresh-token
+expire-nobody@example.com:
 EOF
 
 test_mhlogin
@@ -111,7 +113,7 @@ fake_json_response <<EOF
 EOF
 
 expect_creds <<EOF
-refresh: refresh-token
+refresh-nobody@example.com: refresh-token
 EOF
 
 test_mhlogin
@@ -152,13 +154,63 @@ fake_json_response <<EOF
 EOF
 
 expect_creds <<EOF
-access: test-access
-refresh: refresh-token
-expire:
+access-nobody@example.com: test-access
+refresh-nobody@example.com: refresh-token
+expire-nobody@example.com:
 EOF
 
 test_mhlogin
 
+# TEST
+start_test 'mhlogin multiple users'
+
+expect_http_post_code
+
+fake_json_response <<EOF
+{
+  "access_token": "user3-access",
+  "refresh_token": "user3-refresh",
+  "expires_in": 3600,
+  "token_type": "Bearer"
+}
+EOF
+
+expect_creds <<EOF
+access-nobody@example.com: user1-access
+refresh-nobody@example.com: user1-refresh
+expire-nobody@example.com:
+access-nobody2@example.com: user2-access
+refresh-nobody2@example.com: user2-refresh
+expire-nobody2@example.com:
+access-nobody3@example.com: user3-access
+refresh-nobody3@example.com: user3-refresh
+expire-nobody3@example.com:
+EOF
+
+fake_creds <<EOF
+access-nobody@example.com: user1-access
+refresh-nobody@example.com: user1-refresh
+expire-nobody@example.com: 100
+access-nobody2@example.com: user2-access
+refresh-nobody2@example.com: user2-refresh
+expire-nobody2@example.com: 100
+EOF
+
+start_fakehttp
+run_test 'eval echo code | mhlogin -saslmech xoauth2 -authservice test -user nobody3@example.com' \
+         "Load the following URL in your browser and authorize nmh to access test:
+
+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
+
+Enter the authorization code: $1"
+check_http_req
+check_creds_private
+check_creds
+
+#
+# fail cases
+#
+
 # TEST
 start_test 'mhlogin user enters bad code'
 
@@ -176,10 +228,6 @@ expect_no_creds
 
 test_mhlogin 'Code rejected; try again? '
 
-#
-# fail cases
-#
-
 # TEST
 start_test 'mhlogin response has no content-type'
 
@@ -254,6 +302,36 @@ expect_no_creds
 test_mhlogin 'mhlogin: error exchanging code for OAuth2 token
 mhlogin: bad OAuth request; re-run with -snoop and send REDACTED output to nmh-workers'
 
+# TEST
+start_test 'mhlogin -browser'
+
+#### Strip off the error string because it can vary, e.g.,
+####   "Failed to connect to 127.0.0.1 port 64546: Connection refused", or
+####   "Connection timed out after 1004 milliseconds"
+echo code | mhlogin -saslmech xoauth2 -authservice test -user nobody@example.com \
+                    -browser "echo \$@ > ${MHTMPDIR}/$$.browser" 2>&1 1>/dev/null | \
+    sed 's/\( endpoint:\) .*/\1/' > "${MHTMPDIR}/$$.mhlogin.err"
+cat > "${MHTMPDIR}/$$.mhlogin.err.expected" <<EOF
+mhlogin: error exchanging code for OAuth2 token
+mhlogin: error making HTTP request to OAuth2 authorization endpoint:
+EOF
+check "${MHTMPDIR}/$$.mhlogin.err" "${MHTMPDIR}/$$.mhlogin.err.expected"
+
+cat > "${MHTMPDIR}/$$.browser.expected" <<EOF
+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
+EOF
+check "${MHTMPDIR}/$$.browser" "${MHTMPDIR}/$$.browser.expected"
+
+# TEST
+start_test 'empty authorization code'
+run_test 'eval echo '' | mhlogin -saslmech xoauth2 -authservice test -user nobody@example.com' \
+         "Load the following URL in your browser and authorize nmh to access test:
+
+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
+
+Enter the authorization code: Empty code; try again? "
+
+
 clean_fakehttp
 finish_test