code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_close_connections_to_hostname_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  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/libs/jsonrpc"
    23  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    24  	"code.vegaprotocol.io/vega/wallet/api"
    25  	"code.vegaprotocol.io/vega/wallet/api/mocks"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestAdminCloseConnectionsToHostname(t *testing.T) {
    32  	t.Run("Documentation matches the code", testAdminCloseConnectionsToHostnameSchemaCorrect)
    33  	t.Run("Closing a connection with invalid params fails", testAdminCloseConnectionsToHostnameWithInvalidParamsFails)
    34  	t.Run("Closing a connection with valid params succeeds", testAdminCloseConnectionsToHostnameWithValidParamsSucceeds)
    35  }
    36  
    37  func testAdminCloseConnectionsToHostnameSchemaCorrect(t *testing.T) {
    38  	assertEqualSchema(t, "admin.close_connections_to_hostname", api.AdminCloseConnectionsToHostnameParams{}, nil)
    39  }
    40  
    41  func testAdminCloseConnectionsToHostnameWithInvalidParamsFails(t *testing.T) {
    42  	tcs := []struct {
    43  		name          string
    44  		params        interface{}
    45  		expectedError error
    46  	}{
    47  		{
    48  			name:          "with nil params",
    49  			params:        nil,
    50  			expectedError: api.ErrParamsRequired,
    51  		}, {
    52  			name:          "with wrong type of params",
    53  			params:        "test",
    54  			expectedError: api.ErrParamsDoNotMatch,
    55  		}, {
    56  			name: "with empty hostname",
    57  			params: api.AdminCloseConnectionsToHostnameParams{
    58  				Hostname: "",
    59  			},
    60  			expectedError: api.ErrHostnameIsRequired,
    61  		},
    62  	}
    63  
    64  	for _, tc := range tcs {
    65  		t.Run(tc.name, func(tt *testing.T) {
    66  			// given
    67  			ctx := context.Background()
    68  
    69  			// setup
    70  			handler := newCloseConnectionsToHostnameHandler(tt)
    71  
    72  			// when
    73  			errorDetails := handler.handle(t, ctx, tc.params)
    74  
    75  			// then
    76  			assertInvalidParams(tt, errorDetails, tc.expectedError)
    77  		})
    78  	}
    79  }
    80  
    81  func testAdminCloseConnectionsToHostnameWithValidParamsSucceeds(t *testing.T) {
    82  	// given
    83  	ctx := context.Background()
    84  	hostname1 := vgrand.RandomStr(5)
    85  	hostname2 := vgrand.RandomStr(5)
    86  	wallet1 := vgrand.RandomStr(5)
    87  	wallet2 := vgrand.RandomStr(5)
    88  	wallet3 := vgrand.RandomStr(5)
    89  
    90  	// setup
    91  	handler := newCloseConnectionsToHostnameHandler(t)
    92  	// -- expected calls
    93  	handler.connectionsManager.EXPECT().ListSessionConnections().Times(1).Return([]api.Connection{
    94  		{
    95  			Hostname: hostname1,
    96  			Wallet:   wallet1,
    97  		}, {
    98  			Hostname: hostname1,
    99  			Wallet:   wallet2,
   100  		}, {
   101  			Hostname: hostname1,
   102  			Wallet:   wallet3,
   103  		}, {
   104  			Hostname: hostname2,
   105  			Wallet:   wallet1,
   106  		}, {
   107  			Hostname: hostname2,
   108  			Wallet:   wallet2,
   109  		}, {
   110  			Hostname: hostname2,
   111  			Wallet:   wallet3,
   112  		},
   113  	})
   114  	handler.connectionsManager.EXPECT().EndSessionConnection(hostname1, wallet1).Times(1)
   115  	handler.connectionsManager.EXPECT().EndSessionConnection(hostname1, wallet2).Times(1)
   116  	handler.connectionsManager.EXPECT().EndSessionConnection(hostname1, wallet3).Times(1)
   117  
   118  	// when
   119  	errorDetails := handler.handle(t, ctx, api.AdminCloseConnectionsToHostnameParams{
   120  		Hostname: hostname1,
   121  	})
   122  
   123  	// then
   124  	require.Nil(t, errorDetails)
   125  }
   126  
   127  type adminCloseConnectionsToHostnameHandler struct {
   128  	*api.AdminCloseConnectionsToHostname
   129  	ctrl               *gomock.Controller
   130  	connectionsManager *mocks.MockConnectionsManager
   131  	walletStore        *mocks.MockWalletStore
   132  }
   133  
   134  func (h *adminCloseConnectionsToHostnameHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) *jsonrpc.ErrorDetails {
   135  	t.Helper()
   136  
   137  	rawResult, err := h.Handle(ctx, params)
   138  	require.Empty(t, rawResult)
   139  	return err
   140  }
   141  
   142  func newCloseConnectionsToHostnameHandler(t *testing.T) *adminCloseConnectionsToHostnameHandler {
   143  	t.Helper()
   144  
   145  	ctrl := gomock.NewController(t)
   146  
   147  	walletStore := mocks.NewMockWalletStore(ctrl)
   148  	connectionsManager := mocks.NewMockConnectionsManager(ctrl)
   149  
   150  	return &adminCloseConnectionsToHostnameHandler{
   151  		AdminCloseConnectionsToHostname: api.NewAdminCloseConnectionsToHostname(connectionsManager),
   152  		ctrl:                            ctrl,
   153  		connectionsManager:              connectionsManager,
   154  		walletStore:                     walletStore,
   155  	}
   156  }