cosmos-sdk/x/feegrant/keeper/msg_server_test.go
likhita-809 3495691493
feat: Add server implementation of Group module (#10570)
<!--
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: #9897
Closes: #9905

Adds server implementation of Group module and wires it up in simapp

---

### 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
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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-12-10 11:02:11 +00:00

223 lines
5.3 KiB
Go

package keeper_test
import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
)
func (suite *KeeperTestSuite) TestGrantAllowance() {
oneYear := suite.sdkCtx.BlockTime().AddDate(1, 0, 0)
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: 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(suite.ctx, tc.req())
if tc.expectErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.errMsg)
}
})
}
}
func (suite *KeeperTestSuite) TestRevokeAllowance() {
oneYear := suite.sdkCtx.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)
}
})
}
}