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 |
||
126 | func TestOIDCCredentialsProvider_getCredentials(t *testing.T) { |
||
127 | originHttpDo := httpDo |
||
128 | defer func() { httpDo = originHttpDo }() |
||
129 | |||
130 | // case 0: invalid oidc token file path |
||
131 | p, err := NewOIDCCredentialsProviderBuilder(). |
||
132 | WithOIDCTokenFilePath("/path/to/invalid/oidc.token"). |
||
133 | WithOIDCProviderARN("provider-arn"). |
||
134 | WithRoleArn("roleArn"). |
||
135 | WithRoleSessionName("rsn"). |
||
136 | WithStsRegionId("cn-hangzhou"). |
||
137 | WithPolicy("policy"). |
||
138 | Build() |
||
139 | assert.Nil(t, err) |
||
140 | |||
141 | _, err = p.getCredentials() |
||
142 | assert.NotNil(t, err) |
||
143 | assert.Equal(t, "open /path/to/invalid/oidc.token: no such file or directory", err.Error()) |
||
144 | |||
145 | // case 1: mock new http request failed |
||
146 | wd, _ := os.Getwd() |
||
147 | p, err = NewOIDCCredentialsProviderBuilder(). |
||
148 | // read a normal token |
||
149 | WithOIDCTokenFilePath(path.Join(wd, "fixtures/mock_oidctoken")). |
||
150 | WithOIDCProviderARN("provider-arn"). |
||
151 | WithRoleArn("roleArn"). |
||
152 | WithRoleSessionName("rsn"). |
||
153 | WithStsRegionId("cn-hangzhou"). |
||
154 | WithPolicy("policy"). |
||
155 | Build() |
||
156 | assert.Nil(t, err) |
||
157 | |||
158 | // case 2: server error |
||
159 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
160 | err = errors.New("mock server error") |
||
161 | return |
||
162 | } |
||
163 | _, err = p.getCredentials() |
||
164 | assert.NotNil(t, err) |
||
165 | assert.Equal(t, "mock server error", err.Error()) |
||
166 | |||
167 | // case 3: 4xx error |
||
168 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
169 | res = &httputil.Response{ |
||
170 | StatusCode: 400, |
||
171 | Body: []byte("4xx error"), |
||
172 | } |
||
173 | return |
||
174 | } |
||
175 | _, err = p.getCredentials() |
||
176 | assert.NotNil(t, err) |
||
177 | assert.Equal(t, "get session token failed: 4xx error", err.Error()) |
||
178 | |||
179 | // case 4: invalid json |
||
180 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
181 | res = &httputil.Response{ |
||
182 | StatusCode: 200, |
||
183 | Body: []byte("invalid json"), |
||
184 | } |
||
185 | return |
||
186 | } |
||
187 | _, err = p.getCredentials() |
||
188 | assert.NotNil(t, err) |
||
189 | assert.Equal(t, "get oidc sts token err, json.Unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) |
||
190 | |||
191 | // case 5: empty response json |
||
192 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
193 | res = &httputil.Response{ |
||
194 | StatusCode: 200, |
||
195 | Body: []byte("null"), |
||
196 | } |
||
197 | return |
||
198 | } |
||
199 | _, err = p.getCredentials() |
||
200 | assert.NotNil(t, err) |
||
201 | assert.Equal(t, "get oidc sts token err, fail to get credentials", err.Error()) |
||
202 | |||
203 | // case 6: empty session ak response json |
||
204 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
205 | res = &httputil.Response{ |
||
206 | StatusCode: 200, |
||
207 | Body: []byte(`{"Credentials": {}}`), |
||
208 | } |
||
209 | return |
||
210 | } |
||
211 | _, err = p.getCredentials() |
||
212 | assert.NotNil(t, err) |
||
213 | assert.Equal(t, "refresh RoleArn sts token err, fail to get credentials", err.Error()) |
||
214 | |||
215 | // case 7: mock ok value |
||
216 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
217 | res = &httputil.Response{ |
||
218 | StatusCode: 200, |
||
219 | Body: []byte(`{"Credentials": {"AccessKeyId":"saki","AccessKeySecret":"saks","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token"}}`), |
||
220 | } |
||
221 | return |
||
222 | } |
||
223 | creds, err := p.getCredentials() |
||
224 | assert.Nil(t, err) |
||
225 | assert.Equal(t, "saki", creds.AccessKeyId) |
||
226 | assert.Equal(t, "saks", creds.AccessKeySecret) |
||
227 | assert.Equal(t, "token", creds.SecurityToken) |
||
228 | assert.Equal(t, "2021-10-20T04:27:09Z", creds.Expiration) |
||
229 | |||
230 | // needUpdateCredential |
||
231 | assert.True(t, p.needUpdateCredential()) |
||
232 | p.expirationTimestamp = time.Now().Unix() |
||
233 | assert.True(t, p.needUpdateCredential()) |
||
234 | |||
235 | p.expirationTimestamp = time.Now().Unix() + 300 |
||
236 | assert.False(t, p.needUpdateCredential()) |
||
237 | } |
||
329 |