generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
xpubs_test.go
64 lines (57 loc) · 1.66 KB
/
xpubs_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
package walletclient
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/bitcoin-sv/spv-wallet-go-client/xpriv"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/stretchr/testify/require"
)
type xpub struct {
CurrentBalance uint64 `json:"current_balance"`
Metadata *models.Metadata `json:"metadata"`
}
func TestXpub(t *testing.T) {
var update bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response xpub
// Check path and method to customize the response
switch {
case r.URL.Path == "/v1/xpub":
metadata := &models.Metadata{"key": "value"}
if update {
metadata = &models.Metadata{"updated": "info"}
}
response = xpub{
CurrentBalance: 1234,
Metadata: metadata,
}
}
respBytes, _ := json.Marshal(response)
w.Write(respBytes)
}))
defer server.Close()
keys, err := xpriv.Generate()
require.NoError(t, err)
client, err := NewWithXPriv(server.URL, keys.XPriv())
require.NoError(t, err)
require.NotNil(t, client.xPriv)
t.Run("GetXPub", func(t *testing.T) {
xpub, err := client.GetXPub(context.Background())
require.NoError(t, err)
require.NotNil(t, xpub)
require.Equal(t, uint64(1234), xpub.CurrentBalance)
require.Equal(t, "value", xpub.Metadata["key"])
})
t.Run("UpdateXPubMetadata", func(t *testing.T) {
update = true
metadata := map[string]any{"updated": "info"}
xpub, err := client.UpdateXPubMetadata(context.Background(), metadata)
require.NoError(t, err)
require.NotNil(t, xpub)
require.Equal(t, "info", xpub.Metadata["updated"])
})
}