-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspendable_balance_test.go
116 lines (96 loc) · 3.49 KB
/
spendable_balance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package handcash
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHTTPGetSpendableBalance for mocking requests
type mockHTTPGetSpendableBalance struct{}
// Do is a mock http request
func (m *mockHTTPGetSpendableBalance) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Beta
if req.URL.String() == environments[EnvironmentBeta].APIURL+endpointGetSpendableBalanceRequest {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"spendableSatoshiBalance":1424992,"spendableFiatBalance":2.7792,"currencyCode":"USD"}`)))
}
// Default is valid
return resp, nil
}
// mockHTTPInvalidSpendableBalanceData for mocking requests
type mockHTTPInvalidSpendableBalanceData struct{}
// Do is a mock http request
func (m *mockHTTPInvalidSpendableBalanceData) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"invalid":"currencyCode"}`)))
// Default is valid
return resp, nil
}
func TestClient_GetSpendableBalance(t *testing.T) {
t.Parallel()
t.Run("missing auth token", func(t *testing.T) {
client := newTestClient(&mockHTTPGetSpendableBalance{}, EnvironmentBeta)
assert.NotNil(t, client)
balance, err := client.GetSpendableBalance(context.Background(), "", "")
assert.Error(t, err)
assert.Nil(t, balance)
})
t.Run("missing currency code", func(t *testing.T) {
client := newTestClient(&mockHTTPGetSpendableBalance{}, EnvironmentBeta)
assert.NotNil(t, client)
balance, err := client.GetSpendableBalance(context.Background(), "000000", "")
assert.Error(t, err)
assert.Nil(t, balance)
})
t.Run("invalid auth token", func(t *testing.T) {
client := newTestClient(&mockHTTPGetSpendableBalance{}, EnvironmentBeta)
assert.NotNil(t, client)
balance, err := client.GetSpendableBalance(context.Background(), "0", "USD")
assert.Error(t, err)
assert.Nil(t, balance)
})
t.Run("invalid currency code", func(t *testing.T) {
client := newTestClient(&mockHTTPGetSpendableBalance{}, EnvironmentBeta)
assert.NotNil(t, client)
balance, err := client.GetSpendableBalance(context.Background(), "0", "FOO")
assert.Error(t, err)
assert.Nil(t, balance)
})
t.Run("bad request", func(t *testing.T) {
client := newTestClient(&mockHTTPBadRequest{}, EnvironmentBeta)
assert.NotNil(t, client)
balance, err := client.GetSpendableBalance(context.Background(), "000000", "USD")
assert.Error(t, err)
assert.Nil(t, balance)
})
t.Run("invalid spendable balance data", func(t *testing.T) {
client := newTestClient(&mockHTTPInvalidSpendableBalanceData{}, EnvironmentBeta)
assert.NotNil(t, client)
balance, err := client.GetSpendableBalance(context.Background(), "000000", "USD")
assert.Error(t, err)
assert.Nil(t, balance)
})
t.Run("valid spendable balance response", func(t *testing.T) {
client := newTestClient(&mockHTTPGetSpendableBalance{}, EnvironmentBeta)
assert.NotNil(t, client)
balance, err := client.GetSpendableBalance(context.Background(), "000000", "USD")
assert.NoError(t, err)
assert.NotNil(t, balance)
assert.Equal(t, CurrencyUSD, balance.CurrencyCode)
assert.Equal(t, uint64(1424992), balance.SpendableSatoshiBalance)
assert.Equal(t, float64(2.7792), balance.SpendableFiatBalance)
})
}