code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_close_connection_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 TestAdminCloseConnection(t *testing.T) {
    32  	t.Run("Documentation matches the code", testAdminCloseConnectionSchemaCorrect)
    33  	t.Run("Closing a connection with invalid params fails", testAdminCloseConnectionWithInvalidParamsFails)
    34  	t.Run("Closing a connection with valid params succeeds", testAdminCloseConnectionWithValidParamsSucceeds)
    35  }
    36  
    37  func testAdminCloseConnectionSchemaCorrect(t *testing.T) {
    38  	assertEqualSchema(t, "admin.close_connection", api.AdminCloseConnectionParams{}, nil)
    39  }
    40  
    41  func testAdminCloseConnectionWithInvalidParamsFails(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 wallet",
    57  			params: api.AdminCloseConnectionParams{
    58  				Hostname: vgrand.RandomStr(5),
    59  				Wallet:   "",
    60  			},
    61  			expectedError: api.ErrWalletIsRequired,
    62  		}, {
    63  			name: "with empty hostname",
    64  			params: api.AdminCloseConnectionParams{
    65  				Wallet:   vgrand.RandomStr(5),
    66  				Hostname: "",
    67  			},
    68  			expectedError: api.ErrHostnameIsRequired,
    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 := newCloseConnectionHandler(tt)
    79  
    80  			// when
    81  			errorDetails := handler.handle(t, ctx, tc.params)
    82  
    83  			// then
    84  			assertInvalidParams(tt, errorDetails, tc.expectedError)
    85  		})
    86  	}
    87  }
    88  
    89  func testAdminCloseConnectionWithValidParamsSucceeds(t *testing.T) {
    90  	// given
    91  	ctx := context.Background()
    92  	hostname := vgrand.RandomStr(5)
    93  	wallet := vgrand.RandomStr(5)
    94  
    95  	// setup
    96  	handler := newCloseConnectionHandler(t)
    97  	// -- expected calls
    98  	handler.connectionsManager.EXPECT().EndSessionConnection(hostname, wallet).Times(1)
    99  
   100  	// when
   101  	errorDetails := handler.handle(t, ctx, api.AdminCloseConnectionParams{
   102  		Hostname: hostname,
   103  		Wallet:   wallet,
   104  	})
   105  
   106  	// then
   107  	require.Nil(t, errorDetails)
   108  }
   109  
   110  type adminCloseConnectionHandler struct {
   111  	*api.AdminCloseConnection
   112  	ctrl               *gomock.Controller
   113  	walletStore        *mocks.MockWalletStore
   114  	connectionsManager *mocks.MockConnectionsManager
   115  }
   116  
   117  func (h *adminCloseConnectionHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) *jsonrpc.ErrorDetails {
   118  	t.Helper()
   119  
   120  	rawResult, err := h.Handle(ctx, params)
   121  	require.Empty(t, rawResult)
   122  	return err
   123  }
   124  
   125  func newCloseConnectionHandler(t *testing.T) *adminCloseConnectionHandler {
   126  	t.Helper()
   127  
   128  	ctrl := gomock.NewController(t)
   129  
   130  	walletStore := mocks.NewMockWalletStore(ctrl)
   131  	connectionsManager := mocks.NewMockConnectionsManager(ctrl)
   132  
   133  	return &adminCloseConnectionHandler{
   134  		AdminCloseConnection: api.NewAdminCloseConnection(connectionsManager),
   135  		ctrl:                 ctrl,
   136  		connectionsManager:   connectionsManager,
   137  		walletStore:          walletStore,
   138  	}
   139  }