| Conditions | 15 |
| Total Lines | 108 |
| Code Lines | 77 |
| 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 credentials.Test_oidcCredential_updateCredential 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 credentials |
||
| 32 | func Test_oidcCredential_updateCredential(t *testing.T) { |
||
| 33 | oidcCredential, err := newOIDCRoleArnCredential("accessKeyId", "accessKeySecret", "RoleArn", "OIDCProviderArn", "/path/to/tokenFilePath", "roleSessionName", "Policy", 3600, nil) |
||
| 34 | assert.Nil(t, err) |
||
| 35 | |||
| 36 | c, err := oidcCredential.GetCredential() |
||
| 37 | assert.NotNil(t, err) |
||
| 38 | assert.Equal(t, "read oidc token file failed: open /path/to/tokenFilePath: no such file or directory", err.Error()) |
||
| 39 | assert.Nil(t, c) |
||
| 40 | |||
| 41 | accessKeyId, err := oidcCredential.GetAccessKeyId() |
||
| 42 | assert.NotNil(t, err) |
||
| 43 | assert.Equal(t, "read oidc token file failed: open /path/to/tokenFilePath: no such file or directory", err.Error()) |
||
| 44 | assert.Nil(t, accessKeyId) |
||
| 45 | |||
| 46 | accessKeySecret, err := oidcCredential.GetAccessKeySecret() |
||
| 47 | assert.NotNil(t, err) |
||
| 48 | assert.Equal(t, "read oidc token file failed: open /path/to/tokenFilePath: no such file or directory", err.Error()) |
||
| 49 | assert.Nil(t, accessKeySecret) |
||
| 50 | |||
| 51 | securityToken, err := oidcCredential.GetSecurityToken() |
||
| 52 | assert.NotNil(t, err) |
||
| 53 | assert.Equal(t, "read oidc token file failed: open /path/to/tokenFilePath: no such file or directory", err.Error()) |
||
| 54 | assert.Nil(t, securityToken) |
||
| 55 | |||
| 56 | originGetFileContent := getFileContent |
||
| 57 | defer func() { |
||
| 58 | getFileContent = originGetFileContent |
||
| 59 | }() |
||
| 60 | getFileContent = func(filePath string) (content string, err error) { |
||
| 61 | return "token", nil |
||
| 62 | } |
||
| 63 | // mock server error |
||
| 64 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 65 | return func(req *http.Request) (*http.Response, error) { |
||
| 66 | return mockResponse(500, ``, errors.New("mock server error")) |
||
| 67 | } |
||
| 68 | } |
||
| 69 | c, err = oidcCredential.GetCredential() |
||
| 70 | assert.NotNil(t, err) |
||
| 71 | assert.Equal(t, "get sts token failed with: mock server error", err.Error()) |
||
| 72 | assert.Nil(t, c) |
||
| 73 | // mock unmarshal error |
||
| 74 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 75 | return func(req *http.Request) (*http.Response, error) { |
||
| 76 | return mockResponse(200, `invalid json`, nil) |
||
| 77 | } |
||
| 78 | } |
||
| 79 | c, err = oidcCredential.GetCredential() |
||
| 80 | assert.NotNil(t, err) |
||
| 81 | assert.Equal(t, "get sts token failed with: Json.Unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) |
||
| 82 | assert.Nil(t, c) |
||
| 83 | |||
| 84 | // mock null response |
||
| 85 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 86 | return func(req *http.Request) (*http.Response, error) { |
||
| 87 | return mockResponse(200, `null`, nil) |
||
| 88 | } |
||
| 89 | } |
||
| 90 | c, err = oidcCredential.GetCredential() |
||
| 91 | assert.NotNil(t, err) |
||
| 92 | assert.Equal(t, "get sts token failed with: credentials is empty", err.Error()) |
||
| 93 | assert.Nil(t, c) |
||
| 94 | |||
| 95 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 96 | return func(req *http.Request) (*http.Response, error) { |
||
| 97 | return mockResponse(200, `{}`, nil) |
||
| 98 | } |
||
| 99 | } |
||
| 100 | c, err = oidcCredential.GetCredential() |
||
| 101 | assert.NotNil(t, err) |
||
| 102 | assert.Equal(t, "get sts token failed with: credentials is empty", err.Error()) |
||
| 103 | assert.Nil(t, c) |
||
| 104 | |||
| 105 | // mock empty ak |
||
| 106 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 107 | return func(req *http.Request) (*http.Response, error) { |
||
| 108 | return mockResponse(200, `{"Credentials": {}}`, nil) |
||
| 109 | } |
||
| 110 | } |
||
| 111 | c, err = oidcCredential.GetCredential() |
||
| 112 | assert.NotNil(t, err) |
||
| 113 | assert.Equal(t, "get sts token failed with: AccessKeyId: , AccessKeySecret: , SecurityToken: , Expiration: ", err.Error()) |
||
| 114 | assert.Nil(t, c) |
||
| 115 | |||
| 116 | // mock normal credentials |
||
| 117 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 118 | return func(req *http.Request) (*http.Response, error) { |
||
| 119 | return mockResponse(200, `{"Credentials": {"AccessKeyId":"akid","AccessKeySecret":"aksecret","SecurityToken":"ststoken","Expiration":"2006-01-02T15:04:05Z"}}`, nil) |
||
| 120 | } |
||
| 121 | } |
||
| 122 | c, err = oidcCredential.GetCredential() |
||
| 123 | assert.Nil(t, err) |
||
| 124 | assert.NotNil(t, c) |
||
| 125 | assert.Equal(t, "akid", *c.AccessKeyId) |
||
| 126 | assert.Equal(t, "aksecret", *c.AccessKeySecret) |
||
| 127 | assert.Equal(t, "ststoken", *c.SecurityToken) |
||
| 128 | |||
| 129 | akid, err := oidcCredential.GetAccessKeyId() |
||
| 130 | assert.Nil(t, err) |
||
| 131 | assert.Equal(t, "akid", *akid) |
||
| 132 | |||
| 133 | secret, err := oidcCredential.GetAccessKeySecret() |
||
| 134 | assert.Nil(t, err) |
||
| 135 | assert.Equal(t, "aksecret", *secret) |
||
| 136 | |||
| 137 | ststoken, err := oidcCredential.GetSecurityToken() |
||
| 138 | assert.Nil(t, err) |
||
| 139 | assert.Equal(t, "ststoken", *ststoken) |
||
| 140 | } |
||
| 199 |