2018-08-16 01:03:25 -07:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2018-08-22 08:56:13 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-16 01:03:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseSubmitProposalFlags(t *testing.T) {
|
|
|
|
okJSON, err := ioutil.TempFile("", "proposal")
|
|
|
|
require.Nil(t, err, "unexpected error")
|
|
|
|
okJSON.WriteString(`
|
|
|
|
{
|
|
|
|
"title": "Test Proposal",
|
|
|
|
"description": "My awesome proposal",
|
|
|
|
"type": "Text",
|
|
|
|
"deposit": "1000test"
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
badJSON, err := ioutil.TempFile("", "proposal")
|
|
|
|
require.Nil(t, err, "unexpected error")
|
|
|
|
badJSON.WriteString("bad json")
|
|
|
|
|
|
|
|
// nonexistent json
|
2019-04-30 09:31:38 -07:00
|
|
|
viper.Set(FlagProposal, "fileDoesNotExist")
|
2018-08-16 01:03:25 -07:00
|
|
|
_, err = parseSubmitProposalFlags()
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// invalid json
|
2019-04-30 09:31:38 -07:00
|
|
|
viper.Set(FlagProposal, badJSON.Name())
|
2018-08-16 01:03:25 -07:00
|
|
|
_, err = parseSubmitProposalFlags()
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// ok json
|
2019-04-30 09:31:38 -07:00
|
|
|
viper.Set(FlagProposal, okJSON.Name())
|
2018-08-16 01:03:25 -07:00
|
|
|
proposal1, err := parseSubmitProposalFlags()
|
|
|
|
require.Nil(t, err, "unexpected error")
|
|
|
|
require.Equal(t, "Test Proposal", proposal1.Title)
|
|
|
|
require.Equal(t, "My awesome proposal", proposal1.Description)
|
|
|
|
require.Equal(t, "Text", proposal1.Type)
|
|
|
|
require.Equal(t, "1000test", proposal1.Deposit)
|
|
|
|
|
|
|
|
// flags that can't be used with --proposal
|
2019-04-30 09:31:38 -07:00
|
|
|
for _, incompatibleFlag := range ProposalFlags {
|
2018-08-16 01:03:25 -07:00
|
|
|
viper.Set(incompatibleFlag, "some value")
|
|
|
|
_, err := parseSubmitProposalFlags()
|
|
|
|
require.Error(t, err)
|
|
|
|
viper.Set(incompatibleFlag, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// no --proposal, only flags
|
2019-04-30 09:31:38 -07:00
|
|
|
viper.Set(FlagProposal, "")
|
|
|
|
viper.Set(FlagTitle, proposal1.Title)
|
|
|
|
viper.Set(FlagDescription, proposal1.Description)
|
2018-08-16 01:03:25 -07:00
|
|
|
viper.Set(flagProposalType, proposal1.Type)
|
2019-04-30 09:31:38 -07:00
|
|
|
viper.Set(FlagDeposit, proposal1.Deposit)
|
2018-08-16 01:03:25 -07:00
|
|
|
proposal2, err := parseSubmitProposalFlags()
|
|
|
|
require.Nil(t, err, "unexpected error")
|
|
|
|
require.Equal(t, proposal1.Title, proposal2.Title)
|
|
|
|
require.Equal(t, proposal1.Description, proposal2.Description)
|
|
|
|
require.Equal(t, proposal1.Type, proposal2.Type)
|
|
|
|
require.Equal(t, proposal1.Deposit, proposal2.Deposit)
|
|
|
|
|
|
|
|
err = okJSON.Close()
|
|
|
|
require.Nil(t, err, "unexpected error")
|
|
|
|
err = badJSON.Close()
|
|
|
|
require.Nil(t, err, "unexpected error")
|
|
|
|
}
|