From 6bf09e2c34c568e3cb0d296142d5abed77332635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Wed, 8 Aug 2018 12:04:36 +0200 Subject: MM-11384: Add system install date information to the client config (#9218) * MM-11384: Add system install date information to the client config * Fixing translation text * Fixes from Peer Review --- app/config_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'app/config_test.go') diff --git a/app/config_test.go b/app/config_test.go index 4fc7df5e2..eb3fa8a53 100644 --- a/app/config_test.go +++ b/app/config_test.go @@ -4,11 +4,15 @@ package app import ( + "strconv" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/store/sqlstore" + "github.com/mattermost/mattermost-server/utils" ) func TestConfigListener(t *testing.T) { @@ -76,3 +80,80 @@ func TestClientConfigWithComputed(t *testing.T) { t.Fatal("expected MaxPostSize in returned config") } } + +func TestEnsureInstallationDate(t *testing.T) { + th := Setup() + defer th.TearDown() + + tt := []struct { + Name string + PrevInstallationDate *int64 + UsersCreationDates []int64 + ExpectedInstallationDate *int64 + }{ + { + Name: "New installation: no users, no installation date", + PrevInstallationDate: nil, + UsersCreationDates: nil, + ExpectedInstallationDate: model.NewInt64(utils.MillisFromTime(time.Now())), + }, + { + Name: "Old installation: users, no installation date", + PrevInstallationDate: nil, + UsersCreationDates: []int64{10000000000, 30000000000, 20000000000}, + ExpectedInstallationDate: model.NewInt64(10000000000), + }, + { + Name: "New installation, second run: no users, installation date", + PrevInstallationDate: model.NewInt64(80000000000), + UsersCreationDates: []int64{10000000000, 30000000000, 20000000000}, + ExpectedInstallationDate: model.NewInt64(80000000000), + }, + { + Name: "Old installation already updated: users, installation date", + PrevInstallationDate: model.NewInt64(90000000000), + UsersCreationDates: []int64{10000000000, 30000000000, 20000000000}, + ExpectedInstallationDate: model.NewInt64(90000000000), + }, + } + + for _, tc := range tt { + t.Run(tc.Name, func(t *testing.T) { + sqlStore := th.App.Srv.Store.User().(*sqlstore.SqlUserStore) + sqlStore.GetMaster().Exec("DELETE FROM Users") + + var users []*model.User + for _, createAt := range tc.UsersCreationDates { + user := th.CreateUser() + user.CreateAt = createAt + sqlStore.GetMaster().Exec("UPDATE Users SET CreateAt = :CreateAt WHERE Id = :UserId", map[string]interface{}{"CreateAt": createAt, "UserId": user.Id}) + users = append(users, user) + } + + if tc.PrevInstallationDate == nil { + <-th.App.Srv.Store.System().PermanentDeleteByName(model.SYSTEM_INSTALLATION_DATE_KEY) + } else { + <-th.App.Srv.Store.System().SaveOrUpdate(&model.System{ + Name: model.SYSTEM_INSTALLATION_DATE_KEY, + Value: strconv.FormatInt(*tc.PrevInstallationDate, 10), + }) + } + + err := th.App.ensureInstallationDate() + + if tc.ExpectedInstallationDate == nil { + assert.Error(t, err) + } else { + assert.NoError(t, err) + + result := <-th.App.Srv.Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY) + assert.Nil(t, result.Err) + data, _ := result.Data.(*model.System) + value, _ := strconv.ParseInt(data.Value, 10, 64) + assert.True(t, *tc.ExpectedInstallationDate <= value && *tc.ExpectedInstallationDate+1000 >= value) + } + + sqlStore.GetMaster().Exec("DELETE FROM Users") + }) + } +} -- cgit v1.2.3-1-g7c22