feat: add grants by grantee authz query (#10944)
This commit is contained in:
@@ -28,6 +28,7 @@ func GetQueryCmd() *cobra.Command {
|
||||
authorizationQueryCmd.AddCommand(
|
||||
GetCmdQueryGrants(),
|
||||
GetQueryGranterGrants(),
|
||||
GetQueryGranteeGrants(),
|
||||
)
|
||||
|
||||
return authorizationQueryCmd
|
||||
@@ -140,3 +141,51 @@ $ %s q %s granter-grants cosmos1skj..
|
||||
flags.AddPaginationFlagsToCmd(cmd, "granter-grants")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func GetQueryGranteeGrants() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "grantee-grants [grantee-addr]",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "query authorization grants granted to a grantee",
|
||||
Long: strings.TrimSpace(
|
||||
fmt.Sprintf(`Query authorization grants granted to a grantee.
|
||||
Examples:
|
||||
$ %s q %s grantee-grants cosmos1skj..
|
||||
`,
|
||||
version.AppName, authz.ModuleName),
|
||||
),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
clientCtx, err := client.GetClientQueryContext(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
grantee, err := sdk.AccAddressFromBech32(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pageReq, err := client.ReadPageRequest(cmd.Flags())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
queryClient := authz.NewQueryClient(clientCtx)
|
||||
res, err := queryClient.GranteeGrants(
|
||||
cmd.Context(),
|
||||
&authz.QueryGranteeGrantsRequest{
|
||||
Grantee: grantee.String(),
|
||||
Pagination: pageReq,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintProto(res)
|
||||
},
|
||||
}
|
||||
flags.AddQueryFlagsToCmd(cmd)
|
||||
flags.AddPaginationFlagsToCmd(cmd, "grantee-grants")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build norace
|
||||
// +build norace
|
||||
|
||||
package testutil
|
||||
|
||||
@@ -148,7 +148,9 @@ func (s *IntegrationTestSuite) TestQueryGrantsGRPC() {
|
||||
tc := tc
|
||||
s.Run(tc.name, func() {
|
||||
tc.preRun()
|
||||
resp, _ := rest.GetRequest(tc.url)
|
||||
resp, err := rest.GetRequest(tc.url)
|
||||
s.Require().NoError(err)
|
||||
|
||||
if tc.expectErr {
|
||||
s.Require().Contains(string(resp), tc.errMsg)
|
||||
} else {
|
||||
@@ -176,28 +178,21 @@ func (s *IntegrationTestSuite) TestQueryGranterGrantsGRPC() {
|
||||
}{
|
||||
{
|
||||
"invalid account address",
|
||||
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/%s", val.APIAddress, "invalid 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/%s", val.APIAddress, grantee.String()),
|
||||
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, grantee.String()),
|
||||
false,
|
||||
"",
|
||||
0,
|
||||
},
|
||||
{
|
||||
"valid query",
|
||||
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/%s", val.APIAddress, val.Address.String()),
|
||||
false,
|
||||
"",
|
||||
7,
|
||||
},
|
||||
{
|
||||
"valid query: expect seven grants",
|
||||
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/%s", val.APIAddress, val.Address.String()),
|
||||
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, val.Address.String()),
|
||||
false,
|
||||
"",
|
||||
7,
|
||||
@@ -205,13 +200,69 @@ func (s *IntegrationTestSuite) TestQueryGranterGrantsGRPC() {
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
s.Run(tc.name, func() {
|
||||
resp, _ := rest.GetRequest(tc.url)
|
||||
resp, err := rest.GetRequest(tc.url)
|
||||
require.NoError(err)
|
||||
|
||||
if tc.expectErr {
|
||||
require.Contains(string(resp), tc.errMsg)
|
||||
} else {
|
||||
var authorizations authz.QueryGrantsResponse
|
||||
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 := rest.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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user