1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
package auth
import (
"testing"
"github.com/cli/go-gh/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestTokenForHost(t *testing.T) {
tests := []struct {
name string
host string
githubToken string
githubEnterpriseToken string
ghToken string
ghEnterpriseToken string
config *config.Config
wantToken string
wantSource string
wantNotFound bool
}{
{
name: "token for github.com with no env tokens and no config token",
host: "github.com",
config: testNoHostsConfig(),
wantToken: "",
wantSource: "oauth_token",
wantNotFound: true,
},
{
name: "token for enterprise.com with no env tokens and no config token",
host: "enterprise.com",
config: testNoHostsConfig(),
wantToken: "",
wantSource: "oauth_token",
wantNotFound: true,
},
{
name: "token for github.com with GH_TOKEN, GITHUB_TOKEN, and config token",
host: "github.com",
ghToken: "GH_TOKEN",
githubToken: "GITHUB_TOKEN",
config: testHostsConfig(),
wantToken: "GH_TOKEN",
wantSource: "GH_TOKEN",
},
{
name: "token for github.com with GITHUB_TOKEN, and config token",
host: "github.com",
githubToken: "GITHUB_TOKEN",
config: testHostsConfig(),
wantToken: "GITHUB_TOKEN",
wantSource: "GITHUB_TOKEN",
},
{
name: "token for github.com with config token",
host: "github.com",
config: testHostsConfig(),
wantToken: "xxxxxxxxxxxxxxxxxxxx",
wantSource: "oauth_token",
},
{
name: "token for enterprise.com with GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN, and config token",
host: "enterprise.com",
ghEnterpriseToken: "GH_ENTERPRISE_TOKEN",
githubEnterpriseToken: "GITHUB_ENTERPRISE_TOKEN",
config: testHostsConfig(),
wantToken: "GH_ENTERPRISE_TOKEN",
wantSource: "GH_ENTERPRISE_TOKEN",
},
{
name: "token for enterprise.com with GITHUB_ENTERPRISE_TOKEN, and config token",
host: "enterprise.com",
githubEnterpriseToken: "GITHUB_ENTERPRISE_TOKEN",
config: testHostsConfig(),
wantToken: "GITHUB_ENTERPRISE_TOKEN",
wantSource: "GITHUB_ENTERPRISE_TOKEN",
},
{
name: "token for enterprise.com with config token",
host: "enterprise.com",
config: testHostsConfig(),
wantToken: "yyyyyyyyyyyyyyyyyyyy",
wantSource: "oauth_token",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("GITHUB_TOKEN", tt.githubToken)
t.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.githubEnterpriseToken)
t.Setenv("GH_TOKEN", tt.ghToken)
t.Setenv("GH_ENTERPRISE_TOKEN", tt.ghEnterpriseToken)
token, source := tokenForHost(tt.config, tt.host)
assert.Equal(t, tt.wantToken, token)
assert.Equal(t, tt.wantSource, source)
})
}
}
func TestDefaultHost(t *testing.T) {
tests := []struct {
name string
config *config.Config
ghHost string
wantHost string
wantSource string
wantNotFound bool
}{
{
name: "GH_HOST if set",
config: testHostsConfig(),
ghHost: "test.com",
wantHost: "test.com",
wantSource: "GH_HOST",
},
{
name: "authenticated host if only one",
config: testSingleHostConfig(),
wantHost: "enterprise.com",
wantSource: "hosts",
},
{
name: "default host if more than one authenticated host",
config: testHostsConfig(),
wantHost: "github.com",
wantSource: "default",
wantNotFound: true,
},
{
name: "default host if no authenticated host",
config: testNoHostsConfig(),
wantHost: "github.com",
wantSource: "default",
wantNotFound: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.ghHost != "" {
t.Setenv("GH_HOST", tt.ghHost)
}
host, source := defaultHost(tt.config)
assert.Equal(t, tt.wantHost, host)
assert.Equal(t, tt.wantSource, source)
})
}
}
func TestKnownHosts(t *testing.T) {
tests := []struct {
name string
config *config.Config
ghHost string
ghToken string
wantHosts []string
}{
{
name: "no known hosts",
config: testNoHostsConfig(),
wantHosts: []string{},
},
{
name: "includes GH_HOST",
config: testNoHostsConfig(),
ghHost: "test.com",
wantHosts: []string{"test.com"},
},
{
name: "includes authenticated hosts",
config: testHostsConfig(),
wantHosts: []string{"github.com", "enterprise.com"},
},
{
name: "includes default host if environment auth token",
config: testNoHostsConfig(),
ghToken: "TOKEN",
wantHosts: []string{"github.com"},
},
{
name: "deduplicates hosts",
config: testHostsConfig(),
ghHost: "test.com",
ghToken: "TOKEN",
wantHosts: []string{"test.com", "github.com", "enterprise.com"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.ghHost != "" {
t.Setenv("GH_HOST", tt.ghHost)
}
if tt.ghToken != "" {
t.Setenv("GH_TOKEN", tt.ghToken)
}
hosts := knownHosts(tt.config)
assert.Equal(t, tt.wantHosts, hosts)
})
}
}
func TestIsEnterprise(t *testing.T) {
tests := []struct {
name string
host string
wantOut bool
}{
{
name: "github",
host: "github.com",
wantOut: false,
},
{
name: "localhost",
host: "github.localhost",
wantOut: false,
},
{
name: "enterprise",
host: "mygithub.com",
wantOut: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := isEnterprise(tt.host)
assert.Equal(t, tt.wantOut, out)
})
}
}
func TestNormalizeHostname(t *testing.T) {
tests := []struct {
name string
host string
wantHost string
}{
{
name: "github domain",
host: "test.github.com",
wantHost: "github.com",
},
{
name: "capitalized",
host: "GitHub.com",
wantHost: "github.com",
},
{
name: "localhost domain",
host: "test.github.localhost",
wantHost: "github.localhost",
},
{
name: "enterprise domain",
host: "mygithub.com",
wantHost: "mygithub.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
normalized := normalizeHostname(tt.host)
assert.Equal(t, tt.wantHost, normalized)
})
}
}
func testNoHostsConfig() *config.Config {
var data = ``
return config.ReadFromString(data)
}
func testSingleHostConfig() *config.Config {
var data = `
hosts:
enterprise.com:
user: user2
oauth_token: yyyyyyyyyyyyyyyyyyyy
git_protocol: https
`
return config.ReadFromString(data)
}
func testHostsConfig() *config.Config {
var data = `
hosts:
github.com:
user: user1
oauth_token: xxxxxxxxxxxxxxxxxxxx
git_protocol: ssh
enterprise.com:
user: user2
oauth_token: yyyyyyyyyyyyyyyyyyyy
git_protocol: https
`
return config.ReadFromString(data)
}
|