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 TestDisbursementsService_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.DisbursementToken()}, &requests) |
23
|
|
|
client := New( |
24
|
|
|
WithBaseURL(server.URL), |
25
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
26
|
|
|
WithDisbursementAccount(testAPIUser, testAPIKey), |
27
|
|
|
) |
28
|
|
|
|
29
|
|
|
// Act |
30
|
|
|
authToken, response, err := client.Disbursement.Token(context.Background()) |
31
|
|
|
|
32
|
|
|
// Assert |
33
|
|
|
assert.Nil(t, err) |
34
|
|
|
|
35
|
|
|
assert.GreaterOrEqual(t, 1, len(requests)) |
36
|
|
|
request := requests[0] |
37
|
|
|
username, password, _ := request.BasicAuth() |
38
|
|
|
assert.Equal(t, testAPIUser, username) |
39
|
|
|
assert.Equal(t, testAPIKey, password) |
40
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
41
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
42
|
|
|
assert.Equal(t, int64(3600), authToken.ExpiresIn) |
43
|
|
|
assert.Equal(t, "access_token", authToken.TokenType) |
44
|
|
|
assert.Equal(t, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", authToken.AccessToken) |
45
|
|
|
|
46
|
|
|
// Teardown |
47
|
|
|
server.Close() |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
func TestDisbursementsService_Transfer(t *testing.T) { |
51
|
|
|
// Setup |
52
|
|
|
t.Parallel() |
53
|
|
|
|
54
|
|
|
// Arrange |
55
|
|
|
requests := make([]*http.Request, 0) |
56
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.CollectionToken(), nil}, &requests) |
57
|
|
|
client := New( |
58
|
|
|
WithBaseURL(server.URL), |
59
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
60
|
|
|
WithDisbursementAccount(testAPIUser, testAPIKey), |
61
|
|
|
) |
62
|
|
|
|
63
|
|
|
// Act |
64
|
|
|
response, err := client.Disbursement.Transfer( |
65
|
|
|
context.Background(), |
66
|
|
|
uuid.NewString(), |
67
|
|
|
&TransferParams{ |
68
|
|
|
Amount: "100", |
69
|
|
|
Currency: "EUR", |
70
|
|
|
ExternalID: uuid.NewString(), |
71
|
|
|
Payee: &AccountHolder{ |
72
|
|
|
PartyIDType: "MSISDN", |
73
|
|
|
PartyID: "46733123453", |
74
|
|
|
}, |
75
|
|
|
PayerMessage: "Test Payer Message", |
76
|
|
|
PayeeNote: "Test Payee Note", |
77
|
|
|
}, |
78
|
|
|
nil, |
79
|
|
|
) |
80
|
|
|
|
81
|
|
|
// Assert |
82
|
|
|
assert.Nil(t, err) |
83
|
|
|
|
84
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
85
|
|
|
request := requests[len(requests)-1] |
86
|
|
|
assert.Equal(t, "/disbursement/v1_0/transfer", request.URL.Path) |
87
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
88
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
89
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
90
|
|
|
|
91
|
|
|
// Teardown |
92
|
|
|
server.Close() |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
func TestDisbursementsService_GetTransferStatus(t *testing.T) { |
96
|
|
|
// Setup |
97
|
|
|
t.Parallel() |
98
|
|
|
|
99
|
|
|
// Arrange |
100
|
|
|
requests := make([]*http.Request, 0) |
101
|
|
|
responses := [][]byte{stubs.CollectionToken(), stubs.DisbursementTransferStatus()} |
102
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
103
|
|
|
client := New( |
104
|
|
|
WithBaseURL(server.URL), |
105
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
106
|
|
|
WithDisbursementAccount(testAPIUser, testAPIKey), |
107
|
|
|
) |
108
|
|
|
referenceID := uuid.NewString() |
109
|
|
|
|
110
|
|
|
// Act |
111
|
|
|
status, response, err := client.Disbursement.GetTransferStatus(context.Background(), referenceID) |
112
|
|
|
|
113
|
|
|
// Assert |
114
|
|
|
assert.Nil(t, err) |
115
|
|
|
|
116
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
117
|
|
|
request := requests[len(requests)-1] |
118
|
|
|
assert.Equal(t, "/disbursement/v1_0/transfer/"+referenceID, request.URL.Path) |
119
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
120
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
121
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
122
|
|
|
|
123
|
|
|
financialTransactionID := "23503452" |
124
|
|
|
assert.Equal(t, &DisbursementTransactionStatus{ |
125
|
|
|
Amount: "100", |
126
|
|
|
Currency: "UGX", |
127
|
|
|
FinancialTransactionID: &financialTransactionID, |
128
|
|
|
ExternalID: "947354", |
129
|
|
|
ReferenceID: referenceID, |
130
|
|
|
Payee: &AccountHolder{ |
131
|
|
|
PartyIDType: "MSISDN", |
132
|
|
|
PartyID: "4656473839", |
133
|
|
|
}, |
134
|
|
|
Status: "SUCCESSFUL", |
135
|
|
|
}, status) |
136
|
|
|
|
137
|
|
|
// Teardown |
138
|
|
|
server.Close() |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
func TestDisbursementsService_GetAccountBalance(t *testing.T) { |
142
|
|
|
// Setup |
143
|
|
|
t.Parallel() |
144
|
|
|
|
145
|
|
|
// Arrange |
146
|
|
|
requests := make([]*http.Request, 0) |
147
|
|
|
responses := [][]byte{stubs.CollectionToken(), stubs.DisbursementAccountBalance()} |
148
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
149
|
|
|
client := New( |
150
|
|
|
WithBaseURL(server.URL), |
151
|
|
|
WithSubscriptionKey(testSubscriptionKey), |
152
|
|
|
WithDisbursementAccount(testAPIUser, testAPIKey), |
153
|
|
|
) |
154
|
|
|
|
155
|
|
|
// Act |
156
|
|
|
status, response, err := client.Disbursement.GetAccountBalance(context.Background()) |
157
|
|
|
|
158
|
|
|
// Assert |
159
|
|
|
assert.Nil(t, err) |
160
|
|
|
|
161
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
162
|
|
|
request := requests[len(requests)-1] |
163
|
|
|
assert.Equal(t, "/disbursement/v1_0/account/balance", request.URL.Path) |
164
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Bearer")) |
165
|
|
|
assert.Equal(t, testSubscriptionKey, request.Header.Get(headerKeySubscriptionKey)) |
166
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
167
|
|
|
|
168
|
|
|
assert.Equal(t, &AccountBalance{AvailableBalance: "1000", Currency: "EUR"}, status) |
169
|
|
|
|
170
|
|
|
// Teardown |
171
|
|
|
server.Close() |
172
|
|
|
} |
173
|
|
|
|