code.vegaprotocol.io/vega@v0.79.0/wallet/service/v2/connections/helpers_for_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 connections_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/service/v2/connections"
    27  	"code.vegaprotocol.io/vega/wallet/wallet"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func assertRightAllowedKeys(t *testing.T, expectedKeys []wallet.KeyPair, resultKeys []api.AllowedKey) {
    34  	t.Helper()
    35  
    36  	require.Len(t, resultKeys, len(expectedKeys))
    37  
    38  	for i := 0; i < len(expectedKeys); i++ {
    39  		assertRightAllowedKey(t, expectedKeys[i], resultKeys[i])
    40  	}
    41  }
    42  
    43  func assertRightAllowedKey(t *testing.T, expectedKey wallet.KeyPair, resultKey api.AllowedKey) {
    44  	t.Helper()
    45  
    46  	assert.Equal(t, expectedKey.Name(), resultKey.Name())
    47  	assert.Equal(t, expectedKey.PublicKey(), resultKey.PublicKey())
    48  }
    49  
    50  func randomTraceID(t *testing.T) (context.Context, string) {
    51  	t.Helper()
    52  
    53  	traceID := vgrand.RandomStr(64)
    54  	return context.WithValue(context.Background(), jsonrpc.TraceIDKey, traceID), traceID
    55  }
    56  
    57  func randomWallet(t *testing.T) (wallet.Wallet, []wallet.KeyPair) {
    58  	t.Helper()
    59  
    60  	return randomWalletWithName(t, vgrand.RandomStr(5))
    61  }
    62  
    63  func randomWalletWithName(t *testing.T, walletName string) (wallet.Wallet, []wallet.KeyPair) {
    64  	t.Helper()
    65  
    66  	w, _, err := wallet.NewHDWallet(walletName)
    67  	if err != nil {
    68  		t.Fatalf("could not create wallet for test: %v", err)
    69  	}
    70  
    71  	kps := make([]wallet.KeyPair, 0, 3)
    72  	for i := 0; i < 3; i++ {
    73  		kp, err := w.GenerateKeyPair(nil)
    74  		if err != nil {
    75  			t.Fatalf("could not generate keys on wallet for test: %v", err)
    76  		}
    77  		kps = append(kps, kp)
    78  	}
    79  
    80  	return w, kps
    81  }
    82  
    83  func randomToken(t *testing.T) connections.Token {
    84  	t.Helper()
    85  
    86  	token, err := connections.AsToken(vgrand.RandomStr(64))
    87  	if err != nil {
    88  		panic(fmt.Errorf("could not create a random connection token: %w", err))
    89  	}
    90  	return token
    91  }