File: auth_check_test.go

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (55 lines) | stat: -rw-r--r-- 1,142 bytes parent folder | download | duplicates (2)
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))
		})
	}
}