code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_list_connections_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/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestAdminListConnections(t *testing.T) {
    33  	t.Run("Documentation matches the code", testAdminListConnectionsSchemaCorrect)
    34  	t.Run("Listing the connections succeeds", testAdminListConnectionsSucceeds)
    35  }
    36  
    37  func testAdminListConnectionsSchemaCorrect(t *testing.T) {
    38  	assertEqualSchema(t, "admin.list_connections", nil, api.AdminListConnectionsResult{})
    39  }
    40  
    41  func testAdminListConnectionsSucceeds(t *testing.T) {
    42  	// given
    43  	ctx := context.Background()
    44  	hostname1 := vgrand.RandomStr(5)
    45  	hostname2 := vgrand.RandomStr(5)
    46  	wallet1 := vgrand.RandomStr(5)
    47  	wallet2 := vgrand.RandomStr(5)
    48  	wallet3 := vgrand.RandomStr(5)
    49  	list := []api.Connection{
    50  		{
    51  			Hostname: hostname1,
    52  			Wallet:   wallet1,
    53  		}, {
    54  			Hostname: hostname1,
    55  			Wallet:   wallet2,
    56  		}, {
    57  			Hostname: hostname1,
    58  			Wallet:   wallet3,
    59  		}, {
    60  			Hostname: hostname2,
    61  			Wallet:   wallet1,
    62  		}, {
    63  			Hostname: hostname2,
    64  			Wallet:   wallet2,
    65  		}, {
    66  			Hostname: hostname2,
    67  			Wallet:   wallet3,
    68  		},
    69  	}
    70  
    71  	// setup
    72  	handler := newListConnectionsHandler(t)
    73  	// -- expected calls
    74  	handler.connectionsManager.EXPECT().ListSessionConnections().Times(1).Return(list)
    75  
    76  	// when
    77  	response, errorDetails := handler.handle(t, ctx, nil)
    78  
    79  	// then
    80  	require.Nil(t, errorDetails)
    81  	assert.Equal(t, list, response.ActiveConnections)
    82  }
    83  
    84  type adminListConnectionsHandler struct {
    85  	*api.AdminListConnections
    86  	ctrl               *gomock.Controller
    87  	connectionsManager *mocks.MockConnectionsManager
    88  }
    89  
    90  func (h *adminListConnectionsHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (api.AdminListConnectionsResult, *jsonrpc.ErrorDetails) {
    91  	t.Helper()
    92  
    93  	rawResult, err := h.Handle(ctx, params)
    94  	if rawResult != nil {
    95  		result, ok := rawResult.(api.AdminListConnectionsResult)
    96  		if !ok {
    97  			t.Fatal("AdminIsolateKey handler result is not a api.AdminListConnectionsResult")
    98  		}
    99  		return result, err
   100  	}
   101  	return api.AdminListConnectionsResult{}, err
   102  }
   103  
   104  func newListConnectionsHandler(t *testing.T) *adminListConnectionsHandler {
   105  	t.Helper()
   106  
   107  	ctrl := gomock.NewController(t)
   108  
   109  	connectionsManager := mocks.NewMockConnectionsManager(ctrl)
   110  
   111  	return &adminListConnectionsHandler{
   112  		AdminListConnections: api.NewAdminListConnections(connectionsManager),
   113  		ctrl:                 ctrl,
   114  		connectionsManager:   connectionsManager,
   115  	}
   116  }