cosmos-sdk/x/feegrant/keeper/msg_server_test.go
atheeshp cc10614146
refactor: x/feegrant to stand alone (#14649)
Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com>
2023-01-25 16:39:54 +00:00

272 lines
6.8 KiB
Go

package keeper_test
import (
"time"
"cosmossdk.io/x/feegrant"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/golang/mock/gomock"
)
func (suite *KeeperTestSuite) TestGrantAllowance() {
ctx := suite.ctx.WithBlockTime(time.Now())
oneYear := ctx.BlockTime().AddDate(1, 0, 0)
yesterday := ctx.BlockTime().AddDate(0, 0, -1)
testCases := []struct {
name string
req func() *feegrant.MsgGrantAllowance
expectErr bool
errMsg string
}{
{
"invalid granter address",
func() *feegrant.MsgGrantAllowance {
any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{})
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: "invalid-granter",
Grantee: suite.addrs[1].String(),
Allowance: any,
}
},
true,
"decoding bech32 failed",
},
{
"invalid grantee address",
func() *feegrant.MsgGrantAllowance {
any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{})
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.addrs[0].String(),
Grantee: "invalid-grantee",
Allowance: any,
}
},
true,
"decoding bech32 failed",
},
{
"valid: grantee account doesn't exist",
func() *feegrant.MsgGrantAllowance {
grantee := "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5"
granteeAccAddr := types.MustAccAddressFromBech32(grantee)
any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &oneYear,
})
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), granteeAccAddr).Return(nil).AnyTimes()
acc := authtypes.NewBaseAccountWithAddress(granteeAccAddr)
suite.accountKeeper.EXPECT().NewAccountWithAddress(gomock.Any(), types.MustAccAddressFromBech32(grantee)).Return(acc).AnyTimes()
suite.accountKeeper.EXPECT().SetAccount(gomock.Any(), acc).Return()
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.addrs[0].String(),
Grantee: grantee,
Allowance: any,
}
},
false,
"",
},
{
"invalid: past expiry",
func() *feegrant.MsgGrantAllowance {
any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &yesterday,
})
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
Allowance: any,
}
},
true,
"expiration is before current block time",
},
{
"valid: basic fee allowance",
func() *feegrant.MsgGrantAllowance {
any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &oneYear,
})
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
Allowance: any,
}
},
false,
"",
},
{
"fail: fee allowance exists",
func() *feegrant.MsgGrantAllowance {
any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &oneYear,
})
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
Allowance: any,
}
},
true,
"fee allowance already exists",
},
{
"valid: periodic fee allowance",
func() *feegrant.MsgGrantAllowance {
any, err := codectypes.NewAnyWithValue(&feegrant.PeriodicAllowance{
Basic: feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &oneYear,
},
})
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.addrs[1].String(),
Grantee: suite.addrs[2].String(),
Allowance: any,
}
},
false,
"",
},
{
"error: fee allowance exists",
func() *feegrant.MsgGrantAllowance {
any, err := codectypes.NewAnyWithValue(&feegrant.PeriodicAllowance{
Basic: feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &oneYear,
},
})
suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.addrs[1].String(),
Grantee: suite.addrs[2].String(),
Allowance: any,
}
},
true,
"fee allowance already exists",
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
_, err := suite.msgSrvr.GrantAllowance(ctx, tc.req())
if tc.expectErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.errMsg)
}
})
}
}
func (suite *KeeperTestSuite) TestRevokeAllowance() {
oneYear := suite.ctx.BlockTime().AddDate(1, 0, 0)
testCases := []struct {
name string
request *feegrant.MsgRevokeAllowance
preRun func()
expectErr bool
errMsg string
}{
{
"error: invalid granter",
&feegrant.MsgRevokeAllowance{
Granter: "invalid-granter",
Grantee: suite.addrs[1].String(),
},
func() {},
true,
"decoding bech32 failed",
},
{
"error: invalid grantee",
&feegrant.MsgRevokeAllowance{
Granter: suite.addrs[0].String(),
Grantee: "invalid-grantee",
},
func() {},
true,
"decoding bech32 failed",
},
{
"error: fee allowance not found",
&feegrant.MsgRevokeAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
},
func() {},
true,
"fee-grant not found",
},
{
"success: revoke fee allowance",
&feegrant.MsgRevokeAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
},
func() {
// removing fee allowance from previous tests if exists
suite.msgSrvr.RevokeAllowance(suite.ctx, &feegrant.MsgRevokeAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
})
any, err := codectypes.NewAnyWithValue(&feegrant.PeriodicAllowance{
Basic: feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &oneYear,
},
})
suite.Require().NoError(err)
req := &feegrant.MsgGrantAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
Allowance: any,
}
_, err = suite.msgSrvr.GrantAllowance(suite.ctx, req)
suite.Require().NoError(err)
},
false,
"",
},
{
"error: check fee allowance revoked",
&feegrant.MsgRevokeAllowance{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
},
func() {},
true,
"fee-grant not found",
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.preRun()
_, err := suite.msgSrvr.RevokeAllowance(suite.ctx, tc.request)
if tc.expectErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.errMsg)
}
})
}
}