Skip to content

Commit

Permalink
feat(BUX-444): 99% test coverage of main package methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Jan 25, 2024
1 parent 1243f24 commit 6b20dca
Show file tree
Hide file tree
Showing 11 changed files with 882 additions and 334 deletions.
77 changes: 77 additions & 0 deletions access_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package buxclient

import (
"context"
"testing"

buxmodels "github.com/BuxOrg/bux-models"
"github.com/stretchr/testify/assert"

"github.com/BuxOrg/go-buxclient/fixtures"
)

// 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,
}

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

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

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

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

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

// then
assert.NoError(t, err)
assert.Equal(t, accessKeys, []*buxmodels.AccessKey{fixtures.AccessKey})
})

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

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

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

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

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

// then
assert.NoError(t, err)
assert.Equal(t, accessKey, fixtures.AccessKey)
})
}
396 changes: 71 additions & 325 deletions buxclient_test.go

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions destinations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package buxclient

import (
"context"
"testing"

buxmodels "github.com/BuxOrg/bux-models"
"github.com/stretchr/testify/assert"

"github.com/BuxOrg/go-buxclient/fixtures"
)

// TestDestinations will test the Destinations methods
func TestDestinations(t *testing.T) {
transportHandler := testTransportHandler{
Type: fixtures.RequestType,
Path: "/destination",
Result: fixtures.MarshallForTestHandler(fixtures.Destination),
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}

t.Run("GetDestinationByID", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, false)

// when
destination, err := client.GetDestinationByID(context.Background(), fixtures.Destination.ID)

// then
assert.NoError(t, err)
assert.Equal(t, destination, fixtures.Destination)
})

t.Run("GetDestinationByAddress", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, false)

// when
destination, err := client.GetDestinationByAddress(context.Background(), fixtures.Destination.Address)

// then
assert.NoError(t, err)
assert.Equal(t, destination, fixtures.Destination)
})

t.Run("GetDestinationByLockingScript", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, false)

// when
destination, err := client.GetDestinationByLockingScript(context.Background(), fixtures.Destination.LockingScript)

// then
assert.NoError(t, err)
assert.Equal(t, destination, fixtures.Destination)
})

t.Run("GetDestinations", func(t *testing.T) {
// given
transportHandler := testTransportHandler{
Type: fixtures.RequestType,
Path: "/destination/search",
Result: fixtures.MarshallForTestHandler([]*buxmodels.Destination{fixtures.Destination}),
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}
client := getTestBuxClient(transportHandler, false)

// when
destinations, err := client.GetDestinations(context.Background(), fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, destinations, []*buxmodels.Destination{fixtures.Destination})
})

t.Run("NewDestination", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, false)

// when
destination, err := client.NewDestination(context.Background(), fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, destination, fixtures.Destination)
})

t.Run("UpdateDestinationMetadataByID", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, false)

// when
destination, err := client.UpdateDestinationMetadataByID(context.Background(), fixtures.Destination.ID, fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, destination, fixtures.Destination)
})

t.Run("UpdateDestinationMetadataByAddress", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, false)

// when
destination, err := client.UpdateDestinationMetadataByAddress(context.Background(), fixtures.Destination.Address, fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, destination, fixtures.Destination)
})

t.Run("UpdateDestinationMetadataByLockingScript", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, false)

// when
destination, err := client.UpdateDestinationMetadataByLockingScript(context.Background(), fixtures.Destination.LockingScript, fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, destination, fixtures.Destination)
})
}
191 changes: 191 additions & 0 deletions fixtures/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package fixtures

import (
"encoding/json"

buxmodels "github.com/BuxOrg/bux-models"
common "github.com/BuxOrg/bux-models/common"
)

const (
RequestType = "http"
ServerURL = "https://example.com/"
XPubString = "xpub661MyMwAqRbcFrBJbKwBGCB7d3fr2SaAuXGM95BA62X41m6eW2ehRQGW4xLi9wkEXUGnQZYxVVj4PxXnyrLk7jdqvBAs1Qq9gf6ykMvjR7J"
XPrivString = "xprv9s21ZrQH143K3N6qVJQAu4EP51qMcyrKYJLkLgmYXgz58xmVxVLSsbx2DfJUtjcnXK8NdvkHMKfmmg5AJT2nqqRWUrjSHX29qEJwBgBPkJQ"
AccessKeyString = "7779d24ca6f8821f225042bf55e8f80aa41b08b879b72827f51e41e6523b9cd0"
PaymailAddress = "[email protected]"
)

func MarshallForTestHandler(object any) string {
json, err := json.Marshal(object)
if err != nil {
// as this is just for tests, empty string will make the tests fail,
// so it's acceptable as an "error" here, in case there's a problem with marshall
return ""
}
return string(json)
}

var TestMetadata = &buxmodels.Metadata{"test-key": "test-value"}

var Xpub = &buxmodels.Xpub{
Model: common.Model{Metadata: *TestMetadata},
ID: "cba0be1e753a7609e1a2f792d2e80ea6fce241be86f0690ec437377477809ccc",
CurrentBalance: 16680,
NextInternalNum: 2,
NextExternalNum: 1,
}

var AccessKey = &buxmodels.AccessKey{
Model: common.Model{Metadata: *TestMetadata},
ID: "access-key-id",
XpubID: Xpub.ID,
Key: AccessKeyString,
}

var Destination = &buxmodels.Destination{
Model: common.Model{Metadata: *TestMetadata},
ID: "90d10acb85f37dd009238fe7ec61a1411725825c82099bd8432fcb47ad8326ce",
XpubID: Xpub.ID,
LockingScript: "76a9140e0eb4911d79e9b7683f268964f595b66fa3604588ac",
Type: "pubkeyhash",
Chain: 1,
Num: 19,
Address: "18oETbMcqRB9S7NEGZgwsHKpoTpB3nKBMa",
DraftID: "3a0e1fdd9ac6046c0c82aa36b462e477a455880ceeb08d3aabb1bf031553d1df",
}

var Transaction = &buxmodels.Transaction{
Model: common.Model{Metadata: *TestMetadata},
ID: "caae6e799210dfea7591e3d55455437eb7e1091bb01463ae1e7ddf9e29c75eda",
Hex: "0100000001cf4faa628ce1abdd2cfc641c948898bb7a3dbe043999236c3ea4436a0c79f5dc000000006a47304402206aeca14175e4477031970c1cda0af4d9d1206289212019b54f8e1c9272b5bac2022067c4d32086146ca77640f02a989f51b3c6738ebfa24683c4a923f647cf7f1c624121036295a81525ba33e22c6497c0b758e6a84b60d97c2d8905aa603dd364915c3a0effffffff023e030000000000001976a914f7fc6e0b05e91c3610efd0ce3f04f6502e2ed93d88ac99030000000000001976a914550e06a3aa71ba7414b53922c13f96a882bf027988ac00000000",
XpubInIDs: []string{Xpub.ID},
XpubOutIDs: []string{Xpub.ID},
BlockHash: "00000000000000000896d2b93efa4476c4bd47ed7a554aeac6b38044745a6257",
BlockHeight: 825599,
Fee: 97,
NumberOfInputs: 4,
NumberOfOutputs: 2,
DraftID: "fe6fe12c25b81106b7332d58fe87dab7bc6e56c8c21ca45b4de05f673f3f653c",
TotalValue: 6955,
OutputValue: 1725,
Outputs: map[string]int64{"680d975a403fd9ec90f613e87d17802c029d2d930df1c8373cdcdda2f536a1c0": 62},
Status: "confirmed",
TransactionDirection: "incoming",
}

var DraftTx = &buxmodels.DraftTransaction{
Model: common.Model{Metadata: *TestMetadata},
ID: "3a0e1fdd9ac6046c0c82aa36b462e477a455880ceeb08d3aabb1bf031553d1df",
Hex: "010000000123462f14e60556718916a8cff9dbf2258195a928777c0373200dba1cee105bdb0100000000ffffffff020c000000000000001976a914c4b15e7f65e3e6a062c1d21b7f1d7d2cd3b18e8188ac0b000000000000001976a91455873fd2baa7b51a624f6416b1d824939d99151a88ac00000000",
XpubID: Xpub.ID,
Configuration: buxmodels.TransactionConfig{
ChangeDestinations: []*buxmodels.Destination{Destination},
ChangeStrategy: "",
ChangeMinimumSatoshis: 0,
ChangeNumberOfDestinations: 0,
ChangeSatoshis: 11,
Fee: 1,
FeeUnit: &buxmodels.FeeUnit{
Satoshis: 1,
Bytes: 1000,
},
FromUtxos: []*buxmodels.UtxoPointer{{
TransactionID: "caae6e799210dfea7591e3d55455437eb7e1091bb01463ae1e7ddf9e29c75eda",
OutputIndex: 1,
}},
IncludeUtxos: []*buxmodels.UtxoPointer{{
TransactionID: "caae6e799210dfea7591e3d55455437eb7e1091bb01463ae1e7ddf9e29c75eda",
OutputIndex: 1,
}},
Inputs: []*buxmodels.TransactionInput{{
Utxo: buxmodels.Utxo{
UtxoPointer: buxmodels.UtxoPointer{
TransactionID: "db5b10ee1cba0d2073037c7728a9958125f2dbf9cfa81689715605e6142f4623",
OutputIndex: 1,
},
ID: "041479f86c475603fd510431cf702bc8c9849a9c350390eb86b467d82a13cc24",
XpubID: "9fe44728bf16a2dde3748f72cc65ea661f3bf18653b320d31eafcab37cf7fb36",
Satoshis: 24,
ScriptPubKey: "76a914673d3a53dade2723c48b446578681e253b5c548b88ac",
Type: "pubkeyhash",
DraftID: "3a0e1fdd9ac6046c0c82aa36b462e477a455880ceeb08d3aabb1bf031553d1df",
SpendingTxID: "",
},
Destination: *Destination,
}},
Outputs: []*buxmodels.TransactionOutput{
{
PaymailP4: &buxmodels.PaymailP4{
Alias: "dorzepowski",
Domain: "damiano.4chain.space",
FromPaymail: "[email protected]",
Note: "paymail_note",
PubKey: "1DSsgJdB2AnWaFNgSbv4MZC2m71116JafG",
ReceiveEndpoint: "https://damiano.serveo.net/v1/bsvalias/receive-transaction/{alias}@{domain.tld}",
ReferenceID: "9b48dde1821fa82cf797372a297363c8",
ResolutionType: "p2p",
},
Satoshis: 12,
Scripts: []*buxmodels.ScriptOutput{{
Address: "1Jw1vRUq6pYqiMBAT6x3wBfebXCrXv6Qbr",
Satoshis: 12,
Script: "76a914c4b15e7f65e3e6a062c1d21b7f1d7d2cd3b18e8188ac",
ScriptType: "pubkeyhash",
}},
To: "pubkeyhash",
UseForChange: false,
},
{
Satoshis: 11,
Scripts: []*buxmodels.ScriptOutput{{
Address: "18oETbMcqRB9S7NEGZgwsHKpoTpB3nKBMa",
Satoshis: 11,
Script: "76a91455873fd2baa7b51a624f6416b1d824939d99151a88ac",
ScriptType: "pubkeyhash",
}},
To: "18oETbMcqRB9S7NEGZgwsHKpoTpB3nKBMa",
},
},
SendAllTo: &buxmodels.TransactionOutput{
OpReturn: &buxmodels.OpReturn{
Hex: "0100000001cf4faa628ce1abdd2cfc641c948898bb7a3dbe043999236c3ea4436a0c79f5dc000000006a47304402206aeca14175e4477031970c1cda0af4d9d1206289212019b54f8e1c9272b5bac2022067c4d32086146ca77640f02a989f51b3c6738ebfa24683c4a923f647cf7f1c624121036295a81525ba33e22c6497c0b758e6a84b60d97c2d8905aa603dd364915c3a0effffffff023e030000000000001976a914f7fc6e0b05e91c3610efd0ce3f04f6502e2ed93d88ac99030000000000001976a914550e06a3aa71ba7414b53922c13f96a882bf027988ac00000000",
HexParts: []string{"0100000001cf4faa628ce1abdd2cfc641c948898bb7a3dbe043999236c3ea4436a0c79f5dc000000006a47304402206aeca14175e4477031970c1cda0af4d9d1206289212019b54f8e1c9272b5bac2022067c4d32086146ca77640f02a989f51b3c6738ebfa24683c4a923f647cf7f1c624121036295a81525ba33e22c6497c0b758e6a84b60d97c2d8905aa603dd364915c3a0effffffff023e030000000000001976a914f7fc6e0b05e91c3610efd0ce3f04f6502e2ed93d88ac99030000000000001976a914550e06a3aa71ba7414b53922c13f96a882bf027988ac00000000"},
Map: &buxmodels.MapProtocol{
App: "app_protocol",
Keys: map[string]interface{}{"test-key": "test-value"},
Type: "app_protocol_type",
},
StringParts: []string{"string", "parts"},
},
PaymailP4: &buxmodels.PaymailP4{
Alias: "alias",
Domain: "domain.tld",
FromPaymail: "[email protected]",
Note: "paymail_note",
PubKey: "1DSsgJdB2AnWaFNgSbv4MZC2m71116JafG",
ReceiveEndpoint: "https://bsvalias.example.org/[email protected]/payment-destination-response",
ReferenceID: "3d7c2ca83a46",
ResolutionType: "resolution_type",
},
Satoshis: 1220,
Script: "script",
Scripts: []*buxmodels.ScriptOutput{{
Address: "12HL5RyEy3Rt6SCwxgpiFSTigem1Pzbq22",
Satoshis: 1220,
Script: "script",
ScriptType: "pubkeyhash",
}},
To: "1DSsgJdB2AnWaFNgSbv4MZC2m71116JafG",
UseForChange: false,
},
Sync: &buxmodels.SyncConfig{
Broadcast: true,
BroadcastInstant: true,
PaymailP2P: true,
SyncOnChain: true,
},
},
Status: "draft",
FinalTxID: "caae6e799210dfea7591e3d55455437eb7e1091bb01463ae1e7ddf9e29c75eda",
}
Loading

0 comments on commit 6b20dca

Please sign in to comment.