Skip to content

Commit

Permalink
test: add more tests for periodic vesting accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLarson committed Sep 24, 2021
1 parent 81dec22 commit db3a225
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 1 deletion.
79 changes: 79 additions & 0 deletions x/auth/vesting/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions x/auth/vesting/client/testutil/testdata/badjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not JSON data.
9 changes: 9 additions & 0 deletions x/auth/vesting/client/testutil/testdata/badperiod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"start_time": 1625204910,
"period": [
{
"coins": "10test",
"length_seconds": -500
}
]
}
13 changes: 13 additions & 0 deletions x/auth/vesting/client/testutil/testdata/periods1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"start_time": 1625204910,
"period": [
{
"coins": "10test",
"length_seconds": 2592000
},
{
"coins": "10test",
"length_seconds":2592000
}
]
}
26 changes: 26 additions & 0 deletions x/auth/vesting/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
18 changes: 17 additions & 1 deletion x/auth/vesting/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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))
}
85 changes: 85 additions & 0 deletions x/auth/vesting/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit db3a225

Please sign in to comment.