github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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().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.sessionCache.AddWithExpiresInSecs(session.Token, session, 5*60) 26 27 keys := th.App.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.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().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 mobile session, should not timeout 69 session = &model.Session{ 70 UserId: model.NewId(), 71 DeviceId: "android:" + model.NewId(), 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 oauth session, should not timeout 83 session = &model.Session{ 84 UserId: model.NewId(), 85 IsOAuth: true, 86 } 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 // Test personal access token session, should not timeout 97 session = &model.Session{ 98 UserId: model.NewId(), 99 } 100 session.AddProp(model.SESSION_PROP_TYPE, model.SESSION_TYPE_USER_ACCESS_TOKEN) 101 102 session, _ = th.App.CreateSession(session) 103 time = session.LastActivityAt - (1000 * 60 * 6) 104 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 105 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 106 107 _, err = th.App.GetSession(session.Token) 108 assert.Nil(t, err) 109 110 // Test regular session with license off, should not timeout 111 th.App.SetLicense(nil) 112 113 session = &model.Session{ 114 UserId: model.NewId(), 115 } 116 117 session, _ = th.App.CreateSession(session) 118 time = session.LastActivityAt - (1000 * 60 * 6) 119 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 120 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 121 122 _, err = th.App.GetSession(session.Token) 123 assert.Nil(t, err) 124 125 th.App.SetLicense(model.NewTestLicense("compliance")) 126 127 // Test regular session with timeout set to 0, should not timeout 128 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 0 }) 129 130 session = &model.Session{ 131 UserId: model.NewId(), 132 } 133 134 session, _ = th.App.CreateSession(session) 135 time = session.LastActivityAt - (1000 * 60 * 6) 136 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 137 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 138 139 _, err = th.App.GetSession(session.Token) 140 assert.Nil(t, err) 141 }