mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* first steps towards revoke all sessions endpoint * route added * change permission into a more restrictive one * fix url * add store code * testing & mocking * fixing what merge broke * remove sessions without retrieving them * flush sessions from cache * stop going through sessions to revoke caches, not needed anymore * add test, fix func name * fix tests * remove unneeded code * [MM-7854]remove access tokens, move to users * fix docstring * [MM-7854] improve readability by using require * [MM-7854] fix tests * [MM-7854]fix comment * [MM-7854]improve testing logic
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
)
|
|
|
|
func TestCache(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
session := &model.Session{
|
|
Id: model.NewId(),
|
|
Token: model.NewId(),
|
|
UserId: model.NewId(),
|
|
}
|
|
|
|
session2 := &model.Session{
|
|
Id: model.NewId(),
|
|
Token: model.NewId(),
|
|
UserId: model.NewId(),
|
|
}
|
|
|
|
th.App.Srv.sessionCache.AddWithExpiresInSecs(session.Token, session, 5*60)
|
|
th.App.Srv.sessionCache.AddWithExpiresInSecs(session2.Token, session2, 5*60)
|
|
|
|
keys := th.App.Srv.sessionCache.Keys()
|
|
require.NotEmpty(t, keys)
|
|
|
|
th.App.ClearSessionCacheForUser(session.UserId)
|
|
|
|
rkeys := th.App.Srv.sessionCache.Keys()
|
|
require.Lenf(t, rkeys, len(keys)-1, "should have one less: %d - %d != 1", len(keys), len(rkeys))
|
|
require.NotEmpty(t, rkeys)
|
|
|
|
th.App.ClearSessionCacheForAllUsers()
|
|
|
|
rkeys = th.App.Srv.sessionCache.Keys()
|
|
require.Empty(t, rkeys)
|
|
}
|
|
|
|
func TestGetSessionIdleTimeoutInMinutes(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
session := &model.Session{
|
|
UserId: model.NewId(),
|
|
}
|
|
|
|
session, _ = th.App.CreateSession(session)
|
|
|
|
th.App.SetLicense(model.NewTestLicense("compliance"))
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 5 })
|
|
|
|
rsession, err := th.App.GetSession(session.Token)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, rsession.Id, session.Id)
|
|
|
|
// Test regular session, should timeout
|
|
time := session.LastActivityAt - (1000 * 60 * 6)
|
|
err = th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time)
|
|
require.Nil(t, err)
|
|
th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId)
|
|
|
|
rsession, err = th.App.GetSession(session.Token)
|
|
require.NotNil(t, err)
|
|
assert.Equal(t, "api.context.invalid_token.error", err.Id)
|
|
assert.Equal(t, "idle timeout", err.DetailedError)
|
|
assert.Nil(t, rsession)
|
|
|
|
// Test oauth session, should not timeout
|
|
session = &model.Session{
|
|
UserId: model.NewId(),
|
|
IsOAuth: true,
|
|
}
|
|
|
|
session, _ = th.App.CreateSession(session)
|
|
time = session.LastActivityAt - (1000 * 60 * 6)
|
|
err = th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time)
|
|
require.Nil(t, err)
|
|
th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId)
|
|
|
|
_, err = th.App.GetSession(session.Token)
|
|
assert.Nil(t, err)
|
|
|
|
// Test personal access token session, should not timeout
|
|
session = &model.Session{
|
|
UserId: model.NewId(),
|
|
}
|
|
session.AddProp(model.SESSION_PROP_TYPE, model.SESSION_TYPE_USER_ACCESS_TOKEN)
|
|
|
|
session, _ = th.App.CreateSession(session)
|
|
time = session.LastActivityAt - (1000 * 60 * 6)
|
|
err = th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time)
|
|
require.Nil(t, err)
|
|
th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId)
|
|
|
|
_, err = th.App.GetSession(session.Token)
|
|
assert.Nil(t, err)
|
|
|
|
th.App.SetLicense(model.NewTestLicense("compliance"))
|
|
|
|
// Test regular session with timeout set to 0, should not timeout
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.SessionIdleTimeoutInMinutes = 0 })
|
|
|
|
session = &model.Session{
|
|
UserId: model.NewId(),
|
|
}
|
|
|
|
session, _ = th.App.CreateSession(session)
|
|
time = session.LastActivityAt - (1000 * 60 * 6)
|
|
err = th.App.Srv.Store.Session().UpdateLastActivityAt(session.Id, time)
|
|
require.Nil(t, err)
|
|
th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId)
|
|
|
|
_, err = th.App.GetSession(session.Token)
|
|
assert.Nil(t, err)
|
|
}
|