1
|
|
|
package client |
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_BalanceRequestWithError(t *testing.T) { |
62
|
|
|
// Setup |
63
|
|
|
t.Parallel() |
64
|
|
|
|
65
|
|
|
// Arrange |
66
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.TransferWithError()) |
67
|
|
|
client := New(WithBaseURL(server.URL)) |
68
|
|
|
params := AirtimeTransferParams{} |
69
|
|
|
|
70
|
|
|
// Act |
71
|
|
|
transaction, _, err := client.Airtime.Transfer(context.Background(), params) |
72
|
|
|
|
73
|
|
|
// Assert |
74
|
|
|
assert.NoError(t, err) |
75
|
|
|
assert.Nil(t, transaction.Result) |
76
|
|
|
|
77
|
|
|
assert.Equal(t, "412: bad password", transaction.Message) |
78
|
|
|
assert.Equal(t, http.StatusInternalServerError, transaction.Code) |
79
|
|
|
assert.Nil(t, transaction.Result) |
80
|
|
|
|
81
|
|
|
// Teardown |
82
|
|
|
server.Close() |
83
|
|
|
} |
84
|
|
|
|