code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_describe_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  	"code.vegaprotocol.io/vega/wallet/wallet"
    28  
    29  	"github.com/golang/mock/gomock"
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestAdminDescribeWallet(t *testing.T) {
    35  	t.Run("Documentation matches the code", testAdminDescribeWalletSchemaCorrect)
    36  	t.Run("Describing a wallet with invalid params fails", testDescribingWalletWithInvalidParamsFails)
    37  	t.Run("Describing a wallet with valid params succeeds", testDescribingWalletWithValidParamsSucceeds)
    38  	t.Run("Describing a wallet that does not exists fails", testDescribingWalletThatDoesNotExistsFails)
    39  	t.Run("Getting internal error during verification fails", testAdminDescribeWalletGettingInternalErrorDuringVerificationFails)
    40  	t.Run("Getting internal error during retrieval fails", testAdminDescribeWalletGettingInternalErrorDuringRetrievalFails)
    41  }
    42  
    43  func testAdminDescribeWalletSchemaCorrect(t *testing.T) {
    44  	assertEqualSchema(t, "admin.describe_wallet", api.AdminDescribeWalletParams{}, api.AdminDescribeWalletResult{})
    45  }
    46  
    47  func testDescribingWalletWithInvalidParamsFails(t *testing.T) {
    48  	tcs := []struct {
    49  		name          string
    50  		params        interface{}
    51  		expectedError error
    52  	}{
    53  		{
    54  			name:          "with nil params",
    55  			params:        nil,
    56  			expectedError: api.ErrParamsRequired,
    57  		}, {
    58  			name:          "with wrong type of params",
    59  			params:        "test",
    60  			expectedError: api.ErrParamsDoNotMatch,
    61  		}, {
    62  			name: "with empty name",
    63  			params: api.AdminDescribeWalletParams{
    64  				Wallet: "",
    65  			},
    66  			expectedError: api.ErrWalletIsRequired,
    67  		},
    68  	}
    69  
    70  	for _, tc := range tcs {
    71  		t.Run(tc.name, func(tt *testing.T) {
    72  			// given
    73  			ctx := context.Background()
    74  
    75  			// setup
    76  			handler := newDescribeWalletHandler(tt)
    77  
    78  			// when
    79  			result, errorDetails := handler.handle(t, ctx, tc.params)
    80  
    81  			// then
    82  			require.Empty(tt, result)
    83  			assertInvalidParams(tt, errorDetails, tc.expectedError)
    84  		})
    85  	}
    86  }
    87  
    88  func testDescribingWalletWithValidParamsSucceeds(t *testing.T) {
    89  	// given
    90  	ctx := context.Background()
    91  	name := vgrand.RandomStr(5)
    92  	expectedWallet, _, err := wallet.NewHDWallet(name)
    93  	if err != nil {
    94  		t.Fatalf("could not create wallet for test: %v", err)
    95  	}
    96  
    97  	// setup
    98  	handler := newDescribeWalletHandler(t)
    99  	// -- expected calls
   100  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil)
   101  	handler.walletStore.EXPECT().IsWalletAlreadyUnlocked(ctx, expectedWallet.Name()).Times(1).Return(true, nil)
   102  	handler.walletStore.EXPECT().GetWallet(ctx, name).Times(1).Return(expectedWallet, nil)
   103  
   104  	// when
   105  	result, errorDetails := handler.handle(t, ctx, api.AdminDescribeWalletParams{
   106  		Wallet: name,
   107  	})
   108  
   109  	// then
   110  	require.Nil(t, errorDetails)
   111  	assert.Equal(t, api.AdminDescribeWalletResult{
   112  		Name:                 expectedWallet.Name(),
   113  		ID:                   expectedWallet.ID(),
   114  		Type:                 expectedWallet.Type(),
   115  		KeyDerivationVersion: expectedWallet.KeyDerivationVersion(),
   116  	}, result)
   117  }
   118  
   119  func testDescribingWalletThatDoesNotExistsFails(t *testing.T) {
   120  	// given
   121  	ctx := context.Background()
   122  	name := vgrand.RandomStr(5)
   123  
   124  	// setup
   125  	handler := newDescribeWalletHandler(t)
   126  	// -- expected calls
   127  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, nil)
   128  
   129  	// when
   130  	result, errorDetails := handler.handle(t, ctx, api.AdminDescribeWalletParams{
   131  		Wallet: name,
   132  	})
   133  
   134  	// then
   135  	require.NotNil(t, errorDetails)
   136  	assert.Empty(t, result)
   137  	assertInvalidParams(t, errorDetails, api.ErrWalletDoesNotExist)
   138  }
   139  
   140  func testAdminDescribeWalletGettingInternalErrorDuringVerificationFails(t *testing.T) {
   141  	// given
   142  	ctx := context.Background()
   143  	name := vgrand.RandomStr(5)
   144  
   145  	// setup
   146  	handler := newDescribeWalletHandler(t)
   147  	// -- expected calls
   148  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, assert.AnError)
   149  
   150  	// when
   151  	result, errorDetails := handler.handle(t, ctx, api.AdminDescribeWalletParams{
   152  		Wallet: name,
   153  	})
   154  
   155  	// then
   156  	require.NotNil(t, errorDetails)
   157  	assert.Empty(t, result)
   158  	assertInternalError(t, errorDetails, fmt.Errorf("could not verify the wallet exists: %w", assert.AnError))
   159  }
   160  
   161  func testAdminDescribeWalletGettingInternalErrorDuringRetrievalFails(t *testing.T) {
   162  	// given
   163  	ctx := context.Background()
   164  	name := vgrand.RandomStr(5)
   165  
   166  	// setup
   167  	handler := newDescribeWalletHandler(t)
   168  	// -- expected calls
   169  	handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil)
   170  	handler.walletStore.EXPECT().IsWalletAlreadyUnlocked(ctx, name).Times(1).Return(true, nil)
   171  	handler.walletStore.EXPECT().GetWallet(ctx, name).Times(1).Return(nil, assert.AnError)
   172  
   173  	// when
   174  	result, errorDetails := handler.handle(t, ctx, api.AdminDescribeWalletParams{
   175  		Wallet: name,
   176  	})
   177  
   178  	// then
   179  	require.NotNil(t, errorDetails)
   180  	assert.Empty(t, result)
   181  	assertInternalError(t, errorDetails, fmt.Errorf("could not retrieve the wallet: %w", assert.AnError))
   182  }
   183  
   184  type describeWalletHandler struct {
   185  	*api.AdminDescribeWallet
   186  	ctrl        *gomock.Controller
   187  	walletStore *mocks.MockWalletStore
   188  }
   189  
   190  func (h *describeWalletHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (api.AdminDescribeWalletResult, *jsonrpc.ErrorDetails) {
   191  	t.Helper()
   192  
   193  	rawResult, err := h.Handle(ctx, params)
   194  	if rawResult != nil {
   195  		result, ok := rawResult.(api.AdminDescribeWalletResult)
   196  		if !ok {
   197  			t.Fatal("AdminDescribeWallet handler result is not a AdminDescribeWalletResult")
   198  		}
   199  		return result, err
   200  	}
   201  	return api.AdminDescribeWalletResult{}, err
   202  }
   203  
   204  func newDescribeWalletHandler(t *testing.T) *describeWalletHandler {
   205  	t.Helper()
   206  
   207  	ctrl := gomock.NewController(t)
   208  	walletStore := mocks.NewMockWalletStore(ctrl)
   209  
   210  	return &describeWalletHandler{
   211  		AdminDescribeWallet: api.NewAdminDescribeWallet(walletStore),
   212  		ctrl:                ctrl,
   213  		walletStore:         walletStore,
   214  	}
   215  }