Skip to content

Commit da194c7

Browse files
authored
fix: add restrictions on the number of args in the CLIs (#734)
* Add restrictions on the number of args in the CLIs * Add unit tests * Update CHANGELOG.md * Update CHANGELOG.md
1 parent 3dd44aa commit da194c7

File tree

7 files changed

+88
-7
lines changed

7 files changed

+88
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
113113
* (crypto) [\#731](https://github.com/line/lbm-sdk/pull/731) remove VRFProve function
114114
* (x/foundation) [\#732](https://github.com/line/lbm-sdk/pull/732) add verification on accounts into x/foundation Grants cli
115115
* (x/foundation) [\#730](https://github.com/line/lbm-sdk/pull/730) prune stale x/foundation proposals at voting period end
116+
* (cli) [\#734](https://github.com/line/lbm-sdk/pull/734) add restrictions on the number of args in the CLIs
116117

117118
### Breaking Changes
118119
* (proto) [\#564](https://github.com/line/lbm-sdk/pull/564) change gRPC path to original cosmos path

x/auth/client/cli/query.go

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func GetAccountCmd() *cobra.Command {
115115
func GetAccountsCmd() *cobra.Command {
116116
cmd := &cobra.Command{
117117
Use: "accounts",
118+
Args: cobra.NoArgs,
118119
Short: "Query all the accounts",
119120
RunE: func(cmd *cobra.Command, args []string) error {
120121
clientCtx, err := client.GetClientQueryContext(cmd)
@@ -147,6 +148,7 @@ func GetAccountsCmd() *cobra.Command {
147148
func QueryTxsByEventsCmd() *cobra.Command {
148149
cmd := &cobra.Command{
149150
Use: "txs",
151+
Args: cobra.NoArgs,
150152
Short: "Query for paginated transactions that match a set of events",
151153
Long: strings.TrimSpace(
152154
fmt.Sprintf(`

x/auth/client/testutil/suite.go

+50-7
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() {
453453
testCases := []struct {
454454
name string
455455
args []string
456+
expectErr bool
456457
expectEmpty bool
457458
}{
458459
{
@@ -463,6 +464,7 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() {
463464
fmt.Sprintf("--%s=json", ostcli.OutputFlag),
464465
},
465466
false,
467+
false,
466468
},
467469
{
468470
"no matching fee event",
@@ -471,6 +473,16 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() {
471473
sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(0))).String()),
472474
fmt.Sprintf("--%s=json", ostcli.OutputFlag),
473475
},
476+
false,
477+
true,
478+
},
479+
{
480+
"wrong number of arguments",
481+
[]string{
482+
"extra",
483+
fmt.Sprintf("--%s=json", ostcli.OutputFlag),
484+
},
485+
true,
474486
true,
475487
},
476488
}
@@ -482,6 +494,10 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() {
482494
clientCtx := val.ClientCtx
483495

484496
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
497+
if tc.expectErr {
498+
s.Require().Error(err)
499+
return
500+
}
485501
s.Require().NoError(err)
486502

487503
var result sdk.SearchTxsResult
@@ -1122,16 +1138,43 @@ func (s *IntegrationTestSuite) TestGetAccountCmd() {
11221138

11231139
func (s *IntegrationTestSuite) TestGetAccountsCmd() {
11241140
val := s.network.Validators[0]
1125-
clientCtx := val.ClientCtx
11261141

1127-
out, err := clitestutil.ExecTestCLICmd(clientCtx, authcli.GetAccountsCmd(), []string{
1142+
commonArgs := []string{
11281143
fmt.Sprintf("--%s=json", ostcli.OutputFlag),
1129-
})
1130-
s.Require().NoError(err)
1144+
}
11311145

1132-
var res authtypes.QueryAccountsResponse
1133-
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
1134-
s.Require().NotEmpty(res.Accounts)
1146+
testCases := map[string]struct {
1147+
args []string
1148+
valid bool
1149+
}{
1150+
"valid request": {
1151+
valid: true,
1152+
},
1153+
"wrong number of args": {
1154+
args: []string{
1155+
"extra",
1156+
},
1157+
},
1158+
}
1159+
1160+
for name, tc := range testCases {
1161+
tc := tc
1162+
s.Run(name, func() {
1163+
cmd := authcli.GetAccountsCmd()
1164+
clientCtx := val.ClientCtx
1165+
1166+
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, append(tc.args, commonArgs...))
1167+
if !tc.valid {
1168+
s.Require().Error(err)
1169+
return
1170+
}
1171+
s.Require().NoError(err)
1172+
1173+
var res authtypes.QueryAccountsResponse
1174+
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
1175+
s.Require().NotEmpty(res.Accounts)
1176+
})
1177+
}
11351178
}
11361179

11371180
func TestGetBroadcastCommandOfflineFlag(t *testing.T) {

x/bank/client/cli/query.go

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Example:
105105
func GetCmdDenomsMetadata() *cobra.Command {
106106
cmd := &cobra.Command{
107107
Use: "denom-metadata",
108+
Args: cobra.NoArgs,
108109
Short: "Query the client metadata for coin denominations",
109110
Long: strings.TrimSpace(
110111
fmt.Sprintf(`Query the client metadata for all the registered coin denominations
@@ -158,6 +159,7 @@ To query for the client metadata of a specific coin denomination use:
158159
func GetCmdQueryTotalSupply() *cobra.Command {
159160
cmd := &cobra.Command{
160161
Use: "total",
162+
Args: cobra.NoArgs,
161163
Short: "Query the total supply of coins of the chain",
162164
Long: strings.TrimSpace(
163165
fmt.Sprintf(`Query total supply of coins that are held by accounts in the chain.

x/bank/client/testutil/suite.go

+24
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
214214
Amount: sdk.ZeroInt(),
215215
},
216216
},
217+
{
218+
name: "wrong number of arguments",
219+
args: []string{
220+
"extra",
221+
fmt.Sprintf("--%s=1", flags.FlagHeight),
222+
fmt.Sprintf("--%s=json", ostcli.OutputFlag),
223+
},
224+
expectErr: true,
225+
},
217226
}
218227

219228
for _, tc := range testCases {
@@ -341,6 +350,21 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDenomsMetadata() {
341350
},
342351
},
343352
},
353+
{
354+
name: "wrong number of arguments",
355+
args: []string{
356+
"extra",
357+
fmt.Sprintf("--%s=1", flags.FlagHeight),
358+
fmt.Sprintf("--%s=json", ostcli.OutputFlag),
359+
},
360+
expectErr: true,
361+
respType: &types.QueryDenomMetadataResponse{},
362+
expected: &types.QueryDenomMetadataResponse{
363+
Metadata: types.Metadata{
364+
DenomUnits: []*types.DenomUnit{},
365+
},
366+
},
367+
},
344368
}
345369

346370
for _, tc := range testCases {

x/gov/client/cli/query.go

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ $ %s query gov proposal 1
9494
func GetCmdQueryProposals() *cobra.Command {
9595
cmd := &cobra.Command{
9696
Use: "proposals",
97+
Args: cobra.NoArgs,
9798
Short: "Query proposals with optional filters",
9899
Long: strings.TrimSpace(
99100
fmt.Sprintf(`Query for a all paginated proposals that match optional filters:

x/gov/client/testutil/suite.go

+8
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ func (s *IntegrationTestSuite) TestCmdGetProposals() {
435435
},
436436
true,
437437
},
438+
{
439+
"wrong number of arguments",
440+
[]string{
441+
"extra",
442+
fmt.Sprintf("--%s=json", ostcli.OutputFlag),
443+
},
444+
true,
445+
},
438446
}
439447

440448
for _, tc := range testCases {

0 commit comments

Comments
 (0)