code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_describe_network_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 vgrand "code.vegaprotocol.io/vega/libs/rand" 25 "code.vegaprotocol.io/vega/wallet/api" 26 "code.vegaprotocol.io/vega/wallet/api/mocks" 27 28 "github.com/golang/mock/gomock" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestAdminDescribeNetwork(t *testing.T) { 34 t.Run("Documentation matches the code", testAdminDescribeNetworkSchemaCorrect) 35 t.Run("Describing a network with invalid params fails", testDescribingNetworkWithInvalidParamsFails) 36 t.Run("Describing a network with valid params succeeds", testDescribingNetworkWithValidParamsSucceeds) 37 t.Run("Describing a network with empty hosts returns non-nil slice", testDescribeNetworkEmptyHosts) 38 t.Run("Describing a network that does not exists fails", testDescribingNetworkThatDoesNotExistsFails) 39 t.Run("Getting internal error during verification fails", testGettingInternalErrorDuringNetworkVerificationFails) 40 t.Run("Getting internal error during retrieval fails", testGettingInternalErrorDuringNetworkRetrievalFails) 41 } 42 43 func testAdminDescribeNetworkSchemaCorrect(t *testing.T) { 44 assertEqualSchema(t, "admin.describe_network", api.AdminDescribeNetworkParams{}, api.AdminNetwork{}) 45 } 46 47 func testDescribingNetworkWithInvalidParamsFails(t *testing.T) { 48 tcs := []struct { 49 name string 50 params interface{} 51 expectedError error 52 }{ 53 { 54 name: "with nil params", 55 params: nil, 56 expectedError: api.ErrParamsRequired, 57 }, 58 { 59 name: "with wrong type of params", 60 params: "test", 61 expectedError: api.ErrParamsDoNotMatch, 62 }, 63 { 64 name: "with empty network", 65 params: api.AdminDescribeNetworkParams{ 66 Name: "", 67 }, 68 expectedError: api.ErrNetworkNameIsRequired, 69 }, 70 } 71 72 for _, tc := range tcs { 73 t.Run(tc.name, func(tt *testing.T) { 74 // given 75 ctx := context.Background() 76 77 // setup 78 handler := newDescribeNetworkHandler(tt) 79 80 // when 81 result, errorDetails := handler.handle(t, ctx, tc.params) 82 83 // then 84 require.Empty(tt, result) 85 assertInvalidParams(tt, errorDetails, tc.expectedError) 86 }) 87 } 88 } 89 90 func testDescribingNetworkWithValidParamsSucceeds(t *testing.T) { 91 // given 92 ctx := context.Background() 93 network := newNetwork(t) 94 95 // setup 96 handler := newDescribeNetworkHandler(t) 97 // -- expected calls 98 handler.networkStore.EXPECT().NetworkExists(network.Name).Times(1).Return(true, nil) 99 handler.networkStore.EXPECT().GetNetwork(network.Name).Times(1).Return(&network, nil) 100 101 // when 102 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeNetworkParams{ 103 Name: network.Name, 104 }) 105 106 // then 107 require.Nil(t, errorDetails) 108 assert.Equal(t, network.Name, result.Name) 109 assert.Equal(t, network.API.GRPC.Hosts, result.API.GRPC.Hosts) 110 assert.Equal(t, network.API.REST.Hosts, result.API.REST.Hosts) 111 assert.Equal(t, network.API.GraphQL.Hosts, result.API.GraphQL.Hosts) 112 } 113 114 func testDescribeNetworkEmptyHosts(t *testing.T) { 115 // given 116 ctx := context.Background() 117 network := newNetwork(t) 118 network.API.GRPC.Hosts = nil 119 network.API.REST.Hosts = nil 120 network.API.GraphQL.Hosts = nil 121 122 // setup 123 handler := newDescribeNetworkHandler(t) 124 // -- expected calls 125 handler.networkStore.EXPECT().NetworkExists(network.Name).Times(1).Return(true, nil) 126 handler.networkStore.EXPECT().GetNetwork(network.Name).Times(1).Return(&network, nil) 127 128 // when 129 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeNetworkParams{ 130 Name: network.Name, 131 }) 132 133 // then 134 require.Nil(t, errorDetails) 135 assert.NotNil(t, result.API.GRPC.Hosts) 136 assert.NotNil(t, result.API.REST.Hosts) 137 assert.NotNil(t, result.API.GraphQL.Hosts) 138 } 139 140 func testDescribingNetworkThatDoesNotExistsFails(t *testing.T) { 141 // given 142 ctx := context.Background() 143 name := vgrand.RandomStr(5) 144 145 // setup 146 handler := newDescribeNetworkHandler(t) 147 // -- expected calls 148 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(false, nil) 149 150 // when 151 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeNetworkParams{ 152 Name: name, 153 }) 154 155 // then 156 require.NotNil(t, errorDetails) 157 assert.Empty(t, result) 158 assertInvalidParams(t, errorDetails, api.ErrNetworkDoesNotExist) 159 } 160 161 func testGettingInternalErrorDuringNetworkVerificationFails(t *testing.T) { 162 // given 163 ctx := context.Background() 164 name := vgrand.RandomStr(5) 165 166 // setup 167 handler := newDescribeNetworkHandler(t) 168 // -- expected calls 169 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, assert.AnError) 170 171 // when 172 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeNetworkParams{ 173 Name: name, 174 }) 175 176 // then 177 require.NotNil(t, errorDetails) 178 assert.Empty(t, result) 179 assertInternalError(t, errorDetails, fmt.Errorf("could not verify the network existence: %w", assert.AnError)) 180 } 181 182 func testGettingInternalErrorDuringNetworkRetrievalFails(t *testing.T) { 183 // given 184 ctx := context.Background() 185 name := vgrand.RandomStr(5) 186 187 // setup 188 handler := newDescribeNetworkHandler(t) 189 // -- expected calls 190 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil) 191 handler.networkStore.EXPECT().GetNetwork(name).Times(1).Return(nil, assert.AnError) 192 193 // when 194 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeNetworkParams{ 195 Name: name, 196 }) 197 198 // then 199 require.NotNil(t, errorDetails) 200 assert.Empty(t, result) 201 assertInternalError(t, errorDetails, fmt.Errorf("could not retrieve the network configuration: %w", assert.AnError)) 202 } 203 204 type describeNetworkHandler struct { 205 *api.AdminDescribeNetwork 206 ctrl *gomock.Controller 207 networkStore *mocks.MockNetworkStore 208 } 209 210 func (h *describeNetworkHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (api.AdminNetwork, *jsonrpc.ErrorDetails) { 211 t.Helper() 212 213 rawResult, err := h.Handle(ctx, params) 214 if rawResult != nil { 215 result, ok := rawResult.(api.AdminNetwork) 216 if !ok { 217 t.Fatal("AdminDescribeNetwork handler result is not a AdminNetwork") 218 } 219 return result, err 220 } 221 return api.AdminNetwork{}, err 222 } 223 224 func newDescribeNetworkHandler(t *testing.T) *describeNetworkHandler { 225 t.Helper() 226 227 ctrl := gomock.NewController(t) 228 networkStore := mocks.NewMockNetworkStore(ctrl) 229 230 return &describeNetworkHandler{ 231 AdminDescribeNetwork: api.NewAdminDescribeNetwork(networkStore), 232 ctrl: ctrl, 233 networkStore: networkStore, 234 } 235 }