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
|
package cmdutil
import (
"testing"
"github.com/cli/cli/v2/internal/config"
"github.com/stretchr/testify/require"
)
func Test_CheckAuth(t *testing.T) {
tests := []struct {
name string
env map[string]string
cfgStubs func(*testing.T, config.Config)
expected bool
}{
{
name: "no known hosts, no env auth token",
expected: false,
},
{
name: "no known hosts, env auth token",
env: map[string]string{"GITHUB_TOKEN": "token"},
expected: true,
},
{
name: "known host",
cfgStubs: func(t *testing.T, c config.Config) {
_, err := c.Authentication().Login("github.com", "test-user", "test-token", "https", false)
require.NoError(t, err)
},
expected: true,
},
{
name: "enterprise token",
env: map[string]string{"GH_ENTERPRISE_TOKEN": "token"},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(t, cfg)
}
for k, v := range tt.env {
t.Setenv(k, v)
}
require.Equal(t, tt.expected, CheckAuth(cfg))
})
}
}
|