t5550: factor out http auth setup

The t5550 script sets up a nice askpass helper for simulating user input and checking what git prompted for. Let's make it available to other http scripts by migrating it to lib-httpd. We can use this immediately in t5540 to make our tests more robust (previously, we did not check at all that hitting the password-protected repo actually involved a password). Unfortunately, we end up failing the test because the current code erroneously prompts twice (once for git-remote-http, and then again when the former spawns git-http-push). More importantly, though, it will let us easily add smart-http authentication tests in t5541 and t5551; we currently do not test smart-http authentication at all. As part of making it generic, let's always look for and store auxiliary askpass files at the top-level trash directory; this makes it compatible with t5540, which runs some tests from sub-repositories. We can abstract away the ugliness with a short helper function. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 27, 2012 at 09:24 UTC e837936c7c08ac5829bc53761ecb57d18d458edd
3 files changed +55 -54
t/lib-httpd.sh
+39
@@ -163,3 +163,42 @@ test_http_push_nonff() {
163 test_i18ngrep "Updates were rejected because" output
164 '
165 }
166 +
167 +setup_askpass_helper() {
168 + test_expect_success 'setup askpass helper' '
169 + write_script "$TRASH_DIRECTORY/askpass" <<-\EOF &&
170 + echo >>"$TRASH_DIRECTORY/askpass-query" "askpass: $*" &&
171 + cat "$TRASH_DIRECTORY/askpass-response"
172 + EOF
173 + GIT_ASKPASS="$TRASH_DIRECTORY/askpass" &&
174 + export GIT_ASKPASS &&
175 + export TRASH_DIRECTORY
176 + '
177 +}
178 +
179 +set_askpass() {
180 + >"$TRASH_DIRECTORY/askpass-query" &&
181 + echo "$*" >"$TRASH_DIRECTORY/askpass-response"
182 +}
183 +
184 +expect_askpass() {
185 + dest=$HTTPD_DEST
186 + {
187 + case "$1" in
188 + none)
189 + ;;
190 + pass)
191 + echo "askpass: Password for 'http://$2@$dest': "
192 + ;;
193 + both)
194 + echo "askpass: Username for 'http://$dest': "
195 + echo "askpass: Password for 'http://$2@$dest': "
196 + ;;
197 + *)
198 + false
199 + ;;
200 + esac
201 + } >"$TRASH_DIRECTORY/askpass-expect" &&
202 + test_cmp "$TRASH_DIRECTORY/askpass-expect" \
203 + "$TRASH_DIRECTORY/askpass-query"
204 +}
t/t5540-http-push.sh
+8 -9
@@ -46,15 +46,7 @@ test_expect_success 'create password-protected repository' '
46 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git"
47 '
48
49 -test_expect_success 'setup askpass helper' '
50 - cat >askpass <<-\EOF &&
51 - #!/bin/sh
52 - echo user@host
53 - EOF
54 - chmod +x askpass &&
55 - GIT_ASKPASS="$PWD/askpass" &&
56 - export GIT_ASKPASS
57 -'
49 +setup_askpass_helper
50
51 test_expect_success 'clone remote repository' '
52 cd "$ROOT_PATH" &&
@@ -162,6 +154,7 @@ test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
154
155 test_expect_success 'push to password-protected repository (user in URL)' '
156 test_commit pw-user &&
157 + set_askpass user@host &&
158 git push "$HTTPD_URL_USER/auth/dumb/test_repo.git" HEAD &&
159 git rev-parse --verify HEAD >expect &&
160 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git" \
@@ -169,9 +162,15 @@ test_expect_success 'push to password-protected repository (user in URL)' '
162 test_cmp expect actual
163 '
164
165 +test_expect_failure 'user was prompted only once for password' '
166 + expect_askpass pass user@host
167 +'
168 +
169 test_expect_failure 'push to password-protected repository (no user in URL)' '
170 test_commit pw-nouser &&
171 + set_askpass user@host &&
172 git push "$HTTPD_URL/auth/dumb/test_repo.git" HEAD &&
173 + expect_askpass both user@host
174 git rev-parse --verify HEAD >expect &&
175 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git" \
176 rev-parse --verify HEAD >actual &&
t/t5550-http-fetch.sh
+8 -45
@@ -46,62 +46,28 @@ test_expect_success 'create password-protected repository' '
46 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
47 '
48
49 -test_expect_success 'setup askpass helpers' '
50 - cat >askpass <<-EOF &&
51 - #!/bin/sh
52 - echo >>"$PWD/askpass-query" "askpass: \$*" &&
53 - cat "$PWD/askpass-response"
54 - EOF
55 - chmod +x askpass &&
56 - GIT_ASKPASS="$PWD/askpass" &&
57 - export GIT_ASKPASS
58 -'
59 -
60 -expect_askpass() {
61 - dest=$HTTPD_DEST
62 - {
63 - case "$1" in
64 - none)
65 - ;;
66 - pass)
67 - echo "askpass: Password for 'http://$2@$dest': "
68 - ;;
69 - both)
70 - echo "askpass: Username for 'http://$dest': "
71 - echo "askpass: Password for 'http://$2@$dest': "
72 - ;;
73 - *)
74 - false
75 - ;;
76 - esac
77 - } >askpass-expect &&
78 - test_cmp askpass-expect askpass-query
79 -}
49 +setup_askpass_helper
50
51 test_expect_success 'cloning password-protected repository can fail' '
82 - >askpass-query &&
83 - echo wrong >askpass-response &&
52 + set_askpass wrong &&
53 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
54 expect_askpass both wrong
55 '
56
57 test_expect_success 'http auth can use user/pass in URL' '
89 - >askpass-query &&
90 - echo wrong >askpass-response &&
58 + set_askpass wrong &&
59 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
60 expect_askpass none
61 '
62
63 test_expect_success 'http auth can use just user in URL' '
96 - >askpass-query &&
97 - echo user@host >askpass-response &&
64 + set_askpass user@host &&
65 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
66 expect_askpass pass user@host
67 '
68
69 test_expect_success 'http auth can request both user and pass' '
103 - >askpass-query &&
104 - echo user@host >askpass-response &&
70 + set_askpass user@host &&
71 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
72 expect_askpass both user@host
73 '
@@ -112,24 +78,21 @@ test_expect_success 'http auth respects credential helper config' '
78 echo username=user@host
79 echo password=user@host
80 }; f" &&
115 - >askpass-query &&
116 - echo wrong >askpass-response &&
81 + set_askpass wrong &&
82 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
83 expect_askpass none
84 '
85
86 test_expect_success 'http auth can get username from config' '
87 test_config_global "credential.$HTTPD_URL.username" user@host &&
123 - >askpass-query &&
124 - echo user@host >askpass-response &&
88 + set_askpass user@host &&
89 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
90 expect_askpass pass user@host
91 '
92
93 test_expect_success 'configured username does not override URL' '
94 test_config_global "credential.$HTTPD_URL.username" wrong &&
131 - >askpass-query &&
132 - echo user@host >askpass-response &&
95 + set_askpass user@host &&
96 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
97 expect_askpass pass user@host
98 '