github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/did/didderegister_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/stretchr/testify/require"
    18  
    19  	"github.com/iotexproject/iotex-core/ioctl/config"
    20  	"github.com/iotexproject/iotex-core/ioctl/util"
    21  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    22  )
    23  
    24  func TestNewDeregisterCmd(t *testing.T) {
    25  	require := require.New(t)
    26  	ctrl := gomock.NewController(t)
    27  	defer ctrl.Finish()
    28  	client := mock_ioctlclient.NewMockClient(ctrl)
    29  	apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
    30  
    31  	ks := keystore.NewKeyStore(t.TempDir(), 2, 1)
    32  	acc, err := ks.NewAccount("")
    33  	require.NoError(err)
    34  	accAddr, err := address.FromBytes(acc.Address.Bytes())
    35  	require.NoError(err)
    36  
    37  	client.EXPECT().SelectTranslation(gomock.Any()).Return("did", config.English).AnyTimes()
    38  	client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(2)
    39  	client.EXPECT().Alias(gomock.Any()).Return("producer", nil).AnyTimes()
    40  	client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes()
    41  	client.EXPECT().IsCryptoSm2().Return(false).Times(3)
    42  	client.EXPECT().ReadSecret().Return("", nil).Times(1)
    43  	client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(1)
    44  	client.EXPECT().NewKeyStore().Return(ks).Times(2)
    45  	client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(1)
    46  	client.EXPECT().Config().Return(config.Config{
    47  		Explorer: "iotexscan",
    48  		Endpoint: "testnet1",
    49  	}).Times(2)
    50  
    51  	accountResp := &iotexapi.GetAccountResponse{
    52  		AccountMeta: &iotextypes.AccountMeta{
    53  			IsContract:   false,
    54  			PendingNonce: 10,
    55  			Balance:      "100000000000000000000",
    56  		},
    57  	}
    58  	chainMetaResp := &iotexapi.GetChainMetaResponse{
    59  		ChainMeta: &iotextypes.ChainMeta{
    60  			ChainID: 0,
    61  		},
    62  	}
    63  	sendActionResp := &iotexapi.SendActionResponse{}
    64  	apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil).Times(2)
    65  	apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResp, nil).AnyTimes()
    66  	apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(sendActionResp, nil).Times(1)
    67  
    68  	t.Run("deregister did", func(t *testing.T) {
    69  		cmd := NewDeregisterCmd(client)
    70  		result, err := util.ExecuteCmd(cmd, accAddr.String(), "--signer", accAddr.String())
    71  		require.NoError(err)
    72  		require.Contains(result, "Action has been sent to blockchain")
    73  	})
    74  }