github.com/adevinta/maiao@v0.0.0-20240318133227-b6f9656b5e07/pkg/credentials/git_test.go (about)

     1  package credentials
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestGitCredentialsReturnsNoCredentialsWhenGitCommandFails(t *testing.T) {
    11  	t.Cleanup(func() {
    12  		run = realRun
    13  	})
    14  	run = func(opts runOpts) (string, error) {
    15  		return "", errors.New("test error")
    16  	}
    17  	gitCreds := GitCredentials{GitPath: "git"}
    18  	creds, err := gitCreds.CredentialForHost("test-host")
    19  	assert.Nil(t, creds)
    20  	assert.Error(t, err)
    21  }
    22  
    23  func TestGitCredentialsReturnsNoCredentialsWhenNoGitHelperIsProvided(t *testing.T) {
    24  	t.Cleanup(func() {
    25  		run = realRun
    26  	})
    27  	run = func(opts runOpts) (string, error) {
    28  		return "", nil
    29  	}
    30  	gitCreds := GitCredentials{GitPath: "git"}
    31  	creds, err := gitCreds.CredentialForHost("test-host")
    32  	assert.Nil(t, creds)
    33  	assert.Error(t, err)
    34  }
    35  
    36  func TestGitCredentialsReturnsCredentialsWhenCredentialsAreFound(t *testing.T) {
    37  	t.Cleanup(func() {
    38  		run = realRun
    39  	})
    40  	run = func(opts runOpts) (string, error) {
    41  		assert.Equal(t, "git", opts.path)
    42  		assert.Equal(t, []string{"credential", "fill"}, opts.args)
    43  		assert.Equal(t, "protocol=https\nhost=test-host", opts.stdin)
    44  		return "protocol=https\nhost=test-host\nusername=PersonalAccessToken\npassword=secure-password", nil
    45  	}
    46  	gitCreds := GitCredentials{GitPath: "git"}
    47  	creds, err := gitCreds.CredentialForHost("test-host")
    48  	assert.Equal(t, &Credentials{Username: "PersonalAccessToken", Password: "secure-password"}, creds)
    49  	assert.NoError(t, err)
    50  }
    51  
    52  func TestGitCredentialsReturnsNoCredentialsWhenCredentialsReturnNoPassowrd(t *testing.T) {
    53  	t.Cleanup(func() {
    54  		run = realRun
    55  	})
    56  	run = func(opts runOpts) (string, error) {
    57  		assert.Equal(t, "git", opts.path)
    58  		assert.Equal(t, []string{"credential", "fill"}, opts.args)
    59  		assert.Equal(t, "protocol=https\nhost=test-host", opts.stdin)
    60  		return "protocol=https\nhost=test-host\nusername=PersonalAccessToken\n", nil
    61  	}
    62  	gitCreds := GitCredentials{GitPath: "git"}
    63  	creds, err := gitCreds.CredentialForHost("test-host")
    64  	assert.Nil(t, creds)
    65  	assert.Error(t, err)
    66  }