github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/did/didgethash_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 "encoding/hex" 10 "testing" 11 12 "github.com/golang/mock/gomock" 13 "github.com/iotexproject/iotex-proto/golang/iotexapi" 14 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 15 "github.com/pkg/errors" 16 "github.com/stretchr/testify/require" 17 18 "github.com/iotexproject/iotex-core/ioctl/config" 19 "github.com/iotexproject/iotex-core/ioctl/util" 20 "github.com/iotexproject/iotex-core/test/identityset" 21 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 22 ) 23 24 func TestNewDidGetHashCmd(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 accAddr := identityset.Address(0).String() 31 did := "did:io:0x11111111111111111" 32 33 client.EXPECT().SelectTranslation(gomock.Any()).Return("did", config.English).Times(10) 34 client.EXPECT().Address(gomock.Any()).Return(accAddr, nil).Times(4) 35 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr, nil).Times(4) 36 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(4) 37 38 t.Run("get did hash", func(t *testing.T) { 39 apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(&iotexapi.ReadContractResponse{ 40 Data: hex.EncodeToString([]byte("60fe47b100000000000000000000000000000000000000000000000000000000")), 41 }, nil) 42 cmd := NewDidGetHashCmd(client) 43 _, err := util.ExecuteCmd(cmd, accAddr, did) 44 require.NoError(err) 45 }) 46 47 t.Run("failed to decode contract", func(t *testing.T) { 48 expectedErr := errors.New("failed to decode contract") 49 apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(&iotexapi.ReadContractResponse{ 50 Data: "test", 51 }, nil) 52 cmd := NewDidGetHashCmd(client) 53 _, err := util.ExecuteCmd(cmd, "test", did) 54 require.Contains(err.Error(), expectedErr.Error()) 55 }) 56 57 t.Run("DID does not exist", func(t *testing.T) { 58 expectedErr := errors.New("DID does not exist") 59 apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(&iotexapi.ReadContractResponse{ 60 Data: hex.EncodeToString([]byte("test")), 61 }, nil) 62 cmd := NewDidGetHashCmd(client) 63 _, err := util.ExecuteCmd(cmd, accAddr, did) 64 require.Contains(err.Error(), expectedErr.Error()) 65 }) 66 67 t.Run("failed to read contract", func(t *testing.T) { 68 expectedErr := errors.New("failed to read contract") 69 apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 70 cmd := NewDidGetHashCmd(client) 71 _, err := util.ExecuteCmd(cmd, accAddr, did) 72 require.Contains(err.Error(), expectedErr.Error()) 73 }) 74 75 t.Run("failed to get contract address", func(t *testing.T) { 76 expectedErr := errors.New("failed to get contract address") 77 client.EXPECT().Address(gomock.Any()).Return("", expectedErr) 78 cmd := NewDidGetHashCmd(client) 79 _, err := util.ExecuteCmd(cmd, "test", did) 80 require.Contains(err.Error(), expectedErr.Error()) 81 }) 82 }