github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountcreateadd_test.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package account 7 8 import ( 9 "strings" 10 "testing" 11 12 "github.com/golang/mock/gomock" 13 "github.com/pkg/errors" 14 "github.com/stretchr/testify/require" 15 16 "github.com/iotexproject/iotex-core/ioctl/config" 17 "github.com/iotexproject/iotex-core/ioctl/util" 18 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 19 ) 20 21 func TestNewAccountCreateAdd(t *testing.T) { 22 require := require.New(t) 23 ctrl := gomock.NewController(t) 24 client := mock_ioctlclient.NewMockClient(ctrl) 25 client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() 26 27 testWallet, ks, pwd, _, err := newTestAccountWithKeyStore(t, veryLightScryptN, veryLightScryptP) 28 require.NoError(err) 29 30 client.EXPECT().ReadSecret().Return(pwd, nil).Times(4) 31 client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(2) 32 client.EXPECT().Config().Return(config.Config{ 33 Wallet: testWallet, 34 Aliases: map[string]string{ 35 "aaa": "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx", 36 "bbb": "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542s1", 37 }, 38 }).Times(4) 39 client.EXPECT().NewKeyStore().Return(ks).Times(1) 40 client.EXPECT().SetAliasAndSave(gomock.Any(), gomock.Any()).Return(nil).Times(2) 41 42 t.Run("CryptoSm2 is true", func(t *testing.T) { 43 client.EXPECT().IsCryptoSm2().Return(true) 44 45 cmd := NewAccountCreateAdd(client) 46 _, err = util.ExecuteCmd(cmd, "aaa") 47 require.NoError(err) 48 }) 49 50 t.Run("CryptoSm2 is false", func(t *testing.T) { 51 client.EXPECT().IsCryptoSm2().Return(false) 52 53 cmd := NewAccountCreateAdd(client) 54 _, err = util.ExecuteCmd(cmd, "aaa") 55 require.NoError(err) 56 }) 57 58 t.Run("failed to confirm", func(t *testing.T) { 59 client.EXPECT().AskToConfirm(gomock.Any()).Return(false, nil) 60 61 cmd := NewAccountCreateAdd(client) 62 _, err := util.ExecuteCmd(cmd, "aaa") 63 require.NoError(err) 64 }) 65 66 t.Run("invalid alias", func(t *testing.T) { 67 expectedErr := errors.New("invalid long alias that is more than 40 characters") 68 69 cmd := NewAccountCreateAdd(client) 70 _, err := util.ExecuteCmd(cmd, strings.Repeat("a", 50)) 71 require.Contains(err.Error(), expectedErr.Error()) 72 }) 73 }