github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+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/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/utils" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestCache(t *testing.T) { 17 th := Setup().InitBasic() 18 defer th.TearDown() 19 20 session := &model.Session{ 21 Id: model.NewId(), 22 Token: model.NewId(), 23 UserId: model.NewId(), 24 } 25 26 th.App.sessionCache.AddWithExpiresInSecs(session.Token, session, 5*60) 27 28 keys := th.App.sessionCache.Keys() 29 if len(keys) <= 0 { 30 t.Fatal("should have items") 31 } 32 33 th.App.ClearSessionCacheForUser(session.UserId) 34 35 rkeys := th.App.sessionCache.Keys() 36 if len(rkeys) != len(keys)-1 { 37 t.Fatal("should have one less") 38 } 39 } 40 41 func TestGetSessionIdleTimeoutInMinutes(t *testing.T) { 42 th := Setup().InitBasic() 43 defer th.TearDown() 44 45 session := &model.Session{ 46 UserId: model.NewId(), 47 } 48 49 session, _ = th.App.CreateSession(session) 50 51 isLicensed := utils.IsLicensed() 52 license := utils.License() 53 timeout := *th.App.Config().ServiceSettings.SessionIdleTimeoutInMinutes 54 defer func() { 55 utils.SetIsLicensed(isLicensed) 56 utils.SetLicense(license) 57 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = timeout }) 58 }() 59 utils.SetIsLicensed(true) 60 utils.SetLicense(&model.License{Features: &model.Features{}}) 61 utils.License().Features.SetDefaults() 62 *utils.License().Features.Compliance = true 63 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 5 }) 64 65 rsession, err := th.App.GetSession(session.Token) 66 require.Nil(t, err) 67 assert.Equal(t, rsession.Id, session.Id) 68 69 rsession, err = th.App.GetSession(session.Token) 70 71 // Test regular session, should timeout 72 time := session.LastActivityAt - (1000 * 60 * 6) 73 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 74 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 75 76 rsession, err = th.App.GetSession(session.Token) 77 require.NotNil(t, err) 78 assert.Equal(t, "api.context.invalid_token.error", err.Id) 79 assert.Equal(t, "idle timeout", err.DetailedError) 80 assert.Nil(t, rsession) 81 82 // Test mobile session, should not timeout 83 session = &model.Session{ 84 UserId: model.NewId(), 85 DeviceId: "android:" + model.NewId(), 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 oauth session, should not timeout 97 session = &model.Session{ 98 UserId: model.NewId(), 99 IsOAuth: true, 100 } 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 personal access token session, should not timeout 111 session = &model.Session{ 112 UserId: model.NewId(), 113 } 114 session.AddProp(model.SESSION_PROP_TYPE, model.SESSION_TYPE_USER_ACCESS_TOKEN) 115 116 session, _ = th.App.CreateSession(session) 117 time = session.LastActivityAt - (1000 * 60 * 6) 118 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 119 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 120 121 _, err = th.App.GetSession(session.Token) 122 assert.Nil(t, err) 123 124 // Test regular session with license off, should not timeout 125 *utils.License().Features.Compliance = false 126 127 session = &model.Session{ 128 UserId: model.NewId(), 129 } 130 131 session, _ = th.App.CreateSession(session) 132 time = session.LastActivityAt - (1000 * 60 * 6) 133 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 134 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 135 136 _, err = th.App.GetSession(session.Token) 137 assert.Nil(t, err) 138 139 *utils.License().Features.Compliance = true 140 141 // Test regular session with timeout set to 0, should not timeout 142 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 0 }) 143 144 session = &model.Session{ 145 UserId: model.NewId(), 146 } 147 148 session, _ = th.App.CreateSession(session) 149 time = session.LastActivityAt - (1000 * 60 * 6) 150 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 151 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 152 153 _, err = th.App.GetSession(session.Token) 154 assert.Nil(t, err) 155 }