code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_remove_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 TestAdminRemoveNetwork(t *testing.T) { 34 t.Run("Documentation matches the code", testAdminRemoveNetworkSchemaCorrect) 35 t.Run("Removing a network with invalid params fails", testRemovingNetworkWithInvalidParamsFails) 36 t.Run("Removing a network with valid params succeeds", testRemovingNetworkWithValidParamsSucceeds) 37 t.Run("Removing a wallet that does not exists fails", testRemovingNetworkThatDoesNotExistsFails) 38 t.Run("Getting internal error during verification does not remove the wallet", testGettingInternalErrorDuringVerificationDoesNotRemoveNetwork) 39 } 40 41 func testAdminRemoveNetworkSchemaCorrect(t *testing.T) { 42 assertEqualSchema(t, "admin.remove_network", api.AdminRemoveNetworkParams{}, nil) 43 } 44 45 func testRemovingNetworkWithInvalidParamsFails(t *testing.T) { 46 tcs := []struct { 47 name string 48 params interface{} 49 expectedError error 50 }{ 51 { 52 name: "with nil params", 53 params: nil, 54 expectedError: api.ErrParamsRequired, 55 }, { 56 name: "with wrong type of params", 57 params: "test", 58 expectedError: api.ErrParamsDoNotMatch, 59 }, { 60 name: "with empty name", 61 params: api.AdminRemoveNetworkParams{ 62 Name: "", 63 }, 64 expectedError: api.ErrNetworkNameIsRequired, 65 }, 66 } 67 68 for _, tc := range tcs { 69 t.Run(tc.name, func(tt *testing.T) { 70 // given 71 ctx := context.Background() 72 73 // setup 74 handler := newRemoveNetworkHandler(tt) 75 76 // when 77 result, errorDetails := handler.handle(t, ctx, tc.params) 78 79 // then 80 require.Empty(tt, result) 81 assertInvalidParams(tt, errorDetails, tc.expectedError) 82 }) 83 } 84 } 85 86 func testRemovingNetworkWithValidParamsSucceeds(t *testing.T) { 87 // given 88 ctx := context.Background() 89 name := vgrand.RandomStr(5) 90 91 // setup 92 handler := newRemoveNetworkHandler(t) 93 // -- expected calls 94 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil) 95 handler.networkStore.EXPECT().DeleteNetwork(name).Times(1).Return(nil) 96 97 // when 98 result, errorDetails := handler.handle(t, ctx, api.AdminRemoveNetworkParams{ 99 Name: name, 100 }) 101 102 // then 103 require.Nil(t, errorDetails) 104 assert.Nil(t, result) 105 } 106 107 func testRemovingNetworkThatDoesNotExistsFails(t *testing.T) { 108 // given 109 ctx := context.Background() 110 name := vgrand.RandomStr(5) 111 112 // setup 113 handler := newRemoveNetworkHandler(t) 114 // -- expected calls 115 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(false, nil) 116 117 // when 118 result, errorDetails := handler.handle(t, ctx, api.AdminRemoveNetworkParams{ 119 Name: name, 120 }) 121 122 // then 123 require.NotNil(t, errorDetails) 124 assert.Empty(t, result) 125 assertInvalidParams(t, errorDetails, api.ErrNetworkDoesNotExist) 126 } 127 128 func testGettingInternalErrorDuringVerificationDoesNotRemoveNetwork(t *testing.T) { 129 // given 130 ctx := context.Background() 131 name := vgrand.RandomStr(5) 132 133 // setup 134 handler := newRemoveNetworkHandler(t) 135 // -- expected calls 136 handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(false, assert.AnError) 137 138 // when 139 result, errorDetails := handler.handle(t, ctx, api.AdminRemoveNetworkParams{ 140 Name: name, 141 }) 142 143 // then 144 require.NotNil(t, errorDetails) 145 assert.Empty(t, result) 146 assertInternalError(t, errorDetails, fmt.Errorf("could not verify the network existence: %w", assert.AnError)) 147 } 148 149 type removeNetworkHandler struct { 150 *api.AdminRemoveNetwork 151 ctrl *gomock.Controller 152 networkStore *mocks.MockNetworkStore 153 } 154 155 func (h *removeNetworkHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 156 t.Helper() 157 158 return h.Handle(ctx, params) 159 } 160 161 func newRemoveNetworkHandler(t *testing.T) *removeNetworkHandler { 162 t.Helper() 163 164 ctrl := gomock.NewController(t) 165 networkStore := mocks.NewMockNetworkStore(ctrl) 166 167 return &removeNetworkHandler{ 168 AdminRemoveNetwork: api.NewAdminRemoveNetwork(networkStore), 169 ctrl: ctrl, 170 networkStore: networkStore, 171 } 172 }