code.vegaprotocol.io/vega@v0.79.0/wallet/api/client_get_chain_id_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package api_test 17 18 import ( 19 "context" 20 "testing" 21 22 "code.vegaprotocol.io/vega/libs/jsonrpc" 23 vgrand "code.vegaprotocol.io/vega/libs/rand" 24 "code.vegaprotocol.io/vega/wallet/api" 25 nodemocks "code.vegaprotocol.io/vega/wallet/api/node/mocks" 26 "code.vegaprotocol.io/vega/wallet/api/node/types" 27 28 "github.com/golang/mock/gomock" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestClientGetChainID(t *testing.T) { 34 t.Run("Documentation matches the code", testClientGetChainIDSchemaCorrect) 35 t.Run("Getting chain ID succeeds", testClientGettingChainIDSucceeds) 36 t.Run("No healthy node available does not return the chain ID", testClientGetChainIDNoHealthyNodeAvailableDoesNotReturnChainID) 37 t.Run("Failing to get the last block does not return the chain ID", testClientGetChainIDFailingToGetLastBlockDoesNotReturnChainID) 38 } 39 40 func testClientGetChainIDSchemaCorrect(t *testing.T) { 41 assertEqualSchema(t, "client.get_chain_id", nil, api.ClientGetChainIDResult{}) 42 } 43 44 func testClientGettingChainIDSucceeds(t *testing.T) { 45 // given 46 ctx := context.Background() 47 expectedChainID := vgrand.RandomStr(5) 48 49 // setup 50 handler := newGetChainIDHandler(t) 51 handler.nodeSelector.EXPECT().Node(ctx, gomock.Any()).Times(1).Return(handler.node, nil) 52 handler.node.EXPECT().LastBlock(ctx).Times(1).Return(types.LastBlock{ 53 ChainID: expectedChainID, 54 }, nil) 55 56 // when 57 result, errorDetails := handler.handle(t, ctx) 58 59 // then 60 assert.Nil(t, errorDetails) 61 require.NotEmpty(t, result) 62 assert.Equal(t, expectedChainID, result.ChainID) 63 } 64 65 func testClientGetChainIDNoHealthyNodeAvailableDoesNotReturnChainID(t *testing.T) { 66 // given 67 ctx := context.Background() 68 69 // setup 70 handler := newGetChainIDHandler(t) 71 handler.nodeSelector.EXPECT().Node(ctx, gomock.Any()).Times(1).Return(nil, assert.AnError) 72 73 // when 74 result, errorDetails := handler.handle(t, ctx) 75 76 // then 77 require.NotNil(t, errorDetails) 78 assert.Equal(t, api.ErrorCodeNodeCommunicationFailed, errorDetails.Code) 79 assert.Equal(t, "Network error", errorDetails.Message) 80 assert.Equal(t, api.ErrNoHealthyNodeAvailable.Error(), errorDetails.Data) 81 assert.Empty(t, result) 82 } 83 84 func testClientGetChainIDFailingToGetLastBlockDoesNotReturnChainID(t *testing.T) { 85 // given 86 ctx := context.Background() 87 88 // setup 89 handler := newGetChainIDHandler(t) 90 handler.nodeSelector.EXPECT().Node(ctx, gomock.Any()).Times(1).Return(handler.node, nil) 91 handler.node.EXPECT().LastBlock(ctx).Times(1).Return(types.LastBlock{}, assert.AnError) 92 93 // when 94 result, errorDetails := handler.handle(t, ctx) 95 96 // then 97 require.NotNil(t, errorDetails) 98 assert.Equal(t, api.ErrorCodeNodeCommunicationFailed, errorDetails.Code) 99 assert.Equal(t, "Network error", errorDetails.Message) 100 assert.Equal(t, api.ErrCouldNotGetLastBlockInformation.Error(), errorDetails.Data) 101 assert.Empty(t, result) 102 } 103 104 type GetChainIDHandler struct { 105 *api.ClientGetChainID 106 nodeSelector *nodemocks.MockSelector 107 node *nodemocks.MockNode 108 } 109 110 func (h *GetChainIDHandler) handle(t *testing.T, ctx context.Context) (api.ClientGetChainIDResult, *jsonrpc.ErrorDetails) { 111 t.Helper() 112 113 rawResult, err := h.Handle(ctx) 114 if rawResult != nil { 115 result, ok := rawResult.(api.ClientGetChainIDResult) 116 if !ok { 117 t.Fatal("ClientGetChainID handler result is not a ClientGetChainIDResult") 118 } 119 return result, err 120 } 121 return api.ClientGetChainIDResult{}, err 122 } 123 124 func newGetChainIDHandler(t *testing.T) *GetChainIDHandler { 125 t.Helper() 126 127 ctrl := gomock.NewController(t) 128 nodeSelector := nodemocks.NewMockSelector(ctrl) 129 node := nodemocks.NewMockNode(ctrl) 130 131 return &GetChainIDHandler{ 132 ClientGetChainID: api.NewGetChainID(nodeSelector), 133 nodeSelector: nodeSelector, 134 node: node, 135 } 136 }