Conditions | 8 |
Total Lines | 111 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package providers |
||
137 | func TestOIDCCredentialsProvider_getCredentials(t *testing.T) { |
||
138 | originHttpDo := httpDo |
||
139 | defer func() { httpDo = originHttpDo }() |
||
140 | |||
141 | // case 0: invalid oidc token file path |
||
142 | p, err := NewOIDCCredentialsProviderBuilder(). |
||
143 | WithOIDCTokenFilePath("/path/to/invalid/oidc.token"). |
||
144 | WithOIDCProviderARN("provider-arn"). |
||
145 | WithRoleArn("roleArn"). |
||
146 | WithRoleSessionName("rsn"). |
||
147 | WithStsRegionId("cn-hangzhou"). |
||
148 | WithPolicy("policy"). |
||
149 | Build() |
||
150 | assert.Nil(t, err) |
||
151 | |||
152 | _, err = p.getCredentials() |
||
153 | assert.NotNil(t, err) |
||
154 | assert.Equal(t, "open /path/to/invalid/oidc.token: no such file or directory", err.Error()) |
||
155 | |||
156 | // case 1: mock new http request failed |
||
157 | wd, _ := os.Getwd() |
||
158 | p, err = NewOIDCCredentialsProviderBuilder(). |
||
159 | // read a normal token |
||
160 | WithOIDCTokenFilePath(path.Join(wd, "fixtures/mock_oidctoken")). |
||
161 | WithOIDCProviderARN("provider-arn"). |
||
162 | WithRoleArn("roleArn"). |
||
163 | WithRoleSessionName("rsn"). |
||
164 | WithStsRegionId("cn-hangzhou"). |
||
165 | WithPolicy("policy"). |
||
166 | Build() |
||
167 | assert.Nil(t, err) |
||
168 | |||
169 | // case 2: server error |
||
170 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
171 | err = errors.New("mock server error") |
||
172 | return |
||
173 | } |
||
174 | _, err = p.getCredentials() |
||
175 | assert.NotNil(t, err) |
||
176 | assert.Equal(t, "mock server error", err.Error()) |
||
177 | |||
178 | // case 3: 4xx error |
||
179 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
180 | res = &httputil.Response{ |
||
181 | StatusCode: 400, |
||
182 | Body: []byte("4xx error"), |
||
183 | } |
||
184 | return |
||
185 | } |
||
186 | _, err = p.getCredentials() |
||
187 | assert.NotNil(t, err) |
||
188 | assert.Equal(t, "get session token failed: 4xx error", err.Error()) |
||
189 | |||
190 | // case 4: invalid json |
||
191 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
192 | res = &httputil.Response{ |
||
193 | StatusCode: 200, |
||
194 | Body: []byte("invalid json"), |
||
195 | } |
||
196 | return |
||
197 | } |
||
198 | _, err = p.getCredentials() |
||
199 | assert.NotNil(t, err) |
||
200 | assert.Equal(t, "get oidc sts token err, json.Unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) |
||
201 | |||
202 | // case 5: empty response json |
||
203 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
204 | res = &httputil.Response{ |
||
205 | StatusCode: 200, |
||
206 | Body: []byte("null"), |
||
207 | } |
||
208 | return |
||
209 | } |
||
210 | _, err = p.getCredentials() |
||
211 | assert.NotNil(t, err) |
||
212 | assert.Equal(t, "get oidc sts token err, fail to get credentials", err.Error()) |
||
213 | |||
214 | // case 6: empty session ak response json |
||
215 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
216 | res = &httputil.Response{ |
||
217 | StatusCode: 200, |
||
218 | Body: []byte(`{"Credentials": {}}`), |
||
219 | } |
||
220 | return |
||
221 | } |
||
222 | _, err = p.getCredentials() |
||
223 | assert.NotNil(t, err) |
||
224 | assert.Equal(t, "refresh RoleArn sts token err, fail to get credentials", err.Error()) |
||
225 | |||
226 | // case 7: mock ok value |
||
227 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
228 | res = &httputil.Response{ |
||
229 | StatusCode: 200, |
||
230 | Body: []byte(`{"Credentials": {"AccessKeyId":"saki","AccessKeySecret":"saks","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token"}}`), |
||
231 | } |
||
232 | return |
||
233 | } |
||
234 | creds, err := p.getCredentials() |
||
235 | assert.Nil(t, err) |
||
236 | assert.Equal(t, "saki", creds.AccessKeyId) |
||
237 | assert.Equal(t, "saks", creds.AccessKeySecret) |
||
238 | assert.Equal(t, "token", creds.SecurityToken) |
||
239 | assert.Equal(t, "2021-10-20T04:27:09Z", creds.Expiration) |
||
240 | |||
241 | // needUpdateCredential |
||
242 | assert.True(t, p.needUpdateCredential()) |
||
243 | p.expirationTimestamp = time.Now().Unix() |
||
244 | assert.True(t, p.needUpdateCredential()) |
||
245 | |||
246 | p.expirationTimestamp = time.Now().Unix() + 300 |
||
247 | assert.False(t, p.needUpdateCredential()) |
||
248 | } |
||
340 |