github.com/cosmos/cosmos-sdk@v0.50.10/x/genutil/client/cli/gentx_test.go (about)

     1  package cli_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	abci "github.com/cometbft/cometbft/abci/types"
    11  	rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
    12  	"github.com/stretchr/testify/suite"
    13  
    14  	sdkmath "cosmossdk.io/math"
    15  
    16  	"github.com/cosmos/cosmos-sdk/client"
    17  	"github.com/cosmos/cosmos-sdk/client/flags"
    18  	"github.com/cosmos/cosmos-sdk/codec/address"
    19  	"github.com/cosmos/cosmos-sdk/crypto/keyring"
    20  	svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
    21  	clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
    22  	sdk "github.com/cosmos/cosmos-sdk/types"
    23  	"github.com/cosmos/cosmos-sdk/types/module"
    24  	testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
    25  	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
    26  	"github.com/cosmos/cosmos-sdk/x/genutil"
    27  	"github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
    28  	stakingcli "github.com/cosmos/cosmos-sdk/x/staking/client/cli"
    29  )
    30  
    31  type CLITestSuite struct {
    32  	suite.Suite
    33  
    34  	kr        keyring.Keyring
    35  	encCfg    testutilmod.TestEncodingConfig
    36  	baseCtx   client.Context
    37  	clientCtx client.Context
    38  }
    39  
    40  func TestCLITestSuite(t *testing.T) {
    41  	suite.Run(t, new(CLITestSuite))
    42  }
    43  
    44  func (s *CLITestSuite) SetupSuite() {
    45  	s.encCfg = testutilmod.MakeTestEncodingConfig(genutil.AppModuleBasic{})
    46  	s.kr = keyring.NewInMemory(s.encCfg.Codec)
    47  	s.baseCtx = client.Context{}.
    48  		WithKeyring(s.kr).
    49  		WithTxConfig(s.encCfg.TxConfig).
    50  		WithCodec(s.encCfg.Codec).
    51  		WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
    52  		WithAccountRetriever(client.MockAccountRetriever{}).
    53  		WithOutput(io.Discard).
    54  		WithChainID("test-chain")
    55  
    56  	ctxGen := func() client.Context {
    57  		bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
    58  		c := clitestutil.NewMockCometRPC(abci.ResponseQuery{
    59  			Value: bz,
    60  		})
    61  		return s.baseCtx.WithClient(c)
    62  	}
    63  	s.clientCtx = ctxGen()
    64  }
    65  
    66  func (s *CLITestSuite) TestGenTxCmd() {
    67  	amount := sdk.NewCoin("stake", sdkmath.NewInt(12))
    68  
    69  	tests := []struct {
    70  		name         string
    71  		args         []string
    72  		expCmdOutput string
    73  	}{
    74  		{
    75  			name: "invalid commission rate returns error",
    76  			args: []string{
    77  				fmt.Sprintf("--%s=%s", flags.FlagChainID, s.baseCtx.ChainID),
    78  				fmt.Sprintf("--%s=1", stakingcli.FlagCommissionRate),
    79  				"node0",
    80  				amount.String(),
    81  			},
    82  			expCmdOutput: fmt.Sprintf("--%s=%s --%s=1 %s %s", flags.FlagChainID, s.baseCtx.ChainID, stakingcli.FlagCommissionRate, "node0", amount.String()),
    83  		},
    84  		{
    85  			name: "valid gentx",
    86  			args: []string{
    87  				fmt.Sprintf("--%s=%s", flags.FlagChainID, s.baseCtx.ChainID),
    88  				"node0",
    89  				amount.String(),
    90  			},
    91  			expCmdOutput: fmt.Sprintf("--%s=%s %s %s", flags.FlagChainID, s.baseCtx.ChainID, "node0", amount.String()),
    92  		},
    93  		{
    94  			name: "invalid pubkey",
    95  			args: []string{
    96  				fmt.Sprintf("--%s=%s", flags.FlagChainID, "test-chain-1"),
    97  				fmt.Sprintf("--%s={\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"}", stakingcli.FlagPubKey),
    98  				"node0",
    99  				amount.String(),
   100  			},
   101  			expCmdOutput: fmt.Sprintf("--%s=test-chain-1 --%s={\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"} %s %s ", flags.FlagChainID, stakingcli.FlagPubKey, "node0", amount.String()),
   102  		},
   103  		{
   104  			name: "valid pubkey flag",
   105  			args: []string{
   106  				fmt.Sprintf("--%s=%s", flags.FlagChainID, "test-chain-1"),
   107  				fmt.Sprintf("--%s={\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"}", stakingcli.FlagPubKey),
   108  				"node0",
   109  				amount.String(),
   110  			},
   111  			expCmdOutput: fmt.Sprintf("--%s=test-chain-1 --%s={\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"} %s %s ", flags.FlagChainID, stakingcli.FlagPubKey, "node0", amount.String()),
   112  		},
   113  	}
   114  
   115  	for _, tc := range tests {
   116  		tc := tc
   117  
   118  		dir := s.T().TempDir()
   119  		genTxFile := filepath.Join(dir, "myTx")
   120  		tc.args = append(tc.args, fmt.Sprintf("--%s=%s", flags.FlagOutputDocument, genTxFile))
   121  
   122  		s.Run(tc.name, func() {
   123  			clientCtx := s.clientCtx
   124  			ctx := svrcmd.CreateExecuteContext(context.Background())
   125  
   126  			cmd := cli.GenTxCmd(
   127  				module.NewBasicManager(),
   128  				clientCtx.TxConfig,
   129  				banktypes.GenesisBalancesIterator{},
   130  				clientCtx.HomeDir,
   131  				address.NewBech32Codec("cosmosvaloper"),
   132  			)
   133  			cmd.SetContext(ctx)
   134  			cmd.SetArgs(tc.args)
   135  
   136  			s.Require().NoError(client.SetCmdClientContextHandler(clientCtx, cmd))
   137  
   138  			if len(tc.args) != 0 {
   139  				s.Require().Contains(fmt.Sprint(cmd), tc.expCmdOutput)
   140  			}
   141  		})
   142  	}
   143  }