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 ( ade00f...013a6a )
by
unknown
01:57 queued 14s
created

mollie.TestTerminalsService_List   C

Complexity

Conditions 8

Size

Total Lines 112
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 84
nop 1
dl 0
loc 112
rs 5.6569
c 0
b 0
f 0

How to fix   Long Method   

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:

1
package mollie
2
3
import (
4
	"context"
5
	"fmt"
6
	"net/http"
7
	"testing"
8
9
	"github.com/VictorAvelar/mollie-api-go/v4/testdata"
10
	"github.com/stretchr/testify/assert"
11
)
12
13
func TestTerminalsService_Get(t *testing.T) {
14
	setEnv()
15
	defer unsetEnv()
16
17
	type args struct {
18
		ctx context.Context
19
		id  string
20
	}
21
22
	cases := []struct {
23
		name    string
24
		args    args
25
		wantErr bool
26
		err     error
27
		want    string
28
		pre     func()
29
		handler http.HandlerFunc
30
	}{
31
		{
32
			"get terminal correctly",
33
			args{
34
				context.Background(),
35
				"term_7MgL4wea46qkRcoTZjWEH",
36
			},
37
			false,
38
			nil,
39
			testdata.GetTerminalResponse,
40
			noPre,
41
			func(w http.ResponseWriter, r *http.Request) {
42
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
43
				testMethod(t, r, "GET")
44
45
				if _, ok := r.Header[AuthHeader]; !ok {
46
					w.WriteHeader(http.StatusUnauthorized)
47
				}
48
				_, _ = w.Write([]byte(testdata.GetTerminalResponse))
49
			},
50
		},
51
		{
52
			"get terminal, an error is returned from the server",
53
			args{
54
				context.Background(),
55
				"term_7MgL4wea46qkRcoTZjWEH",
56
			},
57
			true,
58
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
59
			"",
60
			noPre,
61
			errorHandler,
62
		},
63
		{
64
			"get terminal, an error occurs when parsing json",
65
			args{
66
				context.Background(),
67
				"term_7MgL4wea46qkRcoTZjWEH",
68
			},
69
			true,
70
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
71
			"",
72
			noPre,
73
			encodingHandler,
74
		},
75
		{
76
			"get terminal, invalid url when building request",
77
			args{
78
				context.Background(),
79
				"term_7MgL4wea46qkRcoTZjWEH",
80
			},
81
			true,
82
			errBadBaseURL,
83
			"",
84
			crashSrv,
85
			errorHandler,
86
		},
87
	}
88
89
	for _, c := range cases {
90
		setup()
91
		defer teardown()
92
93
		t.Run(c.name, func(t *testing.T) {
94
			c.pre()
95
			tMux.HandleFunc(fmt.Sprintf("/v2/terminals/%s", c.args.id), c.handler)
96
97
			res, m, err := tClient.Terminals.Get(c.args.ctx, c.args.id)
98
			if c.wantErr {
99
				assert.NotNil(t, err)
100
				assert.EqualError(t, err, c.err.Error())
101
			} else {
102
				assert.Nil(t, err)
103
				assert.IsType(t, &Terminal{}, m)
104
				assert.IsType(t, &http.Response{}, res.Response)
105
			}
106
		})
107
	}
108
}
109
110
func TestTerminalsService_List(t *testing.T) {
111
	setEnv()
112
	defer unsetEnv()
113
114
	type args struct {
115
		ctx     context.Context
116
		options *ListTerminalsOptions
117
	}
118
119
	cases := []struct {
120
		name    string
121
		args    args
122
		wantErr bool
123
		err     error
124
		want    string
125
		pre     func()
126
		handler http.HandlerFunc
127
	}{
128
		{
129
			"list terminals correctly",
130
			args{
131
				context.Background(),
132
				&ListTerminalsOptions{},
133
			},
134
			false,
135
			nil,
136
			testdata.GetTerminalResponse,
137
			noPre,
138
			func(w http.ResponseWriter, r *http.Request) {
139
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
140
				testMethod(t, r, "GET")
141
142
				if _, ok := r.Header[AuthHeader]; !ok {
143
					w.WriteHeader(http.StatusUnauthorized)
144
				}
145
				_, _ = w.Write([]byte(testdata.ListTerminalsResponse))
146
			},
147
		},
148
		{
149
			"list terminals correctly with an access token",
150
			args{
151
				context.Background(),
152
				&ListTerminalsOptions{},
153
			},
154
			false,
155
			nil,
156
			testdata.GetTerminalResponse,
157
			setAccessToken,
158
			func(w http.ResponseWriter, r *http.Request) {
159
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
160
				testMethod(t, r, "GET")
161
162
				if _, ok := r.Header[AuthHeader]; !ok {
163
					w.WriteHeader(http.StatusUnauthorized)
164
				}
165
				_, _ = w.Write([]byte(testdata.ListTerminalsResponse))
166
			},
167
		},
168
		{
169
			"get terminals list, an error is returned from the server",
170
			args{
171
				context.Background(),
172
				nil,
173
			},
174
			true,
175
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
176
			"",
177
			noPre,
178
			errorHandler,
179
		},
180
		{
181
			"get terminals list, an error occurs when parsing json",
182
			args{
183
				context.Background(),
184
				nil,
185
			},
186
			true,
187
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
188
			"",
189
			noPre,
190
			encodingHandler,
191
		},
192
		{
193
			"get terminals list, invalid url when building request",
194
			args{
195
				context.Background(),
196
				nil,
197
			},
198
			true,
199
			errBadBaseURL,
200
			"",
201
			crashSrv,
202
			errorHandler,
203
		},
204
	}
205
206
	for _, c := range cases {
207
		setup()
208
		defer teardown()
209
210
		t.Run(c.name, func(t *testing.T) {
211
			c.pre()
212
			tMux.HandleFunc("/v2/terminals", c.handler)
213
214
			res, m, err := tClient.Terminals.List(c.args.ctx, c.args.options)
215
			if c.wantErr {
216
				assert.NotNil(t, err)
217
				assert.EqualError(t, err, c.err.Error())
218
			} else {
219
				assert.Nil(t, err)
220
				assert.IsType(t, &TerminalList{}, m)
221
				assert.IsType(t, &http.Response{}, res.Response)
222
			}
223
		})
224
	}
225
}
226