focalboard/mattermost-plugin/server/boards/data_retention_test.go
Miguel de la Cruz 4b0fb92fba
Multi product architecture (#3381)
- provides support for compiling Boards directly into the Mattermost suite server
- a ServicesAPI interface replaces the PluginAPI to allow for implementations coming from pluginAPI and suite server.
- a new product package provides a place to register Boards as a suite product and handles life-cycle events
- a new boards package replaces much of the mattermost-plugin logic, allowing this to be shared between plugin and product
- Boards now uses module workspaces; run make setup-go-work
2022-07-18 13:21:57 -04:00

142 lines
3.5 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package boards
import (
"os"
"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"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)
type TestHelperMockStore struct {
Server *server.Server
Store *mockstore.MockStore
}
func SetupTestHelperMockStore(t *testing.T) (*TestHelperMockStore, func()) {
th := &TestHelperMockStore{}
origUnitTesting := os.Getenv("FOCALBOARD_UNIT_TESTING")
os.Setenv("FOCALBOARD_UNIT_TESTING", "1")
ctrl := gomock.NewController(t)
mockStore := mockstore.NewMockStore(ctrl)
tearDown := func() {
defer ctrl.Finish()
os.Setenv("FOCALBOARD_UNIT_TESTING", origUnitTesting)
}
th.Server = newTestServerMock(mockStore)
th.Store = mockStore
return th, tearDown
}
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) {
th, tearDown := SetupTestHelperMockStore(t)
defer tearDown()
b := &BoardsApp{
server: th.Server,
logger: mlog.CreateConsoleTestLogger(true, mlog.LvlError),
}
now := time.Now().UnixNano()
t.Run("test null license", func(t *testing.T) {
th.Store.EXPECT().GetLicense().Return(nil)
_, err := b.RunDataRetention(now, 10)
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,
},
},
)
_, err := b.RunDataRetention(now, 10)
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,
},
})
count, err := b.RunDataRetention(now, 10)
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)
b.server.Config().EnableDataRetention = true
count, err := b.RunDataRetention(now, 10)
assert.Nil(t, err)
assert.Equal(t, int64(100), count)
})
}