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 ( ebeb07...87aeff )
by
unknown
01:31
created

mollie.TestDelayedRoutingService_List   C

Complexity

Conditions 6

Size

Total Lines 104
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 78
nop 1
dl 0
loc 104
rs 6.7866
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 TestDelayedRoutingService_Create(t *testing.T) {
14
	setEnv()
15
	defer unsetEnv()
16
17
	type args struct {
18
		ctx     context.Context
19
		payment string
20
		dr      CreateDelayedRouting
21
	}
22
23
	cases := []struct {
24
		name    string
25
		args    args
26
		wantErr bool
27
		err     error
28
		pre     func()
29
		handler http.HandlerFunc
30
	}{
31
		{
32
			"creates a delayed routing successfully",
33
			args{
34
				ctx:     context.Background(),
35
				payment: "tr_123456789",
36
				dr: CreateDelayedRouting{
37
					Description: "Delayed routing description",
38
					Amount: Amount{
39
						Currency: "EUR",
40
						Value:    "10.00",
41
					},
42
					Destination: DelayedRoutingDestination{
43
						Type:           "organization",
44
						OrganizationID: "org_123456789",
45
					},
46
				},
47
			},
48
			false,
49
			nil,
50
			noPre,
51
			func(w http.ResponseWriter, r *http.Request) {
52
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
53
				testMethod(t, r, "POST")
54
55
				w.Header().Set("Content-Type", "application/json")
56
				w.WriteHeader(http.StatusCreated)
57
				_, _ = w.Write([]byte(testdata.GetDelayedRoutingExample))
58
			},
59
		},
60
		{
61
			"creates a delayed routing works as expected with access tokens",
62
			args{
63
				ctx:     context.Background(),
64
				payment: "tr_123456789",
65
				dr: CreateDelayedRouting{
66
					Description: "Delayed routing description",
67
					Amount: Amount{
68
						Currency: "EUR",
69
						Value:    "10.00",
70
					},
71
					Destination: DelayedRoutingDestination{
72
						Type:           "organization",
73
						OrganizationID: "org_123456789",
74
					},
75
				},
76
			},
77
			false,
78
			nil,
79
			setAccessToken,
80
			func(w http.ResponseWriter, r *http.Request) {
81
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
82
				testMethod(t, r, "POST")
83
84
				w.Header().Set("Content-Type", "application/json")
85
				w.WriteHeader(http.StatusCreated)
86
				_, _ = w.Write([]byte(testdata.GetDelayedRoutingExample))
87
			},
88
		},
89
		{
90
			"create delayed routing fails with error in handler",
91
			args{
92
				ctx:     context.Background(),
93
				payment: "tr_123456789",
94
				dr: CreateDelayedRouting{
95
					Description: "Delayed routing description",
96
					Amount: Amount{
97
						Currency: "EUR",
98
						Value:    "10.00",
99
					},
100
					Destination: DelayedRoutingDestination{
101
						Type:           "organization",
102
						OrganizationID: "org_123456789",
103
					},
104
				},
105
			},
106
			true,
107
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
108
			noPre,
109
			errorHandler,
110
		},
111
		{
112
			"create delayed routing, an error occurs when parsing json",
113
			args{
114
				ctx:     context.Background(),
115
				payment: "tr_123456789",
116
				dr: CreateDelayedRouting{
117
					Description: "Delayed routing description",
118
					Amount: Amount{
119
						Currency: "EUR",
120
						Value:    "10.00",
121
					},
122
					Destination: DelayedRoutingDestination{
123
						Type:           "organization",
124
						OrganizationID: "org_123456789",
125
					},
126
				},
127
			},
128
			true,
129
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
130
			noPre,
131
			encodingHandler,
132
		},
133
		{
134
			"create delayed routing, invalid url when building request",
135
			args{
136
				ctx:     context.Background(),
137
				payment: "tr_123456789",
138
				dr: CreateDelayedRouting{
139
					Description: "Delayed routing description",
140
					Amount: Amount{
141
						Currency: "EUR",
142
						Value:    "10.00",
143
					},
144
					Destination: DelayedRoutingDestination{
145
						Type:           "organization",
146
						OrganizationID: "org_123456789",
147
					},
148
				},
149
			},
150
			true,
151
			errBadBaseURL,
152
			crashSrv,
153
			errorHandler,
154
		},
155
	}
156
157
	for _, c := range cases {
158
		setup()
159
		defer teardown()
160
161
		t.Run(c.name, func(t *testing.T) {
162
			c.pre()
163
			tMux.HandleFunc(fmt.Sprintf("/v2/payments/%s/routes", c.args.payment), c.handler)
164
165
			res, m, err := tClient.DelayedRouting.Create(c.args.ctx, c.args.payment, c.args.dr)
166
			if c.wantErr {
167
				assert.NotNil(t, err)
168
				assert.EqualError(t, err, c.err.Error())
169
			} else {
170
				assert.Nil(t, err)
171
				assert.IsType(t, &Route{}, m)
172
				assert.IsType(t, &http.Response{}, res.Response)
173
			}
174
		})
175
	}
176
}
177
178
func TestDelayedRoutingService_List(t *testing.T) {
179
	setEnv()
180
	defer unsetEnv()
181
182
	type args struct {
183
		ctx     context.Context
184
		payment string
185
	}
186
187
	cases := []struct {
188
		name    string
189
		args    args
190
		wantErr bool
191
		err     error
192
		pre     func()
193
		handler http.HandlerFunc
194
	}{
195
		{
196
			"lists delayed routings successfully",
197
			args{
198
				ctx:     context.Background(),
199
				payment: "tr_123456789",
200
			},
201
			false,
202
			nil,
203
			noPre,
204
			func(w http.ResponseWriter, r *http.Request) {
205
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
206
				testMethod(t, r, "GET")
207
208
				w.Header().Set("Content-Type", "application/json")
209
				w.WriteHeader(http.StatusOK)
210
				_, _ = w.Write([]byte(testdata.ListDelayedRoutingsExample))
211
			},
212
		},
213
		{
214
			"lists delayed routings works as expected with access tokens",
215
			args{
216
				ctx:     context.Background(),
217
				payment: "tr_123456789",
218
			},
219
			false,
220
			nil,
221
			setAccessToken,
222
			func(w http.ResponseWriter, r *http.Request) {
223
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
224
				testMethod(t, r, "GET")
225
226
				w.Header().Set("Content-Type", "application/json")
227
				w.WriteHeader(http.StatusOK)
228
				_, _ = w.Write([]byte(testdata.ListDelayedRoutingsExample))
229
			},
230
		},
231
		{
232
			"list delayed routings fails with error in handler",
233
			args{
234
				ctx:     context.Background(),
235
				payment: "tr_123456789",
236
			},
237
			true,
238
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
239
			noPre,
240
			errorHandler,
241
		},
242
		{
243
			"list delayed routings, an error occurs when parsing json",
244
			args{
245
				ctx:     context.Background(),
246
				payment: "tr_123456789",
247
			},
248
			true,
249
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
250
			noPre,
251
			encodingHandler,
252
		},
253
		{
254
			"list delayed routings, invalid url when building request",
255
			args{
256
				ctx:     context.Background(),
257
				payment: "tr_123456789",
258
			},
259
			true,
260
			errBadBaseURL,
261
			crashSrv,
262
			errorHandler,
263
		},
264
	}
265
266
	for _, c := range cases {
267
		setup()
268
		defer teardown()
269
270
		t.Run(c.name, func(t *testing.T) {
271
			c.pre()
272
			tMux.HandleFunc(fmt.Sprintf("/v2/payments/%s/routes", c.args.payment), c.handler)
273
274
			res, m, err := tClient.DelayedRouting.List(c.args.ctx, c.args.payment)
275
			if c.wantErr {
276
				assert.NotNil(t, err)
277
				assert.EqualError(t, err, c.err.Error())
278
			} else {
279
				assert.Nil(t, err)
280
				assert.IsType(t, &PaymentRoutesList{}, m)
281
				assert.IsType(t, &http.Response{}, res.Response)
282
			}
283
		})
284
	}
285
}
286