Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(devgas-cli): CLI tests for devgas txs #1728

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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))
Expand Down
228 changes: 228 additions & 0 deletions x/devgas/v1/client/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
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]
}
Comment on lines +44 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SetupSuite method correctly sets up the encoding configuration, keyring, base context, and client context. It also initializes a test account. However, the error from Marshal is ignored, which could lead to silent failures if marshaling fails.

- bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
+ bz, err := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
+ s.Require().NoError(err, "marshaling TxResponse should not fail")

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// 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]
}
// 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, err := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
s.Require().NoError(err, "marshaling TxResponse should not fail")
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 string
}

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)
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved
s.ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
})
}
Comment on lines +81 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TestCase struct and its associated methods are well-structured for defining and running test cases. The Run method properly encapsulates the test logic within a named subtest. However, the error message check in line 106 should be refined to ensure that it is not too broad and accurately captures the expected error.

- s.ErrorContains(err, tc.wantErr)
+ s.Require().ErrorContains(err, tc.wantErr, "the error message should contain the expected text")

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
type TestCase struct {
name string
args []string
extraArgs []string
wantErr string
}
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)
s.ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
})
}
type TestCase struct {
name string
args []string
extraArgs []string
wantErr string
}
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)
s.Require().ErrorContains(err, tc.wantErr, "the error message should contain the expected text")
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: "",
},
{
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: "decoding bech32 failed",
},
{
name: "sad: contract addr",
args: []string{"register", "sadcontract", addrs[1].String()},
extraArgs: []string{
fmt.Sprintf("--from=%s", s.testAcc.Address),
},
wantErr: "invalid contract address",
},
{
name: "sad: withdraw addr",
args: []string{"register", addrs[0].String(), "sadwithdraw"},
extraArgs: []string{
fmt.Sprintf("--from=%s", s.testAcc.Address),
},
wantErr: "invalid withdraw address",
},
}

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: "",
},
{
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: "decoding bech32 failed",
},
{
name: "sad: contract addr",
args: []string{"cancel", "sadcontract"},
extraArgs: []string{
fmt.Sprintf("--from=%s", s.testAcc.Address),
},
wantErr: "invalid deployer address",
},
}

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: "",
},
{
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: "decoding bech32 failed",
},
{
name: "sad: contract addr",
args: []string{"update", "sadcontract", addrs[1].String()},
extraArgs: []string{
fmt.Sprintf("--from=%s", s.testAcc.Address),
},
wantErr: "invalid contract",
},
{
name: "sad: new withdraw addr",
args: []string{"update", addrs[0].String(), "saddeployer"},
extraArgs: []string{
fmt.Sprintf("--from=%s", s.testAcc.Address),
},
wantErr: "invalid withdraw address",
},
}

for _, tc := range testCases {
tc.Run(s)
}
}
Loading