github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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 rsession, err = th.App.GetSession(session.Token) 58 59 // Test regular session, should timeout 60 time := session.LastActivityAt - (1000 * 60 * 6) 61 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 62 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 63 64 rsession, err = th.App.GetSession(session.Token) 65 require.NotNil(t, err) 66 assert.Equal(t, "api.context.invalid_token.error", err.Id) 67 assert.Equal(t, "idle timeout", err.DetailedError) 68 assert.Nil(t, rsession) 69 70 // Test mobile session, should not timeout 71 session = &model.Session{ 72 UserId: model.NewId(), 73 DeviceId: "android:" + model.NewId(), 74 } 75 76 session, _ = th.App.CreateSession(session) 77 time = session.LastActivityAt - (1000 * 60 * 6) 78 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 79 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 80 81 _, err = th.App.GetSession(session.Token) 82 assert.Nil(t, err) 83 84 // Test oauth session, should not timeout 85 session = &model.Session{ 86 UserId: model.NewId(), 87 IsOAuth: true, 88 } 89 90 session, _ = th.App.CreateSession(session) 91 time = session.LastActivityAt - (1000 * 60 * 6) 92 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 93 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 94 95 _, err = th.App.GetSession(session.Token) 96 assert.Nil(t, err) 97 98 // Test personal access token session, should not timeout 99 session = &model.Session{ 100 UserId: model.NewId(), 101 } 102 session.AddProp(model.SESSION_PROP_TYPE, model.SESSION_TYPE_USER_ACCESS_TOKEN) 103 104 session, _ = th.App.CreateSession(session) 105 time = session.LastActivityAt - (1000 * 60 * 6) 106 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 107 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 108 109 _, err = th.App.GetSession(session.Token) 110 assert.Nil(t, err) 111 112 // Test regular session with license off, should not timeout 113 th.App.SetLicense(nil) 114 115 session = &model.Session{ 116 UserId: model.NewId(), 117 } 118 119 session, _ = th.App.CreateSession(session) 120 time = session.LastActivityAt - (1000 * 60 * 6) 121 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 122 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 123 124 _, err = th.App.GetSession(session.Token) 125 assert.Nil(t, err) 126 127 th.App.SetLicense(model.NewTestLicense("compliance")) 128 129 // Test regular session with timeout set to 0, should not timeout 130 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 0 }) 131 132 session = &model.Session{ 133 UserId: model.NewId(), 134 } 135 136 session, _ = th.App.CreateSession(session) 137 time = session.LastActivityAt - (1000 * 60 * 6) 138 <-th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time) 139 th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) 140 141 _, err = th.App.GetSession(session.Token) 142 assert.Nil(t, err) 143 }