code.vegaprotocol.io/vega@v0.79.0/wallet/service/v2/connections/store/longliving/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 "testing" 20 "time" 21 22 "code.vegaprotocol.io/vega/libs/ptr" 23 vgrand "code.vegaprotocol.io/vega/libs/rand" 24 "code.vegaprotocol.io/vega/paths" 25 "code.vegaprotocol.io/vega/wallet/service/v2/connections" 26 "code.vegaprotocol.io/vega/wallet/service/v2/connections/store/longliving/v1" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestFileStore(t *testing.T) { 33 t.Run("List tokens succeeds", testFileStoreListTokensSucceeds) 34 t.Run("Verifying an existing token succeeds", testFileStoreVerifyingExistingTokenSucceeds) 35 t.Run("Verifying an unknown token fails", testFileStoreVerifyingUnknownTokenFails) 36 t.Run("Deleting an existing token succeeds", testFileStoreDeletingExistingTokenSucceeds) 37 } 38 39 func testFileStoreListTokensSucceeds(t *testing.T) { 40 vegaPaths := testHome(t) 41 store := newTestFileStore(t, vegaPaths) 42 43 // given 44 description1 := connections.TokenDescription{ 45 Description: vgrand.RandomStr(5), 46 CreationDate: time.Now().Add(-2 * time.Hour), 47 ExpirationDate: ptr.From(time.Now().Add(-1 * time.Hour)), 48 Token: connections.GenerateToken(), 49 Wallet: connections.WalletCredentials{ 50 Name: vgrand.RandomStr(5), 51 Passphrase: vgrand.RandomStr(5), 52 }, 53 } 54 55 // when 56 err := store.SaveToken(description1) 57 58 // then 59 require.NoError(t, err) 60 // given 61 description2 := connections.TokenDescription{ 62 Description: vgrand.RandomStr(5), 63 CreationDate: time.Now().Add(-4 * time.Hour), 64 Token: connections.GenerateToken(), 65 Wallet: connections.WalletCredentials{ 66 Name: vgrand.RandomStr(5), 67 Passphrase: vgrand.RandomStr(5), 68 }, 69 } 70 71 // when 72 err = store.SaveToken(description2) 73 74 // then 75 require.NoError(t, err) 76 // when 77 tokenSummaries, err := store.ListTokens() 78 79 // then 80 require.NoError(t, err) 81 assert.Equal(t, description1.Description, tokenSummaries[0].Description) 82 assert.Equal(t, description1.Token, tokenSummaries[0].Token) 83 assert.WithinDuration(t, description1.CreationDate, tokenSummaries[0].CreationDate, 0) 84 assert.WithinDuration(t, *description1.ExpirationDate, *tokenSummaries[0].ExpirationDate, 0) 85 assert.Equal(t, description2.Description, tokenSummaries[1].Description) 86 assert.Equal(t, description2.Token, tokenSummaries[1].Token) 87 assert.WithinDuration(t, description2.CreationDate, tokenSummaries[1].CreationDate, 0) 88 assert.Equal(t, description2.ExpirationDate, tokenSummaries[1].ExpirationDate, 0) 89 } 90 91 func testFileStoreVerifyingExistingTokenSucceeds(t *testing.T) { 92 vegaPaths := testHome(t) 93 store := newTestFileStore(t, vegaPaths) 94 95 // given 96 description1 := connections.TokenDescription{ 97 Description: vgrand.RandomStr(5), 98 CreationDate: time.Now().Add(-2 * time.Hour), 99 ExpirationDate: ptr.From(time.Now().Add(-1 * time.Hour)), 100 Token: connections.GenerateToken(), 101 Wallet: connections.WalletCredentials{ 102 Name: vgrand.RandomStr(5), 103 Passphrase: vgrand.RandomStr(5), 104 }, 105 } 106 107 // when 108 err := store.SaveToken(description1) 109 110 // then 111 require.NoError(t, err) 112 113 // when 114 exists, err := store.TokenExists(description1.Token) 115 116 // then 117 require.NoError(t, err) 118 assert.True(t, exists) 119 } 120 121 func testFileStoreVerifyingUnknownTokenFails(t *testing.T) { 122 vegaPaths := testHome(t) 123 store := newTestFileStore(t, vegaPaths) 124 125 // given 126 unknownToken := connections.GenerateToken() 127 128 // when 129 exists, err := store.TokenExists(unknownToken) 130 131 // then 132 require.NoError(t, err) 133 assert.False(t, exists) 134 } 135 136 func testFileStoreDeletingExistingTokenSucceeds(t *testing.T) { 137 vegaPaths := testHome(t) 138 store := newTestFileStore(t, vegaPaths) 139 140 // given 141 description1 := connections.TokenDescription{ 142 Description: vgrand.RandomStr(5), 143 CreationDate: time.Now().Add(-2 * time.Hour), 144 ExpirationDate: ptr.From(time.Now().Add(-1 * time.Hour)), 145 Token: connections.GenerateToken(), 146 Wallet: connections.WalletCredentials{ 147 Name: vgrand.RandomStr(5), 148 Passphrase: vgrand.RandomStr(5), 149 }, 150 } 151 152 // when 153 err := store.SaveToken(description1) 154 155 // then 156 require.NoError(t, err) 157 158 // given 159 description2 := connections.TokenDescription{ 160 Description: vgrand.RandomStr(5), 161 CreationDate: time.Now().Add(-4 * time.Hour), 162 Token: connections.GenerateToken(), 163 Wallet: connections.WalletCredentials{ 164 Name: vgrand.RandomStr(5), 165 Passphrase: vgrand.RandomStr(5), 166 }, 167 } 168 169 // when 170 err = store.SaveToken(description2) 171 172 // then 173 require.NoError(t, err) 174 175 // when 176 err = store.DeleteToken(description1.Token) 177 178 // then 179 require.NoError(t, err) 180 181 // when 182 exists, err := store.TokenExists(description1.Token) 183 184 // then 185 require.NoError(t, err) 186 assert.False(t, exists) 187 188 // when 189 exists, err = store.TokenExists(description2.Token) 190 191 // then 192 require.NoError(t, err) 193 assert.True(t, exists) 194 } 195 196 type testFileStore struct { 197 *v1.FileStore 198 } 199 200 func testHome(t *testing.T) paths.Paths { 201 t.Helper() 202 return paths.New(t.TempDir()) 203 } 204 205 func newTestFileStore(t *testing.T, vegaPaths paths.Paths) *testFileStore { 206 t.Helper() 207 208 tokenStore, err := v1.ReinitialiseStore(vegaPaths, vgrand.RandomStr(5)) 209 if err != nil { 210 t.Fatalf("could not initialise the file store for tests: %v", err) 211 } 212 t.Cleanup(func() { 213 tokenStore.Close() 214 }) 215 216 return &testFileStore{ 217 FileStore: tokenStore, 218 } 219 }