Skip to content

Commit

Permalink
chore: (x/feegrant) add missing test scenarios (cosmos#14018)
Browse files Browse the repository at this point in the history
* add missing test scenarios

* review changes
  • Loading branch information
atheeshp authored Nov 30, 2022
1 parent ac6b19d commit 4a90621
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions x/feegrant/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4a90621

Please sign in to comment.