Skip to content

Commit

Permalink
Merge branch 'main' into bug/SPV-1342/admin_endpoint_for_paginated_re…
Browse files Browse the repository at this point in the history
…sponse_doesnt_map_status_and_direction_in_response
  • Loading branch information
ac4ch authored Jan 13, 2025
2 parents 12656ab + 50325af commit 4d60002
Show file tree
Hide file tree
Showing 22 changed files with 920 additions and 77 deletions.
2 changes: 1 addition & 1 deletion actions/admin/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type CreatePaymail struct {
// The paymail address
Address string `json:"address" example:"test@spv-wallet.com"`
// The public name of the paymail
PublicName string `json:"public_name" example:"Test"`
PublicName string `json:"publicName" example:"Test"`
// The avatar of the paymail (url address)
Avatar string `json:"avatar" example:"https://example.com/avatar.png"`
}
Expand Down
54 changes: 6 additions & 48 deletions actions/paymailserver/incoming_paymail_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestIncomingPaymailRawTX(t *testing.T) {
givenForAllTests := testabilities.Given(t)
cleanup := givenForAllTests.StartedSPVWalletWithConfiguration(
testengine.WithDomainValidationDisabled(),
testengine.WithNewTransactionFlowEnabled(),
)
defer cleanup()

Expand Down Expand Up @@ -76,6 +77,8 @@ func TestIncomingPaymailRawTX(t *testing.T) {
})

t.Run("step 2 - call receive-transaction capability", func(t *testing.T) {
t.Skip("Not implemented yet")

// given:
txSpec := fixtures.GivenTX(t).
WithInput(satoshis+1).
Expand Down Expand Up @@ -117,36 +120,13 @@ func TestIncomingPaymailRawTX(t *testing.T) {
"note": note,
})
})

t.Run("step 3 - check balance", func(t *testing.T) {
// given:
recipientClient := given.HttpClient().ForGivenUser(fixtures.RecipientInternal)

// when:
res, _ := recipientClient.R().Get("/api/v1/users/current")

// then:
then.Response(res).
IsOK().
WithJSONMatching(`{
"id": "{{ matchID64 }}",
"createdAt": "{{ matchTimestamp }}",
"updatedAt": "{{ matchTimestamp }}",
"currentBalance": {{ .balance }},
"deletedAt": null,
"metadata": "*",
"nextExternalNum": 1,
"nextInternalNum": 0
}`, map[string]any{
"balance": satoshis,
})
})
}

func TestIncomingPaymailBeef(t *testing.T) {
givenForAllTests := testabilities.Given(t)
cleanup := givenForAllTests.StartedSPVWalletWithConfiguration(
testengine.WithDomainValidationDisabled(),
testengine.WithNewTransactionFlowEnabled(),
)
defer cleanup()

Expand Down Expand Up @@ -208,6 +188,8 @@ func TestIncomingPaymailBeef(t *testing.T) {
})

t.Run("step 2 - call beef capability", func(t *testing.T) {
t.Skip("Not implemented yet")

// given:
txSpec := fixtures.GivenTX(t).
WithInput(satoshis+1).
Expand Down Expand Up @@ -254,28 +236,4 @@ func TestIncomingPaymailBeef(t *testing.T) {
"note": note,
})
})

t.Run("step 3 - check balance", func(t *testing.T) {
// given:
recipientClient := given.HttpClient().ForGivenUser(fixtures.RecipientInternal)

// when:
res, _ := recipientClient.R().Get("/api/v1/users/current")

// then:
then.Response(res).
IsOK().
WithJSONMatching(`{
"id": "{{ matchID64 }}",
"createdAt": "{{ matchTimestamp }}",
"updatedAt": "{{ matchTimestamp }}",
"currentBalance": {{ .balance }},
"deletedAt": null,
"metadata": "*",
"nextExternalNum": 1,
"nextInternalNum": 0
}`, map[string]any{
"balance": satoshis,
})
})
}
287 changes: 287 additions & 0 deletions actions/paymailserver/old_incoming_paymail_tx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
package paymailserver_test

import (
"fmt"
"testing"
"time"

"github.com/bitcoin-sv/go-sdk/script"
"github.com/bitcoin-sv/spv-wallet/actions/testabilities"
chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models"
testengine "github.com/bitcoin-sv/spv-wallet/engine/testabilities"
"github.com/bitcoin-sv/spv-wallet/engine/tester/fixtures"
"github.com/stretchr/testify/require"
)

func TestOldIncomingPaymailRawTX(t *testing.T) {
givenForAllTests := testabilities.Given(t)
cleanup := givenForAllTests.StartedSPVWalletWithConfiguration(
testengine.WithDomainValidationDisabled(),
)
defer cleanup()

var testState struct {
reference string
lockingScript *script.Script
}

// given:
given, then := testabilities.NewOf(givenForAllTests, t)
client := given.HttpClient().ForAnonymous()

// and:
senderPaymail := fixtures.SenderExternal.DefaultPaymail()
recipientPaymail := fixtures.RecipientInternal.DefaultPaymail()
satoshis := uint64(1000)
note := "test note"

t.Run("step 1 - call p2p-payment-destination", func(t *testing.T) {
// given:
requestBody := map[string]any{
"satoshis": satoshis,
}

// when:
res, _ := client.R().
SetHeader("Content-Type", "application/json").
SetBody(requestBody).
Post(
fmt.Sprintf(
"https://example.com/v1/bsvalias/p2p-payment-destination/%s",
recipientPaymail,
),
)

// then:
then.Response(res).IsOK().WithJSONMatching(`{
"outputs": [
{
"address": "{{ matchAddress }}",
"satoshis": {{ .satoshis }},
"script": "{{ matchHex }}"
}
],
"reference": "{{ matchHexWithLength 32 }}"
}`, map[string]any{
"satoshis": satoshis,
})

// update:
getter := then.Response(res).JSONValue()
testState.reference = getter.GetString("reference")

// and:
lockingScript, err := script.NewFromHex(getter.GetString("outputs[0]/script"))
require.NoError(t, err)
testState.lockingScript = lockingScript
})

t.Run("step 2 - call receive-transaction capability", func(t *testing.T) {
// given:
txSpec := fixtures.GivenTX(t).
WithInput(satoshis+1).
WithOutputScript(satoshis, testState.lockingScript)

// and:
requestBody := map[string]any{
"hex": txSpec.RawTX(),
"reference": testState.reference,
"metadata": map[string]any{
"note": note,
"sender": senderPaymail,
},
}

// and:
given.ARC().WillRespondForBroadcast(200, &chainmodels.TXInfo{
TxID: txSpec.ID(),
TXStatus: chainmodels.SeenOnNetwork,
})

// when:
res, _ := client.R().
SetHeader("Content-Type", "application/json").
SetBody(requestBody).
Post(
fmt.Sprintf(
"https://example.com/v1/bsvalias/receive-transaction/%s",
recipientPaymail,
),
)

// then:
then.Response(res).IsOK().WithJSONMatching(`{
"txid": "{{ .txid }}",
"note": "{{ .note }}"
}`, map[string]any{
"txid": txSpec.ID(),
"note": note,
})
})

t.Run("step 3 - check balance", func(t *testing.T) {
// given:
recipientClient := given.HttpClient().ForGivenUser(fixtures.RecipientInternal)

// when:
res, _ := recipientClient.R().Get("/api/v1/users/current")

// then:
then.Response(res).
IsOK().
WithJSONMatching(`{
"id": "{{ matchID64 }}",
"createdAt": "{{ matchTimestamp }}",
"updatedAt": "{{ matchTimestamp }}",
"currentBalance": {{ .balance }},
"deletedAt": null,
"metadata": "*",
"nextExternalNum": 1,
"nextInternalNum": 0
}`, map[string]any{
"balance": satoshis,
})
})
}

func TestOldIncomingPaymailBeef(t *testing.T) {
givenForAllTests := testabilities.Given(t)
cleanup := givenForAllTests.StartedSPVWalletWithConfiguration(
testengine.WithDomainValidationDisabled(),
)
defer func() {
// Workaround for the issue with the wallet shutdown
// There is a `go saveBEEFTxInputs` goroutine that is not finished when the test ends
time.Sleep(1 * time.Second)
cleanup()
}()

var testState struct {
reference string
lockingScript *script.Script
txID string
}

// given:
given, then := testabilities.NewOf(givenForAllTests, t)
client := given.HttpClient().ForAnonymous()

// and:
senderPaymail := fixtures.SenderExternal.DefaultPaymail()
recipientPaymail := fixtures.RecipientInternal.DefaultPaymail()
satoshis := uint64(1000)
note := "test note"

t.Run("step 1 - call p2p-payment-destination", func(t *testing.T) {
// given:
requestBody := map[string]any{
"satoshis": satoshis,
}

// when:
res, _ := client.R().
SetHeader("Content-Type", "application/json").
SetBody(requestBody).
Post(
fmt.Sprintf(
"https://example.com/v1/bsvalias/p2p-payment-destination/%s",
recipientPaymail,
),
)

// then:
then.Response(res).IsOK().WithJSONMatching(`{
"outputs": [
{
"address": "{{ matchAddress }}",
"satoshis": {{ .satoshis }},
"script": "{{ matchHex }}"
}
],
"reference": "{{ matchHexWithLength 32 }}"
}`, map[string]any{
"satoshis": satoshis,
})

// update:
getter := then.Response(res).JSONValue()
testState.reference = getter.GetString("reference")

// and:
lockingScript, err := script.NewFromHex(getter.GetString("outputs[0]/script"))
require.NoError(t, err)
testState.lockingScript = lockingScript
})

t.Run("step 2 - call beef capability", func(t *testing.T) {
// given:
txSpec := fixtures.GivenTX(t).
WithInput(satoshis+1).
WithOutputScript(satoshis, testState.lockingScript)

// and:
requestBody := map[string]any{
"beef": txSpec.BEEF(),
"reference": testState.reference,
"metadata": map[string]any{
"note": note,
"sender": senderPaymail,
},
}

// and:
given.ARC().WillRespondForBroadcast(200, &chainmodels.TXInfo{
TxID: txSpec.ID(),
TXStatus: chainmodels.SeenOnNetwork,
})

// and;
given.BHS().WillRespondForMerkleRootsVerify(200, &chainmodels.MerkleRootsConfirmations{
ConfirmationState: chainmodels.MRConfirmed,
})

// when:
res, _ := client.R().
SetHeader("Content-Type", "application/json").
SetBody(requestBody).
Post(
fmt.Sprintf(
"https://example.com/v1/bsvalias/beef/%s",
recipientPaymail,
),
)

// then:
then.Response(res).IsOK().WithJSONMatching(`{
"txid": "{{ .txid }}",
"note": "{{ .note }}"
}`, map[string]any{
"txid": txSpec.ID(),
"note": note,
})
})

t.Run("step 3 - check balance", func(t *testing.T) {
// given:
recipientClient := given.HttpClient().ForGivenUser(fixtures.RecipientInternal)

// when:
res, _ := recipientClient.R().Get("/api/v1/users/current")

// then:
then.Response(res).
IsOK().
WithJSONMatching(`{
"id": "{{ matchID64 }}",
"createdAt": "{{ matchTimestamp }}",
"updatedAt": "{{ matchTimestamp }}",
"currentBalance": {{ .balance }},
"deletedAt": null,
"metadata": "*",
"nextExternalNum": 1,
"nextInternalNum": 0
}`, map[string]any{
"balance": satoshis,
})
})
}
Loading

0 comments on commit 4d60002

Please sign in to comment.