refactor(authz): CLI tests using Tendermint Mock (#13517)

## Description

Closes: #13487 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Likhita Polavarapu
2022-10-14 10:37:08 +00:00
committed by GitHub
parent de49cb4b1c
commit d01aa5b4a8
7 changed files with 1164 additions and 7 deletions
@@ -1,7 +1,7 @@
//go:build e2e
// +build e2e
package testutil
package authz
import (
"testing"
@@ -9,12 +9,12 @@ import (
"github.com/stretchr/testify/suite"
"cosmossdk.io/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
clienttestutil "github.com/cosmos/cosmos-sdk/x/authz/client/testutil"
)
func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg.NumValidators = 1
suite.Run(t, clienttestutil.NewIntegrationTestSuite(cfg))
suite.Run(t, NewIntegrationTestSuite(cfg))
}
+270
View File
@@ -0,0 +1,270 @@
package authz
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/cosmos/cosmos-sdk/x/authz/client/cli"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func (s *IntegrationTestSuite) TestQueryGrantGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
grantsURL := val.APIAddress + "/cosmos/authz/v1beta1/grants?granter=%s&grantee=%s&msg_type_url=%s"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
}{
{
"fail invalid granter address",
fmt.Sprintf(grantsURL, "invalid_granter", grantee.String(), typeMsgSend),
true,
"decoding bech32 failed: invalid separator index -1: invalid request",
},
{
"fail invalid grantee address",
fmt.Sprintf(grantsURL, val.Address.String(), "invalid_grantee", typeMsgSend),
true,
"decoding bech32 failed: invalid separator index -1: invalid request",
},
{
"fail with empty granter",
fmt.Sprintf(grantsURL, "", grantee.String(), typeMsgSend),
true,
"empty address string is not allowed: invalid request",
},
{
"fail with empty grantee",
fmt.Sprintf(grantsURL, val.Address.String(), "", typeMsgSend),
true,
"empty address string is not allowed: invalid request",
},
{
"fail invalid msg-type",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String(), "invalidMsg"),
true,
"authorization not found for invalidMsg type",
},
{
"valid query",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String(), typeMsgSend),
false,
"",
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, _ := testutil.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var g authz.QueryGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &g)
require.NoError(err)
require.Len(g.Grants, 1)
g.Grants[0].UnpackInterfaces(val.ClientCtx.InterfaceRegistry)
auth, err := g.Grants[0].GetAuthorization()
require.NoError(err)
require.Equal(auth.MsgTypeURL(), banktypes.SendAuthorization{}.MsgTypeURL())
}
})
}
}
func (s *IntegrationTestSuite) TestQueryGrantsGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
grantsURL := val.APIAddress + "/cosmos/authz/v1beta1/grants?granter=%s&grantee=%s"
testCases := []struct {
name string
url string
expectErr bool
errMsg string
preRun func()
postRun func(*authz.QueryGrantsResponse)
}{
{
"valid query: expect single grant",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String()),
false,
"",
func() {},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 1)
},
},
{
"valid query: expect two grants",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String()),
false,
"",
func() {
_, err := CreateGrant(val, []string{
grantee.String(),
"generic",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgVote),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()),
fmt.Sprintf("--%s=%d", cli.FlagExpiration, time.Now().Add(time.Minute*time.Duration(120)).Unix()),
})
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 2)
},
},
{
"valid query: expect single grant with pagination",
fmt.Sprintf(grantsURL+"&pagination.limit=1", val.Address.String(), grantee.String()),
false,
"",
func() {},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 1)
},
},
{
"valid query: expect two grants with pagination",
fmt.Sprintf(grantsURL+"&pagination.limit=2", val.Address.String(), grantee.String()),
false,
"",
func() {},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 2)
},
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
tc.preRun()
resp, err := testutil.GetRequest(tc.url)
s.Require().NoError(err)
if tc.expectErr {
s.Require().Contains(string(resp), tc.errMsg)
} else {
var authorizations authz.QueryGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &authorizations)
s.Require().NoError(err)
tc.postRun(&authorizations)
}
})
}
}
func (s *IntegrationTestSuite) TestQueryGranterGrantsGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
require := s.Require()
testCases := []struct {
name string
url string
expectErr bool
errMsg string
numItems int
}{
{
"invalid account address",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, "invalid address"),
true,
"decoding bech32 failed",
0,
},
{
"no authorizations found",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, grantee.String()),
false,
"",
0,
},
{
"valid query",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, val.Address.String()),
false,
"",
8,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
resp, err := testutil.GetRequest(tc.url)
require.NoError(err)
if tc.expectErr {
require.Contains(string(resp), tc.errMsg)
} else {
var authorizations authz.QueryGranterGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &authorizations)
require.NoError(err)
// FIXME: https://github.com/cosmos/cosmos-sdk/issues/10965
require.Len(authorizations.Grants, tc.numItems)
}
})
}
}
func (s *IntegrationTestSuite) TestQueryGranteeGrantsGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
require := s.Require()
testCases := []struct {
name string
url string
expectErr bool
errMsg string
numItems int
}{
{
"invalid account address",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/grantee/%s", val.APIAddress, "invalid address"),
true,
"decoding bech32 failed",
0,
},
{
"no authorizations found",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/grantee/%s", val.APIAddress, val.Address.String()),
false,
"",
0,
},
{
"valid query",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/grantee/%s", val.APIAddress, grantee.String()),
false,
"",
1,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
resp, err := testutil.GetRequest(tc.url)
require.NoError(err)
if tc.expectErr {
require.Contains(string(resp), tc.errMsg)
} else {
var authorizations authz.QueryGranteeGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &authorizations)
require.NoError(err)
// FIXME: https://github.com/cosmos/cosmos-sdk/issues/10965
require.Len(authorizations.Grants, tc.numItems)
}
})
}
}
+267
View File
@@ -0,0 +1,267 @@
package authz
import (
"fmt"
"strings"
"time"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/cosmos/cosmos-sdk/x/authz/client/cli"
)
func (s *IntegrationTestSuite) TestQueryAuthorizations() {
val := s.network.Validators[0]
grantee := s.grantee[0]
twoHours := time.Now().Add(time.Minute * time.Duration(120)).Unix()
_, err := CreateGrant(
val,
[]string{
grantee.String(),
"send",
fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
testCases := []struct {
name string
args []string
expectErr bool
expErrMsg string
}{
{
"Error: Invalid grantee",
[]string{
val.Address.String(),
"invalid grantee",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
"decoding bech32 failed: invalid character in string: ' '",
},
{
"Error: Invalid granter",
[]string{
"invalid granter",
grantee.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
"decoding bech32 failed: invalid character in string: ' '",
},
{
"Valid txn (json)",
[]string{
val.Address.String(),
grantee.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
``,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryGrants()
clientCtx := val.ClientCtx
resp, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
s.Require().Contains(string(resp.Bytes()), tc.expErrMsg)
} else {
s.Require().NoError(err)
var grants authz.QueryGrantsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &grants)
s.Require().NoError(err)
}
})
}
}
func (s *IntegrationTestSuite) TestQueryAuthorization() {
val := s.network.Validators[0]
grantee := s.grantee[0]
twoHours := time.Now().Add(time.Minute * time.Duration(120)).Unix()
_, err := CreateGrant(
val,
[]string{
grantee.String(),
"send",
fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%d", cli.FlagExpiration, twoHours),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
testCases := []struct {
name string
args []string
expectErr bool
expectedOutput string
}{
{
"Error: Invalid grantee",
[]string{
val.Address.String(),
"invalid grantee",
typeMsgSend,
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
"",
},
{
"Error: Invalid granter",
[]string{
"invalid granter",
grantee.String(),
typeMsgSend,
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
"",
},
{
"no authorization found",
[]string{
val.Address.String(),
grantee.String(),
"typeMsgSend",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
"",
},
{
"Valid txn (json)",
[]string{
val.Address.String(),
grantee.String(),
typeMsgSend,
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`{"@type":"/cosmos.bank.v1beta1.SendAuthorization","spend_limit":[{"denom":"stake","amount":"100"}],"allow_list":[]}`,
},
{
"Valid txn with allowed list (json)",
[]string{
val.Address.String(),
s.grantee[3].String(),
typeMsgSend,
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
fmt.Sprintf(`{"@type":"/cosmos.bank.v1beta1.SendAuthorization","spend_limit":[{"denom":"stake","amount":"88"}],"allow_list":["%s"]}`, s.grantee[4]),
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryGrants()
clientCtx := val.ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().Contains(strings.TrimSpace(out.String()), tc.expectedOutput)
}
})
}
}
func (s *IntegrationTestSuite) TestQueryGranterGrants() {
val := s.network.Validators[0]
grantee := s.grantee[0]
require := s.Require()
testCases := []struct {
name string
args []string
expectErr bool
expectedErr string
expItems int
}{
{
"invalid address",
[]string{
"invalid-address",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
true,
"decoding bech32 failed",
0,
},
{
"no authorization found",
[]string{
grantee.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
"",
0,
},
{
"valid case",
[]string{
val.Address.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
"",
8,
},
{
"valid case with pagination",
[]string{
val.Address.String(),
"--limit=2",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
"",
2,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := cli.GetQueryGranterGrants()
clientCtx := val.ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
require.Error(err)
require.Contains(out.String(), tc.expectedErr)
} else {
require.NoError(err)
var grants authz.QueryGranterGrantsResponse
require.NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &grants))
require.Len(grants.Grants, tc.expItems)
}
})
}
}
+14
View File
@@ -0,0 +1,14 @@
package authz
import (
"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/x/authz/client/cli"
)
func CreateGrant(val *network.Validator, args []string) (testutil.BufferWriter, error) {
cmd := cli.NewCmdGrantAuthorization()
clientCtx := val.ClientCtx
return clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
}
File diff suppressed because it is too large Load Diff