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: &RequestToPayPayer{ |
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, &RequestToPayStatus{ |
128
|
|
|
Amount: "100", |
129
|
|
|
Currency: "UGX", |
130
|
|
|
FinancialTransactionID: &financialTransactionID, |
131
|
|
|
ExternalID: "947354", |
132
|
|
|
Payer: &RequestToPayPayer{ |
133
|
|
|
PartyIDType: "MSISDN", |
134
|
|
|
PartyID: "4656473839", |
135
|
|
|
}, |
136
|
|
|
Status: "SUCCESSFUL", |
137
|
|
|
}, status) |
138
|
|
|
|
139
|
|
|
// Teardown |
140
|
|
|
server.Close() |
141
|
|
|
} |
142
|
|
|
|