Skip to content

Commit

Permalink
fix(SPV-896): simplify utils methods to limit outer methods calls
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski committed Aug 11, 2024
1 parent 2b19aa2 commit 4aad98d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
11 changes: 5 additions & 6 deletions regression_tests/regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"testing"

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

Expand All @@ -32,31 +31,31 @@ func TestRegression(t *testing.T) {
rtConfig, err := getEnvVariables()
require.NoError(t, err, fmt.Sprintf(errGettingEnvVariables, err))

var sharedConfigInstanceOne, sharedConfigInstanceTwo *models.SharedConfig
var paymailDomainInstanceOne, paymailDomainInstanceTwo string
var userOne, userTwo *regressionTestUser

t.Run("Initialize Shared Configurations", func(t *testing.T) {
t.Run("Should get sharedConfig for instance one", func(t *testing.T) {
sharedConfigInstanceOne, err = getSharedConfig(adminXPub, rtConfig.ClientOneURL)
paymailDomainInstanceOne, err = getPaymailDomain(adminXPub, rtConfig.ClientOneURL)
require.NoError(t, err, fmt.Sprintf(errGettingSharedConfig, err))
})

t.Run("Should get shared config for instance two", func(t *testing.T) {
sharedConfigInstanceTwo, err = getSharedConfig(adminXPub, rtConfig.ClientTwoURL)
paymailDomainInstanceTwo, err = getPaymailDomain(adminXPub, rtConfig.ClientTwoURL)
require.NoError(t, err, fmt.Sprintf(errGettingSharedConfig, err))
})
})

t.Run("Create Users", func(t *testing.T) {
t.Run("Should create user for instance one", func(t *testing.T) {
userName := "instanceOneUser1"
userOne, err = createUser(ctx, userName, sharedConfigInstanceOne.PaymailDomains[0], rtConfig.ClientOneURL, adminXPriv)
userOne, err = createUser(ctx, userName, paymailDomainInstanceOne, rtConfig.ClientOneURL, adminXPriv)
require.NoError(t, err, fmt.Sprintf(errCreatingUser, err))
})

t.Run("Should create user for instance two", func(t *testing.T) {
userName := "instanceTwoUser1"
userTwo, err = createUser(ctx, userName, sharedConfigInstanceTwo.PaymailDomains[0], rtConfig.ClientTwoURL, adminXPriv)
userTwo, err = createUser(ctx, userName, paymailDomainInstanceTwo, rtConfig.ClientTwoURL, adminXPriv)
require.NoError(t, err, fmt.Sprintf(errCreatingUser, err))
})
})
Expand Down
31 changes: 18 additions & 13 deletions regression_tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ func getEnvVariables() (*regressionTestConfig, error) {
rtConfig.ClientOneURL = "http://localhost:3003"
rtConfig.ClientTwoURL = "http://localhost:3003"
}

rtConfig.ClientOneURL = addPrefixIfNeeded(rtConfig.ClientOneURL)
rtConfig.ClientTwoURL = addPrefixIfNeeded(rtConfig.ClientTwoURL)

return &rtConfig, nil
}

// getSharedConfig retrieves the shared configuration from the SPV Wallet.
func getSharedConfig(xpub string, clientUrl string) (*models.SharedConfig, error) {
// getPaymailDomain retrieves the shared configuration from the SPV Wallet.
func getPaymailDomain(xpub string, clientUrl string) (string, error) {
req, err := http.NewRequest(http.MethodGet, clientUrl+domainSuffixSharedConfig, nil)
if err != nil {
return nil, err
return "", err
}

req.Header.Set(models.AuthHeader, xpub)
Expand All @@ -81,28 +85,29 @@ func getSharedConfig(xpub string, clientUrl string) (*models.SharedConfig, error
}
resp, err := client.Do(req)
if err != nil {
return nil, err
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get shared config: %s", resp.Status)
return "", fmt.Errorf("failed to get shared config: %s", resp.Status)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return "", err
}

var configResponse models.SharedConfig
if err := json.Unmarshal(body, &configResponse); err != nil {
return nil, err
return "", err
}

if len(configResponse.PaymailDomains) != 1 {
return nil, fmt.Errorf("expected 1 paymail domain, got %d", len(configResponse.PaymailDomains))
return "", fmt.Errorf("expected 1 paymail domain, got %d", len(configResponse.PaymailDomains))
}
return &configResponse, nil

return configResponse.PaymailDomains[0], nil
}

// createUser creates a set of keys and new paymail in the SPV Wallet.
Expand All @@ -118,7 +123,7 @@ func createUser(ctx context.Context, paymail string, paymailDomain string, insta
Paymail: preparePaymail(paymail, paymailDomain),
}

adminClient := walletclient.NewWithAdminKey(addPrefixIfNeeded(instanceUrl), adminXPriv)
adminClient := walletclient.NewWithAdminKey(instanceUrl, adminXPriv)

if err := adminClient.AdminNewXpub(ctx, user.XPub, map[string]any{"some_metadata": "remove"}); err != nil {
return nil, err
Expand All @@ -134,7 +139,7 @@ func createUser(ctx context.Context, paymail string, paymailDomain string, insta

// removeRegisteredPaymail soft deletes paymail from the SPV Wallet.
func removeRegisteredPaymail(ctx context.Context, paymail string, instanceURL string, adminXPriv string) error {
adminClient := walletclient.NewWithAdminKey(addPrefixIfNeeded(instanceURL), adminXPriv)
adminClient := walletclient.NewWithAdminKey(instanceURL, adminXPriv)
err := adminClient.AdminDeletePaymail(ctx, paymail)
if err != nil {
return err
Expand All @@ -144,7 +149,7 @@ func removeRegisteredPaymail(ctx context.Context, paymail string, instanceURL st

// getBalance retrieves the balance from the SPV Wallet.
func getBalance(ctx context.Context, fromInstance string, fromXPriv string) (int, error) {
client := walletclient.NewWithXPriv(addPrefixIfNeeded(fromInstance), fromXPriv)
client := walletclient.NewWithXPriv(fromInstance, fromXPriv)

xpubInfo, err := client.GetXPub(ctx)
if err != nil {
Expand All @@ -155,7 +160,7 @@ func getBalance(ctx context.Context, fromInstance string, fromXPriv string) (int

// getTransactions retrieves the transactions from the SPV Wallet.
func getTransactions(ctx context.Context, fromInstance string, fromXPriv string) ([]*models.Transaction, error) {
client := walletclient.NewWithXPriv(addPrefixIfNeeded(fromInstance), fromXPriv)
client := walletclient.NewWithXPriv(fromInstance, fromXPriv)

metadata := map[string]any{}
conditions := filter.TransactionFilter{}
Expand Down

0 comments on commit 4aad98d

Please sign in to comment.