feat: add mocks for x/slashing cli tests (#13215)
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
parent
13ab473a0d
commit
8cd279eeaf
182
x/slashing/client/cli/query_test.go
Normal file
182
x/slashing/client/cli/query_test.go
Normal file
@ -0,0 +1,182 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
tmlibs "github.com/tendermint/tendermint/libs/bytes"
|
||||
tmcli "github.com/tendermint/tendermint/libs/cli"
|
||||
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||
rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock"
|
||||
coretypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/nft/module"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
|
||||
)
|
||||
|
||||
type CLITestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
kr keyring.Keyring
|
||||
baseCtx client.Context
|
||||
clientCtx client.Context
|
||||
encCfg testutilmod.TestEncodingConfig
|
||||
|
||||
pub types.PubKey
|
||||
addr sdk.AccAddress
|
||||
}
|
||||
|
||||
var _ client.TendermintRPC = (*mockTendermintRPC)(nil)
|
||||
|
||||
type mockTendermintRPC struct {
|
||||
rpcclientmock.Client
|
||||
|
||||
responseQuery abci.ResponseQuery
|
||||
}
|
||||
|
||||
func newMockTendermintRPC(respQuery abci.ResponseQuery) mockTendermintRPC {
|
||||
return mockTendermintRPC{responseQuery: respQuery}
|
||||
}
|
||||
|
||||
func (m mockTendermintRPC) BroadcastTxSync(context.Context, tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
|
||||
return &coretypes.ResultBroadcastTx{Code: 0}, nil
|
||||
}
|
||||
|
||||
func (m mockTendermintRPC) ABCIQueryWithOptions(
|
||||
_ context.Context,
|
||||
_ string, _ tmlibs.HexBytes,
|
||||
_ rpcclient.ABCIQueryOptions,
|
||||
) (*coretypes.ResultABCIQuery, error) {
|
||||
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
|
||||
}
|
||||
|
||||
func TestCLITestSuite(t *testing.T) {
|
||||
suite.Run(t, new(CLITestSuite))
|
||||
}
|
||||
|
||||
func (s *CLITestSuite) SetupSuite() {
|
||||
s.T().Log("setting up integration test suite")
|
||||
|
||||
s.encCfg = testutilmod.MakeTestEncodingConfig(module.AppModuleBasic{})
|
||||
s.kr = keyring.NewInMemory(s.encCfg.Codec)
|
||||
s.baseCtx = client.Context{}.
|
||||
WithKeyring(s.kr).
|
||||
WithTxConfig(s.encCfg.TxConfig).
|
||||
WithCodec(s.encCfg.Codec).
|
||||
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
var outBuf bytes.Buffer
|
||||
ctxGen := func() client.Context {
|
||||
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
|
||||
c := newMockTendermintRPC(abci.ResponseQuery{
|
||||
Value: bz,
|
||||
})
|
||||
|
||||
return s.baseCtx.WithClient(c)
|
||||
}
|
||||
s.clientCtx = ctxGen().WithOutput(&outBuf)
|
||||
|
||||
k, _, err := s.clientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
|
||||
s.Require().NoError(err)
|
||||
|
||||
pub, err := k.GetPubKey()
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.pub = pub
|
||||
s.addr = sdk.AccAddress(pub.Address())
|
||||
}
|
||||
|
||||
func (s *CLITestSuite) TestGetCmdQuerySigningInfo() {
|
||||
|
||||
pubKeyBz, err := s.encCfg.Codec.MarshalInterfaceJSON(s.pub)
|
||||
s.Require().NoError(err)
|
||||
pubKeyStr := string(pubKeyBz)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectErr bool
|
||||
}{
|
||||
{"invalid address", []string{"foo"}, true},
|
||||
{
|
||||
"valid address (json output)",
|
||||
[]string{
|
||||
pubKeyStr,
|
||||
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
|
||||
fmt.Sprintf("--%s=1", flags.FlagHeight),
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"valid address (text output)",
|
||||
[]string{
|
||||
pubKeyStr,
|
||||
fmt.Sprintf("--%s=text", tmcli.OutputFlag),
|
||||
fmt.Sprintf("--%s=1", flags.FlagHeight),
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQuerySigningInfo()
|
||||
clientCtx := s.clientCtx
|
||||
|
||||
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
s.Require().Error(err)
|
||||
} else {
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CLITestSuite) TestGetCmdQueryParams() {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryParams()
|
||||
clientCtx := s.clientCtx
|
||||
|
||||
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
s.Require().NoError(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
54
x/slashing/client/cli/tx_test.go
Normal file
54
x/slashing/client/cli/tx_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
|
||||
)
|
||||
|
||||
func (s *CLITestSuite) TestNewUnjailTxCmd() {
|
||||
val := s.addr
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectErr bool
|
||||
expectedCode uint32
|
||||
respType proto.Message
|
||||
}{
|
||||
{
|
||||
"valid transaction",
|
||||
[]string{
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.String()),
|
||||
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), // sync mode as there are no funds yet
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()),
|
||||
},
|
||||
false, 0, &sdk.TxResponse{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.NewUnjailTxCmd()
|
||||
clientCtx := s.clientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
s.Require().Error(err)
|
||||
} else {
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
|
||||
|
||||
txResp := tc.respType.(*sdk.TxResponse)
|
||||
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user