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