2019-02-08 12:45:23 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2020-10-08 10:41:35 -07:00
|
|
|
"context"
|
2020-07-06 12:50:09 -07:00
|
|
|
"fmt"
|
2019-02-08 12:45:23 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-04 07:36:39 -07:00
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-07-06 12:50:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-07-09 05:21:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
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_runDeleteCmd(t *testing.T) {
|
2020-07-20 08:12:33 -07:00
|
|
|
// Now add a temporary keybase
|
2020-09-18 04:08:24 -07:00
|
|
|
kbHome := t.TempDir()
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd := DeleteKeyCommand()
|
2020-07-20 08:12:33 -07:00
|
|
|
cmd.Flags().AddFlagSet(Commands(kbHome).PersistentFlags())
|
2020-07-09 23:55:48 -07:00
|
|
|
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
yesF, _ := cmd.Flags().GetBool(flagYes)
|
|
|
|
forceF, _ := cmd.Flags().GetBool(flagForce)
|
2020-04-08 02:38:28 -07:00
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
require.False(t, yesF)
|
|
|
|
require.False(t, forceF)
|
2019-02-08 12:45:23 -08:00
|
|
|
|
|
|
|
fakeKeyName1 := "runDeleteCmd_Key1"
|
|
|
|
fakeKeyName2 := "runDeleteCmd_Key2"
|
|
|
|
|
2021-02-22 07:14:09 -08:00
|
|
|
path := sdk.GetConfig().GetFullBIP44Path()
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2021-03-31 03:04:33 -07:00
|
|
|
cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)})
|
2020-07-06 12:50:09 -07:00
|
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2020-07-09 05:21:20 -07:00
|
|
|
_, err = kb.NewAccount(fakeKeyName1, testutil.TestMnemonic, "", path, hd.Secp256k1)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2021-02-23 08:49:09 -08:00
|
|
|
_, _, err = kb.NewMnemonic(fakeKeyName2, keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
|
2019-11-14 06:17:21 -08:00
|
|
|
require.NoError(t, err)
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2021-03-31 03:04:33 -07:00
|
|
|
clientCtx := client.Context{}.
|
|
|
|
WithKeyringDir(kbHome).
|
|
|
|
WithKeyring(kb)
|
2020-10-08 10:41:35 -07:00
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
|
|
|
|
|
|
|
err = cmd.ExecuteContext(ctx)
|
2019-02-08 12:45:23 -08:00
|
|
|
require.Error(t, err)
|
2021-03-25 07:53:22 -07:00
|
|
|
require.EqualError(t, err, "blah.info: key not found")
|
2019-02-08 12:45:23 -08:00
|
|
|
|
|
|
|
// User confirmation missing
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{
|
|
|
|
fakeKeyName1,
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
|
|
|
err = cmd.Execute()
|
2019-02-08 12:45:23 -08:00
|
|
|
require.Error(t, err)
|
2020-03-30 11:30:50 -07:00
|
|
|
require.Equal(t, "EOF", err.Error())
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
_, err = kb.Key(fakeKeyName1)
|
|
|
|
require.NoError(t, err)
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
// Now there is a confirmation
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{
|
|
|
|
fakeKeyName1,
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
|
|
|
|
fmt.Sprintf("--%s=true", flagYes),
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
|
|
|
require.NoError(t, cmd.Execute())
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
_, err = kb.Key(fakeKeyName1)
|
|
|
|
require.Error(t, err) // Key1 is gone
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
_, err = kb.Key(fakeKeyName2)
|
2019-02-08 12:45:23 -08:00
|
|
|
require.NoError(t, err)
|
2020-07-06 12:50:09 -07:00
|
|
|
|
|
|
|
cmd.SetArgs([]string{
|
|
|
|
fakeKeyName2,
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
|
|
|
|
fmt.Sprintf("--%s=true", flagYes),
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
|
|
|
require.NoError(t, cmd.Execute())
|
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
_, err = kb.Key(fakeKeyName2)
|
2019-02-08 12:45:23 -08:00
|
|
|
require.Error(t, err) // Key2 is gone
|
|
|
|
}
|