Skip to content

Commit e837936

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 726800a commit e837936

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

t/lib-httpd.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,42 @@ test_http_push_nonff() {
163163
test_i18ngrep "Updates were rejected because" output
164164
'
165165
}
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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,7 @@ test_expect_success 'create password-protected repository' '
4646
"$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git"
4747
'
4848

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
5850

5951
test_expect_success 'clone remote repository' '
6052
cd "$ROOT_PATH" &&
@@ -162,16 +154,23 @@ test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
162154

163155
test_expect_success 'push to password-protected repository (user in URL)' '
164156
test_commit pw-user &&
157+
set_askpass user@host &&
165158
git push "$HTTPD_URL_USER/auth/dumb/test_repo.git" HEAD &&
166159
git rev-parse --verify HEAD >expect &&
167160
git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git" \
168161
rev-parse --verify HEAD >actual &&
169162
test_cmp expect actual
170163
'
171164

165+
test_expect_failure 'user was prompted only once for password' '
166+
expect_askpass pass user@host
167+
'
168+
172169
test_expect_failure 'push to password-protected repository (no user in URL)' '
173170
test_commit pw-nouser &&
171+
set_askpass user@host &&
174172
git push "$HTTPD_URL/auth/dumb/test_repo.git" HEAD &&
173+
expect_askpass both user@host
175174
git rev-parse --verify HEAD >expect &&
176175
git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git" \
177176
rev-parse --verify HEAD >actual &&

t/t5550-http-fetch.sh

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,62 +46,28 @@ test_expect_success 'create password-protected repository' '
4646
"$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
4747
'
4848

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
8050

8151
test_expect_success 'cloning password-protected repository can fail' '
82-
>askpass-query &&
83-
echo wrong >askpass-response &&
52+
set_askpass wrong &&
8453
test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
8554
expect_askpass both wrong
8655
'
8756

8857
test_expect_success 'http auth can use user/pass in URL' '
89-
>askpass-query &&
90-
echo wrong >askpass-response &&
58+
set_askpass wrong &&
9159
git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
9260
expect_askpass none
9361
'
9462

9563
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 &&
9865
git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
9966
expect_askpass pass user@host
10067
'
10168

10269
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 &&
10571
git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
10672
expect_askpass both user@host
10773
'
@@ -112,24 +78,21 @@ test_expect_success 'http auth respects credential helper config' '
11278
echo username=user@host
11379
echo password=user@host
11480
}; f" &&
115-
>askpass-query &&
116-
echo wrong >askpass-response &&
81+
set_askpass wrong &&
11782
git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
11883
expect_askpass none
11984
'
12085

12186
test_expect_success 'http auth can get username from config' '
12287
test_config_global "credential.$HTTPD_URL.username" user@host &&
123-
>askpass-query &&
124-
echo user@host >askpass-response &&
88+
set_askpass user@host &&
12589
git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
12690
expect_askpass pass user@host
12791
'
12892

12993
test_expect_success 'configured username does not override URL' '
13094
test_config_global "credential.$HTTPD_URL.username" wrong &&
131-
>askpass-query &&
132-
echo user@host >askpass-response &&
95+
set_askpass user@host &&
13396
git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
13497
expect_askpass pass user@host
13598
'

0 commit comments

Comments
 (0)