Skip to content

Commit

Permalink
Add prepareTransaction test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Dzianis Andreyenka <[email protected]>
  • Loading branch information
denisandreenko committed Nov 15, 2023
1 parent e3f0deb commit 44dfbd0
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 8 deletions.
11 changes: 4 additions & 7 deletions internal/tezos/prepare_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *tezosConnector) completeOp(ctx context.Context, op *codec.Op, fromStrin
mayNeedReveal := len(op.Contents) > 0 && op.Contents[0].Kind() != tezos.OpTypeReveal
// add reveal if necessary
if mayNeedReveal && !state.IsRevealed() {
key, err := c.getPubKeyFromSignatory(op.Source.String())
key, err := c.getPubKeyFromSignatory(ctx, op.Source.String())
if err != nil {
return err
}
Expand Down Expand Up @@ -178,9 +178,9 @@ func getNetworkParamsByName(name string) *tezos.Params {
}
}

func (c *tezosConnector) getPubKeyFromSignatory(tezosAddress string) (*tezos.Key, error) {
func (c *tezosConnector) getPubKeyFromSignatory(ctx context.Context, tezosAddress string) (*tezos.Key, error) {
url := c.signatoryURL + "/keys/" + tezosAddress
req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
Expand All @@ -195,10 +195,7 @@ func (c *tezosConnector) getPubKeyFromSignatory(tezosAddress string) (*tezos.Key
}
defer resp.Body.Close()

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

var pubKeyJSON struct {
PubKey string `json:"public_key"`
Expand Down
192 changes: 191 additions & 1 deletion internal/tezos/prepare_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tezos
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"

"blockwatch.cc/tzgo/codec"
Expand All @@ -15,7 +17,7 @@ import (
"github.com/stretchr/testify/mock"
)

func TestTransactionPrepareOk(t *testing.T) {
func TestTransactionPrepareSuccess(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

Expand Down Expand Up @@ -66,6 +68,75 @@ func TestTransactionPrepareOk(t *testing.T) {
assert.NotEmpty(t, res.TransactionData)
}

func TestTransactionPrepareWithRevealSuccess(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlockHash", ctx, mock.Anything).
Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil)

mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"public_key\":\"edpkvHVuLHkr5eDiTtQKyUPqgYVAk3Sy4m7qBD8r6abemHkZsMU5Kh\"}"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

mRPC.On("Simulate", ctx, mock.Anything, mock.Anything).
Return(&rpc.Receipt{
Op: &rpc.Operation{
Contents: []rpc.TypedOperation{
rpc.Reveal{
Manager: rpc.Manager{
Generic: rpc.Generic{
Metadata: rpc.OperationMetadata{
Result: rpc.OperationResult{
Status: tezos.OpStatusApplied,
},
},
},
},
},
rpc.Transaction{
Manager: rpc.Manager{
Generic: rpc.Generic{
Metadata: rpc.OperationMetadata{
Result: rpc.OperationResult{
Status: tezos.OpStatusApplied,
},
},
},
},
},
},
},
}, nil)

req := &ffcapi.TransactionPrepareRequest{
TransactionInput: ffcapi.TransactionInput{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
Method: fftypes.JSONAnyPtr("\"pause\""),
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"),
},
},
}
res, reason, err := c.TransactionPrepare(ctx, req)
assert.NoError(t, err)
assert.Empty(t, reason)
assert.NotNil(t, res)
assert.NotEmpty(t, res.TransactionData)
}

func TestTransactionPrepareWrongParamsError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()
Expand Down Expand Up @@ -318,3 +389,122 @@ func Test_estimateAndAssignExceedMaxLimitError(t *testing.T) {
_, err := c.estimateAndAssignTxCost(ctx, op, opts)
assert.Error(t, err)
}

func TestTransactionPrepareWithRevealEmptyServerError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlockHash", ctx, mock.Anything).
Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil)

mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)

req := &ffcapi.TransactionPrepareRequest{
TransactionInput: ffcapi.TransactionInput{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
Method: fftypes.JSONAnyPtr("\"pause\""),
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"),
},
},
}
resp, _, err := c.TransactionPrepare(ctx, req)
assert.Error(t, err)
assert.Nil(t, resp)
}

func Test_getNetworkParamsByName(t *testing.T) {
params := getNetworkParamsByName("ghostnet")
assert.Equal(t, params, tezos.GhostnetParams)

params = getNetworkParamsByName("nairobinet")
assert.Equal(t, params, tezos.NairobinetParams)

params = getNetworkParamsByName("default")
assert.Equal(t, params, tezos.DefaultParams)
}

func Test_getPubKeyFromSignatoryInvalidRespKeySuccess(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"public_key\":\"edpkvHVuLHkr5eDiTtQKyUPqgYVAk3Sy4m7qBD8r6abemHkZsMU5Kh\"}"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

resp, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.NotNil(t, resp)
assert.NoError(t, err)
}

func Test_getPubKeyFromSignatoryyNilContextError(t *testing.T) {
_, c, _, done := newTestConnector(t)
defer done()

_, err := c.getPubKeyFromSignatory(nil, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryHttpError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryHttpWrongStatusError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
w.Write([]byte("internal error"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryUnmarshalRespError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(nil)
}))
defer svr.Close()
c.signatoryURL = svr.URL

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

func Test_getPubKeyFromSignatoryInvalidRespKeyError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

// Set up http mocks
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"public_key\":\"invalid\"}"))
}))
defer svr.Close()
c.signatoryURL = svr.URL

_, err := c.getPubKeyFromSignatory(ctx, "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN")
assert.Error(t, err)
}

0 comments on commit 44dfbd0

Please sign in to comment.