github.com/secman-team/gh-api@v1.8.2/pkg/cmd/auth/gitcredential/helper_test.go (about) 1 package login 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/MakeNowJust/heredoc" 8 "github.com/secman-team/gh-api/pkg/iostreams" 9 ) 10 11 type tinyConfig map[string]string 12 13 func (c tinyConfig) GetWithSource(host, key string) (string, string, error) { 14 return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil 15 } 16 17 func Test_helperRun(t *testing.T) { 18 tests := []struct { 19 name string 20 opts CredentialOptions 21 input string 22 wantStdout string 23 wantStderr string 24 wantErr bool 25 }{ 26 { 27 name: "host only, credentials found", 28 opts: CredentialOptions{ 29 Operation: "get", 30 Config: func() (config, error) { 31 return tinyConfig{ 32 "_source": "/Users/monalisa/.config/gh/hosts.yml", 33 "example.com:user": "monalisa", 34 "example.com:oauth_token": "OTOKEN", 35 }, nil 36 }, 37 }, 38 input: heredoc.Doc(` 39 protocol=https 40 host=example.com 41 `), 42 wantErr: false, 43 wantStdout: heredoc.Doc(` 44 protocol=https 45 host=example.com 46 username=monalisa 47 password=OTOKEN 48 `), 49 wantStderr: "", 50 }, 51 { 52 name: "host plus user", 53 opts: CredentialOptions{ 54 Operation: "get", 55 Config: func() (config, error) { 56 return tinyConfig{ 57 "_source": "/Users/monalisa/.config/gh/hosts.yml", 58 "example.com:user": "monalisa", 59 "example.com:oauth_token": "OTOKEN", 60 }, nil 61 }, 62 }, 63 input: heredoc.Doc(` 64 protocol=https 65 host=example.com 66 username=monalisa 67 `), 68 wantErr: false, 69 wantStdout: heredoc.Doc(` 70 protocol=https 71 host=example.com 72 username=monalisa 73 password=OTOKEN 74 `), 75 wantStderr: "", 76 }, 77 { 78 name: "url input", 79 opts: CredentialOptions{ 80 Operation: "get", 81 Config: func() (config, error) { 82 return tinyConfig{ 83 "_source": "/Users/monalisa/.config/gh/hosts.yml", 84 "example.com:user": "monalisa", 85 "example.com:oauth_token": "OTOKEN", 86 }, nil 87 }, 88 }, 89 input: heredoc.Doc(` 90 url=https://monalisa@example.com 91 `), 92 wantErr: false, 93 wantStdout: heredoc.Doc(` 94 protocol=https 95 host=example.com 96 username=monalisa 97 password=OTOKEN 98 `), 99 wantStderr: "", 100 }, 101 { 102 name: "host only, no credentials found", 103 opts: CredentialOptions{ 104 Operation: "get", 105 Config: func() (config, error) { 106 return tinyConfig{ 107 "_source": "/Users/monalisa/.config/gh/hosts.yml", 108 "example.com:user": "monalisa", 109 }, nil 110 }, 111 }, 112 input: heredoc.Doc(` 113 protocol=https 114 host=example.com 115 `), 116 wantErr: true, 117 wantStdout: "", 118 wantStderr: "", 119 }, 120 { 121 name: "user mismatch", 122 opts: CredentialOptions{ 123 Operation: "get", 124 Config: func() (config, error) { 125 return tinyConfig{ 126 "_source": "/Users/monalisa/.config/gh/hosts.yml", 127 "example.com:user": "monalisa", 128 "example.com:oauth_token": "OTOKEN", 129 }, nil 130 }, 131 }, 132 input: heredoc.Doc(` 133 protocol=https 134 host=example.com 135 username=hubot 136 `), 137 wantErr: true, 138 wantStdout: "", 139 wantStderr: "", 140 }, 141 { 142 name: "token from env", 143 opts: CredentialOptions{ 144 Operation: "get", 145 Config: func() (config, error) { 146 return tinyConfig{ 147 "_source": "GITHUB_ENTERPRISE_TOKEN", 148 "example.com:oauth_token": "OTOKEN", 149 }, nil 150 }, 151 }, 152 input: heredoc.Doc(` 153 protocol=https 154 host=example.com 155 username=hubot 156 `), 157 wantErr: false, 158 wantStdout: heredoc.Doc(` 159 protocol=https 160 host=example.com 161 username=x-access-token 162 password=OTOKEN 163 `), 164 wantStderr: "", 165 }, 166 } 167 for _, tt := range tests { 168 t.Run(tt.name, func(t *testing.T) { 169 io, stdin, stdout, stderr := iostreams.Test() 170 fmt.Fprint(stdin, tt.input) 171 opts := &tt.opts 172 opts.IO = io 173 if err := helperRun(opts); (err != nil) != tt.wantErr { 174 t.Fatalf("helperRun() error = %v, wantErr %v", err, tt.wantErr) 175 } 176 if tt.wantStdout != stdout.String() { 177 t.Errorf("stdout: got %q, wants %q", stdout.String(), tt.wantStdout) 178 } 179 if tt.wantStderr != stderr.String() { 180 t.Errorf("stderr: got %q, wants %q", stderr.String(), tt.wantStderr) 181 } 182 }) 183 } 184 }