code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_describe_key_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 TestAdminDescribeKey(t *testing.T) { 35 t.Run("Documentation matches the code", testAdminDescribeKeySchemaCorrect) 36 t.Run("Describing a key with invalid params fails", testAdminDescribingKeyWithInvalidParamsFails) 37 t.Run("Describing a key with valid params succeeds", testAdminDescribingKeyWithValidParamsSucceeds) 38 t.Run("Describing a key from wallet that does not exists fails", testAdminDescribeKeyDescribingKeyFromWalletThatDoesNotExistsFails) 39 t.Run("Getting internal error during wallet verification fails", testAdminDescribeKeyGettingInternalErrorDuringWalletVerificationFails) 40 t.Run("Getting internal error during wallet retrieval fails", testAdminDescribeKeyGettingInternalErrorDuringWalletRetrievalFails) 41 t.Run("Describing a key that does not exists fails", testAdminDescribingKeyThatDoesNotExistsFails) 42 } 43 44 func testAdminDescribeKeySchemaCorrect(t *testing.T) { 45 assertEqualSchema(t, "admin.describe_key", api.AdminDescribeKeyParams{}, api.AdminDescribeKeyResult{}) 46 } 47 48 func testAdminDescribingKeyWithInvalidParamsFails(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.AdminDescribeKeyParams{ 65 Wallet: "", 66 PublicKey: "b5fd9d3c4ad553cb3196303b6e6df7f484cf7f5331a572a45031239fd71ad8a0", 67 }, 68 expectedError: api.ErrWalletIsRequired, 69 }, { 70 name: "with empty public key", 71 params: api.AdminDescribeKeyParams{ 72 Wallet: vgrand.RandomStr(5), 73 PublicKey: "", 74 }, 75 expectedError: api.ErrPublicKeyIsRequired, 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 := newDescribeKeyHandler(tt) 86 87 // when 88 result, errorDetails := handler.handle(t, ctx, tc.params) 89 90 // then 91 require.Empty(tt, result) 92 assertInvalidParams(tt, errorDetails, tc.expectedError) 93 }) 94 } 95 } 96 97 func testAdminDescribingKeyWithValidParamsSucceeds(t *testing.T) { 98 // given 99 ctx := context.Background() 100 expectedWallet, firstKey := walletWithKey(t) 101 102 // setup 103 handler := newDescribeKeyHandler(t) 104 // -- expected calls 105 handler.walletStore.EXPECT().WalletExists(ctx, expectedWallet.Name()).Times(1).Return(true, nil) 106 handler.walletStore.EXPECT().IsWalletAlreadyUnlocked(ctx, expectedWallet.Name()).Times(1).Return(true, nil) 107 handler.walletStore.EXPECT().GetWallet(ctx, expectedWallet.Name()).Times(1).Return(expectedWallet, nil) 108 109 // when 110 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeKeyParams{ 111 Wallet: expectedWallet.Name(), 112 PublicKey: firstKey.PublicKey(), 113 }) 114 115 // then 116 require.Nil(t, errorDetails) 117 assert.Equal(t, api.AdminDescribeKeyResult{ 118 PublicKey: firstKey.PublicKey(), 119 Name: firstKey.Name(), 120 Algorithm: wallet.Algorithm{ 121 Name: firstKey.AlgorithmName(), 122 Version: firstKey.AlgorithmVersion(), 123 }, 124 Metadata: firstKey.Metadata(), 125 IsTainted: firstKey.IsTainted(), 126 }, result) 127 } 128 129 func testAdminDescribeKeyDescribingKeyFromWalletThatDoesNotExistsFails(t *testing.T) { 130 // given 131 ctx := context.Background() 132 name := vgrand.RandomStr(5) 133 134 // setup 135 handler := newDescribeKeyHandler(t) 136 // -- expected calls 137 handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, nil) 138 139 // when 140 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeKeyParams{ 141 Wallet: name, 142 PublicKey: vgrand.RandomStr(5), 143 }) 144 145 // then 146 require.NotNil(t, errorDetails) 147 assert.Empty(t, result) 148 assertInvalidParams(t, errorDetails, api.ErrWalletDoesNotExist) 149 } 150 151 func testAdminDescribeKeyGettingInternalErrorDuringWalletVerificationFails(t *testing.T) { 152 // given 153 ctx := context.Background() 154 name := vgrand.RandomStr(5) 155 156 // setup 157 handler := newDescribeKeyHandler(t) 158 // -- expected calls 159 handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(false, assert.AnError) 160 161 // when 162 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeKeyParams{ 163 Wallet: name, 164 PublicKey: vgrand.RandomStr(5), 165 }) 166 167 // then 168 require.NotNil(t, errorDetails) 169 assert.Empty(t, result) 170 assertInternalError(t, errorDetails, fmt.Errorf("could not verify the wallet exists: %w", assert.AnError)) 171 } 172 173 func testAdminDescribeKeyGettingInternalErrorDuringWalletRetrievalFails(t *testing.T) { 174 // given 175 ctx := context.Background() 176 name := vgrand.RandomStr(5) 177 178 // setup 179 handler := newDescribeKeyHandler(t) 180 // -- expected calls 181 handler.walletStore.EXPECT().WalletExists(ctx, name).Times(1).Return(true, nil) 182 handler.walletStore.EXPECT().IsWalletAlreadyUnlocked(ctx, name).Times(1).Return(true, nil) 183 handler.walletStore.EXPECT().GetWallet(ctx, name).Times(1).Return(nil, assert.AnError) 184 185 // when 186 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeKeyParams{ 187 Wallet: name, 188 PublicKey: vgrand.RandomStr(5), 189 }) 190 191 // then 192 require.NotNil(t, errorDetails) 193 assert.Empty(t, result) 194 assertInternalError(t, errorDetails, fmt.Errorf("could not retrieve the wallet: %w", assert.AnError)) 195 } 196 197 func testAdminDescribingKeyThatDoesNotExistsFails(t *testing.T) { 198 // given 199 ctx := context.Background() 200 expectedWallet, _ := walletWithKey(t) 201 202 // setup 203 handler := newDescribeKeyHandler(t) 204 // -- expected calls 205 handler.walletStore.EXPECT().WalletExists(ctx, expectedWallet.Name()).Times(1).Return(true, nil) 206 handler.walletStore.EXPECT().IsWalletAlreadyUnlocked(ctx, expectedWallet.Name()).Times(1).Return(true, nil) 207 handler.walletStore.EXPECT().GetWallet(ctx, expectedWallet.Name()).Times(1).Return(expectedWallet, nil) 208 209 // when 210 result, errorDetails := handler.handle(t, ctx, api.AdminDescribeKeyParams{ 211 Wallet: expectedWallet.Name(), 212 PublicKey: vgrand.RandomStr(5), 213 }) 214 215 // then 216 require.NotNil(t, errorDetails) 217 assert.Empty(t, result) 218 assertInvalidParams(t, errorDetails, api.ErrPublicKeyDoesNotExist) 219 } 220 221 type describeKeyHandler struct { 222 *api.AdminDescribeKey 223 ctrl *gomock.Controller 224 walletStore *mocks.MockWalletStore 225 } 226 227 func (h *describeKeyHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (api.AdminDescribeKeyResult, *jsonrpc.ErrorDetails) { 228 t.Helper() 229 230 rawResult, err := h.Handle(ctx, params) 231 if rawResult != nil { 232 result, ok := rawResult.(api.AdminDescribeKeyResult) 233 if !ok { 234 t.Fatal("AdminDescribeKey handler result is not a AdminDescribeKeyResult") 235 } 236 return result, err 237 } 238 return api.AdminDescribeKeyResult{}, err 239 } 240 241 func newDescribeKeyHandler(t *testing.T) *describeKeyHandler { 242 t.Helper() 243 244 ctrl := gomock.NewController(t) 245 walletStore := mocks.NewMockWalletStore(ctrl) 246 247 return &describeKeyHandler{ 248 AdminDescribeKey: api.NewAdminDescribeKey(walletStore), 249 ctrl: ctrl, 250 walletStore: walletStore, 251 } 252 }