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