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 TestAccountService_BalanceRequest(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.AccountBalanceWithError()}, &requests) |
28
|
|
|
client := New( |
29
|
|
|
WithBaseURL(server.URL), |
30
|
|
|
WithAgentID(agentID), |
31
|
|
|
WithAPIKey(apiKey), |
32
|
|
|
WithAgentPassword(agentPassword), |
33
|
|
|
WithAgentPlatform(agentPlatform), |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
expectedRequest := map[string]interface{}{ |
37
|
|
|
"agentid": agentID, |
38
|
|
|
"agentplatform": agentPlatform, |
39
|
|
|
"hash": fmt.Sprintf("%x", md5.Sum([]byte(agentID+apiKey))), |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Act |
43
|
|
|
_, _, err := client.Account.Balance(context.Background()) |
44
|
|
|
assert.NoError(t, err) |
45
|
|
|
|
46
|
|
|
assert.Equal(t, 1, len(requests)) |
47
|
|
|
buf, err := ioutil.ReadAll(requests[0].Body) |
48
|
|
|
assert.NoError(t, err) |
49
|
|
|
|
50
|
|
|
requestBody := map[string]interface{}{} |
51
|
|
|
err = json.Unmarshal(buf, &requestBody) |
52
|
|
|
assert.NoError(t, err) |
53
|
|
|
|
54
|
|
|
// Assert |
55
|
|
|
assert.Equal(t, expectedRequest, requestBody) |
56
|
|
|
|
57
|
|
|
// Teardown |
58
|
|
|
server.Close() |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
func TestAccountService_BalanceWithoutError(t *testing.T) { |
62
|
|
|
// Setup |
63
|
|
|
t.Parallel() |
64
|
|
|
|
65
|
|
|
// Arrange |
66
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.AccountBalance()) |
67
|
|
|
client := New(WithBaseURL(server.URL)) |
68
|
|
|
|
69
|
|
|
// Act |
70
|
|
|
transaction, response, err := client.Account.Balance(context.Background()) |
71
|
|
|
|
72
|
|
|
// Assert |
73
|
|
|
assert.NoError(t, err) |
74
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
75
|
|
|
assert.Equal(t, &AccountBalanceResponse{ |
76
|
|
|
Code: 200, |
77
|
|
|
Message: "Success", |
78
|
|
|
Result: &AccountBalance{ |
79
|
|
|
Name: "COMPANY_NAME", |
80
|
|
|
MainBalance: "100", |
81
|
|
|
MainDeposit: "200", |
82
|
|
|
}, |
83
|
|
|
}, transaction) |
84
|
|
|
|
85
|
|
|
assert.True(t, transaction.IsSuccessfull()) |
86
|
|
|
|
87
|
|
|
// Teardown |
88
|
|
|
server.Close() |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func TestAccountService_BalanceRequestWithError(t *testing.T) { |
92
|
|
|
// Setup |
93
|
|
|
t.Parallel() |
94
|
|
|
|
95
|
|
|
// Arrange |
96
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.TransferWithError()) |
97
|
|
|
client := New(WithBaseURL(server.URL)) |
98
|
|
|
|
99
|
|
|
// Act |
100
|
|
|
transaction, _, err := client.Account.Balance(context.Background()) |
101
|
|
|
|
102
|
|
|
// Assert |
103
|
|
|
assert.NoError(t, err) |
104
|
|
|
assert.Nil(t, transaction.Result) |
105
|
|
|
|
106
|
|
|
assert.Equal(t, "412: bad password", transaction.Message) |
107
|
|
|
assert.Equal(t, http.StatusInternalServerError, transaction.Code) |
108
|
|
|
assert.Nil(t, transaction.Result) |
109
|
|
|
|
110
|
|
|
// Teardown |
111
|
|
|
server.Close() |
112
|
|
|
} |
113
|
|
|
|