|
1
|
|
|
package campay |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"net/http" |
|
6
|
|
|
"strings" |
|
7
|
|
|
"testing" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/NdoleStudio/campay-go-sdk/internal/helpers" |
|
10
|
|
|
"github.com/NdoleStudio/campay-go-sdk/internal/stubs" |
|
11
|
|
|
|
|
12
|
|
|
"github.com/stretchr/testify/assert" |
|
13
|
|
|
) |
|
14
|
|
|
|
|
15
|
|
|
func TestNew(t *testing.T) { |
|
16
|
|
|
t.Run("default configuration is used when no option is set", func(t *testing.T) { |
|
17
|
|
|
// act |
|
18
|
|
|
client := New() |
|
19
|
|
|
|
|
20
|
|
|
// assert |
|
21
|
|
|
assert.NotEmpty(t, client.environment) |
|
22
|
|
|
assert.NotEmpty(t, client.common) |
|
23
|
|
|
|
|
24
|
|
|
assert.Empty(t, client.apiUsername) |
|
25
|
|
|
assert.Empty(t, client.apiPassword) |
|
26
|
|
|
|
|
27
|
|
|
assert.NotNil(t, client.httpClient) |
|
28
|
|
|
assert.NotNil(t, client.Transaction) |
|
29
|
|
|
}) |
|
30
|
|
|
|
|
31
|
|
|
t.Run("single configuration value can be set using options", func(t *testing.T) { |
|
32
|
|
|
// Arrange |
|
33
|
|
|
env := Environment("https://example.com") |
|
34
|
|
|
|
|
35
|
|
|
// Act |
|
36
|
|
|
client := New(WithEnvironment(env)) |
|
37
|
|
|
|
|
38
|
|
|
// Assert |
|
39
|
|
|
assert.NotNil(t, client.environment) |
|
40
|
|
|
assert.Equal(t, env.String(), client.environment.String()) |
|
41
|
|
|
}) |
|
42
|
|
|
|
|
43
|
|
|
t.Run("multiple configuration values can be set using options", func(t *testing.T) { |
|
44
|
|
|
// Arrange |
|
45
|
|
|
env := Environment("https://example.com") |
|
46
|
|
|
newHTTPClient := &http.Client{Timeout: 422} |
|
47
|
|
|
|
|
48
|
|
|
// Act |
|
49
|
|
|
client := New(WithEnvironment(env), WithHTTPClient(newHTTPClient)) |
|
50
|
|
|
|
|
51
|
|
|
// Assert |
|
52
|
|
|
assert.NotEmpty(t, client.environment) |
|
53
|
|
|
assert.Equal(t, env.String(), client.environment.String()) |
|
54
|
|
|
|
|
55
|
|
|
assert.NotNil(t, client.httpClient) |
|
56
|
|
|
assert.Equal(t, newHTTPClient.Timeout, client.httpClient.Timeout) |
|
57
|
|
|
}) |
|
58
|
|
|
|
|
59
|
|
|
t.Run("it sets the Transaction service correctly", func(t *testing.T) { |
|
60
|
|
|
// Arrange |
|
61
|
|
|
client := New() |
|
62
|
|
|
|
|
63
|
|
|
// Assert |
|
64
|
|
|
assert.NotNil(t, client.Transaction) |
|
65
|
|
|
assert.Equal(t, client.environment.String(), client.Transaction.client.environment.String()) |
|
66
|
|
|
}) |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
func TestClient_ValidateCallback(t *testing.T) { |
|
70
|
|
|
t.Run("valid signature and key returns nil error", func(t *testing.T) { |
|
71
|
|
|
// Arrange |
|
72
|
|
|
client := New() |
|
73
|
|
|
key := []byte("geTrvNBLvXS35UvK3PnTnQgpjGmaEGe7wa6k3Ns4zehhvjncsjXnQsV7ZzhDWjDMEt7") |
|
74
|
|
|
signature := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.9kFRoXMiiGWhCbnDPoOMVhgVTzYxu2MjNi-uTWGRcEU" |
|
75
|
|
|
|
|
76
|
|
|
// Act |
|
77
|
|
|
err := client.ValidateCallback(signature, key) |
|
78
|
|
|
|
|
79
|
|
|
// Assert |
|
80
|
|
|
assert.Nil(t, err) |
|
81
|
|
|
}) |
|
82
|
|
|
|
|
83
|
|
|
t.Run("invalid key returns and error", func(t *testing.T) { |
|
84
|
|
|
// Arrange |
|
85
|
|
|
client := New() |
|
86
|
|
|
key := []byte("geTrvNBLvXS35UvK3PnTnQgpjGmaEGe7wa6k3Ns4zehhvjncsjXnQsV7ZzhDWjDMEt7-invalid") |
|
87
|
|
|
signature := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.9kFRoXMiiGWhCbnDPoOMVhgVTzYxu2MjNi-uTWGRcEU" |
|
88
|
|
|
|
|
89
|
|
|
// Act |
|
90
|
|
|
err := client.ValidateCallback(signature, key) |
|
91
|
|
|
|
|
92
|
|
|
// Assert |
|
93
|
|
|
assert.NotNil(t, err) |
|
94
|
|
|
}) |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
func TestClient_Withdraw(t *testing.T) { |
|
98
|
|
|
// Setup |
|
99
|
|
|
t.Parallel() |
|
100
|
|
|
|
|
101
|
|
|
// Arrange |
|
102
|
|
|
requests := make([]*http.Request, 0) |
|
103
|
|
|
responses := [][]byte{stubs.PostTokenResponse(), stubs.PostWithdrawResponse()} |
|
104
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
|
105
|
|
|
client := New(WithEnvironment(Environment(server.URL))) |
|
106
|
|
|
|
|
107
|
|
|
// Act |
|
108
|
|
|
withdrawResponse, response, err := client.Withdraw(context.Background(), &WithdrawParams{ |
|
109
|
|
|
Amount: 100, |
|
110
|
|
|
To: "2376XXXXXXXX", |
|
111
|
|
|
Description: "Test", |
|
112
|
|
|
ExternalReference: nil, |
|
113
|
|
|
}) |
|
114
|
|
|
|
|
115
|
|
|
// Assert |
|
116
|
|
|
assert.Nil(t, err) |
|
117
|
|
|
|
|
118
|
|
|
assert.GreaterOrEqual(t, len(requests), 1) |
|
119
|
|
|
request := requests[len(requests)-1] |
|
120
|
|
|
assert.Equal(t, "/api/withdraw/", request.URL.Path) |
|
121
|
|
|
assert.True(t, strings.HasPrefix(request.Header.Get("Authorization"), "Token")) |
|
122
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
123
|
|
|
|
|
124
|
|
|
assert.Equal(t, &WithdrawResponse{Reference: "26676007-1c31-46d7-9c71-acb031cf0de4", Status: "PENDING"}, withdrawResponse) |
|
125
|
|
|
|
|
126
|
|
|
// Teardown |
|
127
|
|
|
server.Close() |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
func TestClient_WithdrawSync(t *testing.T) { |
|
131
|
|
|
// Setup |
|
132
|
|
|
t.Parallel() |
|
133
|
|
|
|
|
134
|
|
|
// Arrange |
|
135
|
|
|
requests := make([]*http.Request, 0) |
|
136
|
|
|
responses := [][]byte{stubs.PostTokenResponse(), stubs.PostWithdrawResponse(), stubs.GetPendingTransactionResponse(), stubs.GetSuccessfulTransactionResponse()} |
|
137
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, responses, &requests) |
|
138
|
|
|
client := New(WithEnvironment(Environment(server.URL))) |
|
139
|
|
|
|
|
140
|
|
|
// Act |
|
141
|
|
|
transaction, _, err := client.WithdrawSync(context.Background(), &WithdrawParams{ |
|
142
|
|
|
Amount: 100, |
|
143
|
|
|
To: "2376XXXXXXXX", |
|
144
|
|
|
Description: "Test", |
|
145
|
|
|
ExternalReference: nil, |
|
146
|
|
|
}) |
|
147
|
|
|
|
|
148
|
|
|
// Assert |
|
149
|
|
|
assert.Nil(t, err) |
|
150
|
|
|
|
|
151
|
|
|
assert.GreaterOrEqual(t, len(requests), 4) |
|
152
|
|
|
assert.Equal(t, &Transaction{ |
|
153
|
|
|
Reference: "bcedde9b-62a7-4421-96ac-2e6179552a1a", |
|
154
|
|
|
Status: "SUCCESSFUL", |
|
155
|
|
|
Amount: 1, |
|
156
|
|
|
Currency: "XAF", |
|
157
|
|
|
Operator: "MTN", |
|
158
|
|
|
Code: "CP201027T00005", |
|
159
|
|
|
OperatorReference: "1880106956", |
|
160
|
|
|
}, transaction) |
|
161
|
|
|
|
|
162
|
|
|
assert.True(t, transaction.IsSuccessfull()) |
|
163
|
|
|
assert.False(t, transaction.IsPending()) |
|
164
|
|
|
|
|
165
|
|
|
// Teardown |
|
166
|
|
|
server.Close() |
|
167
|
|
|
} |
|
168
|
|
|
|