2022-04-25 20:58:08 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
package boards
|
2022-04-25 20:58:08 +02:00
|
|
|
|
|
|
|
import (
|
2022-07-15 07:51:50 +02:00
|
|
|
"os"
|
2022-04-25 20:58:08 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/mattermost/focalboard/server/server"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
|
|
|
"github.com/mattermost/focalboard/server/services/permissions/localpermissions"
|
|
|
|
"github.com/mattermost/focalboard/server/services/store/mockstore"
|
2022-07-15 07:51:50 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-04-25 20:58:08 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestHelperMockStore struct {
|
|
|
|
Server *server.Server
|
|
|
|
Store *mockstore.MockStore
|
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func SetupTestHelperMockStore(t *testing.T) (*TestHelperMockStore, func()) {
|
2022-04-25 20:58:08 +02:00
|
|
|
th := &TestHelperMockStore{}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
origUnitTesting := os.Getenv("FOCALBOARD_UNIT_TESTING")
|
|
|
|
os.Setenv("FOCALBOARD_UNIT_TESTING", "1")
|
|
|
|
|
2022-04-25 20:58:08 +02:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
mockStore := mockstore.NewMockStore(ctrl)
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
tearDown := func() {
|
|
|
|
defer ctrl.Finish()
|
|
|
|
os.Setenv("FOCALBOARD_UNIT_TESTING", origUnitTesting)
|
|
|
|
}
|
|
|
|
|
2022-04-25 20:58:08 +02:00
|
|
|
th.Server = newTestServerMock(mockStore)
|
|
|
|
th.Store = mockStore
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
return th, tearDown
|
2022-04-25 20:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestServerMock(mockStore *mockstore.MockStore) *server.Server {
|
|
|
|
config := &config.Configuration{
|
|
|
|
EnableDataRetention: false,
|
|
|
|
DataRetentionDays: 10,
|
|
|
|
FilesDriver: "local",
|
|
|
|
FilesPath: "./files",
|
|
|
|
WebPath: "/",
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := mlog.CreateConsoleTestLogger(true, mlog.LvlDebug)
|
|
|
|
|
|
|
|
mockStore.EXPECT().GetTeam(gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockStore.EXPECT().UpsertTeamSignupToken(gomock.Any()).AnyTimes()
|
|
|
|
mockStore.EXPECT().GetSystemSettings().AnyTimes()
|
|
|
|
mockStore.EXPECT().SetSystemSetting(gomock.Any(), gomock.Any()).AnyTimes()
|
|
|
|
|
|
|
|
permissionsService := localpermissions.New(mockStore, logger)
|
|
|
|
|
|
|
|
srv, err := server.New(server.Params{
|
|
|
|
Cfg: config,
|
|
|
|
DBStore: mockStore,
|
|
|
|
Logger: logger,
|
|
|
|
PermissionsService: permissionsService,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunDataRetention(t *testing.T) {
|
2022-07-15 07:51:50 +02:00
|
|
|
th, tearDown := SetupTestHelperMockStore(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
b := &BoardsApp{
|
|
|
|
server: th.Server,
|
|
|
|
logger: mlog.CreateConsoleTestLogger(true, mlog.LvlError),
|
|
|
|
}
|
2022-04-25 20:58:08 +02:00
|
|
|
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
|
|
|
|
t.Run("test null license", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetLicense().Return(nil)
|
2022-07-15 07:51:50 +02:00
|
|
|
_, err := b.RunDataRetention(now, 10)
|
2022-04-25 20:58:08 +02:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, ErrInsufficientLicense, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test invalid license", func(t *testing.T) {
|
|
|
|
falseValue := false
|
|
|
|
|
|
|
|
th.Store.EXPECT().GetLicense().Return(
|
|
|
|
&model.License{
|
|
|
|
Features: &model.Features{
|
|
|
|
DataRetention: &falseValue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-07-15 07:51:50 +02:00
|
|
|
_, err := b.RunDataRetention(now, 10)
|
2022-04-25 20:58:08 +02:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, ErrInsufficientLicense, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test valid license, invalid config", func(t *testing.T) {
|
|
|
|
trueValue := true
|
|
|
|
th.Store.EXPECT().GetLicense().Return(
|
|
|
|
&model.License{
|
|
|
|
Features: &model.Features{
|
|
|
|
DataRetention: &trueValue,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
count, err := b.RunDataRetention(now, 10)
|
2022-04-25 20:58:08 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, int64(0), count)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test valid license, valid config", func(t *testing.T) {
|
|
|
|
trueValue := true
|
|
|
|
th.Store.EXPECT().GetLicense().Return(
|
|
|
|
&model.License{
|
|
|
|
Features: &model.Features{
|
|
|
|
DataRetention: &trueValue,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
th.Store.EXPECT().RunDataRetention(gomock.Any(), int64(10)).Return(int64(100), nil)
|
2022-07-15 07:51:50 +02:00
|
|
|
b.server.Config().EnableDataRetention = true
|
2022-04-25 20:58:08 +02:00
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
count, err := b.RunDataRetention(now, 10)
|
2022-04-25 20:58:08 +02:00
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, int64(100), count)
|
|
|
|
})
|
|
|
|
}
|