code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_list_networks_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 "fmt" 21 "testing" 22 23 "code.vegaprotocol.io/vega/libs/jsonrpc" 24 "code.vegaprotocol.io/vega/wallet/api" 25 "code.vegaprotocol.io/vega/wallet/api/mocks" 26 "code.vegaprotocol.io/vega/wallet/network" 27 28 "github.com/golang/mock/gomock" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestAdminListNetworks(t *testing.T) { 34 t.Run("Documentation matches the code", testAdminListNetworksSchemaCorrect) 35 t.Run("Listing networks succeeds", testListingNetworksSucceeds) 36 t.Run("Getting internal error during listing fails", testGettingInternalErrorDuringListingNetworksFails) 37 } 38 39 func testAdminListNetworksSchemaCorrect(t *testing.T) { 40 assertEqualSchema(t, "admin.list_networks", nil, api.AdminListNetworksResult{}) 41 } 42 43 func testListingNetworksSucceeds(t *testing.T) { 44 // given 45 fairground := &network.Network{ 46 Name: "fairground", 47 Metadata: []network.Metadata{ 48 { 49 Key: "category", 50 Value: "test", 51 }, 52 }, 53 } 54 mainnet := &network.Network{ 55 Name: "mainnet", 56 Metadata: []network.Metadata{ 57 { 58 Key: "category", 59 Value: "main", 60 }, 61 }, 62 } 63 local := &network.Network{ 64 Name: "local", 65 } 66 expectedNetworks := []api.AdminListNetworkResult{ 67 { 68 Name: fairground.Name, 69 Metadata: fairground.Metadata, 70 }, 71 { 72 Name: mainnet.Name, 73 Metadata: mainnet.Metadata, 74 }, 75 { 76 Name: local.Name, 77 Metadata: local.Metadata, 78 }, 79 } 80 81 // setup 82 handler := newListNetworksHandler(t) 83 // -- expected calls 84 handler.networkStore.EXPECT().ListNetworks().Times(1).Return([]string{"fairground", "mainnet", "local"}, nil) 85 gomock.InOrder( 86 handler.networkStore.EXPECT().GetNetwork("fairground").Times(1).Return(fairground, nil), 87 handler.networkStore.EXPECT().GetNetwork("mainnet").Times(1).Return(mainnet, nil), 88 handler.networkStore.EXPECT().GetNetwork("local").Times(1).Return(local, nil), 89 ) 90 91 // when 92 result, errorDetails := handler.handle(t, context.Background(), nil) 93 94 // then 95 require.Nil(t, errorDetails) 96 assert.Equal(t, expectedNetworks, result.Networks) 97 } 98 99 func testGettingInternalErrorDuringListingNetworksFails(t *testing.T) { 100 // setup 101 handler := newListNetworksHandler(t) 102 // -- expected calls 103 handler.networkStore.EXPECT().ListNetworks().Times(1).Return(nil, assert.AnError) 104 105 // when 106 result, errorDetails := handler.handle(t, context.Background(), nil) 107 108 // then 109 require.NotNil(t, errorDetails) 110 assert.Empty(t, result) 111 assertInternalError(t, errorDetails, fmt.Errorf("could not list the networks: %w", assert.AnError)) 112 } 113 114 type listNetworksHandler struct { 115 *api.AdminListNetworks 116 ctrl *gomock.Controller 117 networkStore *mocks.MockNetworkStore 118 } 119 120 func (h *listNetworksHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (api.AdminListNetworksResult, *jsonrpc.ErrorDetails) { 121 t.Helper() 122 123 rawResult, err := h.Handle(ctx, params) 124 if rawResult != nil { 125 result, ok := rawResult.(api.AdminListNetworksResult) 126 if !ok { 127 t.Fatal("AdminListWallets handler result is not a AdminListWalletsResult") 128 } 129 return result, err 130 } 131 return api.AdminListNetworksResult{}, err 132 } 133 134 func newListNetworksHandler(t *testing.T) *listNetworksHandler { 135 t.Helper() 136 137 ctrl := gomock.NewController(t) 138 networkStore := mocks.NewMockNetworkStore(ctrl) 139 140 return &listNetworksHandler{ 141 AdminListNetworks: api.NewAdminListNetworks(networkStore), 142 ctrl: ctrl, 143 networkStore: networkStore, 144 } 145 }