| Conditions | 8 | 
| Total Lines | 92 | 
| Code Lines | 65 | 
| 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 | ||
| 59 | func TestCloudSSOCredentialsProvider_getCredentials(t *testing.T) { | ||
| 60 | originHttpDo := httpDo | ||
| 61 | 	defer func() { httpDo = originHttpDo }() | ||
| 62 | |||
| 63 | p, err := NewCloudSSOCredentialsProviderBuilder(). | ||
| 64 | 		WithSignInUrl("https://signin-cn-shanghai.alibabacloudsso.com/a/login"). | ||
| 65 | 		WithAccountId("uid"). | ||
| 66 | 		WithAccessConfig("config"). | ||
| 67 | 		WithAccessToken("token"). | ||
| 68 | WithAccessTokenExpire(time.Now().Unix() + 1000). | ||
| 69 | Build() | ||
| 70 | assert.Nil(t, err) | ||
| 71 | |||
| 72 | // case 1: mock new http request failed | ||
| 73 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 74 | 		err = errors.New("mock server error") | ||
| 75 | return | ||
| 76 | } | ||
| 77 | _, err = p.getCredentials() | ||
| 78 | assert.NotNil(t, err) | ||
| 79 | assert.Equal(t, "mock server error", err.Error()) | ||
| 80 | |||
| 81 | // case 2: 4xx error | ||
| 82 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 83 | 		res = &httputil.Response{ | ||
| 84 | StatusCode: 400, | ||
| 85 | 			Body:       []byte("4xx error"), | ||
| 86 | } | ||
| 87 | return | ||
| 88 | } | ||
| 89 | _, err = p.getCredentials() | ||
| 90 | assert.NotNil(t, err) | ||
| 91 | assert.Equal(t, "get session token from sso failed: 4xx error", err.Error()) | ||
| 92 | |||
| 93 | // case 3: invalid json | ||
| 94 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 95 | 		res = &httputil.Response{ | ||
| 96 | StatusCode: 200, | ||
| 97 | 			Body:       []byte("invalid json"), | ||
| 98 | } | ||
| 99 | return | ||
| 100 | } | ||
| 101 | _, err = p.getCredentials() | ||
| 102 | assert.NotNil(t, err) | ||
| 103 | assert.Equal(t, "get session token from sso failed, json.Unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) | ||
| 104 | |||
| 105 | // case 4: empty response json | ||
| 106 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 107 | 		res = &httputil.Response{ | ||
| 108 | StatusCode: 200, | ||
| 109 | 			Body:       []byte("null"), | ||
| 110 | } | ||
| 111 | return | ||
| 112 | } | ||
| 113 | _, err = p.getCredentials() | ||
| 114 | assert.NotNil(t, err) | ||
| 115 | assert.Equal(t, "get session token from sso failed, fail to get credentials", err.Error()) | ||
| 116 | |||
| 117 | // case 5: empty session ak response json | ||
| 118 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 119 | 		res = &httputil.Response{ | ||
| 120 | StatusCode: 200, | ||
| 121 | 			Body:       []byte(`{"Credentials": {}}`), | ||
| 122 | } | ||
| 123 | return | ||
| 124 | } | ||
| 125 | _, err = p.getCredentials() | ||
| 126 | assert.NotNil(t, err) | ||
| 127 | assert.Equal(t, "get session token from sso failed, fail to get credentials", err.Error()) | ||
| 128 | |||
| 129 | // case 6: mock ok value | ||
| 130 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 131 | 		res = &httputil.Response{ | ||
| 132 | StatusCode: 200, | ||
| 133 | 			Body:       []byte(`{"RequestId": "123", "CloudCredential": {"AccessKeyId":"ak","AccessKeySecret":"sk","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token"}}`), | ||
| 134 | } | ||
| 135 | return | ||
| 136 | } | ||
| 137 | creds, err := p.getCredentials() | ||
| 138 | assert.Nil(t, err) | ||
| 139 | assert.Equal(t, "ak", creds.AccessKeyId) | ||
| 140 | assert.Equal(t, "sk", creds.AccessKeySecret) | ||
| 141 | assert.Equal(t, "token", creds.SecurityToken) | ||
| 142 | assert.Equal(t, "2021-10-20T04:27:09Z", creds.Expiration) | ||
| 143 | |||
| 144 | // needUpdateCredential | ||
| 145 | assert.True(t, p.needUpdateCredential()) | ||
| 146 | p.expirationTimestamp = time.Now().Unix() | ||
| 147 | assert.True(t, p.needUpdateCredential()) | ||
| 148 | |||
| 149 | p.expirationTimestamp = time.Now().Unix() + 300 | ||
| 150 | assert.False(t, p.needUpdateCredential()) | ||
| 151 | } | ||
| 232 |