t / t0300-credentials.shon commit fmt-merge-msg: make attribution into comment lines (9830a9c)
   1#!/bin/sh
   2
   3test_description='basic credential helper tests'
   4. ./test-lib.sh
   5. "$TEST_DIRECTORY"/lib-credential.sh
   6
   7test_expect_success 'setup helper scripts' '
   8        cat >dump <<-\EOF &&
   9        whoami=`echo $0 | sed s/.*git-credential-//`
  10        echo >&2 "$whoami: $*"
  11        OIFS=$IFS
  12        IFS==
  13        while read key value; do
  14                echo >&2 "$whoami: $key=$value"
  15                eval "$key=$value"
  16        done
  17        IFS=$OIFS
  18        EOF
  19
  20        cat >git-credential-useless <<-\EOF &&
  21        #!/bin/sh
  22        . ./dump
  23        exit 0
  24        EOF
  25        chmod +x git-credential-useless &&
  26
  27        cat >git-credential-verbatim <<-\EOF &&
  28        #!/bin/sh
  29        user=$1; shift
  30        pass=$1; shift
  31        . ./dump
  32        test -z "$user" || echo username=$user
  33        test -z "$pass" || echo password=$pass
  34        EOF
  35        chmod +x git-credential-verbatim &&
  36
  37        PATH="$PWD:$PATH"
  38'
  39
  40test_expect_success 'credential_fill invokes helper' '
  41        check fill "verbatim foo bar" <<-\EOF
  42        --
  43        username=foo
  44        password=bar
  45        --
  46        verbatim: get
  47        EOF
  48'
  49
  50test_expect_success 'credential_fill invokes multiple helpers' '
  51        check fill useless "verbatim foo bar" <<-\EOF
  52        --
  53        username=foo
  54        password=bar
  55        --
  56        useless: get
  57        verbatim: get
  58        EOF
  59'
  60
  61test_expect_success 'credential_fill stops when we get a full response' '
  62        check fill "verbatim one two" "verbatim three four" <<-\EOF
  63        --
  64        username=one
  65        password=two
  66        --
  67        verbatim: get
  68        EOF
  69'
  70
  71test_expect_success 'credential_fill continues through partial response' '
  72        check fill "verbatim one \"\"" "verbatim two three" <<-\EOF
  73        --
  74        username=two
  75        password=three
  76        --
  77        verbatim: get
  78        verbatim: get
  79        verbatim: username=one
  80        EOF
  81'
  82
  83test_expect_success 'credential_fill passes along metadata' '
  84        check fill "verbatim one two" <<-\EOF
  85        protocol=ftp
  86        host=example.com
  87        path=foo.git
  88        --
  89        username=one
  90        password=two
  91        --
  92        verbatim: get
  93        verbatim: protocol=ftp
  94        verbatim: host=example.com
  95        verbatim: path=foo.git
  96        EOF
  97'
  98
  99test_expect_success 'credential_approve calls all helpers' '
 100        check approve useless "verbatim one two" <<-\EOF
 101        username=foo
 102        password=bar
 103        --
 104        --
 105        useless: store
 106        useless: username=foo
 107        useless: password=bar
 108        verbatim: store
 109        verbatim: username=foo
 110        verbatim: password=bar
 111        EOF
 112'
 113
 114test_expect_success 'do not bother storing password-less credential' '
 115        check approve useless <<-\EOF
 116        username=foo
 117        --
 118        --
 119        EOF
 120'
 121
 122
 123test_expect_success 'credential_reject calls all helpers' '
 124        check reject useless "verbatim one two" <<-\EOF
 125        username=foo
 126        password=bar
 127        --
 128        --
 129        useless: erase
 130        useless: username=foo
 131        useless: password=bar
 132        verbatim: erase
 133        verbatim: username=foo
 134        verbatim: password=bar
 135        EOF
 136'
 137
 138test_expect_success 'usernames can be preserved' '
 139        check fill "verbatim \"\" three" <<-\EOF
 140        username=one
 141        --
 142        username=one
 143        password=three
 144        --
 145        verbatim: get
 146        verbatim: username=one
 147        EOF
 148'
 149
 150test_expect_success 'usernames can be overridden' '
 151        check fill "verbatim two three" <<-\EOF
 152        username=one
 153        --
 154        username=two
 155        password=three
 156        --
 157        verbatim: get
 158        verbatim: username=one
 159        EOF
 160'
 161
 162test_expect_success 'do not bother completing already-full credential' '
 163        check fill "verbatim three four" <<-\EOF
 164        username=one
 165        password=two
 166        --
 167        username=one
 168        password=two
 169        --
 170        EOF
 171'
 172
 173# We can't test the basic terminal password prompt here because
 174# getpass() tries too hard to find the real terminal. But if our
 175# askpass helper is run, we know the internal getpass is working.
 176test_expect_success 'empty helper list falls back to internal getpass' '
 177        check fill <<-\EOF
 178        --
 179        username=askpass-username
 180        password=askpass-password
 181        --
 182        askpass: Username:
 183        askpass: Password:
 184        EOF
 185'
 186
 187test_expect_success 'internal getpass does not ask for known username' '
 188        check fill <<-\EOF
 189        username=foo
 190        --
 191        username=foo
 192        password=askpass-password
 193        --
 194        askpass: Password:
 195        EOF
 196'
 197
 198HELPER="!f() {
 199                cat >/dev/null
 200                echo username=foo
 201                echo password=bar
 202        }; f"
 203test_expect_success 'respect configured credentials' '
 204        test_config credential.helper "$HELPER" &&
 205        check fill <<-\EOF
 206        --
 207        username=foo
 208        password=bar
 209        --
 210        EOF
 211'
 212
 213test_expect_success 'match configured credential' '
 214        test_config credential.https://example.com.helper "$HELPER" &&
 215        check fill <<-\EOF
 216        protocol=https
 217        host=example.com
 218        path=repo.git
 219        --
 220        username=foo
 221        password=bar
 222        --
 223        EOF
 224'
 225
 226test_expect_success 'do not match configured credential' '
 227        test_config credential.https://foo.helper "$HELPER" &&
 228        check fill <<-\EOF
 229        protocol=https
 230        host=bar
 231        --
 232        username=askpass-username
 233        password=askpass-password
 234        --
 235        askpass: Username for '\''https://bar'\'':
 236        askpass: Password for '\''https://askpass-username@bar'\'':
 237        EOF
 238'
 239
 240test_expect_success 'pull username from config' '
 241        test_config credential.https://example.com.username foo &&
 242        check fill <<-\EOF
 243        protocol=https
 244        host=example.com
 245        --
 246        username=foo
 247        password=askpass-password
 248        --
 249        askpass: Password for '\''https://foo@example.com'\'':
 250        EOF
 251'
 252
 253test_expect_success 'http paths can be part of context' '
 254        check fill "verbatim foo bar" <<-\EOF &&
 255        protocol=https
 256        host=example.com
 257        path=foo.git
 258        --
 259        username=foo
 260        password=bar
 261        --
 262        verbatim: get
 263        verbatim: protocol=https
 264        verbatim: host=example.com
 265        EOF
 266        test_config credential.https://example.com.useHttpPath true &&
 267        check fill "verbatim foo bar" <<-\EOF
 268        protocol=https
 269        host=example.com
 270        path=foo.git
 271        --
 272        username=foo
 273        password=bar
 274        --
 275        verbatim: get
 276        verbatim: protocol=https
 277        verbatim: host=example.com
 278        verbatim: path=foo.git
 279        EOF
 280'
 281
 282test_done