github.com/cosmos/cosmos-sdk@v0.50.10/x/crisis/keeper/msg_server_test.go (about) 1 package keeper_test 2 3 import ( 4 "testing" 5 6 "github.com/golang/mock/gomock" 7 "github.com/stretchr/testify/suite" 8 9 sdkmath "cosmossdk.io/math" 10 storetypes "cosmossdk.io/store/types" 11 12 addresscodec "github.com/cosmos/cosmos-sdk/codec/address" 13 "github.com/cosmos/cosmos-sdk/crypto/keyring" 14 "github.com/cosmos/cosmos-sdk/runtime" 15 "github.com/cosmos/cosmos-sdk/testutil" 16 sdk "github.com/cosmos/cosmos-sdk/types" 17 moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" 18 "github.com/cosmos/cosmos-sdk/x/crisis" 19 "github.com/cosmos/cosmos-sdk/x/crisis/keeper" 20 crisistestutil "github.com/cosmos/cosmos-sdk/x/crisis/testutil" 21 "github.com/cosmos/cosmos-sdk/x/crisis/types" 22 ) 23 24 type KeeperTestSuite struct { 25 suite.Suite 26 27 ctx sdk.Context 28 supplyKeeper *crisistestutil.MockSupplyKeeper 29 keeper *keeper.Keeper 30 } 31 32 func (s *KeeperTestSuite) SetupTest() { 33 // gomock initializations 34 ctrl := gomock.NewController(s.T()) 35 supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl) 36 37 key := storetypes.NewKVStoreKey(types.StoreKey) 38 storeService := runtime.NewKVStoreService(key) 39 testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) 40 encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}) 41 keeper := keeper.NewKeeper(encCfg.Codec, storeService, 5, supplyKeeper, "", sdk.AccAddress([]byte("addr1_______________")).String(), addresscodec.NewBech32Codec("cosmos")) 42 43 s.ctx = testCtx.Ctx 44 s.keeper = keeper 45 s.supplyKeeper = supplyKeeper 46 } 47 48 func (s *KeeperTestSuite) TestMsgVerifyInvariant() { 49 // default params 50 constantFee := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1000)) 51 err := s.keeper.ConstantFee.Set(s.ctx, constantFee) 52 s.Require().NoError(err) 53 54 encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}) 55 kr := keyring.NewInMemory(encCfg.Codec) 56 testutil.CreateKeyringAccounts(s.T(), kr, 1) 57 58 sender := testutil.CreateKeyringAccounts(s.T(), kr, 1)[0] 59 60 s.supplyKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 61 s.keeper.RegisterRoute("bank", "total-supply", func(sdk.Context) (string, bool) { return "", false }) 62 63 testCases := []struct { 64 name string 65 input *types.MsgVerifyInvariant 66 expErr bool 67 expErrMsg string 68 }{ 69 { 70 name: "empty sender not allowed", 71 input: &types.MsgVerifyInvariant{ 72 Sender: "", 73 InvariantModuleName: "bank", 74 InvariantRoute: "total-supply", 75 }, 76 expErr: true, 77 expErrMsg: "empty address string is not allowed", 78 }, 79 { 80 name: "invalid sender address", 81 input: &types.MsgVerifyInvariant{ 82 Sender: "invalid address", 83 InvariantModuleName: "bank", 84 InvariantRoute: "total-supply", 85 }, 86 expErr: true, 87 expErrMsg: "decoding bech32 failed", 88 }, 89 { 90 name: "unregistered invariant route", 91 input: &types.MsgVerifyInvariant{ 92 Sender: sender.Address.String(), 93 InvariantModuleName: "module", 94 InvariantRoute: "invalidroute", 95 }, 96 expErr: true, 97 expErrMsg: "unknown invariant", 98 }, 99 { 100 name: "valid invariant", 101 input: &types.MsgVerifyInvariant{ 102 Sender: sender.Address.String(), 103 InvariantModuleName: "bank", 104 InvariantRoute: "total-supply", 105 }, 106 expErr: false, 107 }, 108 } 109 110 for _, tc := range testCases { 111 tc := tc 112 s.Run(tc.name, func() { 113 _, err = s.keeper.VerifyInvariant(s.ctx, tc.input) 114 if tc.expErr { 115 s.Require().Error(err) 116 s.Require().Contains(err.Error(), tc.expErrMsg) 117 } else { 118 s.Require().NoError(err) 119 } 120 }) 121 } 122 } 123 124 func (s *KeeperTestSuite) TestMsgUpdateParams() { 125 // default params 126 constantFee := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1000)) 127 128 testCases := []struct { 129 name string 130 input *types.MsgUpdateParams 131 expErr bool 132 expErrMsg string 133 }{ 134 { 135 name: "invalid authority", 136 input: &types.MsgUpdateParams{ 137 Authority: "invalid", 138 ConstantFee: constantFee, 139 }, 140 expErr: true, 141 expErrMsg: "invalid authority", 142 }, 143 { 144 name: "invalid constant fee", 145 input: &types.MsgUpdateParams{ 146 Authority: s.keeper.GetAuthority(), 147 ConstantFee: sdk.Coin{}, 148 }, 149 expErr: true, 150 }, 151 { 152 name: "negative constant fee", 153 input: &types.MsgUpdateParams{ 154 Authority: s.keeper.GetAuthority(), 155 ConstantFee: sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdkmath.NewInt(-1000)}, 156 }, 157 expErr: true, 158 }, 159 { 160 name: "all good", 161 input: &types.MsgUpdateParams{ 162 Authority: s.keeper.GetAuthority(), 163 ConstantFee: constantFee, 164 }, 165 expErr: false, 166 }, 167 } 168 169 for _, tc := range testCases { 170 tc := tc 171 s.Run(tc.name, func() { 172 _, err := s.keeper.UpdateParams(s.ctx, tc.input) 173 174 if tc.expErr { 175 s.Require().Error(err) 176 s.Require().Contains(err.Error(), tc.expErrMsg) 177 } else { 178 s.Require().NoError(err) 179 } 180 }) 181 } 182 } 183 184 func TestKeeperTestSuite(t *testing.T) { 185 suite.Run(t, new(KeeperTestSuite)) 186 }