-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
1 parent
de49cb4
commit d01aa5b
Showing
7 changed files
with
1,164 additions
and
7 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
tests/e2e/authz/client/testutil/cli_test.go → tests/e2e/authz/cli_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package testutil | ||
package authz | ||
|
||
import ( | ||
"testing" | ||
|
||
"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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package testutil | ||
package authz | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package testutil | ||
package authz | ||
|
||
import ( | ||
"fmt" | ||
|
2 changes: 1 addition & 1 deletion
2
x/authz/client/testutil/test_helpers.go → tests/e2e/authz/test_helpers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package testutil | ||
package authz | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package testutil | ||
package authz | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
package cli_test | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
tmcli "github.com/tendermint/tendermint/libs/cli" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
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 *CLITestSuite) TestQueryAuthorizations() { | ||
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) | ||
|
||
grantee := s.grantee[0] | ||
twoHours := time.Now().Add(time.Minute * time.Duration(120)).Unix() | ||
|
||
_, err := s.createGrant( | ||
[]string{ | ||
grantee.String(), | ||
"send", | ||
fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), | ||
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), | ||
fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].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("stake", sdk.NewInt(10))).String()), | ||
}, | ||
) | ||
s.Require().NoError(err) | ||
|
||
testCases := []struct { | ||
name string | ||
args []string | ||
expectErr bool | ||
expErrMsg string | ||
}{ | ||
{ | ||
"Error: Invalid grantee", | ||
[]string{ | ||
val[0].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[0].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() | ||
resp, err := clitestutil.ExecTestCLICmd(s.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 = s.clientCtx.Codec.UnmarshalJSON(resp.Bytes(), &grants) | ||
s.Require().NoError(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (s *CLITestSuite) TestQueryAuthorization() { | ||
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) | ||
|
||
grantee := s.grantee[0] | ||
twoHours := time.Now().Add(time.Minute * time.Duration(120)).Unix() | ||
|
||
_, err := s.createGrant( | ||
[]string{ | ||
grantee.String(), | ||
"send", | ||
fmt.Sprintf("--%s=100stake", cli.FlagSpendLimit), | ||
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), | ||
fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].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("stake", sdk.NewInt(10))).String()), | ||
}, | ||
) | ||
s.Require().NoError(err) | ||
|
||
testCases := []struct { | ||
name string | ||
args []string | ||
expectErr bool | ||
}{ | ||
{ | ||
"Error: Invalid grantee", | ||
[]string{ | ||
val[0].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, | ||
}, | ||
{ | ||
"Valid txn (json)", | ||
[]string{ | ||
val[0].Address.String(), | ||
grantee.String(), | ||
typeMsgSend, | ||
fmt.Sprintf("--%s=json", tmcli.OutputFlag), | ||
}, | ||
false, | ||
}, | ||
{ | ||
"Valid txn with allowed list (json)", | ||
[]string{ | ||
val[0].Address.String(), | ||
s.grantee[3].String(), | ||
typeMsgSend, | ||
fmt.Sprintf("--%s=json", tmcli.OutputFlag), | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
s.Run(tc.name, func() { | ||
cmd := cli.GetCmdQueryGrants() | ||
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args) | ||
if tc.expectErr { | ||
s.Require().Error(err) | ||
} else { | ||
s.Require().NoError(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (s *CLITestSuite) TestQueryGranterGrants() { | ||
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) | ||
|
||
grantee := s.grantee[0] | ||
require := s.Require() | ||
|
||
testCases := []struct { | ||
name string | ||
args []string | ||
expectErr bool | ||
expectedErr string | ||
}{ | ||
{ | ||
"invalid address", | ||
[]string{ | ||
"invalid-address", | ||
fmt.Sprintf("--%s=json", tmcli.OutputFlag), | ||
}, | ||
true, | ||
"decoding bech32 failed", | ||
}, | ||
{ | ||
"no authorization found", | ||
[]string{ | ||
grantee.String(), | ||
fmt.Sprintf("--%s=json", tmcli.OutputFlag), | ||
}, | ||
false, | ||
"", | ||
}, | ||
{ | ||
"valid case", | ||
[]string{ | ||
val[0].Address.String(), | ||
fmt.Sprintf("--%s=json", tmcli.OutputFlag), | ||
}, | ||
false, | ||
"", | ||
}, | ||
{ | ||
"valid case with pagination", | ||
[]string{ | ||
val[0].Address.String(), | ||
"--limit=2", | ||
fmt.Sprintf("--%s=json", tmcli.OutputFlag), | ||
}, | ||
false, | ||
"", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
s.Run(tc.name, func() { | ||
cmd := cli.GetQueryGranterGrants() | ||
out, err := clitestutil.ExecTestCLICmd(s.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(s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &grants)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.