GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( f5d62c...a7ec9d )
by Jackson
08:28
created

providers.Test_configuration   A

Complexity

Conditions 1

Size

Total Lines 40
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 31
nop 1
dl 0
loc 40
rs 9.1359
c 0
b 0
f 0
1
package providers
2
3
import (
4
	"os"
5
	"path"
6
	"strings"
7
	"testing"
8
9
	"github.com/aliyun/credentials-go/credentials/internal/utils"
10
	"github.com/stretchr/testify/assert"
11
)
12
13
func TestCLIProfileCredentialsProvider(t *testing.T) {
14
	rollback := utils.Memory("ALIBABA_CLOUD_PROFILE")
15
	defer rollback()
16
	b := NewCLIProfileCredentialsProviderBuilder().Build()
17
	assert.Equal(t, "", b.profileName)
18
19
	// get from env
20
	os.Setenv("ALIBABA_CLOUD_PROFILE", "custom_profile")
21
	b = NewCLIProfileCredentialsProviderBuilder().Build()
22
	assert.Equal(t, "custom_profile", b.profileName)
23
24
	b = NewCLIProfileCredentialsProviderBuilder().WithProfileName("profilename").Build()
25
	assert.Equal(t, "profilename", b.profileName)
26
}
27
28
func Test_configuration(t *testing.T) {
29
	wd, _ := os.Getwd()
30
	_, err := newConfigurationFromPath(path.Join(wd, "fixtures/inexist_cli_config.json"))
31
	assert.NotNil(t, err)
32
	assert.True(t, strings.HasPrefix(err.Error(), "reading aliyun cli config from "))
33
34
	_, err = newConfigurationFromPath(path.Join(wd, "fixtures/invalid_cli_config.json"))
35
	assert.NotNil(t, err)
36
	assert.True(t, strings.HasPrefix(err.Error(), "unmarshal aliyun cli config from "))
37
38
	_, err = newConfigurationFromPath(path.Join(wd, "fixtures/mock_empty_cli_config.json"))
39
	assert.True(t, strings.HasPrefix(err.Error(), "no any configured profiles in "))
40
41
	conf, err := newConfigurationFromPath(path.Join(wd, "fixtures/mock_cli_config.json"))
42
	assert.Nil(t, err)
43
	assert.Equal(t, &configuration{
44
		Current: "default",
45
		Profiles: []*profile{
46
			{
47
				Mode:            "AK",
48
				Name:            "default",
49
				AccessKeyID:     "akid",
50
				AccessKeySecret: "secret",
51
			},
52
			{
53
				Mode:            "AK",
54
				Name:            "jacksontian",
55
				AccessKeyID:     "akid",
56
				AccessKeySecret: "secret",
57
			},
58
		},
59
	}, conf)
60
61
	_, err = conf.getProfile("inexists")
62
	assert.EqualError(t, err, "unable to get profile with 'inexists'")
63
64
	p, err := conf.getProfile("jacksontian")
65
	assert.Nil(t, err)
66
	assert.Equal(t, p.Name, "jacksontian")
67
	assert.Equal(t, p.Mode, "AK")
68
}
69
70
func TestCLIProfileCredentialsProvider_getCredentialsProvider(t *testing.T) {
71
	conf := &configuration{
72
		Current: "AK",
73
		Profiles: []*profile{
74
			{
75
				Mode:            "AK",
76
				Name:            "AK",
77
				AccessKeyID:     "akid",
78
				AccessKeySecret: "secret",
79
			},
80
			{
81
				Mode:            "RamRoleArn",
82
				Name:            "RamRoleArn",
83
				AccessKeyID:     "akid",
84
				AccessKeySecret: "secret",
85
				RoleArn:         "arn",
86
			},
87
			{
88
				Mode: "RamRoleArn",
89
				Name: "Invalid_RamRoleArn",
90
			},
91
			{
92
				Mode:     "EcsRamRole",
93
				Name:     "EcsRamRole",
94
				RoleName: "rolename",
95
			},
96
			{
97
				Mode:            "OIDC",
98
				Name:            "OIDC",
99
				RoleArn:         "role_arn",
100
				OIDCTokenFile:   "path/to/oidc/file",
101
				OIDCProviderARN: "provider_arn",
102
			},
103
			{
104
				Mode:          "ChainableRamRoleArn",
105
				Name:          "ChainableRamRoleArn",
106
				RoleArn:       "arn",
107
				SourceProfile: "AK",
108
			},
109
			{
110
				Mode:          "ChainableRamRoleArn",
111
				Name:          "ChainableRamRoleArn2",
112
				SourceProfile: "InvalidSource",
113
			},
114
			{
115
				Mode: "Unsupported",
116
				Name: "Unsupported",
117
			},
118
		},
119
	}
120
121
	provider := NewCLIProfileCredentialsProviderBuilder().Build()
122
	_, err := provider.getCredentialsProvider(conf, "inexist")
123
	assert.EqualError(t, err, "unable to get profile with 'inexist'")
124
125
	// AK
126
	cp, err := provider.getCredentialsProvider(conf, "AK")
127
	assert.Nil(t, err)
128
	akcp, ok := cp.(*StaticAKCredentialsProvider)
129
	assert.True(t, ok)
130
	cc, err := akcp.GetCredentials()
131
	assert.Nil(t, err)
132
	assert.Equal(t, cc, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", ProviderName: "static_ak"})
133
	// RamRoleArn
134
	cp, err = provider.getCredentialsProvider(conf, "RamRoleArn")
135
	assert.Nil(t, err)
136
	_, ok = cp.(*RAMRoleARNCredentialsProvider)
137
	assert.True(t, ok)
138
	// RamRoleArn invalid ak
139
	_, err = provider.getCredentialsProvider(conf, "Invalid_RamRoleArn")
140
	assert.EqualError(t, err, "the access key id is empty")
141
	// EcsRamRole
142
	cp, err = provider.getCredentialsProvider(conf, "EcsRamRole")
143
	assert.Nil(t, err)
144
	_, ok = cp.(*ECSRAMRoleCredentialsProvider)
145
	assert.True(t, ok)
146
	// OIDC
147
	cp, err = provider.getCredentialsProvider(conf, "OIDC")
148
	assert.Nil(t, err)
149
	_, ok = cp.(*OIDCCredentialsProvider)
150
	assert.True(t, ok)
151
152
	// ChainableRamRoleArn
153
	cp, err = provider.getCredentialsProvider(conf, "ChainableRamRoleArn")
154
	assert.Nil(t, err)
155
	_, ok = cp.(*RAMRoleARNCredentialsProvider)
156
	assert.True(t, ok)
157
158
	// ChainableRamRoleArn with invalid source profile
159
	_, err = provider.getCredentialsProvider(conf, "ChainableRamRoleArn2")
160
	assert.EqualError(t, err, "get source profile failed: unable to get profile with 'InvalidSource'")
161
162
	// Unsupported
163
	_, err = provider.getCredentialsProvider(conf, "Unsupported")
164
	assert.EqualError(t, err, "unsupported profile mode 'Unsupported'")
165
}
166
167
func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) {
168
	defer func() {
169
		getHomePath = utils.GetHomePath
170
	}()
171
172
	getHomePath = func() string {
173
		return ""
174
	}
175
	provider := NewCLIProfileCredentialsProviderBuilder().Build()
176
	_, err := provider.GetCredentials()
177
	assert.EqualError(t, err, "cannot found home dir")
178
179
	getHomePath = func() string {
180
		return "/path/invalid/home/dir"
181
	}
182
	provider = NewCLIProfileCredentialsProviderBuilder().Build()
183
	_, err = provider.GetCredentials()
184
	assert.EqualError(t, err, "reading aliyun cli config from '/path/invalid/home/dir/.aliyun/config.json' failed open /path/invalid/home/dir/.aliyun/config.json: no such file or directory")
185
186
	getHomePath = func() string {
187
		wd, _ := os.Getwd()
188
		return path.Join(wd, "fixtures")
189
	}
190
191
	// get credentials by current profile
192
	provider = NewCLIProfileCredentialsProviderBuilder().Build()
193
	cc, err := provider.GetCredentials()
194
	assert.Nil(t, err)
195
	assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", ProviderName: "cli_profile/static_ak"}, cc)
196
197
	provider = NewCLIProfileCredentialsProviderBuilder().WithProfileName("inexist").Build()
198
	_, err = provider.GetCredentials()
199
	assert.EqualError(t, err, "unable to get profile with 'inexist'")
200
201
	// The get_credentials_error profile is invalid
202
	provider = NewCLIProfileCredentialsProviderBuilder().WithProfileName("get_credentials_error").Build()
203
	_, err = provider.GetCredentials()
204
	assert.Contains(t, err.Error(), "InvalidAccessKeyId.NotFound")
205
}
206