Skip to content

Commit

Permalink
feat(spv 775) refactor the codebase and sync its functionality with t…
Browse files Browse the repository at this point in the history
…he current state of the js client (#225)
  • Loading branch information
ac4ch authored May 17, 2024
1 parent 0c404a8 commit ab378a1
Show file tree
Hide file tree
Showing 34 changed files with 1,856 additions and 2,797 deletions.
28 changes: 0 additions & 28 deletions access_keys.go

This file was deleted.

84 changes: 33 additions & 51 deletions access_keys_test.go
Original file line number Diff line number Diff line change
@@ -1,77 +1,59 @@
// Package walletclient here we are testing walletclient public methods
package walletclient

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/bitcoin-sv/spv-wallet/models"
"github.com/stretchr/testify/assert"

"github.com/bitcoin-sv/spv-wallet-go-client/fixtures"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/stretchr/testify/require"
)

// TestAccessKeys will test the AccessKey methods
func TestAccessKeys(t *testing.T) {
transportHandler := testTransportHandler{
Type: fixtures.RequestType,
Path: "/access-key",
Result: fixtures.MarshallForTestHandler(fixtures.AccessKey),
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v1/access-key":
switch r.Method {
case http.MethodGet, http.MethodPost, http.MethodDelete:
json.NewEncoder(w).Encode(fixtures.AccessKey)
}
case "/v1/access-key/search":
json.NewEncoder(w).Encode([]*models.AccessKey{fixtures.AccessKey})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()

t.Run("GetAccessKey", func(t *testing.T) {
// given
client := getTestWalletClient(transportHandler, true)
client := NewWithAccessKey(server.URL, fixtures.AccessKeyString)
require.NotNil(t, client.accessKey)

// when
t.Run("GetAccessKey", func(t *testing.T) {
accessKey, err := client.GetAccessKey(context.Background(), fixtures.AccessKey.ID)

// then
assert.NoError(t, err)
assert.Equal(t, accessKey, fixtures.AccessKey)
require.NoError(t, err)
require.Equal(t, fixtures.AccessKey, accessKey)
})

t.Run("GetAccessKeys", func(t *testing.T) {
// given
transportHandler := testTransportHandler{
Type: fixtures.RequestType,
Path: "/access-key/search",
Result: fixtures.MarshallForTestHandler([]*models.AccessKey{fixtures.AccessKey}),
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}
client := getTestWalletClient(transportHandler, true)

// when
accessKeys, err := client.GetAccessKeys(context.Background(), fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, accessKeys, []*models.AccessKey{fixtures.AccessKey})
accessKeys, err := client.GetAccessKeys(context.Background(), nil)
require.NoError(t, err)
require.Equal(t, []*models.AccessKey{fixtures.AccessKey}, accessKeys)
})

t.Run("CreateAccessKey", func(t *testing.T) {
// given
client := getTestWalletClient(transportHandler, true)

// when
accessKey, err := client.CreateAccessKey(context.Background(), fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, accessKey, fixtures.AccessKey)
accessKey, err := client.CreateAccessKey(context.Background(), nil)
require.NoError(t, err)
require.Equal(t, fixtures.AccessKey, accessKey)
})

t.Run("RevokeAccessKey", func(t *testing.T) {
// given
client := getTestWalletClient(transportHandler, true)

// when
accessKey, err := client.RevokeAccessKey(context.Background(), fixtures.AccessKey.ID)

// then
assert.NoError(t, err)
assert.Equal(t, accessKey, fixtures.AccessKey)
require.NoError(t, err)
require.Equal(t, fixtures.AccessKey, accessKey)
})
}
34 changes: 0 additions & 34 deletions admin_contacts.go

This file was deleted.

74 changes: 74 additions & 0 deletions admin_contacts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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/stretchr/testify/require"
)

// TestAdminContactActions testing Admin contacts methods
func TestAdminContactActions(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/v1/admin/contact/search" && r.Method == http.MethodPost:
c := fixtures.Contact
c.ID = "1"
contacts := []*models.Contact{c}
json.NewEncoder(w).Encode(contacts)
case r.URL.Path == "/v1/admin/contact/1" && r.Method == http.MethodPatch:
contact := fixtures.Contact
json.NewEncoder(w).Encode(contact)
case r.URL.Path == "/v1/admin/contact/1" && r.Method == http.MethodDelete:
w.WriteHeader(http.StatusOK)
case r.URL.Path == "/v1/admin/contact/accepted/1" && r.Method == http.MethodPatch:
contact := fixtures.Contact
contact.Status = "accepted"
json.NewEncoder(w).Encode(contact)
case r.URL.Path == "/v1/admin/contact/rejected/1" && r.Method == http.MethodPatch:
contact := fixtures.Contact
contact.Status = "rejected"
json.NewEncoder(w).Encode(contact)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()

client := NewWithAdminKey(server.URL, fixtures.XPrivString)
require.NotNil(t, client.adminXPriv)

t.Run("AdminGetContacts", func(t *testing.T) {
contacts, err := client.AdminGetContacts(context.Background(), nil, nil, nil)
require.NoError(t, err)
require.Equal(t, "1", contacts[0].ID)
})

t.Run("AdminUpdateContact", func(t *testing.T) {
contact, err := client.AdminUpdateContact(context.Background(), "1", "Jane Doe", nil)
require.NoError(t, err)
require.Equal(t, "Test User", contact.FullName)
})

t.Run("AdminDeleteContact", func(t *testing.T) {
err := client.AdminDeleteContact(context.Background(), "1")
require.NoError(t, err)
})

t.Run("AdminAcceptContact", func(t *testing.T) {
contact, err := client.AdminAcceptContact(context.Background(), "1")
require.NoError(t, err)
require.Equal(t, models.ContactStatus("accepted"), contact.Status)
})

t.Run("AdminRejectContact", func(t *testing.T) {
contact, err := client.AdminRejectContact(context.Background(), "1")
require.NoError(t, err)
require.Equal(t, models.ContactStatus("rejected"), contact.Status)
})
}
5 changes: 2 additions & 3 deletions transports/authentication.go → authentication.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package transports
package walletclient

import (
"encoding/hex"
"fmt"
"net/http"
"time"

"github.com/bitcoin-sv/spv-wallet-go-client/utils"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/apierrors"
"github.com/bitcoinschema/go-bitcoin/v2"
Expand All @@ -14,8 +15,6 @@ import (
"github.com/libsv/go-bt/v2"
"github.com/libsv/go-bt/v2/bscript"
"github.com/libsv/go-bt/v2/sighash"

"github.com/bitcoin-sv/spv-wallet-go-client/utils"
)

// SetSignature will set the signature on the header for the request
Expand Down
Loading

0 comments on commit ab378a1

Please sign in to comment.