github.com/secman-team/gh-api@v1.8.2/pkg/cmd/auth/shared/git_credential_test.go (about) 1 package shared 2 3 import ( 4 "testing" 5 6 "github.com/secman-team/gh-api/core/run" 7 ) 8 9 func TestGitCredentialSetup_configureExisting(t *testing.T) { 10 cs, restoreRun := run.Stub() 11 defer restoreRun(t) 12 cs.Register(`git credential reject`, 0, "") 13 cs.Register(`git credential approve`, 0, "") 14 15 f := GitCredentialFlow{ 16 Executable: "gh", 17 helper: "osxkeychain", 18 } 19 20 if err := f.gitCredentialSetup("example.com", "monalisa", "PASSWD"); err != nil { 21 t.Errorf("GitCredentialSetup() error = %v", err) 22 } 23 } 24 25 func TestGitCredentialSetup_setOurs(t *testing.T) { 26 cs, restoreRun := run.Stub() 27 defer restoreRun(t) 28 cs.Register(`git config --global credential\.`, 0, "", func(args []string) { 29 if key := args[len(args)-2]; key != "credential.https://example.com.helper" { 30 t.Errorf("git config key was %q", key) 31 } 32 if val := args[len(args)-1]; val != "" { 33 t.Errorf("global credential helper configured to %q", val) 34 } 35 }) 36 cs.Register(`git config --global --add credential\.`, 0, "", func(args []string) { 37 if key := args[len(args)-2]; key != "credential.https://example.com.helper" { 38 t.Errorf("git config key was %q", key) 39 } 40 if val := args[len(args)-1]; val != "!/path/to/gh auth git-credential" { 41 t.Errorf("global credential helper configured to %q", val) 42 } 43 }) 44 45 f := GitCredentialFlow{ 46 Executable: "/path/to/gh", 47 helper: "", 48 } 49 50 if err := f.gitCredentialSetup("example.com", "monalisa", "PASSWD"); err != nil { 51 t.Errorf("GitCredentialSetup() error = %v", err) 52 } 53 } 54 55 func Test_isOurCredentialHelper(t *testing.T) { 56 tests := []struct { 57 name string 58 arg string 59 want bool 60 }{ 61 { 62 name: "blank", 63 arg: "", 64 want: false, 65 }, 66 { 67 name: "invalid", 68 arg: "!", 69 want: false, 70 }, 71 { 72 name: "osxkeychain", 73 arg: "osxkeychain", 74 want: false, 75 }, 76 { 77 name: "looks like gh but isn't", 78 arg: "gh auth", 79 want: false, 80 }, 81 { 82 name: "ours", 83 arg: "!/path/to/gh auth", 84 want: true, 85 }, 86 } 87 for _, tt := range tests { 88 t.Run(tt.name, func(t *testing.T) { 89 if got := isOurCredentialHelper(tt.arg); got != tt.want { 90 t.Errorf("isOurCredentialHelper() = %v, want %v", got, tt.want) 91 } 92 }) 93 } 94 }