github.com/isacikgoz/mattermost-server@v5.11.1+incompatible/app/session_test.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/mattermost/mattermost-server/model" 13 ) 14 15 func TestCache(t *testing.T) { 16 th := Setup(t).InitBasic() 17 defer th.TearDown() 18 19 session := &model.Session{ 20 Id: model.NewId(), 21 Token: model.NewId(), 22 UserId: model.NewId(), 23 } 24 25 th.App.Srv.sessionCache.AddWithExpiresInSecs(session.Token, session, 5*60) 26 27 keys := th.App.Srv.sessionCache.Keys() 28 if len(keys) <= 0 { 29 t.Fatal("should have items") 30 } 31 32 th.App.ClearSessionCacheForUser(session.UserId) 33 34 rkeys := th.App.Srv.sessionCache.Keys() 35 if len(rkeys) != len(keys)-1 { 36 t.Fatal("should have one less") 37 } 38 } 39 40 func TestGetSessionIdleTimeoutInMinutes(t *testing.T) { 41 th := Setup(t).InitBasic() 42 defer th.TearDown() 43 44 session := &model.Session{ 45 UserId: model.NewId(), 46 } 47 48 session, _ = th.App.CreateSession(session) 49 50 th.App.SetLicense(model.NewTestLicense("compliance")) 51 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 5 }) 52 53 rsession, err := th.App.GetSession(session.Token) 54 require.Nil(t, err) 55 assert.Equal(t, rsession.Id, session.Id) 56 57 // Test regular session, should timeout 58 time := session.LastActivityAt - (1000 * 60 * 6) 59 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 60 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 61 62 rsession, err = th.App.GetSession(session.Token) 63 require.NotNil(t, err) 64 assert.Equal(t, "api.context.invalid_token.error", err.Id) 65 assert.Equal(t, "idle timeout", err.DetailedError) 66 assert.Nil(t, rsession) 67 68 // Test oauth session, should not timeout 69 session = &model.Session{ 70 UserId: model.NewId(), 71 IsOAuth: true, 72 } 73 74 session, _ = th.App.CreateSession(session) 75 time = session.LastActivityAt - (1000 * 60 * 6) 76 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 77 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 78 79 _, err = th.App.GetSession(session.Token) 80 assert.Nil(t, err) 81 82 // Test personal access token session, should not timeout 83 session = &model.Session{ 84 UserId: model.NewId(), 85 } 86 session.AddProp(model.SESSION_PROP_TYPE, model.SESSION_TYPE_USER_ACCESS_TOKEN) 87 88 session, _ = th.App.CreateSession(session) 89 time = session.LastActivityAt - (1000 * 60 * 6) 90 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 91 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 92 93 _, err = th.App.GetSession(session.Token) 94 assert.Nil(t, err) 95 96 th.App.SetLicense(model.NewTestLicense("compliance")) 97 98 // Test regular session with timeout set to 0, should not timeout 99 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 0 }) 100 101 session = &model.Session{ 102 UserId: model.NewId(), 103 } 104 105 session, _ = th.App.CreateSession(session) 106 time = session.LastActivityAt - (1000 * 60 * 6) 107 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 108 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 109 110 _, err = th.App.GetSession(session.Token) 111 assert.Nil(t, err) 112 }