|
1
|
|
|
package afrikpay |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"crypto/md5" |
|
6
|
|
|
"encoding/json" |
|
7
|
|
|
"fmt" |
|
8
|
|
|
"io/ioutil" |
|
9
|
|
|
"net/http" |
|
10
|
|
|
"testing" |
|
11
|
|
|
|
|
12
|
|
|
"github.com/NdoleStudio/afrikpay-go/internal/helpers" |
|
13
|
|
|
"github.com/NdoleStudio/afrikpay-go/internal/stubs" |
|
14
|
|
|
"github.com/stretchr/testify/assert" |
|
15
|
|
|
) |
|
16
|
|
|
|
|
17
|
|
|
func TestAirtimeService_TransferRequest(t *testing.T) { |
|
18
|
|
|
// Setup |
|
19
|
|
|
t.Parallel() |
|
20
|
|
|
|
|
21
|
|
|
// Arrange |
|
22
|
|
|
requests := make([]*http.Request, 0) |
|
23
|
|
|
apiKey := "api-key" |
|
24
|
|
|
agentID := "agent-id" |
|
25
|
|
|
agentPlatform := "agent-platform" |
|
26
|
|
|
agentPassword := "agent-password" |
|
27
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.TransferWithError()}, &requests) |
|
28
|
|
|
client := New( |
|
29
|
|
|
WithBaseURL(server.URL), |
|
30
|
|
|
WithAgentID(agentID), |
|
31
|
|
|
WithAPIKey(apiKey), |
|
32
|
|
|
WithAgentPassword(agentPassword), |
|
33
|
|
|
WithAgentPlatform(agentPlatform), |
|
34
|
|
|
) |
|
35
|
|
|
params := AirtimeTransferParams{ |
|
36
|
|
|
Operator: "mtn", |
|
37
|
|
|
PurchaseReference: "test-ref", |
|
38
|
|
|
Amount: "987", |
|
39
|
|
|
PhoneNumber: "00000000", |
|
40
|
|
|
Mode: AirtimeModeAccount, |
|
41
|
|
|
} |
|
42
|
|
|
expectedRequest := map[string]interface{}{ |
|
43
|
|
|
"operator": params.Operator, |
|
44
|
|
|
"reference": params.PhoneNumber, |
|
45
|
|
|
"amount": params.Amount, |
|
46
|
|
|
"mode": params.Mode.String(), |
|
47
|
|
|
"purchaseref": params.PurchaseReference, |
|
48
|
|
|
"agentid": agentID, |
|
49
|
|
|
"agentplatform": agentPlatform, |
|
50
|
|
|
"agentpwd": agentPassword, |
|
51
|
|
|
"hash": fmt.Sprintf("%x", md5.Sum([]byte(params.Operator+params.PhoneNumber+params.Amount+apiKey))), |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Act |
|
55
|
|
|
_, _, err := client.Airtime.Transfer(context.Background(), params) |
|
56
|
|
|
assert.NoError(t, err) |
|
57
|
|
|
|
|
58
|
|
|
assert.Equal(t, 1, len(requests)) |
|
59
|
|
|
buf, err := ioutil.ReadAll(requests[0].Body) |
|
60
|
|
|
assert.NoError(t, err) |
|
61
|
|
|
|
|
62
|
|
|
requestBody := map[string]interface{}{} |
|
63
|
|
|
err = json.Unmarshal(buf, &requestBody) |
|
64
|
|
|
assert.NoError(t, err) |
|
65
|
|
|
|
|
66
|
|
|
// Assert |
|
67
|
|
|
assert.Equal(t, expectedRequest, requestBody) |
|
68
|
|
|
|
|
69
|
|
|
// Teardown |
|
70
|
|
|
server.Close() |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
func TestAirtimeService_TransferWithoutError(t *testing.T) { |
|
74
|
|
|
// Setup |
|
75
|
|
|
t.Parallel() |
|
76
|
|
|
|
|
77
|
|
|
// Arrange |
|
78
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.Transfer()) |
|
79
|
|
|
client := New(WithBaseURL(server.URL)) |
|
80
|
|
|
params := AirtimeTransferParams{} |
|
81
|
|
|
|
|
82
|
|
|
// Act |
|
83
|
|
|
transaction, response, err := client.Airtime.Transfer(context.Background(), params) |
|
84
|
|
|
|
|
85
|
|
|
// Assert |
|
86
|
|
|
assert.NoError(t, err) |
|
87
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
88
|
|
|
assert.Equal(t, &AirtimeResponse{ |
|
89
|
|
|
Code: 200, |
|
90
|
|
|
Message: "Success", |
|
91
|
|
|
Result: &AirtimeTransaction{ |
|
92
|
|
|
OperatorID: "1647539307", |
|
93
|
|
|
TransactionID: "1069", |
|
94
|
|
|
Status: "SUCCESS", |
|
95
|
|
|
Date: "2022-03-17 18:48:26", |
|
96
|
|
|
Ticket: nil, |
|
97
|
|
|
ReferenceID: "18360", |
|
98
|
|
|
ProcessingNumber: "aaba045a-d571-41e9-9ea4-54cd78782e03", |
|
99
|
|
|
}, |
|
100
|
|
|
}, transaction) |
|
101
|
|
|
|
|
102
|
|
|
assert.True(t, transaction.IsSuccessfull()) |
|
103
|
|
|
|
|
104
|
|
|
// Teardown |
|
105
|
|
|
server.Close() |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
func TestAirtimeService_TransferWithError(t *testing.T) { |
|
109
|
|
|
// Setup |
|
110
|
|
|
t.Parallel() |
|
111
|
|
|
|
|
112
|
|
|
// Arrange |
|
113
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.TransferWithError()) |
|
114
|
|
|
client := New(WithBaseURL(server.URL)) |
|
115
|
|
|
params := AirtimeTransferParams{} |
|
116
|
|
|
|
|
117
|
|
|
// Act |
|
118
|
|
|
transaction, _, err := client.Airtime.Transfer(context.Background(), params) |
|
119
|
|
|
|
|
120
|
|
|
// Assert |
|
121
|
|
|
assert.NoError(t, err) |
|
122
|
|
|
assert.Nil(t, transaction.Result) |
|
123
|
|
|
|
|
124
|
|
|
assert.Equal(t, "412: bad password", transaction.Message) |
|
125
|
|
|
assert.Equal(t, http.StatusInternalServerError, transaction.Code) |
|
126
|
|
|
assert.Nil(t, transaction.Result) |
|
127
|
|
|
assert.False(t, transaction.IsSuccessfull()) |
|
128
|
|
|
|
|
129
|
|
|
// Teardown |
|
130
|
|
|
server.Close() |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
func TestAirtimeService_StatusRequest(t *testing.T) { |
|
134
|
|
|
// Setup |
|
135
|
|
|
t.Parallel() |
|
136
|
|
|
|
|
137
|
|
|
// Arrange |
|
138
|
|
|
requests := make([]*http.Request, 0) |
|
139
|
|
|
apiKey := "api-key" |
|
140
|
|
|
agentID := "agent-id" |
|
141
|
|
|
agentPlatform := "agent-platform" |
|
142
|
|
|
transactionID := "transaction-id" |
|
143
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.TransferWithError()}, &requests) |
|
144
|
|
|
client := New( |
|
145
|
|
|
WithBaseURL(server.URL), |
|
146
|
|
|
WithAgentID(agentID), |
|
147
|
|
|
WithAPIKey(apiKey), |
|
148
|
|
|
WithAgentPlatform(agentPlatform), |
|
149
|
|
|
) |
|
150
|
|
|
expectedRequest := map[string]interface{}{ |
|
151
|
|
|
"processingnumber": transactionID, |
|
152
|
|
|
"agentid": agentID, |
|
153
|
|
|
"agentplatform": agentPlatform, |
|
154
|
|
|
"hash": fmt.Sprintf("%x", md5.Sum([]byte(transactionID+apiKey))), |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Act |
|
158
|
|
|
_, _, err := client.Airtime.Status(context.Background(), transactionID) |
|
159
|
|
|
assert.NoError(t, err) |
|
160
|
|
|
|
|
161
|
|
|
assert.Equal(t, 1, len(requests)) |
|
162
|
|
|
buf, err := ioutil.ReadAll(requests[0].Body) |
|
163
|
|
|
assert.NoError(t, err) |
|
164
|
|
|
|
|
165
|
|
|
requestBody := map[string]interface{}{} |
|
166
|
|
|
err = json.Unmarshal(buf, &requestBody) |
|
167
|
|
|
assert.NoError(t, err) |
|
168
|
|
|
|
|
169
|
|
|
// Assert |
|
170
|
|
|
assert.Equal(t, expectedRequest, requestBody) |
|
171
|
|
|
|
|
172
|
|
|
// Teardown |
|
173
|
|
|
server.Close() |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
func TestAirtimeService_StatusWithoutError(t *testing.T) { |
|
177
|
|
|
// Setup |
|
178
|
|
|
t.Parallel() |
|
179
|
|
|
|
|
180
|
|
|
// Arrange |
|
181
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.Transfer()) |
|
182
|
|
|
client := New(WithBaseURL(server.URL)) |
|
183
|
|
|
transactionID := "transaction-id" |
|
184
|
|
|
|
|
185
|
|
|
// Act |
|
186
|
|
|
transaction, response, err := client.Airtime.Status(context.Background(), transactionID) |
|
187
|
|
|
|
|
188
|
|
|
// Assert |
|
189
|
|
|
assert.NoError(t, err) |
|
190
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
191
|
|
|
assert.Equal(t, &AirtimeResponse{ |
|
192
|
|
|
Code: 200, |
|
193
|
|
|
Message: "Success", |
|
194
|
|
|
Result: &AirtimeTransaction{ |
|
195
|
|
|
OperatorID: "1647539307", |
|
196
|
|
|
TransactionID: "1069", |
|
197
|
|
|
Status: "SUCCESS", |
|
198
|
|
|
Date: "2022-03-17 18:48:26", |
|
199
|
|
|
Ticket: nil, |
|
200
|
|
|
ReferenceID: "18360", |
|
201
|
|
|
ProcessingNumber: "aaba045a-d571-41e9-9ea4-54cd78782e03", |
|
202
|
|
|
}, |
|
203
|
|
|
}, transaction) |
|
204
|
|
|
|
|
205
|
|
|
assert.True(t, transaction.IsSuccessfull()) |
|
206
|
|
|
|
|
207
|
|
|
// Teardown |
|
208
|
|
|
server.Close() |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
func TestAirtimeService_StatusWithError(t *testing.T) { |
|
212
|
|
|
// Setup |
|
213
|
|
|
t.Parallel() |
|
214
|
|
|
|
|
215
|
|
|
// Arrange |
|
216
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.TransferWithError()) |
|
217
|
|
|
client := New(WithBaseURL(server.URL)) |
|
218
|
|
|
transactionID := "transaction-id" |
|
219
|
|
|
|
|
220
|
|
|
// Act |
|
221
|
|
|
transaction, _, err := client.Airtime.Status(context.Background(), transactionID) |
|
222
|
|
|
|
|
223
|
|
|
// Assert |
|
224
|
|
|
assert.NoError(t, err) |
|
225
|
|
|
assert.Nil(t, transaction.Result) |
|
226
|
|
|
|
|
227
|
|
|
assert.Equal(t, "412: bad password", transaction.Message) |
|
228
|
|
|
assert.Equal(t, http.StatusInternalServerError, transaction.Code) |
|
229
|
|
|
assert.Nil(t, transaction.Result) |
|
230
|
|
|
assert.False(t, transaction.IsSuccessfull()) |
|
231
|
|
|
|
|
232
|
|
|
// Teardown |
|
233
|
|
|
server.Close() |
|
234
|
|
|
} |
|
235
|
|
|
|