1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"errors" |
6
|
|
|
"net/http" |
7
|
|
|
"net/http/httptest" |
8
|
|
|
"net/url" |
9
|
|
|
"os" |
10
|
|
|
"reflect" |
11
|
|
|
"testing" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
// ---------- testing utilities ---------- |
15
|
|
|
var ( |
16
|
|
|
testMux *http.ServeMux |
17
|
|
|
testClient *APIClient |
18
|
|
|
testServer *httptest.Server |
19
|
|
|
) |
20
|
|
|
|
21
|
|
|
func setup() { |
22
|
|
|
testMux = http.NewServeMux() |
23
|
|
|
testServer = httptest.NewServer(testMux) |
24
|
|
|
testClient, _ = NewClient(nil, nil, testServer.URL) |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
func teardown() { |
28
|
|
|
testServer.Close() |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
func setEnv() { |
32
|
|
|
_ = os.Setenv(APITokenEnv, "token_X12b31ggg23") |
33
|
|
|
_ = os.Setenv(OrgTokenEnv, "ey1923n23123n1k3b123jv12g312h31v32g13") |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
func unsetEnv() { |
37
|
|
|
_ = os.Unsetenv(APITokenEnv) |
38
|
|
|
_ = os.Unsetenv(OrgTokenEnv) |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// ---------- .testing utilities ---------- |
42
|
|
|
|
43
|
|
|
func TestNewClient(t *testing.T) { |
44
|
|
|
|
45
|
|
|
type args struct { |
46
|
|
|
ctx context.Context |
47
|
|
|
cl httpClient |
48
|
|
|
uri string |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
u, err := url.Parse("http://localhost") |
52
|
|
|
if err != nil { |
53
|
|
|
t.Fail() |
54
|
|
|
} |
55
|
|
|
want := &APIClient{ |
56
|
|
|
Ctx: context.Background(), |
57
|
|
|
HTTPClient: http.DefaultClient, |
58
|
|
|
BaseURL: u, |
59
|
|
|
APIKey: "", |
60
|
|
|
OrganizationToken: "", |
61
|
|
|
} |
62
|
|
|
want.Payments = &PaymentResource{api: want} |
63
|
|
|
|
64
|
|
|
tests := []struct { |
65
|
|
|
name string |
66
|
|
|
check *APIClient |
67
|
|
|
arguments args |
68
|
|
|
wantErr bool |
69
|
|
|
err error |
70
|
|
|
}{ |
71
|
|
|
{ |
72
|
|
|
name: "test client is build properly", |
73
|
|
|
check: want, |
74
|
|
|
arguments: args{ |
75
|
|
|
ctx: nil, |
76
|
|
|
cl: nil, |
77
|
|
|
uri: "http://localhost", |
78
|
|
|
}, |
79
|
|
|
wantErr: false, |
80
|
|
|
err: nil, |
81
|
|
|
}, |
82
|
|
|
{ |
83
|
|
|
name: "test url parser breaks", |
84
|
|
|
check: want, |
85
|
|
|
arguments: args{ |
86
|
|
|
ctx: nil, |
87
|
|
|
cl: nil, |
88
|
|
|
uri: " http:/:/localhost", |
89
|
|
|
}, |
90
|
|
|
wantErr: true, |
91
|
|
|
err: errors.New("parse http:/:/localhost: first path segment in URL cannot contain colon"), |
92
|
|
|
}, |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
for _, tt := range tests { |
96
|
|
|
t.Run(tt.name, func(t *testing.T) { |
97
|
|
|
got, err := NewClient(tt.arguments.ctx, tt.arguments.cl, tt.arguments.uri) |
98
|
|
|
if tt.wantErr { |
99
|
|
|
if err.Error() != tt.err.Error() { |
100
|
|
|
t.Fatalf("failed while initializing the client: got: %v, want: %v", err, tt.err) |
101
|
|
|
} |
102
|
|
|
} else { |
103
|
|
|
if !reflect.DeepEqual(got, tt.check) { |
104
|
|
|
t.Fatalf("failed while initializing the client: got: %v, want: %v", got, tt.check) |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
}) |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
func TestNewClient_WithEnvVars(t *testing.T) { |
112
|
|
|
setEnv() |
113
|
|
|
defer unsetEnv() |
114
|
|
|
u, err := url.Parse("http://localhost:3000") |
115
|
|
|
if err != nil { |
116
|
|
|
t.Fail() |
117
|
|
|
} |
118
|
|
|
want := &APIClient{ |
119
|
|
|
Ctx: context.Background(), |
120
|
|
|
HTTPClient: http.DefaultClient, |
121
|
|
|
BaseURL: u, |
122
|
|
|
APIKey: "token_X12b31ggg23", |
123
|
|
|
OrganizationToken: "ey1923n23123n1k3b123jv12g312h31v32g13", |
124
|
|
|
} |
125
|
|
|
want.Payments = &PaymentResource{api: want} |
126
|
|
|
got, _ := NewClient(nil, nil, "http://localhost:3000") |
127
|
|
|
|
128
|
|
|
if !reflect.DeepEqual(got, want) { |
129
|
|
|
t.Fatalf("client initialization error, got: %v, want %v", got, want) |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
func TestWithAPIToken(t *testing.T) { |
134
|
|
|
setup() |
135
|
|
|
defer teardown() |
136
|
|
|
tests := []struct { |
137
|
|
|
name string |
138
|
|
|
check string |
139
|
|
|
wantErr bool |
140
|
|
|
err error |
141
|
|
|
afterTest func() |
142
|
|
|
beforeTest func() |
143
|
|
|
}{ |
144
|
|
|
{ |
145
|
|
|
name: "test an empty string returns an error", |
146
|
|
|
check: "", |
147
|
|
|
wantErr: true, |
148
|
|
|
err: errEmptyAPIKey, |
149
|
|
|
}, |
150
|
|
|
{ |
151
|
|
|
name: "test a valid test token is set", |
152
|
|
|
check: "test_$DJAO@A##MKao23u#N", |
153
|
|
|
wantErr: false, |
154
|
|
|
err: nil, |
155
|
|
|
}, |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
for _, tt := range tests { |
159
|
|
|
t.Run(tt.name, func(t *testing.T) { |
160
|
|
|
got := testClient.WithAPIKey(tt.check) |
161
|
|
|
if tt.wantErr { |
162
|
|
|
if !reflect.DeepEqual(got, tt.err) { |
163
|
|
|
t.Fatalf("failed while attaching API key, got: %v, want: %v", got, tt.err) |
164
|
|
|
} |
165
|
|
|
} else { |
166
|
|
|
if testClient.APIKey != tt.check { |
167
|
|
|
t.Fail() |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
}) |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
func TestWithOrganizationToken(t *testing.T) { |
175
|
|
|
setup() |
176
|
|
|
defer teardown() |
177
|
|
|
tests := []struct { |
178
|
|
|
name string |
179
|
|
|
check string |
180
|
|
|
wantErr bool |
181
|
|
|
err error |
182
|
|
|
afterTest func() |
183
|
|
|
beforeTest func() |
184
|
|
|
}{ |
185
|
|
|
{ |
186
|
|
|
name: "test an empty string returns an error", |
187
|
|
|
check: "", |
188
|
|
|
wantErr: true, |
189
|
|
|
err: errEmptyAPIKey, |
190
|
|
|
}, |
191
|
|
|
{ |
192
|
|
|
name: "test a valid test token is set", |
193
|
|
|
check: "styduasjkdlmqwhdbw", |
194
|
|
|
wantErr: false, |
195
|
|
|
err: nil, |
196
|
|
|
}, |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
for _, tt := range tests { |
200
|
|
|
t.Run(tt.name, func(t *testing.T) { |
201
|
|
|
got := testClient.WithOrganizationToken(tt.check) |
202
|
|
|
if tt.wantErr { |
203
|
|
|
if !reflect.DeepEqual(got, tt.err) { |
204
|
|
|
t.Fatalf("failed while attaching API key, got: %v, want: %v", got, tt.err) |
205
|
|
|
} |
206
|
|
|
} else { |
207
|
|
|
if testClient.OrganizationToken != tt.check { |
208
|
|
|
t.Fail() |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
}) |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
func TestNewAPIRequest_WellformedRequest(t *testing.T) { |
216
|
|
|
setup() |
217
|
|
|
defer teardown() |
218
|
|
|
|
219
|
|
|
inURI, outURI := "/testing", testServer.URL+"/v2/testing" |
220
|
|
|
inMethod, outMethod := "", http.MethodGet |
221
|
|
|
|
222
|
|
|
got, err := testClient.NewAPIRequest(inMethod, inURI, nil, false) |
223
|
|
|
if err != nil { |
224
|
|
|
t.Fatalf("failed while building the API Request: %v", err) |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if got.URL.String() != outURI { |
228
|
|
|
t.Fatalf("mailformed request uri: want: %v, got: %v", outURI, got.URL) |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if got.Method != outMethod { |
232
|
|
|
t.Fatalf("method assignment failed: got: %v, want: %v", got.Method, outMethod) |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
func TestNewAPIRequest_WithAPIKeyAuthHeader(t *testing.T) { |
237
|
|
|
setup() |
238
|
|
|
defer teardown() |
239
|
|
|
|
240
|
|
|
var testKey = "test_demoAPIKey1234" |
241
|
|
|
|
242
|
|
|
testClient.WithAPIKey(testKey) |
243
|
|
|
|
244
|
|
|
req, err := testClient.NewAPIRequest("", "/testing", nil, true) |
245
|
|
|
if err != nil { |
246
|
|
|
t.Fatalf("failed while building the API Request: %v", err) |
247
|
|
|
} |
248
|
|
|
got := req.Header.Get(AuthHeader) |
249
|
|
|
want := "Bearer " + testKey |
250
|
|
|
|
251
|
|
|
if got != want { |
252
|
|
|
t.Fatalf("API Key header is not set: got %v, want %v", got, want) |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
func TestNewAPIRequest_WithOrgTokenAuthHeader(t *testing.T) { |
257
|
|
|
setup() |
258
|
|
|
defer teardown() |
259
|
|
|
|
260
|
|
|
var apiKey = "test_demoAPIKey1234" |
261
|
|
|
var orgToken = "token_demoOrgToken" |
262
|
|
|
|
263
|
|
|
// Accoring to the token hierarchy Org Token should be used. |
264
|
|
|
testClient.WithOrganizationToken(orgToken) |
265
|
|
|
testClient.WithAPIKey(apiKey) |
266
|
|
|
|
267
|
|
|
req, err := testClient.NewAPIRequest("", "/testing", nil, true) |
268
|
|
|
if err != nil { |
269
|
|
|
t.Fatalf("failed while building the API Request: %v", err) |
270
|
|
|
} |
271
|
|
|
got := req.Header.Get(AuthHeader) |
272
|
|
|
want := "Bearer " + orgToken |
273
|
|
|
|
274
|
|
|
if got != want { |
275
|
|
|
t.Fatalf("API Key header is not set: got %v, want %v", got, want) |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
func TestNewAPIRequest_AuthError(t *testing.T) { |
280
|
|
|
setup() |
281
|
|
|
defer teardown() |
282
|
|
|
|
283
|
|
|
_, err := testClient.NewAPIRequest("", "/testing", nil, true) |
284
|
|
|
|
285
|
|
|
if err != errEmptyAPIKey { |
286
|
|
|
t.Fatalf("mismatching error message: got %v, want %v", err, errEmptyAPIKey) |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|