code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_update_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  	"code.vegaprotocol.io/vega/wallet/network"
    28  
    29  	"github.com/golang/mock/gomock"
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestAdminUpdateNetwork(t *testing.T) {
    35  	t.Run("Documentation matches the code", testAdminUpdateNetworkSchemaCorrect)
    36  	t.Run("Updating a network with invalid params fails", testUpdatingNetworkWithInvalidParamsFails)
    37  	t.Run("Updating a network with valid params succeeds", testUpdatingNetworkWithValidParamsSucceeds)
    38  	t.Run("Updating a network that does not exists fails", testUpdatingNetworkThatDoesNotExistsFails)
    39  	t.Run("Getting internal error during verification fails", testAdminUpdateNetworkGettingInternalErrorDuringNetworkVerificationFails)
    40  	t.Run("Getting internal error during retrieval fails", testAdminUpdateNetworkGettingInternalErrorDuringNetworkSavingFails)
    41  }
    42  
    43  func testAdminUpdateNetworkSchemaCorrect(t *testing.T) {
    44  	assertEqualSchema(t, "admin.update_network", network.Network{}, nil)
    45  }
    46  
    47  func testUpdatingNetworkWithInvalidParamsFails(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 name",
    65  			params: api.AdminNetwork{
    66  				Name: "",
    67  			},
    68  			expectedError: api.ErrNetworkNameIsRequired,
    69  		},
    70  		{
    71  			name: "without a single GRPC node",
    72  			params: api.AdminNetwork{
    73  				Name: "testnet",
    74  				API: api.AdminAPIConfig{
    75  					GRPC: api.AdminGRPCConfig{
    76  						Hosts: []string{},
    77  					},
    78  				},
    79  			},
    80  			expectedError: network.ErrNetworkDoesNotHaveGRPCHostConfigured,
    81  		},
    82  	}
    83  
    84  	for _, tc := range tcs {
    85  		t.Run(tc.name, func(tt *testing.T) {
    86  			// given
    87  			ctx := context.Background()
    88  
    89  			// setup
    90  			handler := newUpdateNetworkHandler(tt)
    91  
    92  			// when
    93  			errorDetails := handler.handle(t, ctx, tc.params)
    94  
    95  			// then
    96  			assertInvalidParams(tt, errorDetails, tc.expectedError)
    97  		})
    98  	}
    99  }
   100  
   101  func testUpdatingNetworkWithValidParamsSucceeds(t *testing.T) {
   102  	// given
   103  	ctx := context.Background()
   104  	name := vgrand.RandomStr(5)
   105  
   106  	// setup
   107  	handler := newUpdateNetworkHandler(t)
   108  	// -- expected calls
   109  	handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil)
   110  	handler.networkStore.EXPECT().SaveNetwork(&network.Network{
   111  		Name: name,
   112  		API: network.APIConfig{
   113  			GRPC: network.HostConfig{
   114  				Hosts: []string{
   115  					"localhost:1234",
   116  				},
   117  			},
   118  		},
   119  	}).Times(1).Return(nil)
   120  
   121  	// when
   122  	errorDetails := handler.handle(t, ctx, api.AdminNetwork{
   123  		Name: name,
   124  		API: api.AdminAPIConfig{
   125  			GRPC: api.AdminGRPCConfig{
   126  				Hosts: []string{
   127  					"localhost:1234",
   128  				},
   129  			},
   130  		},
   131  	})
   132  
   133  	// then
   134  	require.Nil(t, errorDetails)
   135  }
   136  
   137  func testUpdatingNetworkThatDoesNotExistsFails(t *testing.T) {
   138  	// given
   139  	ctx := context.Background()
   140  	name := vgrand.RandomStr(5)
   141  
   142  	// setup
   143  	handler := newUpdateNetworkHandler(t)
   144  	// -- expected calls
   145  	handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(false, nil)
   146  
   147  	// when
   148  	errorDetails := handler.handle(t, ctx, api.AdminNetwork{
   149  		Name: name,
   150  		API: api.AdminAPIConfig{
   151  			GRPC: api.AdminGRPCConfig{
   152  				Hosts: []string{
   153  					"localhost:1234",
   154  				},
   155  			},
   156  		},
   157  	})
   158  
   159  	// then
   160  	require.NotNil(t, errorDetails)
   161  	assertInvalidParams(t, errorDetails, api.ErrNetworkDoesNotExist)
   162  }
   163  
   164  func testAdminUpdateNetworkGettingInternalErrorDuringNetworkVerificationFails(t *testing.T) {
   165  	// given
   166  	ctx := context.Background()
   167  	name := vgrand.RandomStr(5)
   168  
   169  	// setup
   170  	handler := newUpdateNetworkHandler(t)
   171  	// -- expected calls
   172  	handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(false, assert.AnError)
   173  
   174  	// when
   175  	errorDetails := handler.handle(t, ctx, api.AdminNetwork{
   176  		Name: name,
   177  		API: api.AdminAPIConfig{
   178  			GRPC: api.AdminGRPCConfig{
   179  				Hosts: []string{
   180  					"localhost:1234",
   181  				},
   182  			},
   183  		},
   184  	})
   185  
   186  	// then
   187  	require.NotNil(t, errorDetails)
   188  	assertInternalError(t, errorDetails, fmt.Errorf("could not verify the network existence: %w", assert.AnError))
   189  }
   190  
   191  func testAdminUpdateNetworkGettingInternalErrorDuringNetworkSavingFails(t *testing.T) {
   192  	// given
   193  	ctx := context.Background()
   194  	name := vgrand.RandomStr(5)
   195  
   196  	// setup
   197  	handler := newUpdateNetworkHandler(t)
   198  	// -- expected calls
   199  	handler.networkStore.EXPECT().NetworkExists(name).Times(1).Return(true, nil)
   200  	handler.networkStore.EXPECT().SaveNetwork(gomock.Any()).Times(1).Return(assert.AnError)
   201  
   202  	// when
   203  	errorDetails := handler.handle(t, ctx, api.AdminNetwork{
   204  		Name: name,
   205  		API: api.AdminAPIConfig{
   206  			GRPC: api.AdminGRPCConfig{
   207  				Hosts: []string{
   208  					"localhost:1234",
   209  				},
   210  			},
   211  		},
   212  	})
   213  
   214  	// then
   215  	require.NotNil(t, errorDetails)
   216  	assertInternalError(t, errorDetails, fmt.Errorf("could not save the network: %w", assert.AnError))
   217  }
   218  
   219  type updateNetworkHandler struct {
   220  	*api.AdminUpdateNetwork
   221  	ctrl         *gomock.Controller
   222  	networkStore *mocks.MockNetworkStore
   223  }
   224  
   225  func (h *updateNetworkHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) *jsonrpc.ErrorDetails {
   226  	t.Helper()
   227  
   228  	rawResult, err := h.Handle(ctx, params)
   229  	assert.Nil(t, rawResult)
   230  	return err
   231  }
   232  
   233  func newUpdateNetworkHandler(t *testing.T) *updateNetworkHandler {
   234  	t.Helper()
   235  
   236  	ctrl := gomock.NewController(t)
   237  	networkStore := mocks.NewMockNetworkStore(ctrl)
   238  
   239  	return &updateNetworkHandler{
   240  		AdminUpdateNetwork: api.NewAdminUpdateNetwork(networkStore),
   241  		ctrl:               ctrl,
   242  		networkStore:       networkStore,
   243  	}
   244  }