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.TestCapturesService_Create   D

Complexity

Conditions 9

Size

Total Lines 141
Code Lines 105

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 105
nop 1
dl 0
loc 141
rs 4.6666
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 TestCapturesService_Get(t *testing.T) {
14
	setEnv()
15
	defer unsetEnv()
16
17
	type args struct {
18
		ctx     context.Context
19
		payment string
20
		capture string
21
		options *CaptureOptions
22
	}
23
24
	cases := []struct {
25
		name    string
26
		args    args
27
		wantErr bool
28
		err     error
29
		handler http.HandlerFunc
30
		pre     func()
31
	}{
32
		{
33
			"get captures works as expected",
34
			args{
35
				context.Background(),
36
				"tr_WDqYK6vllg",
37
				"cpt_4qqhO89gsT",
38
				nil,
39
			},
40
			false,
41
			nil,
42
			func(w http.ResponseWriter, r *http.Request) {
43
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
44
				testMethod(t, r, "GET")
45
				if _, ok := r.Header[AuthHeader]; !ok {
46
					w.WriteHeader(http.StatusUnauthorized)
47
				}
48
49
				_, _ = w.Write([]byte(testdata.GetCaptureResponse))
50
			},
51
			noPre,
52
		},
53
		{
54
			"get captures works expands query params correctly",
55
			args{
56
				context.Background(),
57
				"tr_WDqYK6vllg",
58
				"cpt_4qqhO89gsT",
59
				&CaptureOptions{
60
					Embed: []EmbedValue{EmbedPayments},
61
				},
62
			},
63
			false,
64
			nil,
65
			func(w http.ResponseWriter, r *http.Request) {
66
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
67
				testMethod(t, r, "GET")
68
				testQuery(t, r, "embed=payments")
69
				if _, ok := r.Header[AuthHeader]; !ok {
70
					w.WriteHeader(http.StatusUnauthorized)
71
				}
72
73
				_, _ = w.Write([]byte(testdata.GetCaptureResponse))
74
			},
75
			noPre,
76
		},
77
		{
78
			"get captures returns an http error from the server",
79
			args{
80
				context.Background(),
81
				"tr_WDqYK6vllg",
82
				"cpt_4qqhO89gsT",
83
				nil,
84
			},
85
			true,
86
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
87
			errorHandler,
88
			noPre,
89
		},
90
		{
91
			"get captures returns an error when creating the request",
92
			args{
93
				context.Background(),
94
				"tr_WDqYK6vllg",
95
				"cpt_4qqhO89gsT",
96
				nil,
97
			},
98
			true,
99
			errBadBaseURL,
100
			errorHandler,
101
			crashSrv,
102
		},
103
		{
104
			"get captures returns an error when trying to parse the json response",
105
			args{
106
				context.Background(),
107
				"tr_WDqYK6vllg",
108
				"cpt_4qqhO89gsT",
109
				nil,
110
			},
111
			true,
112
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
113
			encodingHandler,
114
			noPre,
115
		},
116
	}
117
118
	for _, c := range cases {
119
		setup()
120
		defer teardown()
121
122
		t.Run(c.name, func(t *testing.T) {
123
			c.pre()
124
125
			tMux.HandleFunc(
126
				fmt.Sprintf(
127
					"/v2/payments/%s/captures/%s",
128
					c.args.payment,
129
					c.args.capture,
130
				),
131
				c.handler,
132
			)
133
134
			res, capture, err := tClient.Captures.Get(c.args.ctx, c.args.payment, c.args.capture, c.args.options)
135
			if c.wantErr {
136
				assert.NotNil(t, err)
137
				assert.EqualError(t, err, c.err.Error())
138
			} else {
139
				assert.Nil(t, err)
140
				assert.IsType(t, &Capture{}, capture)
141
				assert.EqualValues(t, c.args.ctx, res.Request.Context())
142
				assert.IsType(t, &http.Response{}, res.Response)
143
			}
144
		})
145
	}
146
}
147
148
func TestCapturesService_Create(t *testing.T) {
149
	setEnv()
150
	defer unsetEnv()
151
152
	type args struct {
153
		ctx     context.Context
154
		payment string
155
		capture CreateCapture
156
		options *CaptureOptions
157
	}
158
159
	cases := []struct {
160
		name    string
161
		args    args
162
		wantErr bool
163
		err     error
164
		handler http.HandlerFunc
165
		pre     func()
166
	}{
167
		{
168
			"create captures works as expected",
169
			args{
170
				context.Background(),
171
				"tr_WDqYK6vllg",
172
				CreateCapture{
173
					Amount: &Amount{
174
						Value:    "20.00",
175
						Currency: "EUR",
176
					},
177
					Description: "Order #12345",
178
				},
179
				nil,
180
			},
181
			false,
182
			nil,
183
			func(w http.ResponseWriter, r *http.Request) {
184
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
185
				testMethod(t, r, "POST")
186
				if _, ok := r.Header[AuthHeader]; !ok {
187
					w.WriteHeader(http.StatusUnauthorized)
188
				}
189
190
				_, _ = w.Write([]byte(testdata.CreateCaptureResponse))
191
			},
192
			noPre,
193
		},
194
		{
195
			"create captures works as with access token expected",
196
			args{
197
				context.Background(),
198
				"tr_WDqYK6vllg",
199
				CreateCapture{
200
					Amount: &Amount{
201
						Value:    "20.00",
202
						Currency: "EUR",
203
					},
204
					Description: "Order #12345",
205
				},
206
				nil,
207
			},
208
			false,
209
			nil,
210
			func(w http.ResponseWriter, r *http.Request) {
211
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
212
				testMethod(t, r, "POST")
213
				if _, ok := r.Header[AuthHeader]; !ok {
214
					w.WriteHeader(http.StatusUnauthorized)
215
				}
216
217
				_, _ = w.Write([]byte(testdata.CreateCaptureWithAccessTokenResponse))
218
			},
219
			setAccessToken,
220
		},
221
		{
222
			"create captures returns an http error from the server",
223
			args{
224
				context.Background(),
225
				"tr_WDqYK6vllg",
226
				CreateCapture{},
227
				nil,
228
			},
229
			true,
230
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
231
			errorHandler,
232
			noPre,
233
		},
234
		{
235
			"create captures returns an error when creating the request",
236
			args{
237
				context.Background(),
238
				"tr_WDqYK6vllg",
239
				CreateCapture{},
240
				nil,
241
			},
242
			true,
243
			errBadBaseURL,
244
			errorHandler,
245
			crashSrv,
246
		},
247
		{
248
			"create captures returns an error when trying to parse the json response",
249
			args{
250
				context.Background(),
251
				"tr_WDqYK6vllg",
252
				CreateCapture{},
253
				nil,
254
			},
255
			true,
256
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
257
			encodingHandler,
258
			noPre,
259
		},
260
	}
261
262
	for _, c := range cases {
263
		setup()
264
		defer teardown()
265
266
		t.Run(c.name, func(t *testing.T) {
267
			c.pre()
268
269
			tMux.HandleFunc(
270
				fmt.Sprintf(
271
					"/v2/payments/%s/captures",
272
					c.args.payment,
273
				),
274
				c.handler,
275
			)
276
277
			res, capture, err := tClient.Captures.Create(c.args.ctx, c.args.payment, c.args.capture)
278
			if c.wantErr {
279
				assert.NotNil(t, err)
280
				assert.EqualError(t, err, c.err.Error())
281
				if tClient.HasAccessToken() {
282
					assert.True(t, capture.Testmode)
283
				}
284
			} else {
285
				assert.Nil(t, err)
286
				assert.IsType(t, &Capture{}, capture)
287
				assert.EqualValues(t, c.args.ctx, res.Request.Context())
288
				assert.IsType(t, &http.Response{}, res.Response)
289
			}
290
		})
291
	}
292
}
293
294
func TestCapturesService_List(t *testing.T) {
295
	setEnv()
296
	defer unsetEnv()
297
298
	type args struct {
299
		ctx     context.Context
300
		payment string
301
		capture string
302
		options *CaptureOptions
303
	}
304
305
	type key string
306
307
	cases := []struct {
308
		name    string
309
		args    args
310
		wantErr bool
311
		err     error
312
		handler http.HandlerFunc
313
		pre     func()
314
	}{
315
		{
316
			"list captures works as expected",
317
			args{
318
				context.Background(),
319
				"tr_WDqYK6vllg",
320
				"cpt_4qqhO89gsT",
321
				&CaptureOptions{
322
					Embed: []EmbedValue{EmbedPayments},
323
				},
324
			},
325
			false,
326
			nil,
327
			func(w http.ResponseWriter, r *http.Request) {
328
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
329
				testMethod(t, r, "GET")
330
				testQuery(t, r, "embed=payments")
331
332
				if _, ok := r.Header[AuthHeader]; !ok {
333
					w.WriteHeader(http.StatusUnauthorized)
334
				}
335
336
				_, _ = w.Write([]byte(testdata.ListCapturesResponse))
337
			},
338
			noPre,
339
		},
340
		{
341
			"list captures returns an http error from the server",
342
			args{
343
				context.WithValue(context.Background(), key("test"), "test-value"),
344
				"tr_WDqYK6vllg",
345
				"cpt_4qqhO89gsT",
346
				&CaptureOptions{},
347
			},
348
			true,
349
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
350
			errorHandler,
351
			noPre,
352
		},
353
		{
354
			"list captures returns an error when creating the request",
355
			args{
356
				context.Background(),
357
				"tr_WDqYK6vllg",
358
				"cpt_4qqhO89gsT",
359
				&CaptureOptions{},
360
			},
361
			true,
362
			errBadBaseURL,
363
			errorHandler,
364
			crashSrv,
365
		},
366
		{
367
			"list captures returns an error when trying to parse the json response",
368
			args{
369
				context.Background(),
370
				"tr_WDqYK6vllg",
371
				"cpt_4qqhO89gsT",
372
				&CaptureOptions{},
373
			},
374
			true,
375
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
376
			encodingHandler,
377
			noPre,
378
		},
379
	}
380
381
	for _, c := range cases {
382
		setup()
383
		defer teardown()
384
385
		t.Run(c.name, func(t *testing.T) {
386
			c.pre()
387
388
			tMux.HandleFunc(
389
				fmt.Sprintf(
390
					"/v2/payments/%s/captures",
391
					c.args.payment,
392
				),
393
				c.handler,
394
			)
395
396
			res, list, err := tClient.Captures.List(c.args.ctx, c.args.payment, c.args.options)
397
			if c.wantErr {
398
				assert.NotNil(t, err)
399
				assert.EqualError(t, err, c.err.Error())
400
			} else {
401
				assert.Nil(t, err)
402
				assert.IsType(t, &CapturesList{}, list)
403
				assert.EqualValues(t, c.args.ctx, res.Request.Context())
404
				assert.IsType(t, &http.Response{}, res.Response)
405
			}
406
		})
407
	}
408
}
409