github.com/hyperledger/aries-framework-go@v0.3.2/pkg/wallet/sesson_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package wallet 8 9 import ( 10 "testing" 11 12 "github.com/google/uuid" 13 "github.com/stretchr/testify/require" 14 15 mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms" 16 ) 17 18 func TestSessionManager_CreateSession(t *testing.T) { 19 t.Run("successfully create session", func(t *testing.T) { 20 token, err := sessionManager().createSession(uuid.New().String(), &mockkms.KeyManager{}, 0) 21 22 require.NoError(t, err) 23 require.NotEmpty(t, token) 24 }) 25 26 t.Run("fail to create created session - wallet already unlocked", func(t *testing.T) { 27 user := uuid.New().String() 28 29 token, err := sessionManager().createSession(user, &mockkms.KeyManager{}, 0) 30 31 require.NoError(t, err) 32 require.NotEmpty(t, token) 33 34 _, err = sessionManager().createSession(user, &mockkms.KeyManager{}, 0) 35 require.EqualError(t, err, "wallet already unlocked") 36 }) 37 } 38 39 func TestSessionManager_GetSession(t *testing.T) { 40 t.Run("successfully get session", func(t *testing.T) { 41 token, err := sessionManager().createSession(uuid.New().String(), &mockkms.KeyManager{}, 0) 42 43 require.NoError(t, err) 44 require.NotEmpty(t, token) 45 46 sess, err := sessionManager().getSession(token) 47 48 require.NoError(t, err) 49 require.NotEmpty(t, sess) 50 }) 51 52 t.Run("fail to create created session - wallet already unlocked", func(t *testing.T) { 53 token := "token with invalid data" 54 55 err := sessionManager().gstore.Set(token, "invalid sess object") 56 57 require.NoError(t, err) 58 59 _, err = sessionManager().getSession(token) 60 61 require.EqualError(t, err, "failed to cast session object: expects Session, gets string") 62 63 sessionManager().gstore.Remove(token) 64 }) 65 } 66 67 func TestSessionManager_CloseSession(t *testing.T) { 68 t.Run("successfully close session", func(t *testing.T) { 69 user := uuid.New().String() 70 71 token, err := sessionManager().createSession(user, &mockkms.KeyManager{}, 0) 72 73 require.NoError(t, err) 74 require.NotEmpty(t, token) 75 76 sess, err := sessionManager().getSession(token) 77 78 require.NoError(t, err) 79 require.NotEmpty(t, sess) 80 81 closed := sessionManager().closeSession(user) 82 require.True(t, closed) 83 84 closed = sessionManager().closeSession(user) 85 require.False(t, closed) 86 87 _, err = sessionManager().getSession(token) 88 89 require.EqualError(t, err, "invalid auth token") 90 }) 91 }