Skip to content

Commit

Permalink
Merge branch 'main' into julien/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jul 3, 2024
2 parents 0621147 + 2718b7f commit cb0fa95
Show file tree
Hide file tree
Showing 6 changed files with 535 additions and 1 deletion.
8 changes: 8 additions & 0 deletions testutil/cli/cmt_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func NewMockCometRPC(respQuery abci.QueryResponse) MockCometRPC {
return MockCometRPC{responseQuery: respQuery}
}

// NewMockCometRPCWithValue returns a mock CometBFT RPC implementation with value only.
// It is used for CLI testing.
func NewMockCometRPCWithValue(bz []byte) MockCometRPC {
return MockCometRPC{responseQuery: abci.QueryResponse{
Value: bz,
}}
}

func (MockCometRPC) BroadcastTxSync(context.Context, cmttypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return &coretypes.ResultBroadcastTx{Code: 0}, nil
}
Expand Down
145 changes: 145 additions & 0 deletions x/accounts/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package cli_test

import (
"context"
"fmt"
"io"
"testing"

"github.com/gogo/protobuf/types"
"github.com/stretchr/testify/suite"

"cosmossdk.io/math"
"cosmossdk.io/x/accounts/cli"
v1 "cosmossdk.io/x/accounts/v1"
"cosmossdk.io/x/bank"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

type CLITestSuite struct {
suite.Suite

kr keyring.Keyring
encCfg testutilmod.TestEncodingConfig
baseCtx client.Context
clientCtx client.Context
}

func TestCLITestSuite(t *testing.T) {
suite.Run(t, new(CLITestSuite))
}

func (s *CLITestSuite) SetupSuite() {
s.encCfg = testutilmod.MakeTestEncodingConfig(codectestutil.CodecOptions{}, bank.AppModule{})
s.kr = keyring.NewInMemory(s.encCfg.Codec)

s.baseCtx = client.Context{}.
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
}

func (s *CLITestSuite) TestTxInitCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
accountStr := make([]string, len(accounts))
for i, acc := range accounts {
addrStr, err := s.baseCtx.AddressCodec.BytesToString(acc.Address)
s.Require().NoError(err)
accountStr[i] = addrStr
}

s.baseCtx = s.baseCtx.WithFromAddress(accounts[0].Address)

extraArgs := []string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("photon", math.NewInt(10))).String()),
fmt.Sprintf("--%s=test-chain", flags.FlagChainID),
fmt.Sprintf("--%s=%s", flags.FlagFrom, accountStr[0]),
}

cmd := cli.GetTxInitCmd()
cmd.SetOutput(io.Discard)

ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&v1.SchemaResponse{
InitSchema: &v1.SchemaResponse_Handler{
Request: sdk.MsgTypeURL(&types.Empty{})[1:],
Response: sdk.MsgTypeURL(&types.Empty{})[1:],
},
})
c := clitestutil.NewMockCometRPCWithValue(bz)
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen()

testCases := []struct {
name string
accountType string
jsonMsg string
extraArgs []string
expectErrMsg string
}{
{
name: "valid json msg",
accountType: "test",
jsonMsg: `{}`,
extraArgs: extraArgs,
expectErrMsg: "",
},
{
name: "invalid json msg",
accountType: "test",
jsonMsg: `{"test": "jsonmsg"}`,
extraArgs: extraArgs,
expectErrMsg: "provided message is not valid",
},
{
name: "invalid sender",
accountType: "test",
jsonMsg: `{}`,
extraArgs: append(extraArgs, fmt.Sprintf("--%s=%s", flags.FlagFrom, "bar")),
expectErrMsg: "failed to convert address field to address",
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
ctx := svrcmd.CreateExecuteContext(context.Background())

var args []string
args = append(args, tc.accountType)
args = append(args, tc.jsonMsg)
args = append(args, tc.extraArgs...)

cmd.SetContext(ctx)
cmd.SetArgs(args)

out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, args)
if tc.expectErrMsg != "" {
s.Require().Error(err)
s.Require().Contains(out.String(), tc.expectErrMsg)
} else {
s.Require().NoError(err)
msg := &sdk.TxResponse{}
s.Require().NoError(s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), msg), out.String())
}
})
}
}
Loading

0 comments on commit cb0fa95

Please sign in to comment.