github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/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/ungtb10d/cli/v2/pkg/iostreams" 9 ) 10 11 // why not just use the config stub argh 12 type tinyConfig map[string]string 13 14 func (c tinyConfig) AuthToken(host string) (string, string) { 15 return c[fmt.Sprintf("%s:%s", host, "oauth_token")], c["_source"] 16 } 17 18 func (c tinyConfig) Get(host, key string) (string, error) { 19 return c[fmt.Sprintf("%s:%s", host, key)], nil 20 } 21 22 func Test_helperRun(t *testing.T) { 23 tests := []struct { 24 name string 25 opts CredentialOptions 26 input string 27 wantStdout string 28 wantStderr string 29 wantErr bool 30 }{ 31 { 32 name: "host only, credentials found", 33 opts: CredentialOptions{ 34 Operation: "get", 35 Config: func() (config, error) { 36 return tinyConfig{ 37 "_source": "/Users/monalisa/.config/gh/hosts.yml", 38 "example.com:user": "monalisa", 39 "example.com:oauth_token": "OTOKEN", 40 }, nil 41 }, 42 }, 43 input: heredoc.Doc(` 44 protocol=https 45 host=example.com 46 `), 47 wantErr: false, 48 wantStdout: heredoc.Doc(` 49 protocol=https 50 host=example.com 51 username=monalisa 52 password=OTOKEN 53 `), 54 wantStderr: "", 55 }, 56 { 57 name: "host plus user", 58 opts: CredentialOptions{ 59 Operation: "get", 60 Config: func() (config, error) { 61 return tinyConfig{ 62 "_source": "/Users/monalisa/.config/gh/hosts.yml", 63 "example.com:user": "monalisa", 64 "example.com:oauth_token": "OTOKEN", 65 }, nil 66 }, 67 }, 68 input: heredoc.Doc(` 69 protocol=https 70 host=example.com 71 username=monalisa 72 `), 73 wantErr: false, 74 wantStdout: heredoc.Doc(` 75 protocol=https 76 host=example.com 77 username=monalisa 78 password=OTOKEN 79 `), 80 wantStderr: "", 81 }, 82 { 83 name: "gist host", 84 opts: CredentialOptions{ 85 Operation: "get", 86 Config: func() (config, error) { 87 return tinyConfig{ 88 "_source": "/Users/monalisa/.config/gh/hosts.yml", 89 "github.com:user": "monalisa", 90 "github.com:oauth_token": "OTOKEN", 91 }, nil 92 }, 93 }, 94 input: heredoc.Doc(` 95 protocol=https 96 host=gist.github.com 97 username=monalisa 98 `), 99 wantErr: false, 100 wantStdout: heredoc.Doc(` 101 protocol=https 102 host=gist.github.com 103 username=monalisa 104 password=OTOKEN 105 `), 106 wantStderr: "", 107 }, 108 { 109 name: "url input", 110 opts: CredentialOptions{ 111 Operation: "get", 112 Config: func() (config, error) { 113 return tinyConfig{ 114 "_source": "/Users/monalisa/.config/gh/hosts.yml", 115 "example.com:user": "monalisa", 116 "example.com:oauth_token": "OTOKEN", 117 }, nil 118 }, 119 }, 120 input: heredoc.Doc(` 121 url=https://monalisa@example.com 122 `), 123 wantErr: false, 124 wantStdout: heredoc.Doc(` 125 protocol=https 126 host=example.com 127 username=monalisa 128 password=OTOKEN 129 `), 130 wantStderr: "", 131 }, 132 { 133 name: "host only, no credentials found", 134 opts: CredentialOptions{ 135 Operation: "get", 136 Config: func() (config, error) { 137 return tinyConfig{ 138 "_source": "/Users/monalisa/.config/gh/hosts.yml", 139 "example.com:user": "monalisa", 140 }, nil 141 }, 142 }, 143 input: heredoc.Doc(` 144 protocol=https 145 host=example.com 146 `), 147 wantErr: true, 148 wantStdout: "", 149 wantStderr: "", 150 }, 151 { 152 name: "user mismatch", 153 opts: CredentialOptions{ 154 Operation: "get", 155 Config: func() (config, error) { 156 return tinyConfig{ 157 "_source": "/Users/monalisa/.config/gh/hosts.yml", 158 "example.com:user": "monalisa", 159 "example.com:oauth_token": "OTOKEN", 160 }, nil 161 }, 162 }, 163 input: heredoc.Doc(` 164 protocol=https 165 host=example.com 166 username=hubot 167 `), 168 wantErr: true, 169 wantStdout: "", 170 wantStderr: "", 171 }, 172 { 173 name: "no username configured", 174 opts: CredentialOptions{ 175 Operation: "get", 176 Config: func() (config, error) { 177 return tinyConfig{ 178 "_source": "/Users/monalisa/.config/gh/hosts.yml", 179 "example.com:oauth_token": "OTOKEN", 180 }, nil 181 }, 182 }, 183 input: heredoc.Doc(` 184 protocol=https 185 host=example.com 186 `), 187 wantErr: false, 188 wantStdout: heredoc.Doc(` 189 protocol=https 190 host=example.com 191 username=x-access-token 192 password=OTOKEN 193 `), 194 wantStderr: "", 195 }, 196 { 197 name: "token from env", 198 opts: CredentialOptions{ 199 Operation: "get", 200 Config: func() (config, error) { 201 return tinyConfig{ 202 "_source": "GITHUB_ENTERPRISE_TOKEN", 203 "example.com:oauth_token": "OTOKEN", 204 }, nil 205 }, 206 }, 207 input: heredoc.Doc(` 208 protocol=https 209 host=example.com 210 username=hubot 211 `), 212 wantErr: false, 213 wantStdout: heredoc.Doc(` 214 protocol=https 215 host=example.com 216 username=x-access-token 217 password=OTOKEN 218 `), 219 wantStderr: "", 220 }, 221 } 222 for _, tt := range tests { 223 t.Run(tt.name, func(t *testing.T) { 224 ios, stdin, stdout, stderr := iostreams.Test() 225 fmt.Fprint(stdin, tt.input) 226 opts := &tt.opts 227 opts.IO = ios 228 if err := helperRun(opts); (err != nil) != tt.wantErr { 229 t.Fatalf("helperRun() error = %v, wantErr %v", err, tt.wantErr) 230 } 231 if tt.wantStdout != stdout.String() { 232 t.Errorf("stdout: got %q, wants %q", stdout.String(), tt.wantStdout) 233 } 234 if tt.wantStderr != stderr.String() { 235 t.Errorf("stderr: got %q, wants %q", stderr.String(), tt.wantStderr) 236 } 237 }) 238 } 239 }