code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_list_wallets_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 "sort" 22 "testing" 23 24 "code.vegaprotocol.io/vega/libs/jsonrpc" 25 vgrand "code.vegaprotocol.io/vega/libs/rand" 26 "code.vegaprotocol.io/vega/wallet/api" 27 "code.vegaprotocol.io/vega/wallet/api/mocks" 28 "code.vegaprotocol.io/vega/wallet/wallet" 29 30 "github.com/golang/mock/gomock" 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestAdminListWallets(t *testing.T) { 36 t.Run("Documentation matches the code", testAdminListWalletsSchemaCorrect) 37 t.Run("Listing wallets succeeds", testListingWalletsSucceeds) 38 t.Run("Getting internal error during listing fails", testGettingInternalErrorDuringListingFails) 39 } 40 41 func testAdminListWalletsSchemaCorrect(t *testing.T) { 42 assertEqualSchema(t, "admin.list_wallets", nil, api.AdminListWalletsResult{}) 43 } 44 45 func testListingWalletsSucceeds(t *testing.T) { 46 // given 47 ctx := context.Background() 48 expectedWallet1, _, err := wallet.NewHDWallet(vgrand.RandomStr(5)) 49 if err != nil { 50 t.Fatalf("could not create wallet for test: %v", err) 51 } 52 expectedWallet2, _, err := wallet.NewHDWallet(vgrand.RandomStr(5)) 53 if err != nil { 54 t.Fatalf("could not create wallet for test: %v", err) 55 } 56 57 // setup 58 handler := newListWalletHandlers(t) 59 // -- expected calls 60 expectedWallets := []string{expectedWallet1.Name(), expectedWallet2.Name()} 61 sort.Strings(expectedWallets) 62 handler.walletStore.EXPECT().ListWallets(ctx).Times(1).Return(expectedWallets, nil) 63 64 // when 65 result, errorDetails := handler.handle(t, ctx, nil) 66 67 // then 68 require.Nil(t, errorDetails) 69 assert.Equal(t, expectedWallets, result.Wallets) 70 } 71 72 func testGettingInternalErrorDuringListingFails(t *testing.T) { 73 // given 74 ctx := context.Background() 75 76 // setup 77 handler := newListWalletHandlers(t) 78 // -- expected calls 79 handler.walletStore.EXPECT().ListWallets(gomock.Any()).Times(1).Return(nil, assert.AnError) 80 81 // when 82 result, errorDetails := handler.handle(t, ctx, nil) 83 84 // then 85 require.NotNil(t, errorDetails) 86 assert.Empty(t, result) 87 assertInternalError(t, errorDetails, fmt.Errorf("could not list the wallets: %w", assert.AnError)) 88 } 89 90 type listWalletsHandler struct { 91 *api.AdminListWallets 92 ctrl *gomock.Controller 93 walletStore *mocks.MockWalletStore 94 } 95 96 func (h *listWalletsHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (api.AdminListWalletsResult, *jsonrpc.ErrorDetails) { 97 t.Helper() 98 99 rawResult, err := h.Handle(ctx, params) 100 if rawResult != nil { 101 result, ok := rawResult.(api.AdminListWalletsResult) 102 if !ok { 103 t.Fatal("AdminListWallets handler result is not a AdminListWalletsResult") 104 } 105 return result, err 106 } 107 return api.AdminListWalletsResult{}, err 108 } 109 110 func newListWalletHandlers(t *testing.T) *listWalletsHandler { 111 t.Helper() 112 113 ctrl := gomock.NewController(t) 114 walletStore := mocks.NewMockWalletStore(ctrl) 115 116 return &listWalletsHandler{ 117 AdminListWallets: api.NewAdminListWallets(walletStore), 118 ctrl: ctrl, 119 walletStore: walletStore, 120 } 121 }