cli: add tests for wallet/account creation

This commit is contained in:
Evgenii Stratonikov 2020-08-31 16:17:44 +03:00
parent d30c7db49a
commit 5d7f177811
2 changed files with 88 additions and 1 deletions

View file

@ -474,7 +474,9 @@ func importWallet(ctx *cli.Context) error {
acc.Contract.Script = ctr acc.Contract.Script = ctr
} }
if acc.Label == "" {
acc.Label = ctx.String("name") acc.Label = ctx.String("name")
}
if err := addAccountAndSave(wall, acc); err != nil { if err := addAccountAndSave(wall, acc); err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }

85
cli/wallet_test.go Normal file
View file

@ -0,0 +1,85 @@
package main
import (
"os"
"path"
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
)
func TestWalletInit(t *testing.T) {
tmpDir := os.TempDir()
e := newExecutor(t, false)
defer e.Close(t)
walletPath := path.Join(tmpDir, "wallet.json")
e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath)
defer os.Remove(walletPath)
t.Run("CreateAccount", func(t *testing.T) {
e.In.WriteString("testname\r")
e.In.WriteString("testpass\r")
e.In.WriteString("testpass\r")
e.Run(t, "neo-go", "wallet", "create", "--wallet", walletPath)
w, err := wallet.NewWalletFromFile(walletPath)
require.NoError(t, err)
defer w.Close()
require.Len(t, w.Accounts, 1)
require.Equal(t, w.Accounts[0].Label, "testname")
require.NoError(t, w.Accounts[0].Decrypt("testpass"))
})
t.Run("Import", func(t *testing.T) {
t.Run("WIF", func(t *testing.T) {
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
e.In.WriteString("test_account\r")
e.In.WriteString("qwerty\r")
e.In.WriteString("qwerty\r")
e.Run(t, "neo-go", "wallet", "import", "--wallet", walletPath,
"--wif", priv.WIF())
w, err := wallet.NewWalletFromFile(walletPath)
require.NoError(t, err)
defer w.Close()
acc := w.GetAccount(priv.GetScriptHash())
require.NotNil(t, acc)
require.Equal(t, "test_account", acc.Label)
require.NoError(t, acc.Decrypt("qwerty"))
t.Run("AlreadyExists", func(t *testing.T) {
e.In.WriteString("test_account_2\r")
e.In.WriteString("qwerty2\r")
e.In.WriteString("qwerty2\r")
e.RunWithError(t, "neo-go", "wallet", "import",
"--wallet", walletPath, "--wif", priv.WIF())
})
})
t.Run("EncryptedWIF", func(t *testing.T) {
acc, err := wallet.NewAccount()
require.NoError(t, err)
require.NoError(t, acc.Encrypt("somepass"))
t.Run("InvalidPassword", func(t *testing.T) {
e.In.WriteString("password1\r")
e.RunWithError(t, "neo-go", "wallet", "import", "--wallet", walletPath,
"--wif", acc.EncryptedWIF)
})
e.In.WriteString("somepass\r")
e.Run(t, "neo-go", "wallet", "import", "--wallet", walletPath,
"--wif", acc.EncryptedWIF)
w, err := wallet.NewWalletFromFile(walletPath)
require.NoError(t, err)
defer w.Close()
actual := w.GetAccount(acc.PrivateKey().GetScriptHash())
require.NotNil(t, actual)
require.NoError(t, actual.Decrypt("somepass"))
})
})
}