GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 4702c6...c6e412 )
by Jackson
13:16 queued 03:19
created

ider_updateCredential   C

Complexity

Conditions 10

Size

Total Lines 74
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 52
nop 1
dl 0
loc 74
rs 5.7709
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like credentials.TestURLCredentialsProvider_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
2
3
import (
4
	"errors"
5
	"net/http"
6
	"testing"
7
8
	"github.com/stretchr/testify/assert"
9
)
10
11
func TestURLCredentialsProvider_updateCredential(t *testing.T) {
12
	provider := newURLCredential("http://127.0.0.1")
13
14
	origTestHookDo := hookDo
15
	defer func() { hookDo = origTestHookDo }()
16
	hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
17
		return func(req *http.Request) (*http.Response, error) {
18
			return mockResponse(300, ``, errors.New("sdk test"))
19
		}
20
	}
21
22
	cred, err := provider.GetCredential()
23
	assert.NotNil(t, err)
24
	assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())
25
	assert.Nil(t, cred)
26
27
	_, err = provider.GetAccessKeyId()
28
	assert.NotNil(t, err)
29
	assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())
30
31
	_, err = provider.GetAccessKeySecret()
32
	assert.NotNil(t, err)
33
	assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())
34
35
	_, err = provider.GetSecurityToken()
36
	assert.NotNil(t, err)
37
	assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error())
38
39
	hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
40
		return func(req *http.Request) (*http.Response, error) {
41
			return mockResponse(200, `invalid json`, nil)
42
		}
43
	}
44
45
	cred, err = provider.GetCredential()
46
	assert.NotNil(t, err)
47
	assert.Equal(t, "get credentials from http://127.0.0.1 failed with error, json unmarshal fail: invalid character 'i' looking for beginning of value", err.Error())
48
	assert.Nil(t, cred)
49
50
	hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
51
		return func(req *http.Request) (*http.Response, error) {
52
			return mockResponse(200, `{}`, nil)
53
		}
54
	}
55
56
	cred, err = provider.GetCredential()
57
	assert.NotNil(t, err)
58
	assert.Equal(t, "get credentials failed: AccessKeyId: , AccessKeySecret: , SecurityToken: , Expiration: ", err.Error())
59
	assert.Nil(t, cred)
60
61
	hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
62
		return func(req *http.Request) (*http.Response, error) {
63
			return mockResponse(200, `{"AccessKeyId":"akid", "AccessKeySecret":"aksecret","SecurityToken":"sts","Expiration":"2006-01-02T15:04:05Z"}`, nil)
64
		}
65
	}
66
67
	cred, err = provider.GetCredential()
68
	assert.Nil(t, err)
69
	assert.NotNil(t, cred)
70
	assert.Equal(t, "akid", *cred.AccessKeyId)
71
	assert.Equal(t, "aksecret", *cred.AccessKeySecret)
72
	assert.Equal(t, "sts", *cred.SecurityToken)
73
74
	akid, err := provider.GetAccessKeyId()
75
	assert.Nil(t, err)
76
	assert.Equal(t, "akid", *akid)
77
78
	aksecret, err := provider.GetAccessKeySecret()
79
	assert.Nil(t, err)
80
	assert.Equal(t, "aksecret", *aksecret)
81
82
	sts, err := provider.GetSecurityToken()
83
	assert.Nil(t, err)
84
	assert.Equal(t, "sts", *sts)
85
}
86
87
func TestURLCredentialsProviderGetBearerToken(t *testing.T) {
88
	provider := newURLCredential("http://127.0.0.1")
89
	assert.Equal(t, "", *provider.GetBearerToken())
90
}
91
92
func TestURLCredentialsProviderGetType(t *testing.T) {
93
	provider := newURLCredential("http://127.0.0.1")
94
	assert.Equal(t, "credential_uri", *provider.GetType())
95
}
96