chore: (x/feegrant) add missing test scenarios (#14018)

* add missing test scenarios

* review changes
This commit is contained in:
atheeshp 2022-11-30 13:16:01 +05:30 committed by GitHub
parent ac6b19df21
commit 4a9062122b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,19 @@
package keeper_test
import (
"time"
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/cosmos/cosmos-sdk/x/feegrant"
"github.com/golang/mock/gomock"
)
func (suite *KeeperTestSuite) TestGrantAllowance() {
oneYear := suite.ctx.BlockTime().AddDate(1, 0, 0)
ctx := suite.ctx.WithBlockTime(time.Now())
oneYear := ctx.BlockTime().AddDate(1, 0, 0)
yesterday := ctx.BlockTime().AddDate(0, 0, -1)
testCases := []struct {
name string
@ -42,6 +49,49 @@ func (suite *KeeperTestSuite) TestGrantAllowance() {
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 {
@ -117,7 +167,7 @@ func (suite *KeeperTestSuite) TestGrantAllowance() {
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
_, err := suite.msgSrvr.GrantAllowance(suite.ctx, tc.req())
_, err := suite.msgSrvr.GrantAllowance(ctx, tc.req())
if tc.expectErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.errMsg)