From 544304d1c64f7dd792b26e31fb3796191265c1ca Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Wed, 18 Jan 2023 10:34:42 +0100 Subject: [PATCH] MM-49720 Remove the check for active sessions in IsFirstUserAccount (#22102) --- app/platform/config.go | 13 ++--------- app/platform/config_test.go | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/app/platform/config.go b/app/platform/config.go index e8eb425b96..808c1f4ebe 100644 --- a/app/platform/config.go +++ b/app/platform/config.go @@ -320,21 +320,12 @@ func (ps *PlatformService) LimitedClientConfig() map[string]string { } func (ps *PlatformService) IsFirstUserAccount() bool { - cachedSessions, err := ps.sessionCache.Len() + count, err := ps.Store.User().Count(model.UserCountOptions{IncludeDeleted: true}) if err != nil { return false } - if cachedSessions == 0 { - count, err := ps.Store.User().Count(model.UserCountOptions{IncludeDeleted: true}) - if err != nil { - return false - } - if count <= 0 { - return true - } - } - return false + return count <= 0 } func (ps *PlatformService) MaxPostSize() int { diff --git a/app/platform/config_test.go b/app/platform/config_test.go index 8268b32929..6b390784ae 100644 --- a/app/platform/config_test.go +++ b/app/platform/config_test.go @@ -4,6 +4,7 @@ package platform import ( + "errors" "testing" "github.com/stretchr/testify/assert" @@ -12,6 +13,7 @@ import ( "github.com/mattermost/mattermost-server/v6/einterfaces/mocks" "github.com/mattermost/mattermost-server/v6/model" + smocks "github.com/mattermost/mattermost-server/v6/store/storetest/mocks" ) func TestConfigListener(t *testing.T) { @@ -98,3 +100,47 @@ func TestConfigSave(t *testing.T) { metricsMock.AssertNumberOfCalls(t, "Register", 1) }) } + +func TestIsFirstUserAccount(t *testing.T) { + th := SetupWithStoreMock(t) + defer th.TearDown() + storeMock := th.Service.Store.(*smocks.Store) + userStoreMock := &smocks.UserStore{} + storeMock.On("User").Return(userStoreMock) + + type test struct { + name string + count int64 + err error + result bool + } + + tests := []test{ + {"success no users", 0, nil, true}, + {"success one user", 1, nil, false}, + {"success multiple users", 42, nil, false}, + {"success negative users", -100, nil, true}, + {"failed request", 0, errors.New("error"), false}, + } + + for _, te := range tests { + t.Run(te.name, func(t *testing.T) { + *userStoreMock = smocks.UserStore{} + + userStoreMock.On("Count", model.UserCountOptions{IncludeDeleted: true}).Return(te.count, te.err) + require.Equal(t, te.result, th.Service.IsFirstUserAccount()) + }) + } + + // create a session, this should not affect IsFirstUserAccount + th.Service.sessionCache.Set("mock_session", 1) + + for _, te := range tests { + t.Run(te.name+" with session", func(t *testing.T) { + *userStoreMock = smocks.UserStore{} + + userStoreMock.On("Count", model.UserCountOptions{IncludeDeleted: true}).Return(te.count, te.err) + require.Equal(t, te.result, th.Service.IsFirstUserAccount()) + }) + } +}