<!-- 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 **Step1** from #9690 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> - Add auth keeper methods and gRPC queries: - the `NewAccountKeeper` should take a `string bech32Prefix` argument - ` auth AccountKeeper` implements `Codec` interface that contains 2 methods: `ConvertAddressStringToBytes` and `ConvertAddressBytesToString` - Add the 3 following gRPC queries: - Get bech32 prefix - `AddressStringToBytes` (converts `AccountAddr` string to `AccountAddr` bytes) - `AddressBytesToString` ( converts `AccountAddr` bytes to `AccountAddr` str) - Add the corresponding keeper methods - Add tests --- ### 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... - [ ] 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 - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] 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) - [x] 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)
378 lines
8.4 KiB
Go
378 lines
8.4 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"fmt"
|
|
"context"
|
|
"bytes"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
)
|
|
|
|
const addrStr = "cosmos13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv"
|
|
var addrBytes = []byte{0x8e, 0x22, 0xda, 0xb8, 0xa, 0x5a, 0x94, 0xdf, 0xbd, 0xb0, 0x58, 0xfa, 0x93, 0xcb, 0x11, 0x49, 0x5e, 0xac, 0xc5, 0x30}
|
|
|
|
func (suite *KeeperTestSuite) TestGRPCQueryAccounts() {
|
|
var (
|
|
req *types.QueryAccountsRequest
|
|
)
|
|
_, _, first := testdata.KeyTestPubAddr()
|
|
_, _, second := testdata.KeyTestPubAddr()
|
|
|
|
testCases := []struct {
|
|
msg string
|
|
malleate func()
|
|
expPass bool
|
|
posttests func(res *types.QueryAccountsResponse)
|
|
}{
|
|
{
|
|
"success",
|
|
func() {
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx,
|
|
suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, first))
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx,
|
|
suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, second))
|
|
req = &types.QueryAccountsRequest{}
|
|
},
|
|
true,
|
|
func(res *types.QueryAccountsResponse) {
|
|
for _, acc := range res.Accounts {
|
|
var account types.AccountI
|
|
err := suite.app.InterfaceRegistry().UnpackAny(acc, &account)
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().True(
|
|
first.Equals(account.GetAddress()) || second.Equals(account.GetAddress()))
|
|
}
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
suite.SetupTest() // reset
|
|
|
|
tc.malleate()
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.Accounts(ctx, req)
|
|
|
|
if tc.expPass {
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(res)
|
|
} else {
|
|
suite.Require().Error(err)
|
|
suite.Require().Nil(res)
|
|
}
|
|
|
|
tc.posttests(res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestGRPCQueryAccount() {
|
|
var (
|
|
req *types.QueryAccountRequest
|
|
)
|
|
_, _, addr := testdata.KeyTestPubAddr()
|
|
|
|
testCases := []struct {
|
|
msg string
|
|
malleate func()
|
|
expPass bool
|
|
posttests func(res *types.QueryAccountResponse)
|
|
}{
|
|
{
|
|
"empty request",
|
|
func() {
|
|
req = &types.QueryAccountRequest{}
|
|
},
|
|
false,
|
|
func(res *types.QueryAccountResponse) {},
|
|
},
|
|
{
|
|
"invalid request",
|
|
func() {
|
|
req = &types.QueryAccountRequest{Address: ""}
|
|
},
|
|
false,
|
|
func(res *types.QueryAccountResponse) {},
|
|
},
|
|
{
|
|
"invalid request with empty byte array",
|
|
func() {
|
|
req = &types.QueryAccountRequest{Address: ""}
|
|
},
|
|
false,
|
|
func(res *types.QueryAccountResponse) {},
|
|
},
|
|
{
|
|
"account not found",
|
|
func() {
|
|
req = &types.QueryAccountRequest{Address: addr.String()}
|
|
},
|
|
false,
|
|
func(res *types.QueryAccountResponse) {},
|
|
},
|
|
{
|
|
"success",
|
|
func() {
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx,
|
|
suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr))
|
|
req = &types.QueryAccountRequest{Address: addr.String()}
|
|
},
|
|
true,
|
|
func(res *types.QueryAccountResponse) {
|
|
var newAccount types.AccountI
|
|
err := suite.app.InterfaceRegistry().UnpackAny(res.Account, &newAccount)
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(newAccount)
|
|
suite.Require().True(addr.Equals(newAccount.GetAddress()))
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
suite.SetupTest() // reset
|
|
|
|
tc.malleate()
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.Account(ctx, req)
|
|
|
|
if tc.expPass {
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(res)
|
|
} else {
|
|
suite.Require().Error(err)
|
|
suite.Require().Nil(res)
|
|
}
|
|
|
|
tc.posttests(res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestGRPCQueryParameters() {
|
|
var (
|
|
req *types.QueryParamsRequest
|
|
expParams types.Params
|
|
)
|
|
|
|
testCases := []struct {
|
|
msg string
|
|
malleate func()
|
|
expPass bool
|
|
}{
|
|
{
|
|
"success",
|
|
func() {
|
|
req = &types.QueryParamsRequest{}
|
|
expParams = suite.app.AccountKeeper.GetParams(suite.ctx)
|
|
},
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
suite.SetupTest() // reset
|
|
|
|
tc.malleate()
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.Params(ctx, req)
|
|
|
|
if tc.expPass {
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(res)
|
|
suite.Require().Equal(expParams, res.Params)
|
|
} else {
|
|
suite.Require().Error(err)
|
|
suite.Require().Nil(res)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() {
|
|
var (
|
|
req *types.QueryModuleAccountsRequest
|
|
)
|
|
|
|
testCases := []struct {
|
|
msg string
|
|
malleate func()
|
|
expPass bool
|
|
posttests func(res *types.QueryModuleAccountsResponse)
|
|
}{
|
|
{
|
|
"success",
|
|
func() {
|
|
req = &types.QueryModuleAccountsRequest{}
|
|
},
|
|
true,
|
|
func(res *types.QueryModuleAccountsResponse) {
|
|
var mintModuleExists = false
|
|
for _, acc := range res.Accounts {
|
|
var account types.AccountI
|
|
err := suite.app.InterfaceRegistry().UnpackAny(acc, &account)
|
|
suite.Require().NoError(err)
|
|
|
|
moduleAccount, ok := account.(types.ModuleAccountI)
|
|
|
|
suite.Require().True(ok)
|
|
if moduleAccount.GetName() == "mint" {
|
|
mintModuleExists = true
|
|
}
|
|
}
|
|
suite.Require().True(mintModuleExists)
|
|
},
|
|
},
|
|
{
|
|
"invalid module name",
|
|
func() {
|
|
req = &types.QueryModuleAccountsRequest{}
|
|
},
|
|
true,
|
|
func(res *types.QueryModuleAccountsResponse) {
|
|
var mintModuleExists = false
|
|
for _, acc := range res.Accounts {
|
|
var account types.AccountI
|
|
err := suite.app.InterfaceRegistry().UnpackAny(acc, &account)
|
|
suite.Require().NoError(err)
|
|
|
|
moduleAccount, ok := account.(types.ModuleAccountI)
|
|
|
|
suite.Require().True(ok)
|
|
if moduleAccount.GetName() == "falseCase" {
|
|
mintModuleExists = true
|
|
}
|
|
}
|
|
suite.Require().False(mintModuleExists)
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
suite.SetupTest() // reset
|
|
|
|
tc.malleate()
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.ModuleAccounts(ctx, req)
|
|
|
|
if tc.expPass {
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(res)
|
|
} else {
|
|
suite.Require().Error(err)
|
|
suite.Require().Nil(res)
|
|
}
|
|
|
|
tc.posttests(res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestBech32Prefix() {
|
|
suite.SetupTest() // reset
|
|
req := &types.Bech32PrefixRequest{}
|
|
res, err := suite.queryClient.Bech32Prefix(context.Background(), req)
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(res)
|
|
suite.Require().Equal(sdk.Bech32MainPrefix, res.Bech32Prefix)
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestAddressBytesToString() {
|
|
testCases := []struct {
|
|
msg string
|
|
req *types.AddressBytesToStringRequest
|
|
expPass bool
|
|
}{
|
|
{
|
|
"success",
|
|
&types.AddressBytesToStringRequest{AddressBytes: addrBytes},
|
|
true,
|
|
},
|
|
{
|
|
"request is empty",
|
|
&types.AddressBytesToStringRequest{},
|
|
false,
|
|
},
|
|
{
|
|
"empty account address in request",
|
|
&types.AddressBytesToStringRequest{AddressBytes: []byte{}},
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
suite.SetupTest() // reset
|
|
|
|
res, err := suite.queryClient.AddressBytesToString(context.Background(), tc.req)
|
|
|
|
if tc.expPass {
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(res)
|
|
suite.Require().Equal(res.AddressString, addrStr)
|
|
} else {
|
|
suite.Require().Error(err)
|
|
suite.Require().Nil(res)
|
|
}
|
|
|
|
})
|
|
}
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestAddressStringToBytes() {
|
|
testCases := []struct {
|
|
msg string
|
|
req *types.AddressStringToBytesRequest
|
|
expPass bool
|
|
}{
|
|
{
|
|
"success",
|
|
&types.AddressStringToBytesRequest{AddressString: addrStr},
|
|
true,
|
|
},
|
|
{
|
|
"request is empty",
|
|
&types.AddressStringToBytesRequest{},
|
|
false,
|
|
},
|
|
{
|
|
"AddressString field in request is empty",
|
|
&types.AddressStringToBytesRequest{AddressString: ""},
|
|
false,
|
|
},
|
|
{
|
|
"address prefix is incorrect",
|
|
&types.AddressStringToBytesRequest{AddressString: "regen13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv" },
|
|
false,
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
suite.SetupTest() // reset
|
|
|
|
res, err := suite.queryClient.AddressStringToBytes(context.Background(), tc.req)
|
|
|
|
if tc.expPass {
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(res)
|
|
suite.Require().True(bytes.Equal(res.AddressBytes, addrBytes))
|
|
} else {
|
|
suite.Require().Error(err)
|
|
suite.Require().Nil(res)
|
|
}
|
|
|
|
})
|
|
}
|
|
} |