Skip to content

Commit

Permalink
move test settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ulbqb committed Feb 5, 2024
1 parent dcdcfa2 commit 9643fc7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 84 deletions.
90 changes: 8 additions & 82 deletions x/stakingplus/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package keeper_test

import (
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/Finschia/finschia-sdk/x/stakingplus"
"github.com/Finschia/finschia-sdk/x/stakingplus/keeper"
"github.com/Finschia/finschia-sdk/x/stakingplus/testutil"
)
Expand All @@ -31,21 +25,17 @@ type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context

app *runtime.App
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
msgServer stakingtypes.MsgServer

stranger sdk.AccAddress
grantee sdk.AccAddress

balance math.Int
app *runtime.App
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
foundationKeeper *testutil.MockFoundationKeeper
stakingKeeper *stakingkeeper.Keeper
msgServer stakingtypes.MsgServer
}

func (s *KeeperTestSuite) SetupTest() {
ctrl := gomock.NewController(s.T())
foundationKeeper := testutil.NewMockFoundationKeeper(ctrl)
s.foundationKeeper = testutil.NewMockFoundationKeeper(ctrl)

app, err := simtestutil.Setup(
depinject.Configs(
Expand All @@ -61,73 +51,9 @@ func (s *KeeperTestSuite) SetupTest() {
s.app = app
s.ctx = s.app.BaseApp.NewContext(false)

s.msgServer = keeper.NewMsgServerImpl(s.stakingKeeper, foundationKeeper)

createAddress := func() sdk.AccAddress {
return sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
}

s.stranger = createAddress()
s.grantee = createAddress()

s.balance = math.NewInt(1000000)
holders := []sdk.AccAddress{
s.stranger,
s.grantee,
}
for _, holder := range holders {
amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance))

// using minttypes here introduces dependency on x/mint
// the work around would be registering a new module account on this suite
// because x/bank already has dependency on x/mint, and we must have dependency
// on x/bank, it's OK to use x/mint here.
minterName := minttypes.ModuleName
err := s.bankKeeper.MintCoins(s.ctx, minterName, amount)
s.Require().NoError(err)

minter := s.accountKeeper.GetModuleAccount(s.ctx, minterName).GetAddress()
err = s.bankKeeper.SendCoins(s.ctx, minter, holder, amount)
s.Require().NoError(err)
}

// approve Msg/CreateValidator to grantee
foundationKeeper.
EXPECT().
Accept(gomock.Any(), s.grantee, NewCreateValidatorAuthorizationMatcher(s.grantee)).
Return(nil)
foundationKeeper.
EXPECT().
Accept(gomock.Any(), gomock.Any(), gomock.Any()).
Return(sdkerrors.ErrUnauthorized)
s.msgServer = keeper.NewMsgServerImpl(s.stakingKeeper, s.foundationKeeper)
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

type CreateValidatorAuthorizationMatcher struct {
authz stakingplus.CreateValidatorAuthorization
}

func NewCreateValidatorAuthorizationMatcher(grantee sdk.AccAddress) *CreateValidatorAuthorizationMatcher {
return &CreateValidatorAuthorizationMatcher{
authz: stakingplus.CreateValidatorAuthorization{
ValidatorAddress: sdk.ValAddress(grantee).String(),
},
}
}

func (c CreateValidatorAuthorizationMatcher) Matches(x interface{}) bool {
msg, ok := x.(sdk.Msg)
if !ok {
return false
}

resp, err := c.authz.Accept(sdk.Context{}, msg)
return resp.Accept && (err == nil)
}

func (c CreateValidatorAuthorizationMatcher) String() string {
return fmt.Sprintf("grants %s to %s", c.authz.MsgTypeURL(), c.authz.ValidatorAddress)
}
70 changes: 68 additions & 2 deletions x/stakingplus/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,62 @@
package keeper_test

import (
"fmt"

"cosmossdk.io/math"

"github.com/Finschia/finschia-sdk/x/stakingplus"
"github.com/cometbft/cometbft/crypto/secp256k1"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/golang/mock/gomock"
)

func (s *KeeperTestSuite) TestMsgCreateValidator() {
stranger := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
grantee := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
balance := math.NewInt(1000000)

for _, holder := range []sdk.AccAddress{stranger, grantee} {
amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, balance))

// using minttypes here introduces dependency on x/mint
// the work around would be registering a new module account on this suite
// because x/bank already has dependency on x/mint, and we must have dependency
// on x/bank, it's OK to use x/mint here.
minterName := minttypes.ModuleName
err := s.bankKeeper.MintCoins(s.ctx, minterName, amount)
s.Require().NoError(err)

minter := s.accountKeeper.GetModuleAccount(s.ctx, minterName).GetAddress()
err = s.bankKeeper.SendCoins(s.ctx, minter, holder, amount)
s.Require().NoError(err)
}

// approve Msg/CreateValidator to grantee
s.foundationKeeper.
EXPECT().
Accept(gomock.Any(), grantee, NewCreateValidatorAuthorizationMatcher(grantee)).
Return(nil)
s.foundationKeeper.
EXPECT().
Accept(gomock.Any(), gomock.Any(), gomock.Any()).
Return(sdkerrors.ErrUnauthorized)

testCases := map[string]struct {
delegator sdk.AccAddress
valid bool
}{
"valid request": {
delegator: s.grantee,
delegator: grantee,
valid: true,
},
"no grant found": {
delegator: s.stranger,
delegator: stranger,
},
}

Expand Down Expand Up @@ -50,3 +88,31 @@ func (s *KeeperTestSuite) TestMsgCreateValidator() {
})
}
}

var _ gomock.Matcher = (*CreateValidatorAuthorizationMatcher)(nil)

type CreateValidatorAuthorizationMatcher struct {
authz stakingplus.CreateValidatorAuthorization
}

func NewCreateValidatorAuthorizationMatcher(grantee sdk.AccAddress) *CreateValidatorAuthorizationMatcher {
return &CreateValidatorAuthorizationMatcher{
authz: stakingplus.CreateValidatorAuthorization{
ValidatorAddress: sdk.ValAddress(grantee).String(),
},
}
}

func (c CreateValidatorAuthorizationMatcher) Matches(x interface{}) bool {
msg, ok := x.(sdk.Msg)
if !ok {
return false
}

resp, err := c.authz.Accept(sdk.Context{}, msg)
return resp.Accept && (err == nil)
}

func (c CreateValidatorAuthorizationMatcher) String() string {
return fmt.Sprintf("grants %s to %s", c.authz.MsgTypeURL(), c.authz.ValidatorAddress)
}

0 comments on commit 9643fc7

Please sign in to comment.