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