2021-01-25 08:41:30 -08:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
gocontext "context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-05-14 15:18:59 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-01-25 08:41:30 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-10-11 06:30:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
2021-05-06 11:23:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
2021-02-19 22:57:57 -08:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2021-01-25 08:41:30 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *TestSuite) TestGRPCQueryAuthorization() {
|
|
|
|
app, ctx, queryClient, addrs := suite.app, suite.ctx, suite.queryClient, suite.addrs
|
|
|
|
var (
|
2021-05-06 11:23:48 -07:00
|
|
|
req *authz.QueryGrantsRequest
|
|
|
|
expAuthorization authz.Authorization
|
2021-01-25 08:41:30 -08:00
|
|
|
)
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
2021-05-06 11:23:48 -07:00
|
|
|
malleate func(require *require.Assertions)
|
|
|
|
expError string
|
|
|
|
postTest func(require *require.Assertions, res *authz.QueryGrantsResponse)
|
2021-01-25 08:41:30 -08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail invalid granter addr",
|
2021-05-06 11:23:48 -07:00
|
|
|
func(require *require.Assertions) {
|
|
|
|
req = &authz.QueryGrantsRequest{}
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
2021-05-06 11:23:48 -07:00
|
|
|
"empty address string is not allowed",
|
|
|
|
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail invalid grantee addr",
|
2021-05-06 11:23:48 -07:00
|
|
|
func(require *require.Assertions) {
|
|
|
|
req = &authz.QueryGrantsRequest{
|
2021-01-25 08:41:30 -08:00
|
|
|
Granter: addrs[0].String(),
|
|
|
|
}
|
|
|
|
},
|
2021-05-06 11:23:48 -07:00
|
|
|
"empty address string is not allowed",
|
|
|
|
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail invalid msg-type",
|
2021-05-06 11:23:48 -07:00
|
|
|
func(require *require.Assertions) {
|
|
|
|
req = &authz.QueryGrantsRequest{
|
|
|
|
Granter: addrs[0].String(),
|
|
|
|
Grantee: addrs[1].String(),
|
|
|
|
MsgTypeUrl: "unknown",
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|
|
|
|
},
|
2021-05-06 11:23:48 -07:00
|
|
|
"no authorization found for unknown type",
|
|
|
|
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Success",
|
2021-05-06 11:23:48 -07:00
|
|
|
func(require *require.Assertions) {
|
2021-01-25 08:41:30 -08:00
|
|
|
now := ctx.BlockHeader().Time
|
|
|
|
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
|
2021-02-19 22:57:57 -08:00
|
|
|
expAuthorization = &banktypes.SendAuthorization{SpendLimit: newCoins}
|
2021-05-06 11:23:48 -07:00
|
|
|
err := app.AuthzKeeper.SaveGrant(ctx, addrs[0], addrs[1], expAuthorization, now.Add(time.Hour))
|
|
|
|
require.NoError(err)
|
|
|
|
req = &authz.QueryGrantsRequest{
|
2021-01-25 08:41:30 -08:00
|
|
|
Granter: addrs[1].String(),
|
|
|
|
Grantee: addrs[0].String(),
|
2021-05-06 11:23:48 -07:00
|
|
|
MsgTypeUrl: expAuthorization.MsgTypeURL(),
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|
|
|
|
},
|
2021-05-06 11:23:48 -07:00
|
|
|
"",
|
|
|
|
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())
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-05-06 11:23:48 -07:00
|
|
|
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)
|
2021-01-25 08:41:30 -08:00
|
|
|
} else {
|
2021-05-06 11:23:48 -07:00
|
|
|
require.Error(err)
|
|
|
|
require.Contains(err.Error(), tc.expError)
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|
2021-05-06 11:23:48 -07:00
|
|
|
tc.postTest(require, result)
|
2021-01-25 08:41:30 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TestSuite) TestGRPCQueryAuthorizations() {
|
|
|
|
app, ctx, queryClient, addrs := suite.app, suite.ctx, suite.queryClient, suite.addrs
|
|
|
|
var (
|
2021-05-06 11:23:48 -07:00
|
|
|
req *authz.QueryGrantsRequest
|
|
|
|
expAuthorization authz.Authorization
|
2021-01-25 08:41:30 -08:00
|
|
|
)
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
2021-05-06 11:23:48 -07:00
|
|
|
postTest func(res *authz.QueryGrantsResponse)
|
2021-01-25 08:41:30 -08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail invalid granter addr",
|
|
|
|
func() {
|
2021-05-06 11:23:48 -07:00
|
|
|
req = &authz.QueryGrantsRequest{}
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
|
|
|
false,
|
2021-05-06 11:23:48 -07:00
|
|
|
func(res *authz.QueryGrantsResponse) {},
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail invalid grantee addr",
|
|
|
|
func() {
|
2021-05-06 11:23:48 -07:00
|
|
|
req = &authz.QueryGrantsRequest{
|
2021-01-25 08:41:30 -08:00
|
|
|
Granter: addrs[0].String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
2021-05-06 11:23:48 -07:00
|
|
|
func(res *authz.QueryGrantsResponse) {},
|
2021-01-25 08:41:30 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Success",
|
|
|
|
func() {
|
|
|
|
now := ctx.BlockHeader().Time
|
|
|
|
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
|
2021-02-19 22:57:57 -08:00
|
|
|
expAuthorization = &banktypes.SendAuthorization{SpendLimit: newCoins}
|
2021-05-06 11:23:48 -07:00
|
|
|
err := app.AuthzKeeper.SaveGrant(ctx, addrs[0], addrs[1], expAuthorization, now.Add(time.Hour))
|
2021-01-25 08:41:30 -08:00
|
|
|
suite.Require().NoError(err)
|
2021-05-06 11:23:48 -07:00
|
|
|
req = &authz.QueryGrantsRequest{
|
2021-01-25 08:41:30 -08:00
|
|
|
Granter: addrs[1].String(),
|
|
|
|
Grantee: addrs[0].String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
2021-05-06 11:23:48 -07:00
|
|
|
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)
|
2021-01-25 08:41:30 -08:00
|
|
|
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()
|
2021-05-06 11:23:48 -07:00
|
|
|
result, err := queryClient.Grants(gocontext.Background(), req)
|
2021-01-25 08:41:30 -08:00
|
|
|
if testCase.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
testCase.postTest(result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 06:30:12 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 07:56:44 -08:00
|
|
|
|
|
|
|
func (suite *TestSuite) TestGRPCQueryGranteeGrants() {
|
|
|
|
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.QueryGranteeGrantsRequest
|
|
|
|
numItems int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail invalid granter addr",
|
|
|
|
func() {},
|
|
|
|
true,
|
|
|
|
authz.QueryGranteeGrantsRequest{},
|
|
|
|
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[0], addrs[1], authorization, now.Add(time.Hour))
|
|
|
|
require.NoError(err)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
authz.QueryGranteeGrantsRequest{
|
|
|
|
Grantee: 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[0], addrs[2], authorization, now.Add(time.Hour))
|
|
|
|
require.NoError(err)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
authz.QueryGranteeGrantsRequest{
|
|
|
|
Grantee: addrs[0].String(),
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid case, pagination",
|
|
|
|
func() {},
|
|
|
|
false,
|
|
|
|
authz.QueryGranteeGrantsRequest{
|
|
|
|
Grantee: 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.GranteeGrants(gocontext.Background(), &tc.request)
|
|
|
|
if tc.expError {
|
|
|
|
require.Error(err)
|
|
|
|
} else {
|
|
|
|
require.NoError(err)
|
|
|
|
require.Len(result.Grants, tc.numItems)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|