From db3a225229f4115aaefdeb1e5d2e0ca87dbfbd18 Mon Sep 17 00:00:00 2001 From: Jim Larson Date: Fri, 24 Sep 2021 15:25:14 -0700 Subject: [PATCH] test: add more tests for periodic vesting accounts --- x/auth/vesting/client/testutil/suite.go | 79 +++++++++++++++++ .../vesting/client/testutil/testdata/badjson | 1 + .../client/testutil/testdata/badperiod.json | 9 ++ .../client/testutil/testdata/periods1.json | 13 +++ x/auth/vesting/handler_test.go | 26 ++++++ x/auth/vesting/types/genesis_test.go | 18 +++- x/auth/vesting/types/msgs_test.go | 85 +++++++++++++++++++ 7 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 x/auth/vesting/client/testutil/testdata/badjson create mode 100644 x/auth/vesting/client/testutil/testdata/badperiod.json create mode 100644 x/auth/vesting/client/testutil/testdata/periods1.json create mode 100644 x/auth/vesting/types/msgs_test.go diff --git a/x/auth/vesting/client/testutil/suite.go b/x/auth/vesting/client/testutil/suite.go index f619f31165c8..d41a92effb25 100644 --- a/x/auth/vesting/client/testutil/suite.go +++ b/x/auth/vesting/client/testutil/suite.go @@ -130,3 +130,82 @@ func (s *IntegrationTestSuite) TestNewMsgCreateVestingAccountCmd() { }) } } + +func (s *IntegrationTestSuite) TestNewMsgCreatePeriodicVestingAccountCmd() { + val := s.network.Validators[0] + testCases := map[string]struct { + args []string + expectErr bool + expectedCode uint32 + respType proto.Message + }{ + "create a periodic vesting account": { + args: []string{ + sdk.AccAddress("addr5_______________").String(), + "testdata/periods1.json", + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + }, + expectErr: false, + expectedCode: 0, + respType: &sdk.TxResponse{}, + }, + "bad to address": { + args: []string{ + "foo", + "testdata/periods1.json", + }, + expectErr: true, + }, + "bad from address": { + args: []string{ + sdk.AccAddress("addr5_______________").String(), + "testdata/periods1.json", + fmt.Sprintf("--%s=%s", flags.FlagFrom, "foo"), + }, + expectErr: true, + }, + "bad file": { + args: []string{ + sdk.AccAddress("addr6_______________").String(), + "testdata/noexist", + }, + expectErr: true, + }, + "bad json": { + args: []string{ + sdk.AccAddress("addr7_______________").String(), + "testdata/badjson", + }, + expectErr: true, + }, + "bad periods": { + args: []string{ + sdk.AccAddress("addr8_______________").String(), + "testdata/badperiod.json", + }, + expectErr: true, + }, + } + + for name, tc := range testCases { + tc := tc + + s.Run(name, func() { + clientCtx := val.ClientCtx + + bw, err := clitestutil.ExecTestCLICmd(clientCtx, cli.NewMsgCreatePeriodicVestingAccountCmd(), tc.args) + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bw.Bytes(), tc.respType), bw.String()) + + txResp := tc.respType.(*sdk.TxResponse) + s.Require().Equal(tc.expectedCode, txResp.Code) + } + }) + } +} diff --git a/x/auth/vesting/client/testutil/testdata/badjson b/x/auth/vesting/client/testutil/testdata/badjson new file mode 100644 index 000000000000..aad60f4a0e85 --- /dev/null +++ b/x/auth/vesting/client/testutil/testdata/badjson @@ -0,0 +1 @@ +Not JSON data. diff --git a/x/auth/vesting/client/testutil/testdata/badperiod.json b/x/auth/vesting/client/testutil/testdata/badperiod.json new file mode 100644 index 000000000000..f95fdb5f6abf --- /dev/null +++ b/x/auth/vesting/client/testutil/testdata/badperiod.json @@ -0,0 +1,9 @@ +{ + "start_time": 1625204910, + "period": [ + { + "coins": "10test", + "length_seconds": -500 + } + ] +} diff --git a/x/auth/vesting/client/testutil/testdata/periods1.json b/x/auth/vesting/client/testutil/testdata/periods1.json new file mode 100644 index 000000000000..82c7ea6ea08c --- /dev/null +++ b/x/auth/vesting/client/testutil/testdata/periods1.json @@ -0,0 +1,13 @@ +{ + "start_time": 1625204910, + "period": [ + { + "coins": "10test", + "length_seconds": 2592000 + }, + { + "coins": "10test", + "length_seconds":2592000 + } + ] +} diff --git a/x/auth/vesting/handler_test.go b/x/auth/vesting/handler_test.go index b127a333ba8c..805c68a441e6 100644 --- a/x/auth/vesting/handler_test.go +++ b/x/auth/vesting/handler_test.go @@ -114,6 +114,31 @@ func (suite *HandlerTestSuite) TestMsgCreatePeriodicVestingAccount() { msg: types.NewMsgCreatePeriodicVestingAccount(addr1, addr3, 0, period), expectErr: false, }, + { + name: "bad from addr", + msg: &types.MsgCreatePeriodicVestingAccount{ + FromAddress: "foo", + ToAddress: addr3.String(), + StartTime: 0, + VestingPeriods: period, + }, + expectErr: true, + }, + { + name: "bad to addr", + msg: &types.MsgCreatePeriodicVestingAccount{ + FromAddress: addr1.String(), + ToAddress: "foo", + StartTime: 0, + VestingPeriods: period, + }, + expectErr: true, + }, + { + name: "account exists", + msg: types.NewMsgCreatePeriodicVestingAccount(addr1, addr1, 0, period), + expectErr: true, + }, } for _, tc := range testCases { @@ -135,6 +160,7 @@ func (suite *HandlerTestSuite) TestMsgCreatePeriodicVestingAccount() { accI := suite.app.AccountKeeper.GetAccount(ctx, toAddr) suite.Require().NotNil(accI) + suite.Require().IsType(&types.PeriodicVestingAccount{}, accI) balanceSource := suite.app.BankKeeper.GetBalance(ctx, fromAddr, "test") suite.Require().Equal(balanceSource, sdk.NewInt64Coin("test", 0)) balanceDest := suite.app.BankKeeper.GetBalance(ctx, toAddr, "test") diff --git a/x/auth/vesting/types/genesis_test.go b/x/auth/vesting/types/genesis_test.go index 98aabb15cfde..36710e0b9a6d 100644 --- a/x/auth/vesting/types/genesis_test.go +++ b/x/auth/vesting/types/genesis_test.go @@ -21,7 +21,8 @@ var ( func TestValidateGenesisInvalidAccounts(t *testing.T) { acc1 := authtypes.NewBaseAccountWithAddress(sdk.AccAddress(addr1)) acc1Balance := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 150)) - baseVestingAcc := NewBaseVestingAccount(acc1, acc1Balance, 1548775410) + endTime := int64(1548775410) + baseVestingAcc := NewBaseVestingAccount(acc1, acc1Balance, endTime) // invalid delegated vesting baseVestingAcc.DelegatedVesting = acc1Balance.Add(acc1Balance...) @@ -40,4 +41,19 @@ func TestValidateGenesisInvalidAccounts(t *testing.T) { // invalid start time genAccs[0] = NewContinuousVestingAccountRaw(baseVestingAcc, 1548888000) require.Error(t, authtypes.ValidateGenAccounts(genAccs)) + // invalid period: duration + genAccs[0] = NewPeriodicVestingAccountRaw(baseVestingAcc, endTime-100000, []Period{ + {Length: 100000 + 20, Amount: acc1Balance}, + }) + require.Error(t, authtypes.ValidateGenAccounts(genAccs)) + // invalid period: amount + genAccs[0] = NewPeriodicVestingAccountRaw(baseVestingAcc, endTime-100000, []Period{ + {Length: 100000, Amount: acc1Balance.Add(acc1Balance...)}, + }) + require.Error(t, authtypes.ValidateGenAccounts(genAccs)) + // Passing case + genAccs[0] = NewPeriodicVestingAccountRaw(baseVestingAcc, endTime-100000, []Period{ + {Length: 100000, Amount: acc1Balance}, + }) + require.NoError(t, authtypes.ValidateGenAccounts(genAccs)) } diff --git a/x/auth/vesting/types/msgs_test.go b/x/auth/vesting/types/msgs_test.go new file mode 100644 index 000000000000..94f4fe2400d1 --- /dev/null +++ b/x/auth/vesting/types/msgs_test.go @@ -0,0 +1,85 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestVestingAccountMsg(t *testing.T) { + _, _, fromAddr := KeyTestPubAddr() + _, _, toAddr := KeyTestPubAddr() + amount := NewTestCoins() + endTime := int64(123456789) + msg := NewMsgCreateVestingAccount(fromAddr, toAddr, amount, endTime, false) + require.NotNil(t, msg) + route := msg.Route() + require.Equal(t, RouterKey, route) + tp := msg.Type() + require.Equal(t, TypeMsgCreateVestingAccount, tp) + err := msg.ValidateBasic() + require.Nil(t, err) + + badFromMsg := MsgCreateVestingAccount{ + FromAddress: "foo", + ToAddress: toAddr.String(), + Amount: amount, + EndTime: endTime, + } + err = badFromMsg.ValidateBasic() + require.Error(t, err) + + badToMsg := MsgCreateVestingAccount{ + FromAddress: fromAddr.String(), + ToAddress: "foo", + Amount: amount, + EndTime: endTime, + } + err = badToMsg.ValidateBasic() + require.Error(t, err) + + badEndTime := NewMsgCreateVestingAccount(fromAddr, toAddr, amount, int64(-1), false) + err = badEndTime.ValidateBasic() + require.Error(t, err) +} + +func TestPeriodicVestingAccountMsg(t *testing.T) { + _, _, fromAddr := KeyTestPubAddr() + _, _, toAddr := KeyTestPubAddr() + amount := NewTestCoins() + startTime := int64(123456789) + periods := []Period{ + {Length: 86400, Amount: amount}, + } + msg := NewMsgCreatePeriodicVestingAccount(fromAddr, toAddr, startTime, periods) + route := msg.Route() + require.Equal(t, RouterKey, route) + tp := msg.Type() + require.Equal(t, TypeMsgCreatePeriodicVestingAccount, tp) + err := msg.ValidateBasic() + require.Nil(t, err) + + badFromMsg := MsgCreatePeriodicVestingAccount{ + FromAddress: "foo", + ToAddress: toAddr.String(), + StartTime: startTime, + VestingPeriods: periods, + } + err = badFromMsg.ValidateBasic() + require.Error(t, err) + + badToMsg := MsgCreatePeriodicVestingAccount{ + FromAddress: fromAddr.String(), + ToAddress: "foo", + StartTime: startTime, + VestingPeriods: periods, + } + err = badToMsg.ValidateBasic() + require.Error(t, err) + + badPeriods := NewMsgCreatePeriodicVestingAccount(fromAddr, toAddr, startTime, []Period{ + {Length: 0, Amount: amount}, + }) + err = badPeriods.ValidateBasic() + require.Error(t, err) +}