cosmos-sdk/x/authz/keeper/grpc_query_test.go
MD Aleem 36372701cf
feat: authz add query all grants by granter (#10326)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #9966 

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### 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...

- [x] 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
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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)
2021-10-11 13:30:12 +00:00

242 lines
6.4 KiB
Go

package keeper_test
import (
gocontext "context"
"fmt"
"time"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func (suite *TestSuite) TestGRPCQueryAuthorization() {
app, ctx, queryClient, addrs := suite.app, suite.ctx, suite.queryClient, suite.addrs
var (
req *authz.QueryGrantsRequest
expAuthorization authz.Authorization
)
testCases := []struct {
msg string
malleate func(require *require.Assertions)
expError string
postTest func(require *require.Assertions, res *authz.QueryGrantsResponse)
}{
{
"fail invalid granter addr",
func(require *require.Assertions) {
req = &authz.QueryGrantsRequest{}
},
"empty address string is not allowed",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
},
{
"fail invalid grantee addr",
func(require *require.Assertions) {
req = &authz.QueryGrantsRequest{
Granter: addrs[0].String(),
}
},
"empty address string is not allowed",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
},
{
"fail invalid msg-type",
func(require *require.Assertions) {
req = &authz.QueryGrantsRequest{
Granter: addrs[0].String(),
Grantee: addrs[1].String(),
MsgTypeUrl: "unknown",
}
},
"no authorization found for unknown type",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
},
{
"Success",
func(require *require.Assertions) {
now := ctx.BlockHeader().Time
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
expAuthorization = &banktypes.SendAuthorization{SpendLimit: newCoins}
err := app.AuthzKeeper.SaveGrant(ctx, addrs[0], addrs[1], expAuthorization, now.Add(time.Hour))
require.NoError(err)
req = &authz.QueryGrantsRequest{
Granter: addrs[1].String(),
Grantee: addrs[0].String(),
MsgTypeUrl: expAuthorization.MsgTypeURL(),
}
},
"",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {
var auth authz.Authorization
require.Equal(1, len(res.Grants))
err := suite.app.InterfaceRegistry().UnpackAny(res.Grants[0].Authorization, &auth)
require.NoError(err)
require.NotNil(auth)
require.Equal(auth.String(), expAuthorization.String())
},
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
require := suite.Require()
tc.malleate(require)
result, err := queryClient.Grants(gocontext.Background(), req)
if tc.expError == "" {
require.NoError(err)
} else {
require.Error(err)
require.Contains(err.Error(), tc.expError)
}
tc.postTest(require, result)
})
}
}
func (suite *TestSuite) TestGRPCQueryAuthorizations() {
app, ctx, queryClient, addrs := suite.app, suite.ctx, suite.queryClient, suite.addrs
var (
req *authz.QueryGrantsRequest
expAuthorization authz.Authorization
)
testCases := []struct {
msg string
malleate func()
expPass bool
postTest func(res *authz.QueryGrantsResponse)
}{
{
"fail invalid granter addr",
func() {
req = &authz.QueryGrantsRequest{}
},
false,
func(res *authz.QueryGrantsResponse) {},
},
{
"fail invalid grantee addr",
func() {
req = &authz.QueryGrantsRequest{
Granter: addrs[0].String(),
}
},
false,
func(res *authz.QueryGrantsResponse) {},
},
{
"Success",
func() {
now := ctx.BlockHeader().Time
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
expAuthorization = &banktypes.SendAuthorization{SpendLimit: newCoins}
err := app.AuthzKeeper.SaveGrant(ctx, addrs[0], addrs[1], expAuthorization, now.Add(time.Hour))
suite.Require().NoError(err)
req = &authz.QueryGrantsRequest{
Granter: addrs[1].String(),
Grantee: addrs[0].String(),
}
},
true,
func(res *authz.QueryGrantsResponse) {
var auth authz.Authorization
suite.Require().Equal(1, len(res.Grants))
err := suite.app.InterfaceRegistry().UnpackAny(res.Grants[0].Authorization, &auth)
suite.Require().NoError(err)
suite.Require().NotNil(auth)
suite.Require().Equal(auth.String(), expAuthorization.String())
},
},
}
for _, testCase := range testCases {
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
testCase.malleate()
result, err := queryClient.Grants(gocontext.Background(), req)
if testCase.expPass {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
}
testCase.postTest(result)
})
}
}
func (suite *TestSuite) TestGRPCQueryGranterGrants() {
require := suite.Require()
app, ctx, queryClient, addrs := suite.app, suite.ctx, suite.queryClient, suite.addrs
testCases := []struct {
msg string
preRun func()
expError bool
request authz.QueryGranterGrantsRequest
numItems int
}{
{
"fail invalid granter addr",
func() {},
true,
authz.QueryGranterGrantsRequest{},
0,
},
{
"valid case, single authorization",
func() {
now := ctx.BlockHeader().Time
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
authorization := &banktypes.SendAuthorization{SpendLimit: newCoins}
err := app.AuthzKeeper.SaveGrant(ctx, addrs[1], addrs[0], authorization, now.Add(time.Hour))
require.NoError(err)
},
false,
authz.QueryGranterGrantsRequest{
Granter: addrs[0].String(),
},
1,
},
{
"valid case, multiple authorization",
func() {
now := ctx.BlockHeader().Time
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
authorization := &banktypes.SendAuthorization{SpendLimit: newCoins}
err := app.AuthzKeeper.SaveGrant(ctx, addrs[2], addrs[0], authorization, now.Add(time.Hour))
require.NoError(err)
},
false,
authz.QueryGranterGrantsRequest{
Granter: addrs[0].String(),
},
2,
},
{
"valid case, pagination",
func() {},
false,
authz.QueryGranterGrantsRequest{
Granter: addrs[0].String(),
Pagination: &query.PageRequest{
Limit: 1,
},
},
1,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
tc.preRun()
result, err := queryClient.GranterGrants(gocontext.Background(), &tc.request)
if tc.expError {
require.Error(err)
} else {
require.NoError(err)
require.Len(result.Grants, tc.numItems)
}
})
}
}