cosmos-sdk/client/context/context_test.go
Alessio Treglia a1feca39c2
Enter the new keyring interface (#5904)
crypto/keyring:

`Keybase` interface gives way to its successor: `Keyring`. `LegacyKeybase`
interface is added in order to guarantee limited backward compatibility with
the old `Keybase` interface for the sole purpose of migrating keys across
the new keyring backends.

The package no longer depends on the `github.com/types.Config`
singleton.

`SupportedAlgos` and `SupportedLedgerAlgos` methods have been removed.
The keyring just fails when trying to perform an action with an unsupported
algorithm.

crypto/ subdirs reorganization:

`crypto/keys/hd` was moved to `crypto/hd`, which now groups together
all HD wallets related types and utilities.

client/input:

* Removal of unnecessary `GetCheckPassword`, `PrintPrefixed` functions.
* `GetConfirmation`'s signature changed to take in a io.Writer for better integration
  with `cobra.Command` types.

client/context:

* In-memory keyring is allocated in the context when `--gen-only` flag is passed
  in. `GetFromFields` does no longer silently allocate a keyring, it takes one as
  argument.

Co-authored with @jgimeno

Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>
2020-04-08 11:38:28 +02:00

88 lines
2.0 KiB
Go

package context_test
import (
"os"
"testing"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
)
func TestCLIContext_WithOffline(t *testing.T) {
viper.Set(flags.FlagOffline, true)
viper.Set(flags.FlagNode, "tcp://localhost:26657")
ctx := context.NewCLIContext()
require.True(t, ctx.Offline)
require.Nil(t, ctx.Client)
viper.Reset()
viper.Set(flags.FlagOffline, false)
viper.Set(flags.FlagNode, "tcp://localhost:26657")
ctx = context.NewCLIContext()
require.False(t, ctx.Offline)
require.NotNil(t, ctx.Client)
}
func TestCLIContext_WithGenOnly(t *testing.T) {
viper.Set(flags.FlagGenerateOnly, true)
validFromAddr := "cosmos1q7380u26f7ntke3facjmynajs4umlr329vr4ja"
fromAddr, err := sdk.AccAddressFromBech32(validFromAddr)
require.NoError(t, err)
tests := []struct {
name string
from string
expectedFromAddr sdk.AccAddress
expectedFromName string
}{
{
name: "valid from",
from: validFromAddr,
expectedFromAddr: fromAddr,
expectedFromName: "",
},
{
name: "empty from",
from: "",
expectedFromAddr: nil,
expectedFromName: "",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
ctx := context.NewCLIContextWithFrom(tt.from)
require.Equal(t, tt.expectedFromAddr, ctx.FromAddress)
require.Equal(t, tt.expectedFromName, ctx.FromName)
})
}
}
func TestCLIContext_WithKeyring(t *testing.T) {
viper.Set(flags.FlagGenerateOnly, true)
ctx := context.NewCLIContextWithFrom("cosmos1q7380u26f7ntke3facjmynajs4umlr329vr4ja")
require.NotNil(t, ctx.Keyring)
kr := ctx.Keyring
ctx = ctx.WithKeyring(nil)
require.Nil(t, ctx.Keyring)
ctx = ctx.WithKeyring(kr)
require.Equal(t, kr, ctx.Keyring)
}
func TestMain(m *testing.M) {
viper.Set(flags.FlagKeyringBackend, keyring.BackendMemory)
os.Exit(m.Run())
}