|
1
|
|
|
package mtnmomo |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"net/http" |
|
6
|
|
|
"strings" |
|
7
|
|
|
"testing" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/google/uuid" |
|
10
|
|
|
|
|
11
|
|
|
"github.com/NdoleStudio/mtnmomo-go/internal/helpers" |
|
12
|
|
|
"github.com/NdoleStudio/mtnmomo-go/internal/stubs" |
|
13
|
|
|
"github.com/stretchr/testify/assert" |
|
14
|
|
|
) |
|
15
|
|
|
|
|
16
|
|
|
func TestCollectionService_Token(t *testing.T) { |
|
17
|
|
|
// Setup |
|
18
|
|
|
t.Parallel() |
|
19
|
|
|
|
|
20
|
|
|
// Arrange |
|
21
|
|
|
requests := make([]*http.Request, 0) |
|
22
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.CollectionToken()}, &requests) |
|
23
|
|
|
client := New( |
|
24
|
|
|
WithBaseURL(server.URL), |
|
25
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
|
26
|
|
|
WithAPIUser(testAPIUser), |
|
27
|
|
|
WithAPIKey(testAPIKey), |
|
28
|
|
|
) |
|
29
|
|
|
|
|
30
|
|
|
// Act |
|
31
|
|
|
authToken, response, err := client.Collection.Token(context.Background()) |
|
32
|
|
|
|
|
33
|
|
|
// Assert |
|
34
|
|
|
assert.Nil(t, err) |
|
35
|
|
|
|
|
36
|
|
|
assert.GreaterOrEqual(t, 1, len(requests)) |
|
37
|
|
|
request := requests[0] |
|
38
|
|
|
username, password, _ := request.BasicAuth() |
|
39
|
|
|
assert.Equal(t, testAPIUser, username) |
|
40
|
|
|
assert.Equal(t, testAPIKey, password) |
|
41
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
|
42
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
43
|
|
|
assert.Equal(t, int64(3600), authToken.ExpiresIn) |
|
44
|
|
|
assert.Equal(t, "access_token", authToken.TokenType) |
|
45
|
|
|
assert.Equal(t, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", authToken.AccessToken) |
|
46
|
|
|
|
|
47
|
|
|
// Teardown |
|
48
|
|
|
server.Close() |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
func TestCollectionService_RequestToPay(t *testing.T) { |
|
52
|
|
|
// Setup |
|
53
|
|
|
t.Parallel() |
|
54
|
|
|
|
|
55
|
|
|
// Arrange |
|
56
|
|
|
requests := make([]*http.Request, 0) |
|
57
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.CollectionToken(), nil}, &requests) |
|
58
|
|
|
client := New( |
|
59
|
|
|
WithBaseURL(server.URL), |
|
60
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
|
61
|
|
|
WithAPIUser(testAPIUser), |
|
62
|
|
|
WithAPIKey(testAPIKey), |
|
63
|
|
|
) |
|
64
|
|
|
|
|
65
|
|
|
// Act |
|
66
|
|
|
response, err := client.Collection.RequestToPay( |
|
67
|
|
|
context.Background(), |
|
68
|
|
|
uuid.NewString(), |
|
69
|
|
|
&RequestToPayParams{ |
|
70
|
|
|
Amount: "10", |
|
71
|
|
|
Currency: "EUR", |
|
72
|
|
|
ExternalID: uuid.NewString(), |
|
73
|
|
|
Payer: &AccountHolder{ |
|
74
|
|
|
PartyIDType: "MSISDN", |
|
75
|
|
|
PartyID: "46733123453", |
|
76
|
|
|
}, |
|
77
|
|
|
PayerMessage: "Test Payer Message", |
|
78
|
|
|
PayeeNote: "Test Payee Note", |
|
79
|
|
|
}, |
|
80
|
|
|
nil, |
|
81
|
|
|
) |
|
82
|
|
|
|
|
83
|
|
|
// Assert |
|
84
|
|
|
assert.Nil(t, err) |
|
85
|
|
|
|
|
86
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
|
87
|
|
|
request := requests[len(requests)-1] |
|
88
|
|
|
assert.Equal(t, "/collection/v1_0/requesttopay", request.URL.Path) |
|
89
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
|
90
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
|
91
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
92
|
|
|
|
|
93
|
|
|
// Teardown |
|
94
|
|
|
server.Close() |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
func TestCollectionService_GetRequestToPayStatus(t *testing.T) { |
|
98
|
|
|
// Setup |
|
99
|
|
|
t.Parallel() |
|
100
|
|
|
|
|
101
|
|
|
// Arrange |
|
102
|
|
|
requests := make([]*http.Request, 0) |
|
103
|
|
|
responses := [][]byte{stubs.CollectionToken(), stubs.CollectionRequestToPayStatus()} |
|
104
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
|
105
|
|
|
client := New( |
|
106
|
|
|
WithBaseURL(server.URL), |
|
107
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
|
108
|
|
|
WithAPIUser(testAPIUser), |
|
109
|
|
|
WithAPIKey(testAPIKey), |
|
110
|
|
|
) |
|
111
|
|
|
referenceID := uuid.NewString() |
|
112
|
|
|
|
|
113
|
|
|
// Act |
|
114
|
|
|
status, response, err := client.Collection.GetRequestToPayStatus(context.Background(), referenceID) |
|
115
|
|
|
|
|
116
|
|
|
// Assert |
|
117
|
|
|
assert.Nil(t, err) |
|
118
|
|
|
|
|
119
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
|
120
|
|
|
request := requests[len(requests)-1] |
|
121
|
|
|
assert.Equal(t, "/collection/v1_0/requesttopay/"+referenceID, request.URL.Path) |
|
122
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
|
123
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
|
124
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
125
|
|
|
|
|
126
|
|
|
financialTransactionID := "23503452" |
|
127
|
|
|
assert.Equal(t, &CollectionTransactionStatus{ |
|
128
|
|
|
Amount: "100", |
|
129
|
|
|
Currency: "UGX", |
|
130
|
|
|
FinancialTransactionID: &financialTransactionID, |
|
131
|
|
|
ExternalID: "947354", |
|
132
|
|
|
ReferenceID: referenceID, |
|
133
|
|
|
Payer: &AccountHolder{ |
|
134
|
|
|
PartyIDType: "MSISDN", |
|
135
|
|
|
PartyID: "4656473839", |
|
136
|
|
|
}, |
|
137
|
|
|
Status: "SUCCESSFUL", |
|
138
|
|
|
}, status) |
|
139
|
|
|
|
|
140
|
|
|
// Teardown |
|
141
|
|
|
server.Close() |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
func TestCollectionService_GetAccountBalance(t *testing.T) { |
|
145
|
|
|
// Setup |
|
146
|
|
|
t.Parallel() |
|
147
|
|
|
|
|
148
|
|
|
// Arrange |
|
149
|
|
|
requests := make([]*http.Request, 0) |
|
150
|
|
|
responses := [][]byte{stubs.CollectionToken(), stubs.CollectionAccountBalance()} |
|
151
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
|
152
|
|
|
client := New( |
|
153
|
|
|
WithBaseURL(server.URL), |
|
154
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
|
155
|
|
|
WithAPIUser(testAPIUser), |
|
156
|
|
|
WithAPIKey(testAPIKey), |
|
157
|
|
|
) |
|
158
|
|
|
|
|
159
|
|
|
// Act |
|
160
|
|
|
status, response, err := client.Collection.GetAccountBalance(context.Background()) |
|
161
|
|
|
|
|
162
|
|
|
// Assert |
|
163
|
|
|
assert.Nil(t, err) |
|
164
|
|
|
|
|
165
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
|
166
|
|
|
request := requests[len(requests)-1] |
|
167
|
|
|
assert.Equal(t, "/collection/v1_0/account/balance", request.URL.Path) |
|
168
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
|
169
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
|
170
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
171
|
|
|
|
|
172
|
|
|
assert.Equal(t, &AccountBalance{AvailableBalance: "1000", Currency: "EUR"}, status) |
|
173
|
|
|
|
|
174
|
|
|
// Teardown |
|
175
|
|
|
server.Close() |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
func TestCollectionService_ValidateAccountHolderStatus(t *testing.T) { |
|
179
|
|
|
// Setup |
|
180
|
|
|
t.Parallel() |
|
181
|
|
|
|
|
182
|
|
|
// Arrange |
|
183
|
|
|
requests := make([]*http.Request, 0) |
|
184
|
|
|
responses := [][]byte{stubs.CollectionToken(), stubs.CollectionValidateAccountHolderStatus()} |
|
185
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
|
186
|
|
|
client := New( |
|
187
|
|
|
WithBaseURL(server.URL), |
|
188
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
|
189
|
|
|
WithAPIUser(testAPIUser), |
|
190
|
|
|
WithAPIKey(testAPIKey), |
|
191
|
|
|
) |
|
192
|
|
|
|
|
193
|
|
|
// Act |
|
194
|
|
|
status, response, err := client.Collection.ValidateAccountHolderStatus(context.Background(), AccountHolderIDTypeMSISDN, "23777777777") |
|
195
|
|
|
|
|
196
|
|
|
// Assert |
|
197
|
|
|
assert.Nil(t, err) |
|
198
|
|
|
|
|
199
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
|
200
|
|
|
request := requests[len(requests)-1] |
|
201
|
|
|
assert.Equal(t, "/collection/v1_0/accountholder/msisdn/23777777777/active", request.URL.Path) |
|
202
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
|
203
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
|
204
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
205
|
|
|
|
|
206
|
|
|
assert.True(t, status.IsActive) |
|
207
|
|
|
|
|
208
|
|
|
// Teardown |
|
209
|
|
|
server.Close() |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
func TestCollectionService_ValidateAccountHolderStatusWithInternalServerError(t *testing.T) { |
|
213
|
|
|
// Setup |
|
214
|
|
|
t.Parallel() |
|
215
|
|
|
|
|
216
|
|
|
// Arrange |
|
217
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
|
218
|
|
|
client := New(WithBaseURL(server.URL)) |
|
219
|
|
|
|
|
220
|
|
|
// Act |
|
221
|
|
|
_, _, err := client.Collection.ValidateAccountHolderStatus(context.Background(), AccountHolderIDTypeMSISDN, "23777777777") |
|
222
|
|
|
|
|
223
|
|
|
// Assert |
|
224
|
|
|
assert.NotNil(t, err) |
|
225
|
|
|
|
|
226
|
|
|
// Teardown |
|
227
|
|
|
server.Close() |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
func TestCollectionService_GetBasicUserinfo(t *testing.T) { |
|
231
|
|
|
// Setup |
|
232
|
|
|
t.Parallel() |
|
233
|
|
|
|
|
234
|
|
|
// Arrange |
|
235
|
|
|
requests := make([]*http.Request, 0) |
|
236
|
|
|
responses := [][]byte{stubs.CollectionToken(), stubs.CollectionGetBasicUserinfo()} |
|
237
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
|
238
|
|
|
client := New( |
|
239
|
|
|
WithBaseURL(server.URL), |
|
240
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
|
241
|
|
|
WithAPIUser(testAPIUser), |
|
242
|
|
|
WithAPIKey(testAPIKey), |
|
243
|
|
|
) |
|
244
|
|
|
|
|
245
|
|
|
// Act |
|
246
|
|
|
userInfo, response, err := client.Collection.GetBasicUserinfo(context.Background(), AccountHolderIDTypeMSISDN, "23777777777") |
|
247
|
|
|
|
|
248
|
|
|
// Assert |
|
249
|
|
|
assert.Nil(t, err) |
|
250
|
|
|
|
|
251
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
|
252
|
|
|
request := requests[len(requests)-1] |
|
253
|
|
|
assert.Equal(t, "/collection/v1_0/accountholder/msisdn/23777777777/basicuserinfo", request.URL.Path) |
|
254
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
|
255
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
|
256
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
257
|
|
|
|
|
258
|
|
|
assert.Equal(t, "JOHN DOE", userInfo.FullName()) |
|
259
|
|
|
|
|
260
|
|
|
// Teardown |
|
261
|
|
|
server.Close() |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
func TestCollectionService_GetBasicUserinfoWithInternalServerError(t *testing.T) { |
|
265
|
|
|
// Setup |
|
266
|
|
|
t.Parallel() |
|
267
|
|
|
|
|
268
|
|
|
// Arrange |
|
269
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
|
270
|
|
|
client := New(WithBaseURL(server.URL)) |
|
271
|
|
|
|
|
272
|
|
|
// Act |
|
273
|
|
|
_, _, err := client.Collection.GetBasicUserinfo(context.Background(), AccountHolderIDTypeMSISDN, "23777777777") |
|
274
|
|
|
|
|
275
|
|
|
// Assert |
|
276
|
|
|
assert.NotNil(t, err) |
|
277
|
|
|
|
|
278
|
|
|
// Teardown |
|
279
|
|
|
server.Close() |
|
280
|
|
|
} |
|
281
|
|
|
|