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