forked from bitcoin-sv/spv-wallet-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestinations_test.go
95 lines (82 loc) · 3.64 KB
/
destinations_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
package walletclient
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/bitcoin-sv/spv-wallet-go-client/fixtures"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
"github.com/stretchr/testify/require"
)
func TestDestinations(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sendJSONResponse := func(data interface{}) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
const dest = "/v1/destination"
switch {
case r.URL.Path == "/v1/v1/destination/address/"+fixtures.Destination.Address && r.Method == http.MethodGet:
sendJSONResponse(fixtures.Destination)
case r.URL.Path == "/v1/destination/lockingScript/"+fixtures.Destination.LockingScript && r.Method == http.MethodGet:
sendJSONResponse(fixtures.Destination)
case r.URL.Path == "/v1/destination/search" && r.Method == http.MethodPost:
sendJSONResponse([]*models.Destination{fixtures.Destination})
case r.URL.Path == dest && r.Method == http.MethodGet:
sendJSONResponse(fixtures.Destination)
case r.URL.Path == dest && r.Method == http.MethodPatch:
sendJSONResponse(fixtures.Destination)
case r.URL.Path == dest && r.Method == http.MethodPost:
sendJSONResponse(fixtures.Destination)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
client := NewWithAccessKey(server.URL, fixtures.AccessKeyString)
require.NotNil(t, client.accessKey)
t.Run("GetDestinationByID", func(t *testing.T) {
destination, err := client.GetDestinationByID(context.Background(), fixtures.Destination.ID)
require.NoError(t, err)
require.Equal(t, fixtures.Destination, destination)
})
t.Run("GetDestinationByAddress", func(t *testing.T) {
destination, err := client.GetDestinationByAddress(context.Background(), fixtures.Destination.Address)
require.NoError(t, err)
require.Equal(t, fixtures.Destination, destination)
})
t.Run("GetDestinationByLockingScript", func(t *testing.T) {
destination, err := client.GetDestinationByLockingScript(context.Background(), fixtures.Destination.LockingScript)
require.NoError(t, err)
require.Equal(t, fixtures.Destination, destination)
})
t.Run("GetDestinations", func(t *testing.T) {
destinations, err := client.GetDestinations(context.Background(), &filter.DestinationFilter{}, nil, nil)
require.NoError(t, err)
require.Equal(t, []*models.Destination{fixtures.Destination}, destinations)
})
t.Run("NewDestination", func(t *testing.T) {
destination, err := client.NewDestination(context.Background(), fixtures.TestMetadata)
require.NoError(t, err)
require.Equal(t, fixtures.Destination, destination)
})
t.Run("UpdateDestinationMetadataByID", func(t *testing.T) {
destination, err := client.UpdateDestinationMetadataByID(context.Background(), fixtures.Destination.ID, fixtures.TestMetadata)
require.NoError(t, err)
require.Equal(t, fixtures.Destination, destination)
})
t.Run("UpdateDestinationMetadataByAddress", func(t *testing.T) {
destination, err := client.UpdateDestinationMetadataByAddress(context.Background(), fixtures.Destination.Address, fixtures.TestMetadata)
require.NoError(t, err)
require.Equal(t, fixtures.Destination, destination)
})
t.Run("UpdateDestinationMetadataByLockingScript", func(t *testing.T) {
destination, err := client.UpdateDestinationMetadataByLockingScript(context.Background(), fixtures.Destination.LockingScript, fixtures.TestMetadata)
require.NoError(t, err)
require.Equal(t, fixtures.Destination, destination)
})
}