From e82200253011a0ea6fb0ada656e00b57f3a8e28d Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Wed, 13 Jul 2022 16:04:17 -0300 Subject: [PATCH] refactor: use mocks for x/capability testing (#12553) ## Description Also defined `stakingModuleName` and `bankModuleName` to avoid depending on those modules just for a couple of strings Closes: #12498 --- ### 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/main/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/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] 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) --- x/capability/keeper/keeper_test.go | 52 ++++++++++++++++-------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/x/capability/keeper/keeper_test.go b/x/capability/keeper/keeper_test.go index 0d6a5f0655..c859032013 100644 --- a/x/capability/keeper/keeper_test.go +++ b/x/capability/keeper/keeper_test.go @@ -5,15 +5,18 @@ import ( "testing" "github.com/stretchr/testify/suite" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/capability" "github.com/cosmos/cosmos-sdk/x/capability/keeper" - "github.com/cosmos/cosmos-sdk/x/capability/testutil" "github.com/cosmos/cosmos-sdk/x/capability/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +var ( + stakingModuleName string = "staking" + bankModuleName string = "bank" ) type KeeperTestSuite struct { @@ -24,16 +27,15 @@ type KeeperTestSuite struct { } func (suite *KeeperTestSuite) SetupTest() { - app, err := simtestutil.Setup(testutil.AppConfig, - &suite.keeper, - ) - suite.Require().NoError(err) - - suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1}) + key := sdk.NewKVStoreKey(types.StoreKey) + testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test")) + suite.ctx = testCtx.Ctx + encCfg := moduletestutil.MakeTestEncodingConfig(capability.AppModuleBasic{}) + suite.keeper = keeper.NewKeeper(encCfg.Codec, key, key) } func (suite *KeeperTestSuite) TestSeal() { - sk := suite.keeper.ScopeToModule(banktypes.ModuleName) + sk := suite.keeper.ScopeToModule(bankModuleName) suite.Require().Panics(func() { suite.keeper.ScopeToModule(" ") }) @@ -67,12 +69,12 @@ func (suite *KeeperTestSuite) TestSeal() { }) suite.Require().Panics(func() { - _ = suite.keeper.ScopeToModule(stakingtypes.ModuleName) + _ = suite.keeper.ScopeToModule(stakingModuleName) }) } func (suite *KeeperTestSuite) TestNewCapability() { - sk := suite.keeper.ScopeToModule(banktypes.ModuleName) + sk := suite.keeper.ScopeToModule(bankModuleName) got, ok := sk.GetCapability(suite.ctx, "transfer") suite.Require().False(ok) @@ -111,8 +113,8 @@ func (suite *KeeperTestSuite) TestNewCapability() { } func (suite *KeeperTestSuite) TestAuthenticateCapability() { - sk1 := suite.keeper.ScopeToModule(banktypes.ModuleName) - sk2 := suite.keeper.ScopeToModule(stakingtypes.ModuleName) + sk1 := suite.keeper.ScopeToModule(bankModuleName) + sk2 := suite.keeper.ScopeToModule(stakingModuleName) cap1, err := sk1.NewCapability(suite.ctx, "transfer") suite.Require().NoError(err) @@ -150,8 +152,8 @@ func (suite *KeeperTestSuite) TestAuthenticateCapability() { } func (suite *KeeperTestSuite) TestClaimCapability() { - sk1 := suite.keeper.ScopeToModule(banktypes.ModuleName) - sk2 := suite.keeper.ScopeToModule(stakingtypes.ModuleName) + sk1 := suite.keeper.ScopeToModule(bankModuleName) + sk2 := suite.keeper.ScopeToModule(stakingModuleName) sk3 := suite.keeper.ScopeToModule("foo") cap, err := sk1.NewCapability(suite.ctx, "transfer") @@ -174,8 +176,8 @@ func (suite *KeeperTestSuite) TestClaimCapability() { } func (suite *KeeperTestSuite) TestGetOwners() { - sk1 := suite.keeper.ScopeToModule(banktypes.ModuleName) - sk2 := suite.keeper.ScopeToModule(stakingtypes.ModuleName) + sk1 := suite.keeper.ScopeToModule(bankModuleName) + sk2 := suite.keeper.ScopeToModule(stakingModuleName) sk3 := suite.keeper.ScopeToModule("foo") sks := []keeper.ScopedKeeper{sk1, sk2, sk3} @@ -187,7 +189,7 @@ func (suite *KeeperTestSuite) TestGetOwners() { suite.Require().NoError(sk2.ClaimCapability(suite.ctx, cap, "transfer")) suite.Require().NoError(sk3.ClaimCapability(suite.ctx, cap, "transfer")) - expectedOrder := []string{banktypes.ModuleName, "foo", stakingtypes.ModuleName} + expectedOrder := []string{bankModuleName, "foo", stakingModuleName} // Ensure all scoped keepers can get owners for _, sk := range sks { owners, ok := sk.GetOwners(suite.ctx, "transfer") @@ -214,7 +216,7 @@ func (suite *KeeperTestSuite) TestGetOwners() { suite.Require().Nil(err, "could not release capability") // new expected order and scoped capabilities - expectedOrder = []string{banktypes.ModuleName, stakingtypes.ModuleName} + expectedOrder = []string{bankModuleName, stakingModuleName} sks = []keeper.ScopedKeeper{sk1, sk2} // Ensure all scoped keepers can get owners @@ -242,8 +244,8 @@ func (suite *KeeperTestSuite) TestGetOwners() { } func (suite *KeeperTestSuite) TestReleaseCapability() { - sk1 := suite.keeper.ScopeToModule(banktypes.ModuleName) - sk2 := suite.keeper.ScopeToModule(stakingtypes.ModuleName) + sk1 := suite.keeper.ScopeToModule(bankModuleName) + sk2 := suite.keeper.ScopeToModule(stakingModuleName) cap1, err := sk1.NewCapability(suite.ctx, "transfer") suite.Require().NoError(err) @@ -271,7 +273,7 @@ func (suite *KeeperTestSuite) TestReleaseCapability() { } func (suite KeeperTestSuite) TestRevertCapability() { - sk := suite.keeper.ScopeToModule(banktypes.ModuleName) + sk := suite.keeper.ScopeToModule(bankModuleName) ms := suite.ctx.MultiStore()