From 48bf7c67b25167251d7d647b6379cfb2116ee86e Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 19 Dec 2023 00:45:01 -0600 Subject: [PATCH 1/2] test(devgas-cli): CLI tests for devgas txs --- CHANGELOG.md | 1 + x/devgas/v1/client/cli/cli_test.go | 227 +++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 x/devgas/v1/client/cli/cli_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index a75f8d419..100d2208b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1714](https://github.com/NibiruChain/nibiru/pull/1714) - ci(localnet.sh): Fix script, simplify, and test in CI. * [#1719](https://github.com/NibiruChain/nibiru/pull/1719) - refactor(test): add is not mandatory interface to action * [#1723](https://github.com/NibiruChain/nibiru/pull/1723) - ci(e2e-wasm.yml): rm unused workflow +* [#1728](https://github.com/NibiruChain/nibiru/pull/1728) - test(devgas-cli): CLI tests for devgas txs ### Dependencies - Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720)) diff --git a/x/devgas/v1/client/cli/cli_test.go b/x/devgas/v1/client/cli/cli_test.go new file mode 100644 index 000000000..6ae1f8b42 --- /dev/null +++ b/x/devgas/v1/client/cli/cli_test.go @@ -0,0 +1,227 @@ +package cli_test + +import ( + "bytes" + "context" + "fmt" + "io" + "testing" + + abci "github.com/cometbft/cometbft/abci/types" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" + "github.com/stretchr/testify/suite" + + rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock" + sdkclient "github.com/cosmos/cosmos-sdk/client" + sdktestutilcli "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/crypto/keyring" + svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" + + "github.com/NibiruChain/nibiru/x/common/testutil" + devgas "github.com/NibiruChain/nibiru/x/devgas/v1" + "github.com/NibiruChain/nibiru/x/devgas/v1/client/cli" +) + +// CLITestSuite: Tests all tx commands for the module. +type CLITestSuite struct { + suite.Suite + + keyring keyring.Keyring + encCfg testutilmod.TestEncodingConfig + baseCtx sdkclient.Context + clientCtx sdkclient.Context + + testAcc sdktestutil.TestAccount +} + +func TestCLITestSuite(t *testing.T) { + suite.Run(t, new(CLITestSuite)) +} + +// Runs once before the entire test suite. +func (s *CLITestSuite) SetupSuite() { + s.encCfg = testutilmod.MakeTestEncodingConfig(devgas.AppModuleBasic{}) + s.keyring = keyring.NewInMemory(s.encCfg.Codec) + s.baseCtx = sdkclient.Context{}. + WithKeyring(s.keyring). + WithTxConfig(s.encCfg.TxConfig). + WithCodec(s.encCfg.Codec). + WithClient(sdktestutilcli.MockTendermintRPC{Client: rpcclientmock.Client{}}). + WithAccountRetriever(sdkclient.MockAccountRetriever{}). + WithOutput(io.Discard). + WithChainID("test-chain") + + var outBuf bytes.Buffer + ctxGen := func() sdkclient.Context { + bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{}) + c := sdktestutilcli.NewMockTendermintRPC(abci.ResponseQuery{ + Value: bz, + }) + return s.baseCtx.WithClient(c) + } + s.clientCtx = ctxGen().WithOutput(&outBuf) + + testAccs := sdktestutil.CreateKeyringAccounts(s.T(), s.keyring, 1) + s.testAcc = testAccs[0] +} + +// Flags for broadcasting transactions +func commonTxArgs() []string { + return []string{ + "--yes=true", // skip confirmation + "--broadcast-mode=sync", + "--fees=1unibi", + "--chain-id=test-chain", + } +} + +type TestCase struct { + name string + args []string + extraArgs []string + wantErr bool +} + +func (tc TestCase) NewCtx(s *CLITestSuite) sdkclient.Context { + return s.baseCtx +} + +func (tc TestCase) Run(s *CLITestSuite) { + s.Run(tc.name, func() { + ctx := svrcmd.CreateExecuteContext(context.Background()) + + cmd := cli.NewTxCmd() + cmd.SetContext(ctx) + args := append(tc.args, commonTxArgs()...) + cmd.SetArgs(append(args, tc.extraArgs...)) + + s.Require().NoError(sdkclient.SetCmdClientContextHandler(tc.NewCtx(s), cmd)) + + err := cmd.Execute() + if tc.wantErr { + s.Require().Error(err) + return + } + s.Require().NoError(err) + }) +} + +func (s *CLITestSuite) TestCmdRegisterFeeShare() { + _, addrs := testutil.PrivKeyAddressPairs(3) + + testCases := []TestCase{ + { + name: "happy path: devgas register", + args: []string{"register", addrs[0].String(), addrs[1].String()}, + extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, + wantErr: false, + }, + { + name: "sad: fee payer", + args: []string{"register", addrs[0].String(), addrs[1].String()}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + fmt.Sprintf("--fee-payer=%s", "invalid-fee-payer"), + }, + wantErr: true, + }, + { + name: "sad: contract addr", + args: []string{"register", "sadcontract", addrs[1].String()}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + }, + wantErr: true, + }, + { + name: "sad: deployer addr", + args: []string{"register", addrs[0].String(), "saddeployer"}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + }, + wantErr: true, + }, + } + + for _, tc := range testCases { + tc.Run(s) + } +} + +func (s *CLITestSuite) TestCmdCancelFeeShare() { + _, addrs := testutil.PrivKeyAddressPairs(1) + testCases := []TestCase{ + { + name: "happy path: devgas cancel", + args: []string{"cancel", addrs[0].String()}, + extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, + wantErr: false, + }, + { + name: "sad: fee payer", + args: []string{"cancel", addrs[0].String()}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + fmt.Sprintf("--fee-payer=%s", "invalid-fee-payer"), + }, + wantErr: true, + }, + { + name: "sad: contract addr", + args: []string{"cancel", "sadcontract"}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + }, + wantErr: true, + }, + } + + for _, tc := range testCases { + tc.Run(s) + } +} + +func (s *CLITestSuite) TestCmdUpdateFeeShare() { + _, addrs := testutil.PrivKeyAddressPairs(3) + + testCases := []TestCase{ + { + name: "happy path: devgas update", + args: []string{"update", addrs[0].String(), addrs[1].String()}, + extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, + wantErr: false, + }, + { + name: "sad: fee payer", + args: []string{"update", addrs[0].String(), addrs[1].String()}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + fmt.Sprintf("--fee-payer=%s", "invalid-fee-payer"), + }, + wantErr: true, + }, + { + name: "sad: contract addr", + args: []string{"update", "sadcontract", addrs[1].String()}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + }, + wantErr: true, + }, + { + name: "sad: new withdraw addr", + args: []string{"update", addrs[0].String(), "saddeployer"}, + extraArgs: []string{ + fmt.Sprintf("--from=%s", s.testAcc.Address), + }, + wantErr: true, + }, + } + + for _, tc := range testCases { + tc.Run(s) + } +} From c4c24ca132775a1f885cb8a4870b77386c6ed880 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Wed, 27 Dec 2023 23:40:15 -0600 Subject: [PATCH 2/2] test(cli_test): test for specific errors rather than boolean checks --- x/devgas/v1/client/cli/cli_test.go | 31 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/x/devgas/v1/client/cli/cli_test.go b/x/devgas/v1/client/cli/cli_test.go index 6ae1f8b42..e2209046e 100644 --- a/x/devgas/v1/client/cli/cli_test.go +++ b/x/devgas/v1/client/cli/cli_test.go @@ -82,7 +82,7 @@ type TestCase struct { name string args []string extraArgs []string - wantErr bool + wantErr string } func (tc TestCase) NewCtx(s *CLITestSuite) sdkclient.Context { @@ -101,8 +101,9 @@ func (tc TestCase) Run(s *CLITestSuite) { s.Require().NoError(sdkclient.SetCmdClientContextHandler(tc.NewCtx(s), cmd)) err := cmd.Execute() - if tc.wantErr { + if tc.wantErr != "" { s.Require().Error(err) + s.ErrorContains(err, tc.wantErr) return } s.Require().NoError(err) @@ -117,7 +118,7 @@ func (s *CLITestSuite) TestCmdRegisterFeeShare() { name: "happy path: devgas register", args: []string{"register", addrs[0].String(), addrs[1].String()}, extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, - wantErr: false, + wantErr: "", }, { name: "sad: fee payer", @@ -126,7 +127,7 @@ func (s *CLITestSuite) TestCmdRegisterFeeShare() { fmt.Sprintf("--from=%s", s.testAcc.Address), fmt.Sprintf("--fee-payer=%s", "invalid-fee-payer"), }, - wantErr: true, + wantErr: "decoding bech32 failed", }, { name: "sad: contract addr", @@ -134,15 +135,15 @@ func (s *CLITestSuite) TestCmdRegisterFeeShare() { extraArgs: []string{ fmt.Sprintf("--from=%s", s.testAcc.Address), }, - wantErr: true, + wantErr: "invalid contract address", }, { - name: "sad: deployer addr", - args: []string{"register", addrs[0].String(), "saddeployer"}, + name: "sad: withdraw addr", + args: []string{"register", addrs[0].String(), "sadwithdraw"}, extraArgs: []string{ fmt.Sprintf("--from=%s", s.testAcc.Address), }, - wantErr: true, + wantErr: "invalid withdraw address", }, } @@ -158,7 +159,7 @@ func (s *CLITestSuite) TestCmdCancelFeeShare() { name: "happy path: devgas cancel", args: []string{"cancel", addrs[0].String()}, extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, - wantErr: false, + wantErr: "", }, { name: "sad: fee payer", @@ -167,7 +168,7 @@ func (s *CLITestSuite) TestCmdCancelFeeShare() { fmt.Sprintf("--from=%s", s.testAcc.Address), fmt.Sprintf("--fee-payer=%s", "invalid-fee-payer"), }, - wantErr: true, + wantErr: "decoding bech32 failed", }, { name: "sad: contract addr", @@ -175,7 +176,7 @@ func (s *CLITestSuite) TestCmdCancelFeeShare() { extraArgs: []string{ fmt.Sprintf("--from=%s", s.testAcc.Address), }, - wantErr: true, + wantErr: "invalid deployer address", }, } @@ -192,7 +193,7 @@ func (s *CLITestSuite) TestCmdUpdateFeeShare() { name: "happy path: devgas update", args: []string{"update", addrs[0].String(), addrs[1].String()}, extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, - wantErr: false, + wantErr: "", }, { name: "sad: fee payer", @@ -201,7 +202,7 @@ func (s *CLITestSuite) TestCmdUpdateFeeShare() { fmt.Sprintf("--from=%s", s.testAcc.Address), fmt.Sprintf("--fee-payer=%s", "invalid-fee-payer"), }, - wantErr: true, + wantErr: "decoding bech32 failed", }, { name: "sad: contract addr", @@ -209,7 +210,7 @@ func (s *CLITestSuite) TestCmdUpdateFeeShare() { extraArgs: []string{ fmt.Sprintf("--from=%s", s.testAcc.Address), }, - wantErr: true, + wantErr: "invalid contract", }, { name: "sad: new withdraw addr", @@ -217,7 +218,7 @@ func (s *CLITestSuite) TestCmdUpdateFeeShare() { extraArgs: []string{ fmt.Sprintf("--from=%s", s.testAcc.Address), }, - wantErr: true, + wantErr: "invalid withdraw address", }, }