code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_remove_wallet_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 TestAdminRemoveWallet(t *testing.T) {
    34  	t.Run("Documentation matches the code", testAdminRemoveWalletSchemaCorrect)
    35  	t.Run("Removing a wallet with invalid params fails", testRemovingWalletWithInvalidParamsFails)
    36  	t.Run("Removing a wallet with valid params succeeds", testRemovingWalletWithValidParamsSucceeds)
    37  	t.Run("Removing a wallet that does not exists fails", testRemovingWalletThatDoesNotExistsFails)
    38  	t.Run("Getting internal error during verification does not remove the wallet", testGettingInternalErrorDuringVerificationDoesNotRemoveWallet)
    39  }
    40  
    41  func testAdminRemoveWalletSchemaCorrect(t *testing.T) {
    42  	assertEqualSchema(t, "admin.remove_wallet", api.AdminRemoveWalletParams{}, nil)
    43  }
    44  
    45  func testRemovingWalletWithInvalidParamsFails(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.AdminRemoveWalletParams{
    62  				Wallet: "",
    63  			},
    64  			expectedError: api.ErrWalletIsRequired,
    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 := newRemoveWalletHandler(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 testRemovingWalletWithValidParamsSucceeds(t *testing.T) {
    87  	// given
    88  	ctx := context.Background()
    89  	name := vgrand.RandomStr(5)
    90  
    91  	// setup
    92  	handler := newRemoveWalletHandler(t)
    93  	// -- expected calls
    94  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil)
    95  	handler.walletStore.EXPECT().DeleteWallet(ctx, name).Times(1).Return(nil)
    96  
    97  	// when
    98  	result, errorDetails := handler.handle(t, ctx, api.AdminRemoveWalletParams{
    99  		Wallet: name,
   100  	})
   101  
   102  	// then
   103  	require.Nil(t, errorDetails)
   104  	assert.Nil(t, result)
   105  }
   106  
   107  func testRemovingWalletThatDoesNotExistsFails(t *testing.T) {
   108  	// given
   109  	ctx := context.Background()
   110  	name := vgrand.RandomStr(5)
   111  
   112  	// setup
   113  	handler := newRemoveWalletHandler(t)
   114  	// -- expected calls
   115  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, nil)
   116  
   117  	// when
   118  	result, errorDetails := handler.handle(t, ctx, api.AdminRemoveWalletParams{
   119  		Wallet: name,
   120  	})
   121  
   122  	// then
   123  	require.NotNil(t, errorDetails)
   124  	assert.Empty(t, result)
   125  	assertInvalidParams(t, errorDetails, api.ErrWalletDoesNotExist)
   126  }
   127  
   128  func testGettingInternalErrorDuringVerificationDoesNotRemoveWallet(t *testing.T) {
   129  	// given
   130  	ctx := context.Background()
   131  	name := vgrand.RandomStr(5)
   132  
   133  	// setup
   134  	handler := newRemoveWalletHandler(t)
   135  	// -- expected calls
   136  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, assert.AnError)
   137  
   138  	// when
   139  	result, errorDetails := handler.handle(t, ctx, api.AdminRemoveWalletParams{
   140  		Wallet: 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 wallet exists: %w", assert.AnError))
   147  }
   148  
   149  type removeWalletHandler struct {
   150  	*api.AdminRemoveWallet
   151  	ctrl        *gomock.Controller
   152  	walletStore *mocks.MockWalletStore
   153  }
   154  
   155  func (h *removeWalletHandler) 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 newRemoveWalletHandler(t *testing.T) *removeWalletHandler {
   162  	t.Helper()
   163  
   164  	ctrl := gomock.NewController(t)
   165  	walletStore := mocks.NewMockWalletStore(ctrl)
   166  
   167  	return &removeWalletHandler{
   168  		AdminRemoveWallet: api.NewAdminRemoveWallet(walletStore),
   169  		ctrl:              ctrl,
   170  		walletStore:       walletStore,
   171  	}
   172  }