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

Add cli test for query Account #6973

Merged
merged 33 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
eccdf04
Add cli tests for query account
sahith-narahari Aug 7, 2020
0242dfd
Merge branch 'master' into sahith/add-query-clitests
sahith-narahari Aug 7, 2020
3148528
lint
sahith-narahari Aug 7, 2020
6deed40
Merge branch 'sahith/add-query-clitests' of github.com:cosmos/cosmos-…
sahith-narahari Aug 7, 2020
858e294
Refactor auth cli tests
sahith-narahari Aug 7, 2020
a737639
Merge branch 'master' into sahith/add-query-clitests
sahith-narahari Aug 7, 2020
1193532
Refactor existing cli tests
sahith-narahari Aug 9, 2020
74d8afa
Add setup test
sahith-narahari Aug 9, 2020
3c94717
Add test for proposal
sahith-narahari Aug 11, 2020
c756391
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/add…
sahith-narahari Aug 11, 2020
e06827b
Move gov tests
sahith-narahari Aug 11, 2020
dc377d5
Update test network
sahith-narahari Aug 11, 2020
a325d13
Merge branch 'master' into sahith/add-query-clitests
sahith-narahari Aug 11, 2020
6412223
Move gov tests
sahith-narahari Aug 11, 2020
3074efe
Merge remote-tracking branch 'origin/sahith/add-query-clitests' into …
sahith-narahari Aug 11, 2020
a8dd691
Merge branch 'master' into sahith/add-query-clitests
aaronc Aug 11, 2020
48ca2e9
Add test for unknown addres
sahith-narahari Aug 11, 2020
b47a289
Merge branch 'sahith/add-query-clitests' of github.com:cosmos/cosmos-…
sahith-narahari Aug 11, 2020
f5b0cf3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/add…
sahith-narahari Aug 12, 2020
0eb56f0
Fix lint
sahith-narahari Aug 12, 2020
a5d9041
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/add…
sahith-narahari Aug 13, 2020
e6c0f95
Add check for internal
sahith-narahari Aug 13, 2020
944d7c4
Fix #7007
aaronc Aug 13, 2020
e32cb18
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into sa…
aaronc Aug 13, 2020
87fbb84
Fixes
aaronc Aug 13, 2020
3b05b2b
Fixes
aaronc Aug 13, 2020
7e035fe
Merge branch 'master' into sahith/add-query-clitests
aaronc Aug 13, 2020
692a29a
Merge remote-tracking branch 'sahith-narahari/sahith/add-query-clites…
aaronc Aug 13, 2020
cbb5455
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/add…
sahith-narahari Aug 13, 2020
8293794
Address changes
sahith-narahari Aug 13, 2020
8f81531
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/add…
sahith-narahari Aug 16, 2020
886a76b
Merge branch 'master' into sahith/add-query-clitests
sahith-narahari Aug 17, 2020
d0adaca
Merge branch 'master' into sahith/add-query-clitests
Aug 17, 2020
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
25 changes: 25 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"syscall"
"time"

"google.golang.org/grpc/codes"

sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
grpcstatus "google.golang.org/grpc/status"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -356,6 +360,7 @@ func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQu

res, err := handler(ctx, req)
if err != nil {
err = grpcErrorToSDKError(err)
res = sdkerrors.QueryResult(err)
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
res.Height = req.Height
return res
Expand All @@ -364,6 +369,26 @@ func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQu
return res
}

func grpcErrorToSDKError(err error) error {
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
status, ok := grpcstatus.FromError(err)
if !ok {
return err
}

switch status.Code() {
case codes.NotFound:
return sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, err.Error())
case codes.InvalidArgument:
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
case codes.FailedPrecondition:
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
case codes.Unauthenticated:
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, err.Error())
default:
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, err.Error())
}
}

// createQueryContext creates a new sdk.Context for a query, taking as args
// the block height and whether the query needs a proof or not.
func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, error) {
Expand Down
36 changes: 19 additions & 17 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,24 @@ type Config struct {
Codec codec.Marshaler
LegacyAmino *codec.LegacyAmino // TODO: Remove!
InterfaceRegistry codectypes.InterfaceRegistry
TxConfig client.TxConfig
AccountRetriever client.AccountRetriever
AppConstructor AppConstructor // the ABCI application constructor
GenesisState map[string]json.RawMessage // custom gensis state to provide
TimeoutCommit time.Duration // the consensus commitment timeout
ChainID string // the network chain-id
NumValidators int // the total number of validators to create and bond
BondDenom string // the staking bond denomination
MinGasPrices string // the minimum gas prices each validator will accept
AccountTokens sdk.Int // the amount of unique validator tokens (e.g. 1000node0)
StakingTokens sdk.Int // the amount of tokens each validator has available to stake
BondedTokens sdk.Int // the amount of tokens each validator stakes
PruningStrategy string // the pruning strategy each validator will have
EnableLogging bool // enable Tendermint logging to STDOUT
CleanupDir bool // remove base temporary directory during cleanup
SigningAlgo string // signing algorithm for keys
KeyringOptions []keyring.Option

TxConfig client.TxConfig
AccountRetriever client.AccountRetriever
AppConstructor AppConstructor // the ABCI application constructor
GenesisState map[string]json.RawMessage // custom gensis state to provide
TimeoutCommit time.Duration // the consensus commitment timeout
ChainID string // the network chain-id
NumValidators int // the total number of validators to create and bond
BondDenom string // the staking bond denomination
MinGasPrices string // the minimum gas prices each validator will accept
AccountTokens sdk.Int // the amount of unique validator tokens (e.g. 1000node0)
StakingTokens sdk.Int // the amount of tokens each validator has available to stake
BondedTokens sdk.Int // the amount of tokens each validator stakes
PruningStrategy string // the pruning strategy each validator will have
EnableLogging bool // enable Tendermint logging to STDOUT
CleanupDir bool // remove base temporary directory during cleanup
SigningAlgo string // signing algorithm for keys
KeyringOptions []keyring.Option
}

// DefaultConfig returns a sane default configuration suitable for nearly all
Expand Down Expand Up @@ -324,6 +325,7 @@ func New(t *testing.T, cfg Config) *Network {
WithKeyring(kb).
WithHomeDir(tmCfg.RootDir).
WithChainID(cfg.ChainID).
WithInterfaceRegistry(cfg.InterfaceRegistry).
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
WithJSONMarshaler(cfg.Codec).
WithLegacyAmino(cfg.LegacyAmino).
WithTxConfig(cfg.TxConfig).
Expand Down
64 changes: 57 additions & 7 deletions x/auth/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/codec/types"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmcrypto "github.com/tendermint/tendermint/crypto"
tmcli "github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -20,10 +23,13 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
authcli "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authtest "github.com/cosmos/cosmos-sdk/x/auth/client/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
Expand Down Expand Up @@ -211,7 +217,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
s.Require().Equal(len(txBuilder.GetTx().GetMsgs()), 1)
s.Require().Equal(0, len(txBuilder.GetTx().GetSignatures()))

resp, err := bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), val1.Address)
resp, err := bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
s.Require().NoError(err)

var balRes banktypes.QueryAllBalancesResponse
Expand Down Expand Up @@ -281,7 +287,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
s.Require().True(strings.Contains(res.String(), "[OK]"))

// Ensure foo has right amount of funds
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), val1.Address)
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
s.Require().NoError(err)

err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &balRes)
Expand All @@ -304,15 +310,15 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
s.Require().NoError(s.network.WaitForNextBlock())

// Ensure destiny account state
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), account.GetAddress())
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, account.GetAddress())
s.Require().NoError(err)

err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &balRes)
s.Require().NoError(err)
s.Require().Equal(sendTokens.Amount, balRes.Balances.AmountOf(s.cfg.BondDenom))

// Ensure origin account state
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), val1.Address)
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
s.Require().NoError(err)

err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &balRes)
Expand Down Expand Up @@ -448,7 +454,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
multisigInfo, err := val1.ClientCtx.Keyring.Key("multi")
s.Require().NoError(err)

resp, err := bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), multisigInfo.GetAddress())
resp, err := bankcli.QueryBalancesExec(val1.ClientCtx, multisigInfo.GetAddress())
s.Require().NoError(err)

var balRes banktypes.QueryAllBalancesResponse
Expand All @@ -472,7 +478,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {

s.Require().NoError(s.network.WaitForNextBlock())

resp, err = bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), multisigInfo.GetAddress())
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, multisigInfo.GetAddress())
s.Require().NoError(err)

err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &balRes)
Expand Down Expand Up @@ -564,7 +570,7 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {

s.Require().NoError(s.network.WaitForNextBlock())

resp, err := bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), multisigInfo.GetAddress())
resp, err := bankcli.QueryBalancesExec(val1.ClientCtx, multisigInfo.GetAddress())
s.Require().NoError(err)

var balRes banktypes.QueryAllBalancesResponse
Expand Down Expand Up @@ -628,6 +634,50 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {
s.Require().NoError(s.network.WaitForNextBlock())
}

func (s *IntegrationTestSuite) TestGetAccountCmd() {
val := s.network.Validators[0]
_, _, addr1 := testdata.KeyTestPubAddr()

testCases := []struct {
name string
args []string
expectErr bool
}{
{
"invalid address",
[]string{addr1.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
true,
},
{
"valid address",
[]string{val.Address.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
false,
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := authcli.GetAccountCmd()
clientCtx := val.ClientCtx

out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
s.Require().NotEqual("internal", err.Error())
} else {
var any types.Any
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), &any))
var acc authtypes.AccountI
s.Require().NoError(val.ClientCtx.InterfaceRegistry.UnpackAny(&any, &acc))
s.Require().Equal(val.Address, acc.GetAddress())
}
})
}
}

func TestGetBroadcastCommand_OfflineFlag(t *testing.T) {
clientCtx := client.Context{}.WithOffline(true)
clientCtx = clientCtx.WithTxConfig(simapp.MakeEncodingConfig().TxConfig)
Expand Down
22 changes: 2 additions & 20 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package keeper

import (
"context"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

proto "github.com/gogo/protobuf/proto"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -32,12 +29,12 @@ func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountReques
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
}

acc, err := ConvertAccount(account)
any, err := codectypes.NewAnyWithValue(account)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}

return &types.QueryAccountResponse{Account: acc}, nil
return &types.QueryAccountResponse{Account: any}, nil
}

// Params returns parameters of auth module
Expand All @@ -50,18 +47,3 @@ func (ak AccountKeeper) Params(c context.Context, req *types.QueryParamsRequest)

return &types.QueryParamsResponse{Params: params}, nil
}

// ConvertAccount converts AccountI to Any type
func ConvertAccount(account types.AccountI) (*codectypes.Any, error) {
msg, ok := account.(proto.Message)
if !ok {
return nil, fmt.Errorf("can't protomarshal %T", msg)
}

any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, err
}

return any, nil
}
4 changes: 4 additions & 0 deletions x/auth/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"strings"

"github.com/gogo/protobuf/proto"

"github.com/tendermint/tendermint/crypto"
yaml "gopkg.in/yaml.v2"

Expand Down Expand Up @@ -292,6 +294,8 @@ func (ma *ModuleAccount) UnmarshalJSON(bz []byte) error {
//
// Many complex conditions can be used in the concrete struct which implements AccountI.
type AccountI interface {
proto.Message

GetAddress() sdk.AccAddress
SetAddress(sdk.AccAddress) error // errors if already set.

Expand Down
10 changes: 10 additions & 0 deletions x/auth/types/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

import codectypes "github.com/cosmos/cosmos-sdk/codec/types"

func (m *QueryAccountResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var account AccountI
return unpacker.UnpackAny(m.Account, &account)
}

var _ codectypes.UnpackInterfacesMessage = &QueryAccountResponse{}
33 changes: 7 additions & 26 deletions x/bank/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/client"

"github.com/gogo/protobuf/proto"

"github.com/stretchr/testify/suite"
tmcli "github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -101,22 +102,13 @@ func (s *IntegrationTestSuite) TestGetBalancesCmd() {

s.Run(tc.name, func() {
cmd := cli.GetBalancesCmd()
_, out := testutil.ApplyMockIO(cmd)

clientCtx := val.ClientCtx.WithOutput(out)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

out.Reset()
cmd.SetArgs(tc.args)
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, tc.args)

err := cmd.ExecuteContext(ctx)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType))
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType))
s.Require().Equal(tc.expected.String(), tc.respType.String())
}
})
Expand Down Expand Up @@ -173,17 +165,9 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {

s.Run(tc.name, func() {
cmd := cli.GetCmdQueryTotalSupply()
_, out := testutil.ApplyMockIO(cmd)

clientCtx := val.ClientCtx.WithOutput(out)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

out.Reset()
cmd.SetArgs(tc.args)
clientCtx := val.ClientCtx

err := cmd.ExecuteContext(ctx)
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
Expand Down Expand Up @@ -295,9 +279,6 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() {
s.Run(tc.name, func() {
clientCtx := val.ClientCtx

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

bz, err := banktestutil.MsgSendExec(clientCtx, tc.from, tc.to, tc.amount, tc.args...)
if tc.expectErr {
s.Require().Error(err)
Expand Down
Loading