github.com/Finschia/finschia-sdk@v0.48.1/x/stakingplus/keeper/keeper_test.go (about) 1 package keeper_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/golang/mock/gomock" 8 "github.com/stretchr/testify/suite" 9 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 10 11 "github.com/Finschia/finschia-sdk/crypto/keys/secp256k1" 12 "github.com/Finschia/finschia-sdk/simapp" 13 sdk "github.com/Finschia/finschia-sdk/types" 14 sdkerrors "github.com/Finschia/finschia-sdk/types/errors" 15 minttypes "github.com/Finschia/finschia-sdk/x/mint/types" 16 stakingkeeper "github.com/Finschia/finschia-sdk/x/staking/keeper" 17 stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types" 18 "github.com/Finschia/finschia-sdk/x/stakingplus" 19 "github.com/Finschia/finschia-sdk/x/stakingplus/keeper" 20 "github.com/Finschia/finschia-sdk/x/stakingplus/testutil" 21 ) 22 23 type KeeperTestSuite struct { 24 suite.Suite 25 ctx sdk.Context 26 27 app *simapp.SimApp 28 keeper stakingkeeper.Keeper 29 msgServer stakingtypes.MsgServer 30 31 stranger sdk.AccAddress 32 grantee sdk.AccAddress 33 34 balance sdk.Int 35 } 36 37 func (s *KeeperTestSuite) SetupTest() { 38 ctrl := gomock.NewController(s.T()) 39 foundationKeeper := testutil.NewMockFoundationKeeper(ctrl) 40 41 checkTx := false 42 s.app = simapp.Setup(checkTx) 43 s.ctx = s.app.BaseApp.NewContext(checkTx, tmproto.Header{}) 44 s.keeper = s.app.StakingKeeper 45 46 s.msgServer = keeper.NewMsgServerImpl(s.keeper, foundationKeeper) 47 48 createAddress := func() sdk.AccAddress { 49 return sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) 50 } 51 52 s.stranger = createAddress() 53 s.grantee = createAddress() 54 55 s.balance = sdk.NewInt(1000000) 56 holders := []sdk.AccAddress{ 57 s.stranger, 58 s.grantee, 59 } 60 for _, holder := range holders { 61 amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)) 62 63 // using minttypes here introduces dependency on x/mint 64 // the work around would be registering a new module account on this suite 65 // because x/bank already has dependency on x/mint, and we must have dependency 66 // on x/bank, it's OK to use x/mint here. 67 minterName := minttypes.ModuleName 68 err := s.app.BankKeeper.MintCoins(s.ctx, minterName, amount) 69 s.Require().NoError(err) 70 71 minter := s.app.AccountKeeper.GetModuleAccount(s.ctx, minterName).GetAddress() 72 err = s.app.BankKeeper.SendCoins(s.ctx, minter, holder, amount) 73 s.Require().NoError(err) 74 } 75 76 // approve Msg/CreateValidator to grantee 77 foundationKeeper. 78 EXPECT(). 79 Accept(gomock.Any(), s.grantee, NewCreateValidatorAuthorizationMatcher(s.grantee)). 80 Return(nil) 81 foundationKeeper. 82 EXPECT(). 83 Accept(gomock.Any(), gomock.Any(), gomock.Any()). 84 Return(sdkerrors.ErrUnauthorized) 85 } 86 87 func TestKeeperTestSuite(t *testing.T) { 88 suite.Run(t, new(KeeperTestSuite)) 89 } 90 91 type CreateValidatorAuthorizationMatcher struct { 92 authz stakingplus.CreateValidatorAuthorization 93 } 94 95 func NewCreateValidatorAuthorizationMatcher(grantee sdk.AccAddress) *CreateValidatorAuthorizationMatcher { 96 return &CreateValidatorAuthorizationMatcher{ 97 authz: stakingplus.CreateValidatorAuthorization{ 98 ValidatorAddress: sdk.ValAddress(grantee).String(), 99 }, 100 } 101 } 102 103 func (c CreateValidatorAuthorizationMatcher) Matches(x interface{}) bool { 104 msg, ok := x.(sdk.Msg) 105 if !ok { 106 return false 107 } 108 109 resp, err := c.authz.Accept(sdk.Context{}, msg) 110 return resp.Accept && (err == nil) 111 } 112 113 func (c CreateValidatorAuthorizationMatcher) String() string { 114 return fmt.Sprintf("grants %s to %s", c.authz.MsgTypeURL(), c.authz.ValidatorAddress) 115 }