github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/did/didupdate_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 did 7 8 import ( 9 "testing" 10 11 "github.com/ethereum/go-ethereum/accounts/keystore" 12 "github.com/golang/mock/gomock" 13 "github.com/iotexproject/iotex-address/address" 14 "github.com/iotexproject/iotex-proto/golang/iotexapi" 15 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 16 "github.com/iotexproject/iotex-proto/golang/iotextypes" 17 "github.com/pkg/errors" 18 "github.com/stretchr/testify/require" 19 20 "github.com/iotexproject/iotex-core/ioctl/config" 21 "github.com/iotexproject/iotex-core/ioctl/util" 22 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 23 ) 24 25 func TestNewDidUpdateCmd(t *testing.T) { 26 require := require.New(t) 27 ctrl := gomock.NewController(t) 28 defer ctrl.Finish() 29 client := mock_ioctlclient.NewMockClient(ctrl) 30 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 31 payload := "0a10080118a08d062202313062040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a4161e219c2c5d5987f8a9efa33e8df0cde9d5541689fff05784cdc24f12e9d9ee8283a5aa720f494b949535b7969c07633dfb68c4ef9359eb16edb9abc6ebfadc801" 32 33 ks := keystore.NewKeyStore(t.TempDir(), 2, 1) 34 acc, err := ks.NewAccount("") 35 require.NoError(err) 36 accAddr, err := address.FromBytes(acc.Address.Bytes()) 37 require.NoError(err) 38 39 client.EXPECT().SelectTranslation(gomock.Any()).Return("did", config.English).AnyTimes() 40 client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(3) 41 client.EXPECT().Alias(gomock.Any()).Return("producer", nil).AnyTimes() 42 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes() 43 client.EXPECT().IsCryptoSm2().Return(false).Times(3) 44 client.EXPECT().ReadSecret().Return("", nil).Times(1) 45 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(1) 46 client.EXPECT().NewKeyStore().Return(ks).Times(2) 47 client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(1) 48 client.EXPECT().Config().Return(config.Config{ 49 Explorer: "iotexscan", 50 Endpoint: "testnet1", 51 }).Times(2) 52 53 accountResp := &iotexapi.GetAccountResponse{ 54 AccountMeta: &iotextypes.AccountMeta{ 55 IsContract: false, 56 PendingNonce: 10, 57 Balance: "100000000000000000000", 58 }, 59 } 60 chainMetaResp := &iotexapi.GetChainMetaResponse{ 61 ChainMeta: &iotextypes.ChainMeta{ 62 ChainID: 0, 63 }, 64 } 65 sendActionResp := &iotexapi.SendActionResponse{} 66 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil).Times(2) 67 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResp, nil).Times(1) 68 apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(sendActionResp, nil).Times(1) 69 70 t.Run("update did", func(t *testing.T) { 71 cmd := NewDidUpdateCmd(client) 72 result, err := util.ExecuteCmd(cmd, accAddr.String(), payload, "test", "--signer", accAddr.String()) 73 require.NoError(err) 74 require.Contains(result, "Action has been sent to blockchain.") 75 }) 76 77 t.Run("failed to decode data", func(t *testing.T) { 78 expectedErr := errors.New("failed to decode data") 79 cmd := NewDidUpdateCmd(client) 80 _, err := util.ExecuteCmd(cmd, accAddr.String(), "test", "test", "--signer", accAddr.String()) 81 require.Contains(err.Error(), expectedErr.Error()) 82 }) 83 84 t.Run("failed to get contract address", func(t *testing.T) { 85 expectedErr := errors.New("failed to get contract address") 86 client.EXPECT().Address(gomock.Any()).Return("", expectedErr) 87 cmd := NewDidUpdateCmd(client) 88 _, err := util.ExecuteCmd(cmd, accAddr.String(), "test", "test", "--signer", accAddr.String()) 89 require.Contains(err.Error(), expectedErr.Error()) 90 }) 91 }