Conditions | 19 |
Total Lines | 143 |
Code Lines | 98 |
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:
Complex classes like providers.TestOIDCCredentialsProvider_getCredentials often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package providers |
||
125 | func TestOIDCCredentialsProvider_getCredentials(t *testing.T) { |
||
126 | // case 0: invalid oidc token file path |
||
127 | p, err := NewOIDCCredentialsProviderBuilder(). |
||
128 | WithOIDCTokenFilePath("/path/to/invalid/oidc.token"). |
||
129 | WithOIDCProviderARN("provider-arn"). |
||
130 | WithRoleArn("roleArn"). |
||
131 | WithRoleSessionName("rsn"). |
||
132 | WithStsRegionId("cn-hangzhou"). |
||
133 | WithPolicy("policy"). |
||
134 | Build() |
||
135 | assert.Nil(t, err) |
||
136 | |||
137 | _, err = p.getCredentials() |
||
138 | assert.NotNil(t, err) |
||
139 | assert.Equal(t, "open /path/to/invalid/oidc.token: no such file or directory", err.Error()) |
||
140 | |||
141 | // case 1: mock new http request failed |
||
142 | wd, _ := os.Getwd() |
||
143 | p, err = NewOIDCCredentialsProviderBuilder(). |
||
144 | // read a normal token |
||
145 | WithOIDCTokenFilePath(path.Join(wd, "fixtures/mock_oidctoken")). |
||
146 | WithOIDCProviderARN("provider-arn"). |
||
147 | WithRoleArn("roleArn"). |
||
148 | WithRoleSessionName("rsn"). |
||
149 | WithStsRegionId("cn-hangzhou"). |
||
150 | WithPolicy("policy"). |
||
151 | Build() |
||
152 | assert.Nil(t, err) |
||
153 | |||
154 | originNewRequest := hookNewRequest |
||
155 | defer func() { hookNewRequest = originNewRequest }() |
||
156 | |||
157 | hookNewRequest = func(fn newReuqest) newReuqest { |
||
158 | return func(method, url string, body io.Reader) (*http.Request, error) { |
||
159 | return nil, errors.New("new http request failed") |
||
160 | } |
||
161 | } |
||
162 | |||
163 | _, err = p.getCredentials() |
||
164 | assert.NotNil(t, err) |
||
165 | assert.Equal(t, "new http request failed", err.Error()) |
||
166 | |||
167 | // reset new request |
||
168 | hookNewRequest = originNewRequest |
||
169 | |||
170 | originDo := hookDo |
||
171 | defer func() { hookDo = originDo }() |
||
172 | |||
173 | // case 2: server error |
||
174 | hookDo = func(fn do) do { |
||
175 | return func(req *http.Request) (res *http.Response, err error) { |
||
176 | err = errors.New("mock server error") |
||
177 | return |
||
178 | } |
||
179 | } |
||
180 | _, err = p.getCredentials() |
||
181 | assert.NotNil(t, err) |
||
182 | assert.Equal(t, "mock server error", err.Error()) |
||
183 | |||
184 | // case 3: mock read response error |
||
185 | hookDo = func(fn do) do { |
||
186 | return func(req *http.Request) (res *http.Response, err error) { |
||
187 | status := strconv.Itoa(200) |
||
188 | res = &http.Response{ |
||
189 | Proto: "HTTP/1.1", |
||
190 | ProtoMajor: 1, |
||
191 | Header: map[string][]string{}, |
||
192 | StatusCode: 200, |
||
193 | Status: status + " " + http.StatusText(200), |
||
194 | } |
||
195 | res.Body = ioutil.NopCloser(&errorReader{}) |
||
196 | return |
||
197 | } |
||
198 | } |
||
199 | _, err = p.getCredentials() |
||
200 | assert.NotNil(t, err) |
||
201 | assert.Equal(t, "read failed", err.Error()) |
||
202 | |||
203 | // case 4: 4xx error |
||
204 | hookDo = func(fn do) do { |
||
205 | return func(req *http.Request) (res *http.Response, err error) { |
||
206 | res = mockResponse(400, "4xx error") |
||
207 | return |
||
208 | } |
||
209 | } |
||
210 | _, err = p.getCredentials() |
||
211 | assert.NotNil(t, err) |
||
212 | assert.Equal(t, "get session token failed: 4xx error", err.Error()) |
||
213 | |||
214 | // case 5: invalid json |
||
215 | hookDo = func(fn do) do { |
||
216 | return func(req *http.Request) (res *http.Response, err error) { |
||
217 | res = mockResponse(200, "invalid json") |
||
218 | return |
||
219 | } |
||
220 | } |
||
221 | _, err = p.getCredentials() |
||
222 | assert.NotNil(t, err) |
||
223 | assert.Equal(t, "get oidc sts token err, json.Unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) |
||
224 | |||
225 | // case 6: empty response json |
||
226 | hookDo = func(fn do) do { |
||
227 | return func(req *http.Request) (res *http.Response, err error) { |
||
228 | res = mockResponse(200, "null") |
||
229 | return |
||
230 | } |
||
231 | } |
||
232 | _, err = p.getCredentials() |
||
233 | assert.NotNil(t, err) |
||
234 | assert.Equal(t, "get oidc sts token err, fail to get credentials", err.Error()) |
||
235 | |||
236 | // case 7: empty session ak response json |
||
237 | hookDo = func(fn do) do { |
||
238 | return func(req *http.Request) (res *http.Response, err error) { |
||
239 | res = mockResponse(200, `{"Credentials": {}}`) |
||
240 | return |
||
241 | } |
||
242 | } |
||
243 | _, err = p.getCredentials() |
||
244 | assert.NotNil(t, err) |
||
245 | assert.Equal(t, "refresh RoleArn sts token err, fail to get credentials", err.Error()) |
||
246 | |||
247 | // case 8: mock ok value |
||
248 | hookDo = func(fn do) do { |
||
249 | return func(req *http.Request) (res *http.Response, err error) { |
||
250 | res = mockResponse(200, `{"Credentials": {"AccessKeyId":"saki","AccessKeySecret":"saks","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token"}}`) |
||
251 | return |
||
252 | } |
||
253 | } |
||
254 | creds, err := p.getCredentials() |
||
255 | assert.Nil(t, err) |
||
256 | assert.Equal(t, "saki", creds.AccessKeyId) |
||
257 | assert.Equal(t, "saks", creds.AccessKeySecret) |
||
258 | assert.Equal(t, "token", creds.SecurityToken) |
||
259 | assert.Equal(t, "2021-10-20T04:27:09Z", creds.Expiration) |
||
260 | |||
261 | // needUpdateCredential |
||
262 | assert.True(t, p.needUpdateCredential()) |
||
263 | p.expirationTimestamp = time.Now().Unix() |
||
264 | assert.True(t, p.needUpdateCredential()) |
||
265 | |||
266 | p.expirationTimestamp = time.Now().Unix() + 300 |
||
267 | assert.False(t, p.needUpdateCredential()) |
||
268 | } |
||
365 |