Skip to content

Commit

Permalink
finish add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanchristo committed Jun 11, 2021
1 parent b61fccb commit 9e3edfe
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
65 changes: 65 additions & 0 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
package keys

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -124,3 +126,66 @@ func Test_runAddCmdLedger(t *testing.T) {
"PubKeySecp256k1{034FEF9CD7C4C63588D3B03FEB5281B9D232CBA34D6F3D71AEE59211FFBFE1FE87}",
key1.GetPubKey().String())
}

func Test_runAddCmdLedgerDryRun(t *testing.T) {
testData := []struct {
name string
args []string
removed bool
}{
{
name: "ledger account is not removed",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "false"),
fmt.Sprintf("--%s=%s", flags.FlagUseLedger, "true"),
},
removed: false,
},
{
name: "ledger account is removed with dry run",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "true"),
fmt.Sprintf("--%s=%s", flags.FlagUseLedger, "true"),
},
removed: true,
},
}
for _, tt := range testData {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())

kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
require.NoError(t, err)

clientCtx := client.Context{}.
WithKeyringDir(kbHome).
WithKeyring(kb)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

b := bytes.NewBufferString("")
cmd.SetOut(b)

cmd.SetArgs(tt.args)
require.NoError(t, cmd.ExecuteContext(ctx))

if tt.removed {
_, err = kb.Key("testkey")
require.Error(t, err)
require.Equal(t, "testkey.info: key not found", err.Error())
} else {
_, err = kb.Key("testkey")
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
require.NoError(t, err)
require.Contains(t, string(out), "name: testkey")
}
})
}
}
40 changes: 16 additions & 24 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package keys

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -126,23 +128,23 @@ func Test_runAddCmdDryRun(t *testing.T) {
removed bool
}{
{
name: "key is not removed",
name: "account is not removed",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "false"),
},
removed: false,
},
{
name: "key is removed with dry run",
name: "account is removed with dry run",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "true"),
},
removed: true,
},
{
name: "multisig is not removed",
name: "multisig account is not removed",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "false"),
Expand All @@ -151,7 +153,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
removed: false,
},
{
name: "multisig is removed with dry run",
name: "multisig account is removed with dry run",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "true"),
Expand All @@ -160,7 +162,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
removed: true,
},
{
name: "pubkey is not removed",
name: "pubkey account is not removed",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "false"),
Expand All @@ -169,32 +171,14 @@ func Test_runAddCmdDryRun(t *testing.T) {
removed: false,
},
{
name: "pubkey is removed with dry run",
name: "pubkey account is removed with dry run",
args: []string{
"testkey",
fmt.Sprintf("--%s=%s", flags.FlagDryRun, "true"),
fmt.Sprintf("--%s=%s", FlagPublicKey, pubkey2),
},
removed: true,
},
// {
// name: "ledger key is not removed",
// args: []string{
// "testkey",
// fmt.Sprintf("--%s=%s", flags.FlagDryRun, "false"),
// fmt.Sprintf("--%s=%s", flags.FlagUseLedger, "true"),
// },
// removed: false,
// },
// {
// name: "ledger key is removed with dry run",
// args: []string{
// "testkey",
// fmt.Sprintf("--%s=%s", flags.FlagDryRun, "true"),
// fmt.Sprintf("--%s=%s", flags.FlagUseLedger, "true"),
// },
// removed: true,
// },
}
for _, tt := range testData {
tt := tt
Expand Down Expand Up @@ -222,15 +206,23 @@ func Test_runAddCmdDryRun(t *testing.T) {
_ = kb.Delete("subkey")
})

b := bytes.NewBufferString("")
cmd.SetOut(b)

cmd.SetArgs(tt.args)
require.NoError(t, cmd.ExecuteContext(ctx))

if tt.removed {
_, err = kb.Key("testkey")
require.Error(t, err)
require.Equal(t, "testkey.info: key not found", err.Error())
} else {
_, err = kb.Key("testkey")
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
require.NoError(t, err)
require.Contains(t, string(out), "name: testkey")
}
})
}
Expand Down

0 comments on commit 9e3edfe

Please sign in to comment.