code.vegaprotocol.io/vega@v0.79.0/wallet/service/v2/connections/store/session/v1/file_store_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 v1_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    23  	"code.vegaprotocol.io/vega/paths"
    24  	"code.vegaprotocol.io/vega/wallet/service/v2/connections"
    25  	v1 "code.vegaprotocol.io/vega/wallet/service/v2/connections/store/session/v1"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestFileStore(t *testing.T) {
    32  	t.Run("List tokens succeeds", testFileStoreListTokensSucceeds)
    33  	t.Run("Deleting an existing token succeeds", testFileStoreDeletingExistingSessionSucceeds)
    34  }
    35  
    36  func testFileStoreListTokensSucceeds(t *testing.T) {
    37  	ctx := context.Background()
    38  	vegaPaths := testHome(t)
    39  	store := newTestFileStore(t, vegaPaths)
    40  
    41  	hostnameA := "a" + vgrand.RandomStr(5)
    42  	hostnameB := "b" + vgrand.RandomStr(5)
    43  	walletA := "a" + vgrand.RandomStr(5)
    44  	walletB := "b" + vgrand.RandomStr(5)
    45  
    46  	// given
    47  	session1 := connections.Session{
    48  		Hostname: hostnameA,
    49  		Token:    connections.GenerateToken(),
    50  		Wallet:   walletA,
    51  	}
    52  
    53  	// when
    54  	err := store.TrackSession(session1)
    55  
    56  	// then
    57  	require.NoError(t, err)
    58  
    59  	// given
    60  	session2 := connections.Session{
    61  		Hostname: hostnameB,
    62  		Token:    connections.GenerateToken(),
    63  		Wallet:   walletA,
    64  	}
    65  
    66  	// when
    67  	err = store.TrackSession(session2)
    68  
    69  	// then
    70  	require.NoError(t, err)
    71  
    72  	// given
    73  
    74  	session3 := connections.Session{
    75  		Hostname: hostnameA,
    76  		Token:    connections.GenerateToken(),
    77  		Wallet:   walletB,
    78  	}
    79  
    80  	// when
    81  	err = store.TrackSession(session3)
    82  
    83  	// then
    84  	require.NoError(t, err)
    85  
    86  	// when
    87  	sessions, err := store.ListSessions(ctx)
    88  
    89  	// then
    90  	require.NoError(t, err)
    91  	assert.Equal(t, []connections.Session{session1, session3, session2}, sessions)
    92  }
    93  
    94  func testFileStoreDeletingExistingSessionSucceeds(t *testing.T) {
    95  	ctx := context.Background()
    96  	vegaPaths := testHome(t)
    97  	store := newTestFileStore(t, vegaPaths)
    98  
    99  	// given
   100  	session1 := connections.Session{
   101  		Hostname: vgrand.RandomStr(5),
   102  		Token:    connections.GenerateToken(),
   103  		Wallet:   vgrand.RandomStr(5),
   104  	}
   105  
   106  	// when
   107  	err := store.TrackSession(session1)
   108  
   109  	// then
   110  	require.NoError(t, err)
   111  
   112  	// given
   113  	session2 := connections.Session{
   114  		Hostname: vgrand.RandomStr(5),
   115  		Token:    connections.GenerateToken(),
   116  		Wallet:   vgrand.RandomStr(5),
   117  	}
   118  
   119  	// when
   120  	err = store.TrackSession(session2)
   121  
   122  	// then
   123  	require.NoError(t, err)
   124  
   125  	// when
   126  	err = store.DeleteSession(ctx, session1.Token)
   127  
   128  	// then
   129  	require.NoError(t, err)
   130  
   131  	// when
   132  	sessions, err := store.ListSessions(ctx)
   133  
   134  	// then
   135  	require.NoError(t, err)
   136  	assert.Equal(t, []connections.Session{session2}, sessions)
   137  }
   138  
   139  type testFileStore struct {
   140  	*v1.FileStore
   141  }
   142  
   143  func testHome(t *testing.T) paths.Paths {
   144  	t.Helper()
   145  	return paths.New(t.TempDir())
   146  }
   147  
   148  func newTestFileStore(t *testing.T, vegaPaths paths.Paths) *testFileStore {
   149  	t.Helper()
   150  
   151  	tokenStore, err := v1.InitialiseStore(vegaPaths)
   152  	if err != nil {
   153  		t.Fatalf("could not initialise the file store for tests: %v", err)
   154  	}
   155  
   156  	return &testFileStore{
   157  		FileStore: tokenStore,
   158  	}
   159  }