code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_rename_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 TestAdminRenameWallet(t *testing.T) {
    34  	t.Run("Documentation matches the code", testAdminRenameWalletSchemaCorrect)
    35  	t.Run("Renaming a wallet with invalid params fails", testRenamingWalletWithInvalidParamsFails)
    36  	t.Run("Renaming a wallet with valid params succeeds", testRenamingWalletWithValidParamsSucceeds)
    37  	t.Run("Renaming a wallet that does not exists fails", testRenamingWalletThatDoesNotExistsFails)
    38  	t.Run("Getting internal error during existing wallet verification does not rename the wallet", testGettingInternalErrorDuringExistingWalletVerificationDoesNotRenameWallet)
    39  	t.Run("Renaming a wallet that with name that is already taken fails", testRenamingWalletWithNameAlreadyTakenFails)
    40  	t.Run("Getting internal error during non-existing wallet verification does not rename the wallet", testGettingInternalErrorDuringNonExistingWalletVerificationDoesNotRenameWallet)
    41  	t.Run("Getting internal error during renaming does not rename the wallet", testGettingInternalErrorDuringRenamingDoesNotRenameWallet)
    42  }
    43  
    44  func testAdminRenameWalletSchemaCorrect(t *testing.T) {
    45  	assertEqualSchema(t, "admin.rename_wallet", api.AdminRenameWalletParams{}, nil)
    46  }
    47  
    48  func testRenamingWalletWithInvalidParamsFails(t *testing.T) {
    49  	tcs := []struct {
    50  		name          string
    51  		params        interface{}
    52  		expectedError error
    53  	}{
    54  		{
    55  			name:          "with nil params",
    56  			params:        nil,
    57  			expectedError: api.ErrParamsRequired,
    58  		}, {
    59  			name:          "with wrong type of params",
    60  			params:        "test",
    61  			expectedError: api.ErrParamsDoNotMatch,
    62  		}, {
    63  			name: "with empty name",
    64  			params: api.AdminRenameWalletParams{
    65  				Wallet:  "",
    66  				NewName: vgrand.RandomStr(5),
    67  			},
    68  			expectedError: api.ErrWalletIsRequired,
    69  		}, {
    70  			name: "with empty new name",
    71  			params: api.AdminRenameWalletParams{
    72  				Wallet:  vgrand.RandomStr(5),
    73  				NewName: "",
    74  			},
    75  			expectedError: api.ErrNewNameIsRequired,
    76  		},
    77  	}
    78  
    79  	for _, tc := range tcs {
    80  		t.Run(tc.name, func(tt *testing.T) {
    81  			// given
    82  			ctx := context.Background()
    83  
    84  			// setup
    85  			handler := newRenameWalletHandler(tt)
    86  
    87  			// when
    88  			errorDetails := handler.handle(t, ctx, tc.params)
    89  
    90  			// then
    91  			assertInvalidParams(tt, errorDetails, tc.expectedError)
    92  		})
    93  	}
    94  }
    95  
    96  func testRenamingWalletWithValidParamsSucceeds(t *testing.T) {
    97  	// given
    98  	ctx := context.Background()
    99  	name := vgrand.RandomStr(5)
   100  	newName := vgrand.RandomStr(5)
   101  
   102  	// setup
   103  	handler := newRenameWalletHandler(t)
   104  	// -- expected calls
   105  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil)
   106  	handler.walletStore.EXPECT().WalletExists(ctx, newName).Times(1).Return(false, nil)
   107  	handler.walletStore.EXPECT().RenameWallet(ctx, name, newName).Times(1).Return(nil)
   108  
   109  	// when
   110  	errorDetails := handler.handle(t, ctx, api.AdminRenameWalletParams{
   111  		Wallet:  name,
   112  		NewName: newName,
   113  	})
   114  
   115  	// then
   116  	require.Nil(t, errorDetails)
   117  }
   118  
   119  func testRenamingWalletThatDoesNotExistsFails(t *testing.T) {
   120  	// given
   121  	ctx := context.Background()
   122  	name := vgrand.RandomStr(5)
   123  	newName := vgrand.RandomStr(5)
   124  
   125  	// setup
   126  	handler := newRenameWalletHandler(t)
   127  	// -- expected calls
   128  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, nil)
   129  
   130  	// when
   131  	errorDetails := handler.handle(t, ctx, api.AdminRenameWalletParams{
   132  		Wallet:  name,
   133  		NewName: newName,
   134  	})
   135  
   136  	// then
   137  	require.NotNil(t, errorDetails)
   138  	assertInvalidParams(t, errorDetails, api.ErrWalletDoesNotExist)
   139  }
   140  
   141  func testGettingInternalErrorDuringExistingWalletVerificationDoesNotRenameWallet(t *testing.T) {
   142  	// given
   143  	ctx := context.Background()
   144  	name := vgrand.RandomStr(5)
   145  	newName := vgrand.RandomStr(5)
   146  
   147  	// setup
   148  	handler := newRenameWalletHandler(t)
   149  	// -- expected calls
   150  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, assert.AnError)
   151  
   152  	// when
   153  	errorDetails := handler.handle(t, ctx, api.AdminRenameWalletParams{
   154  		Wallet:  name,
   155  		NewName: newName,
   156  	})
   157  
   158  	// then
   159  	require.NotNil(t, errorDetails)
   160  	assertInternalError(t, errorDetails, fmt.Errorf("could not verify the wallet exists: %w", assert.AnError))
   161  }
   162  
   163  func testRenamingWalletWithNameAlreadyTakenFails(t *testing.T) {
   164  	// given
   165  	ctx := context.Background()
   166  	name := vgrand.RandomStr(5)
   167  	newName := vgrand.RandomStr(5)
   168  
   169  	// setup
   170  	handler := newRenameWalletHandler(t)
   171  	// -- expected calls
   172  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil)
   173  	handler.walletStore.EXPECT().WalletExists(ctx, newName).Times(1).Return(true, nil)
   174  
   175  	// when
   176  	errorDetails := handler.handle(t, ctx, api.AdminRenameWalletParams{
   177  		Wallet:  name,
   178  		NewName: newName,
   179  	})
   180  
   181  	// then
   182  	require.NotNil(t, errorDetails)
   183  	assertInvalidParams(t, errorDetails, api.ErrWalletAlreadyExists)
   184  }
   185  
   186  func testGettingInternalErrorDuringNonExistingWalletVerificationDoesNotRenameWallet(t *testing.T) {
   187  	// given
   188  	ctx := context.Background()
   189  	name := vgrand.RandomStr(5)
   190  	newName := vgrand.RandomStr(5)
   191  
   192  	// setup
   193  	handler := newRenameWalletHandler(t)
   194  	// -- expected calls
   195  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil)
   196  	handler.walletStore.EXPECT().WalletExists(ctx, newName).Times(1).Return(false, assert.AnError)
   197  
   198  	// when
   199  	errorDetails := handler.handle(t, ctx, api.AdminRenameWalletParams{
   200  		Wallet:  name,
   201  		NewName: newName,
   202  	})
   203  
   204  	// then
   205  	require.NotNil(t, errorDetails)
   206  	assertInternalError(t, errorDetails, fmt.Errorf("could not verify the wallet exists: %w", assert.AnError))
   207  }
   208  
   209  func testGettingInternalErrorDuringRenamingDoesNotRenameWallet(t *testing.T) {
   210  	// given
   211  	ctx := context.Background()
   212  	name := vgrand.RandomStr(5)
   213  	newName := vgrand.RandomStr(5)
   214  
   215  	// setup
   216  	handler := newRenameWalletHandler(t)
   217  	// -- expected calls
   218  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil)
   219  	handler.walletStore.EXPECT().WalletExists(ctx, newName).Times(1).Return(false, nil)
   220  	handler.walletStore.EXPECT().RenameWallet(ctx, name, newName).Times(1).Return(assert.AnError)
   221  
   222  	// when
   223  	errorDetails := handler.handle(t, ctx, api.AdminRenameWalletParams{
   224  		Wallet:  name,
   225  		NewName: newName,
   226  	})
   227  
   228  	// then
   229  	require.NotNil(t, errorDetails)
   230  	assertInternalError(t, errorDetails, fmt.Errorf("could not rename the wallet: %w", assert.AnError))
   231  }
   232  
   233  type renameWalletHandler struct {
   234  	*api.AdminRenameWallet
   235  	ctrl        *gomock.Controller
   236  	walletStore *mocks.MockWalletStore
   237  }
   238  
   239  func (h *renameWalletHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) *jsonrpc.ErrorDetails {
   240  	t.Helper()
   241  
   242  	rawResult, err := h.Handle(ctx, params)
   243  	require.Nil(t, rawResult)
   244  	return err
   245  }
   246  
   247  func newRenameWalletHandler(t *testing.T) *renameWalletHandler {
   248  	t.Helper()
   249  
   250  	ctrl := gomock.NewController(t)
   251  	walletStore := mocks.NewMockWalletStore(ctrl)
   252  
   253  	return &renameWalletHandler{
   254  		AdminRenameWallet: api.NewAdminRenameWallet(walletStore),
   255  		ctrl:              ctrl,
   256  		walletStore:       walletStore,
   257  	}
   258  }