|
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: &RequestToPayParamsPayer{ |
|
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
|
|
|
|