code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_rename_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 TestAdminRenameNetwork(t *testing.T) { 34 t.Run("Documentation matches the code", testAdminRenameNetworkSchemaCorrect) 35 t.Run("Renaming a network with invalid params fails", testRenamingNetworkWithInvalidParamsFails) 36 t.Run("Renaming a network with valid params succeeds", testRenamingNetworkWithValidParamsSucceeds) 37 t.Run("Renaming a network that does not exists fails", testRenamingNetworkThatDoesNotExistsFails) 38 t.Run("Getting internal error during existing network verification does not rename the network", testGettingInternalErrorDuringExistingNetworkVerificationDoesNotRenameNetwork) 39 t.Run("Renaming a network that with name that is already taken fails", testRenamingNetworkWithNameAlreadyTakenFails) 40 t.Run("Getting internal error during non-existing network verification does not rename the network", testGettingInternalErrorDuringNonExistingNetworkVerificationDoesNotRenameNetwork) 41 t.Run("Getting internal error during renaming does not rename the network", testGettingInternalErrorDuringRenamingDoesNotRenameNetwork) 42 } 43 44 func testAdminRenameNetworkSchemaCorrect(t *testing.T) { 45 assertEqualSchema(t, "admin.rename_network", api.AdminRenameNetworkParams{}, nil) 46 } 47 48 func testRenamingNetworkWithInvalidParamsFails(t *testing.T) { 49 tcs := []struct { 50 name string 51 params interface{} 52 expectedError error 53 }{ 54 { 55 name: "with nil params", 56 params: nil, 57 expectedError: api.ErrParamsRequired, 58 }, { 59 name: "with wrong type of params", 60 params: "test", 61 expectedError: api.ErrParamsDoNotMatch, 62 }, { 63 name: "with empty name", 64 params: api.AdminRenameNetworkParams{ 65 Network: "", 66 NewName: vgrand.RandomStr(5), 67 }, 68 expectedError: api.ErrNetworkIsRequired, 69 }, { 70 name: "with empty new name", 71 params: api.AdminRenameNetworkParams{ 72 Network: vgrand.RandomStr(5), 73 NewName: "", 74 }, 75 expectedError: api.ErrNewNameIsRequired, 76 }, 77 } 78 79 for _, tc := range tcs { 80 t.Run(tc.name, func(tt *testing.T) { 81 // given 82 ctx := context.Background() 83 84 // setup 85 handler := newRenameNetworkHandler(tt) 86 87 // when 88 errorDetails := handler.handle(t, ctx, tc.params) 89 90 // then 91 assertInvalidParams(tt, errorDetails, tc.expectedError) 92 }) 93 } 94 } 95 96 func testRenamingNetworkWithValidParamsSucceeds(t *testing.T) { 97 // given 98 ctx := context.Background() 99 name := vgrand.RandomStr(5) 100 newName := vgrand.RandomStr(5) 101 102 // setup 103 handler := newRenameNetworkHandler(t) 104 // -- expected calls 105 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil) 106 handler.networkStore.EXPECT().NetworkExists(newName).Times(1).Return(false, nil) 107 handler.networkStore.EXPECT().RenameNetwork(name, newName).Times(1).Return(nil) 108 109 // when 110 errorDetails := handler.handle(t, ctx, api.AdminRenameNetworkParams{ 111 Network: name, 112 NewName: newName, 113 }) 114 115 // then 116 require.Nil(t, errorDetails) 117 } 118 119 func testRenamingNetworkThatDoesNotExistsFails(t *testing.T) { 120 // given 121 ctx := context.Background() 122 name := vgrand.RandomStr(5) 123 newName := vgrand.RandomStr(5) 124 125 // setup 126 handler := newRenameNetworkHandler(t) 127 // -- expected calls 128 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(false, nil) 129 130 // when 131 errorDetails := handler.handle(t, ctx, api.AdminRenameNetworkParams{ 132 Network: name, 133 NewName: newName, 134 }) 135 136 // then 137 require.NotNil(t, errorDetails) 138 assertInvalidParams(t, errorDetails, api.ErrNetworkDoesNotExist) 139 } 140 141 func testGettingInternalErrorDuringExistingNetworkVerificationDoesNotRenameNetwork(t *testing.T) { 142 // given 143 ctx := context.Background() 144 name := vgrand.RandomStr(5) 145 newName := vgrand.RandomStr(5) 146 147 // setup 148 handler := newRenameNetworkHandler(t) 149 // -- expected calls 150 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(false, assert.AnError) 151 152 // when 153 errorDetails := handler.handle(t, ctx, api.AdminRenameNetworkParams{ 154 Network: name, 155 NewName: newName, 156 }) 157 158 // then 159 require.NotNil(t, errorDetails) 160 assertInternalError(t, errorDetails, fmt.Errorf("could not verify the network existence: %w", assert.AnError)) 161 } 162 163 func testRenamingNetworkWithNameAlreadyTakenFails(t *testing.T) { 164 // given 165 ctx := context.Background() 166 name := vgrand.RandomStr(5) 167 newName := vgrand.RandomStr(5) 168 169 // setup 170 handler := newRenameNetworkHandler(t) 171 // -- expected calls 172 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil) 173 handler.networkStore.EXPECT().NetworkExists(newName).Times(1).Return(true, nil) 174 175 // when 176 errorDetails := handler.handle(t, ctx, api.AdminRenameNetworkParams{ 177 Network: name, 178 NewName: newName, 179 }) 180 181 // then 182 require.NotNil(t, errorDetails) 183 assertInvalidParams(t, errorDetails, api.ErrNetworkAlreadyExists) 184 } 185 186 func testGettingInternalErrorDuringNonExistingNetworkVerificationDoesNotRenameNetwork(t *testing.T) { 187 // given 188 ctx := context.Background() 189 name := vgrand.RandomStr(5) 190 newName := vgrand.RandomStr(5) 191 192 // setup 193 handler := newRenameNetworkHandler(t) 194 // -- expected calls 195 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil) 196 handler.networkStore.EXPECT().NetworkExists(newName).Times(1).Return(false, assert.AnError) 197 198 // when 199 errorDetails := handler.handle(t, ctx, api.AdminRenameNetworkParams{ 200 Network: name, 201 NewName: newName, 202 }) 203 204 // then 205 require.NotNil(t, errorDetails) 206 assertInternalError(t, errorDetails, fmt.Errorf("could not verify the network existence: %w", assert.AnError)) 207 } 208 209 func testGettingInternalErrorDuringRenamingDoesNotRenameNetwork(t *testing.T) { 210 // given 211 ctx := context.Background() 212 name := vgrand.RandomStr(5) 213 newName := vgrand.RandomStr(5) 214 215 // setup 216 handler := newRenameNetworkHandler(t) 217 // -- expected calls 218 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil) 219 handler.networkStore.EXPECT().NetworkExists(newName).Times(1).Return(false, nil) 220 handler.networkStore.EXPECT().RenameNetwork(name, newName).Times(1).Return(assert.AnError) 221 222 // when 223 errorDetails := handler.handle(t, ctx, api.AdminRenameNetworkParams{ 224 Network: name, 225 NewName: newName, 226 }) 227 228 // then 229 require.NotNil(t, errorDetails) 230 assertInternalError(t, errorDetails, fmt.Errorf("could not rename the network: %w", assert.AnError)) 231 } 232 233 type renameNetworkHandler struct { 234 *api.AdminRenameNetwork 235 ctrl *gomock.Controller 236 networkStore *mocks.MockNetworkStore 237 } 238 239 func (h *renameNetworkHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) *jsonrpc.ErrorDetails { 240 t.Helper() 241 242 rawResult, err := h.Handle(ctx, params) 243 require.Nil(t, rawResult) 244 return err 245 } 246 247 func newRenameNetworkHandler(t *testing.T) *renameNetworkHandler { 248 t.Helper() 249 250 ctrl := gomock.NewController(t) 251 networkStore := mocks.NewMockNetworkStore(ctrl) 252 253 return &renameNetworkHandler{ 254 AdminRenameNetwork: api.NewAdminRenameNetwork(networkStore), 255 ctrl: ctrl, 256 networkStore: networkStore, 257 } 258 }