2019-04-30 09:31:38 -07:00
|
|
|
package types
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
import (
|
2019-01-29 14:23:25 -08:00
|
|
|
"strings"
|
2018-06-21 17:19:14 -07:00
|
|
|
"testing"
|
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-19 09:06:27 -07:00
|
|
|
coinsPos = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000))
|
|
|
|
coinsZero = sdk.NewCoins()
|
|
|
|
coinsMulti = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000), sdk.NewInt64Coin("foo", 10000))
|
|
|
|
addrs = []sdk.AccAddress{
|
2019-04-30 09:31:38 -07:00
|
|
|
sdk.AccAddress("test1"),
|
|
|
|
sdk.AccAddress("test2"),
|
|
|
|
}
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
2018-11-12 10:54:18 -08:00
|
|
|
func init() {
|
|
|
|
coinsMulti.Sort()
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// test ValidateBasic for MsgCreateValidator
|
|
|
|
func TestMsgSubmitProposal(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
title, description string
|
2019-04-30 09:31:38 -07:00
|
|
|
proposalType string
|
2018-07-06 00:06:53 -07:00
|
|
|
proposerAddr sdk.AccAddress
|
2018-06-21 17:19:14 -07:00
|
|
|
initialDeposit sdk.Coins
|
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, true},
|
|
|
|
{"", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, false},
|
|
|
|
{"Test Proposal", "", ProposalTypeText, addrs[0], coinsPos, false},
|
2018-07-06 00:06:53 -07:00
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, sdk.AccAddress{}, coinsPos, false},
|
2018-06-21 17:19:14 -07:00
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsZero, true},
|
|
|
|
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, true},
|
2019-01-29 14:23:25 -08:00
|
|
|
{strings.Repeat("#", MaxTitleLength*2), "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, false},
|
|
|
|
{"Test Proposal", strings.Repeat("#", MaxDescriptionLength*2), ProposalTypeText, addrs[0], coinsMulti, false},
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
2020-05-19 13:17:29 -07:00
|
|
|
msg, err := NewMsgSubmitProposal(
|
2019-04-30 09:31:38 -07:00
|
|
|
ContentFromProposalType(tc.title, tc.description, tc.proposalType),
|
|
|
|
tc.initialDeposit,
|
|
|
|
tc.proposerAddr,
|
|
|
|
)
|
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
if tc.expectPass {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.NoError(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
} else {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.Error(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 10:22:20 -08:00
|
|
|
func TestMsgDepositGetSignBytes(t *testing.T) {
|
|
|
|
addr := sdk.AccAddress("addr1")
|
|
|
|
msg := NewMsgDeposit(addr, 0, coinsPos)
|
|
|
|
res := msg.GetSignBytes()
|
|
|
|
|
|
|
|
expected := `{"type":"cosmos-sdk/MsgDeposit","value":{"amount":[{"amount":"1000","denom":"stake"}],"depositor":"cosmos1v9jxgu33kfsgr5","proposal_id":"0"}}`
|
|
|
|
require.Equal(t, expected, string(res))
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// test ValidateBasic for MsgDeposit
|
|
|
|
func TestMsgDeposit(t *testing.T) {
|
|
|
|
tests := []struct {
|
2018-11-06 23:33:18 -08:00
|
|
|
proposalID uint64
|
2018-11-21 15:53:33 -08:00
|
|
|
depositorAddr sdk.AccAddress
|
2018-06-21 17:19:14 -07:00
|
|
|
depositAmount sdk.Coins
|
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{0, addrs[0], coinsPos, true},
|
2018-07-06 00:06:53 -07:00
|
|
|
{1, sdk.AccAddress{}, coinsPos, false},
|
2018-06-21 17:19:14 -07:00
|
|
|
{1, addrs[0], coinsZero, true},
|
|
|
|
{1, addrs[0], coinsMulti, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
2018-11-21 15:53:33 -08:00
|
|
|
msg := NewMsgDeposit(tc.depositorAddr, tc.proposalID, tc.depositAmount)
|
2018-06-21 17:19:14 -07:00
|
|
|
if tc.expectPass {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.NoError(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
} else {
|
2018-11-12 10:54:18 -08:00
|
|
|
require.Error(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgDeposit
|
|
|
|
func TestMsgVote(t *testing.T) {
|
|
|
|
tests := []struct {
|
2018-11-06 23:33:18 -08:00
|
|
|
proposalID uint64
|
2018-07-06 00:06:53 -07:00
|
|
|
voterAddr sdk.AccAddress
|
2018-06-21 17:19:14 -07:00
|
|
|
option VoteOption
|
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{0, addrs[0], OptionYes, true},
|
2018-07-06 00:06:53 -07:00
|
|
|
{0, sdk.AccAddress{}, OptionYes, false},
|
2018-06-21 17:19:14 -07:00
|
|
|
{0, addrs[0], OptionNo, true},
|
|
|
|
{0, addrs[0], OptionNoWithVeto, true},
|
|
|
|
{0, addrs[0], OptionAbstain, true},
|
|
|
|
{0, addrs[0], VoteOption(0x13), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
msg := NewMsgVote(tc.voterAddr, tc.proposalID, tc.option)
|
|
|
|
if tc.expectPass {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, msg.ValidateBasic(), "test: %v", i)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 13:17:29 -07:00
|
|
|
|
|
|
|
// this tests that Amino JSON MsgSubmitProposal.GetSignBytes() still works with Content as Any using the ModuleCdc
|
|
|
|
func TestMsgSubmitProposal_GetSignBytes(t *testing.T) {
|
|
|
|
msg, err := NewMsgSubmitProposal(NewTextProposal("test", "abcd"), sdk.NewCoins(), sdk.AccAddress{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
var bz []byte
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
bz = msg.GetSignBytes()
|
|
|
|
})
|
|
|
|
require.Equal(t,
|
|
|
|
`{"type":"cosmos-sdk/MsgSubmitProposal","value":{"content":{"type":"cosmos-sdk/TextProposal","value":{"description":"abcd","title":"test"}},"initial_deposit":[]}}`,
|
|
|
|
string(bz))
|
|
|
|
}
|