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