2019-02-08 12:45:23 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2019-11-14 06:17:21 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-05-28 01:44:04 -07:00
|
|
|
|
2019-02-08 12:45:23 -08:00
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2019-02-08 12:45:23 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/tests"
|
2020-01-22 09:54:56 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-08 12:45:23 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_runAddCmdBasic(t *testing.T) {
|
2019-12-12 13:52:24 -08:00
|
|
|
cmd := AddKeyCommand()
|
2020-04-08 02:38:28 -07:00
|
|
|
require.NotNil(t, cmd)
|
2019-06-22 02:24:59 -07:00
|
|
|
mockIn, _, _ := tests.ApplyMockIO(cmd)
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2019-02-08 13:45:41 -08:00
|
|
|
kbHome, kbCleanUp := tests.NewTestCaseDir(t)
|
2020-04-08 02:38:28 -07:00
|
|
|
require.NotNil(t, kbHome)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(kbCleanUp)
|
2019-05-28 01:44:04 -07:00
|
|
|
viper.Set(flags.FlagHome, kbHome)
|
2019-02-08 12:45:23 -08:00
|
|
|
viper.Set(cli.OutputFlag, OutputFormatText)
|
2020-04-08 02:38:28 -07:00
|
|
|
viper.Set(flags.FlagUseLedger, false)
|
2019-06-22 02:24:59 -07:00
|
|
|
|
2020-03-30 11:30:50 -07:00
|
|
|
mockIn.Reset("y\n")
|
2020-04-08 02:38:28 -07:00
|
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), kbHome, mockIn)
|
2020-03-30 11:30:50 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
2020-04-08 02:38:28 -07:00
|
|
|
kb.Delete("keyname1") // nolint:errcheck
|
|
|
|
kb.Delete("keyname2") // nolint:errcheck
|
2020-03-30 11:30:50 -07:00
|
|
|
})
|
2020-04-08 02:38:28 -07:00
|
|
|
require.NoError(t, runAddCmd(cmd, []string{"keyname1"}))
|
2019-11-14 06:17:21 -08:00
|
|
|
|
2020-03-30 11:30:50 -07:00
|
|
|
mockIn.Reset("N\n")
|
2020-04-08 02:38:28 -07:00
|
|
|
require.Error(t, runAddCmd(cmd, []string{"keyname1"}))
|
2019-11-14 06:17:21 -08:00
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
require.NoError(t, runAddCmd(cmd, []string{"keyname2"}))
|
|
|
|
require.Error(t, runAddCmd(cmd, []string{"keyname2"}))
|
2020-03-30 11:30:50 -07:00
|
|
|
mockIn.Reset("y\n")
|
2020-04-08 02:38:28 -07:00
|
|
|
require.NoError(t, runAddCmd(cmd, []string{"keyname2"}))
|
2020-03-30 11:30:50 -07:00
|
|
|
|
|
|
|
// test --dry-run
|
2020-04-08 02:38:28 -07:00
|
|
|
require.NoError(t, runAddCmd(cmd, []string{"keyname4"}))
|
|
|
|
require.Error(t, runAddCmd(cmd, []string{"keyname4"}))
|
2020-03-30 11:30:50 -07:00
|
|
|
|
|
|
|
viper.Set(flags.FlagDryRun, true)
|
2020-04-08 02:38:28 -07:00
|
|
|
require.NoError(t, runAddCmd(cmd, []string{"keyname4"}))
|
2019-02-08 12:45:23 -08:00
|
|
|
}
|