github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/auth/shared/git_credential_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ungtb10d/cli/v2/git"
     7  	"github.com/ungtb10d/cli/v2/internal/run"
     8  )
     9  
    10  func TestGitCredentialSetup_configureExisting(t *testing.T) {
    11  	cs, restoreRun := run.Stub()
    12  	defer restoreRun(t)
    13  	cs.Register(`git credential reject`, 0, "")
    14  	cs.Register(`git credential approve`, 0, "")
    15  
    16  	f := GitCredentialFlow{
    17  		Executable: "gh",
    18  		helper:     "osxkeychain",
    19  		GitClient:  &git.Client{GitPath: "some/path/git"},
    20  	}
    21  
    22  	if err := f.gitCredentialSetup("example.com", "monalisa", "PASSWD"); err != nil {
    23  		t.Errorf("GitCredentialSetup() error = %v", err)
    24  	}
    25  }
    26  
    27  func TestGitCredentialsSetup_setOurs_GH(t *testing.T) {
    28  	cs, restoreRun := run.Stub()
    29  	defer restoreRun(t)
    30  	cs.Register(`git config --global --replace-all credential\.`, 0, "", func(args []string) {
    31  		if key := args[len(args)-2]; key != "credential.https://github.com.helper" {
    32  			t.Errorf("git config key was %q", key)
    33  		}
    34  		if val := args[len(args)-1]; val != "" {
    35  			t.Errorf("global credential helper configured to %q", val)
    36  		}
    37  	})
    38  	cs.Register(`git config --global --add credential\.`, 0, "", func(args []string) {
    39  		if key := args[len(args)-2]; key != "credential.https://github.com.helper" {
    40  			t.Errorf("git config key was %q", key)
    41  		}
    42  		if val := args[len(args)-1]; val != "!/path/to/gh auth git-credential" {
    43  			t.Errorf("global credential helper configured to %q", val)
    44  		}
    45  	})
    46  	cs.Register(`git config --global --replace-all credential\.`, 0, "", func(args []string) {
    47  		if key := args[len(args)-2]; key != "credential.https://gist.github.com.helper" {
    48  			t.Errorf("git config key was %q", key)
    49  		}
    50  		if val := args[len(args)-1]; val != "" {
    51  			t.Errorf("global credential helper configured to %q", val)
    52  		}
    53  	})
    54  	cs.Register(`git config --global --add credential\.`, 0, "", func(args []string) {
    55  		if key := args[len(args)-2]; key != "credential.https://gist.github.com.helper" {
    56  			t.Errorf("git config key was %q", key)
    57  		}
    58  		if val := args[len(args)-1]; val != "!/path/to/gh auth git-credential" {
    59  			t.Errorf("global credential helper configured to %q", val)
    60  		}
    61  	})
    62  
    63  	f := GitCredentialFlow{
    64  		Executable: "/path/to/gh",
    65  		helper:     "",
    66  		GitClient:  &git.Client{GitPath: "some/path/git"},
    67  	}
    68  
    69  	if err := f.gitCredentialSetup("github.com", "monalisa", "PASSWD"); err != nil {
    70  		t.Errorf("GitCredentialSetup() error = %v", err)
    71  	}
    72  
    73  }
    74  
    75  func TestGitCredentialSetup_setOurs_nonGH(t *testing.T) {
    76  	cs, restoreRun := run.Stub()
    77  	defer restoreRun(t)
    78  	cs.Register(`git config --global --replace-all credential\.`, 0, "", func(args []string) {
    79  		if key := args[len(args)-2]; key != "credential.https://example.com.helper" {
    80  			t.Errorf("git config key was %q", key)
    81  		}
    82  		if val := args[len(args)-1]; val != "" {
    83  			t.Errorf("global credential helper configured to %q", val)
    84  		}
    85  	})
    86  	cs.Register(`git config --global --add credential\.`, 0, "", func(args []string) {
    87  		if key := args[len(args)-2]; key != "credential.https://example.com.helper" {
    88  			t.Errorf("git config key was %q", key)
    89  		}
    90  		if val := args[len(args)-1]; val != "!/path/to/gh auth git-credential" {
    91  			t.Errorf("global credential helper configured to %q", val)
    92  		}
    93  	})
    94  
    95  	f := GitCredentialFlow{
    96  		Executable: "/path/to/gh",
    97  		helper:     "",
    98  		GitClient:  &git.Client{GitPath: "some/path/git"},
    99  	}
   100  
   101  	if err := f.gitCredentialSetup("example.com", "monalisa", "PASSWD"); err != nil {
   102  		t.Errorf("GitCredentialSetup() error = %v", err)
   103  	}
   104  }
   105  
   106  func Test_isOurCredentialHelper(t *testing.T) {
   107  	tests := []struct {
   108  		name string
   109  		arg  string
   110  		want bool
   111  	}{
   112  		{
   113  			name: "blank",
   114  			arg:  "",
   115  			want: false,
   116  		},
   117  		{
   118  			name: "invalid",
   119  			arg:  "!",
   120  			want: false,
   121  		},
   122  		{
   123  			name: "osxkeychain",
   124  			arg:  "osxkeychain",
   125  			want: false,
   126  		},
   127  		{
   128  			name: "looks like gh but isn't",
   129  			arg:  "gh auth",
   130  			want: false,
   131  		},
   132  		{
   133  			name: "ours",
   134  			arg:  "!/path/to/gh auth",
   135  			want: true,
   136  		},
   137  	}
   138  	for _, tt := range tests {
   139  		t.Run(tt.name, func(t *testing.T) {
   140  			if got := isOurCredentialHelper(tt.arg); got != tt.want {
   141  				t.Errorf("isOurCredentialHelper() = %v, want %v", got, tt.want)
   142  			}
   143  		})
   144  	}
   145  }