Conditions | 14 |
Total Lines | 95 |
Code Lines | 71 |
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_RoleArnCredential 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 |
||
28 | func Test_RoleArnCredential(t *testing.T) { |
||
29 | auth := newRAMRoleArnCredential("accessKeyID", "accessKeySecret", "roleArn", "roleSessionName", "policy", 300, nil) |
||
30 | origTestHookDo := hookDo |
||
31 | defer func() { hookDo = origTestHookDo }() |
||
32 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
33 | return func(req *http.Request) (*http.Response, error) { |
||
34 | return mockResponse(200, `{"Credentials":{"AccessKeyID":"accessKeyID","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration"}}`, errors.New("Internal error")) |
||
35 | } |
||
36 | } |
||
37 | accesskeyID, err := auth.GetAccessKeyID() |
||
38 | assert.NotNil(t, err) |
||
39 | assert.Equal(t, "[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr", err.Error()) |
||
40 | assert.Equal(t, "", accesskeyID) |
||
41 | |||
42 | accesskeySecret, err := auth.GetAccessSecret() |
||
43 | assert.NotNil(t, err) |
||
44 | assert.Equal(t, "[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr", err.Error()) |
||
45 | assert.Equal(t, "", accesskeySecret) |
||
46 | |||
47 | ststoken, err := auth.GetSecurityToken() |
||
48 | assert.NotNil(t, err) |
||
49 | assert.Equal(t, "[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr", err.Error()) |
||
50 | assert.Equal(t, "", ststoken) |
||
51 | |||
52 | assert.Equal(t, "", auth.GetBearerToken()) |
||
53 | assert.Equal(t, "ram_role_arn", auth.GetType()) |
||
54 | |||
55 | auth.RoleSessionExpiration = 1000 |
||
56 | accesskeyID, err = auth.GetAccessKeyID() |
||
57 | assert.NotNil(t, err) |
||
58 | assert.Equal(t, "refresh RoleArn sts token err: Internal error", err.Error()) |
||
59 | assert.Equal(t, "", accesskeyID) |
||
60 | |||
61 | auth.RoleSessionExpiration = 0 |
||
62 | accesskeyID, err = auth.GetAccessKeyID() |
||
63 | assert.NotNil(t, err) |
||
64 | assert.Equal(t, "refresh RoleArn sts token err: Internal error", err.Error()) |
||
65 | assert.Equal(t, "", accesskeyID) |
||
66 | |||
67 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
68 | return func(req *http.Request) (*http.Response, error) { |
||
69 | return mockResponse(300, ``, nil) |
||
70 | } |
||
71 | } |
||
72 | accesskeyID, err = auth.GetAccessKeyID() |
||
73 | assert.NotNil(t, err) |
||
74 | assert.Equal(t, "refresh RoleArn sts token err: httpStatus: 300, message = ", err.Error()) |
||
75 | assert.Equal(t, "", accesskeyID) |
||
76 | |||
77 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
78 | return func(req *http.Request) (*http.Response, error) { |
||
79 | return mockResponse(200, `"Credentials":{"AccessKeyID":"accessKeyID","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration"}}`, nil) |
||
80 | } |
||
81 | } |
||
82 | accesskeyID, err = auth.GetAccessKeyID() |
||
83 | assert.NotNil(t, err) |
||
84 | assert.Equal(t, "refresh RoleArn sts token err: Json.Unmarshal fail: invalid character ':' after top-level value", err.Error()) |
||
85 | assert.Equal(t, "", accesskeyID) |
||
86 | |||
87 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
88 | return func(req *http.Request) (*http.Response, error) { |
||
89 | return mockResponse(200, `{"Credentials":{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration"}}`, nil) |
||
90 | } |
||
91 | } |
||
92 | accesskeyID, err = auth.GetAccessKeyID() |
||
93 | assert.NotNil(t, err) |
||
94 | assert.Equal(t, "refresh RoleArn sts token err: AccessKeyID: , AccessKeySecret: accessKeySecret, SecurityToken: securitytoken, Expiration: expiration", err.Error()) |
||
95 | assert.Equal(t, "", accesskeyID) |
||
96 | |||
97 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
98 | return func(req *http.Request) (*http.Response, error) { |
||
99 | return mockResponse(200, `{}`, nil) |
||
100 | } |
||
101 | } |
||
102 | accesskeyID, err = auth.GetAccessKeyID() |
||
103 | assert.NotNil(t, err) |
||
104 | assert.Equal(t, "refresh RoleArn sts token err: Credentials is empty", err.Error()) |
||
105 | assert.Equal(t, "", accesskeyID) |
||
106 | |||
107 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
108 | return func(req *http.Request) (*http.Response, error) { |
||
109 | return mockResponse(200, `{"Credentials":{"AccessKeyID":"accessKeyID","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"2020-01-02T15:04:05Z"}}`, nil) |
||
110 | } |
||
111 | } |
||
112 | accesskeyID, err = auth.GetAccessKeyID() |
||
113 | assert.Nil(t, err) |
||
114 | assert.Equal(t, "accessKeyID", accesskeyID) |
||
115 | |||
116 | accesskeySecret, err = auth.GetAccessSecret() |
||
117 | assert.Nil(t, err) |
||
118 | assert.Equal(t, "accessKeySecret", accesskeySecret) |
||
119 | |||
120 | ststoken, err = auth.GetSecurityToken() |
||
121 | assert.Nil(t, err) |
||
122 | assert.Equal(t, "securitytoken", ststoken) |
||
123 | } |
||
124 |