github.com/nhannv/mattermost-server@v5.11.1+incompatible/app/config_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "strconv" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/mattermost/mattermost-server/model" 14 "github.com/mattermost/mattermost-server/store/sqlstore" 15 "github.com/mattermost/mattermost-server/utils" 16 ) 17 18 func TestConfigListener(t *testing.T) { 19 th := Setup(t).InitBasic() 20 defer th.TearDown() 21 22 originalSiteName := th.App.Config().TeamSettings.SiteName 23 24 listenerCalled := false 25 listener := func(oldConfig *model.Config, newConfig *model.Config) { 26 assert.False(t, listenerCalled, "listener called twice") 27 28 assert.Equal(t, *originalSiteName, *oldConfig.TeamSettings.SiteName, "old config contains incorrect site name") 29 assert.Equal(t, "test123", *newConfig.TeamSettings.SiteName, "new config contains incorrect site name") 30 31 listenerCalled = true 32 } 33 listenerId := th.App.AddConfigListener(listener) 34 defer th.App.RemoveConfigListener(listenerId) 35 36 listener2Called := false 37 listener2 := func(oldConfig *model.Config, newConfig *model.Config) { 38 assert.False(t, listener2Called, "listener2 called twice") 39 40 listener2Called = true 41 } 42 listener2Id := th.App.AddConfigListener(listener2) 43 defer th.App.RemoveConfigListener(listener2Id) 44 45 th.App.UpdateConfig(func(cfg *model.Config) { 46 *cfg.TeamSettings.SiteName = "test123" 47 }) 48 49 assert.True(t, listenerCalled, "listener should've been called") 50 assert.True(t, listener2Called, "listener 2 should've been called") 51 } 52 53 func TestAsymmetricSigningKey(t *testing.T) { 54 th := Setup(t).InitBasic() 55 defer th.TearDown() 56 assert.NotNil(t, th.App.AsymmetricSigningKey()) 57 assert.NotEmpty(t, th.App.ClientConfig()["AsymmetricSigningPublicKey"]) 58 } 59 60 func TestPostActionCookieSecret(t *testing.T) { 61 th := Setup(t).InitBasic() 62 defer th.TearDown() 63 assert.Equal(t, 32, len(th.App.PostActionCookieSecret())) 64 } 65 66 func TestClientConfigWithComputed(t *testing.T) { 67 th := Setup(t).InitBasic() 68 defer th.TearDown() 69 70 config := th.App.ClientConfigWithComputed() 71 if _, ok := config["NoAccounts"]; !ok { 72 t.Fatal("expected NoAccounts in returned config") 73 } 74 if _, ok := config["MaxPostSize"]; !ok { 75 t.Fatal("expected MaxPostSize in returned config") 76 } 77 } 78 79 func TestEnsureInstallationDate(t *testing.T) { 80 th := Setup(t) 81 defer th.TearDown() 82 83 tt := []struct { 84 Name string 85 PrevInstallationDate *int64 86 UsersCreationDates []int64 87 ExpectedInstallationDate *int64 88 }{ 89 { 90 Name: "New installation: no users, no installation date", 91 PrevInstallationDate: nil, 92 UsersCreationDates: nil, 93 ExpectedInstallationDate: model.NewInt64(utils.MillisFromTime(time.Now())), 94 }, 95 { 96 Name: "Old installation: users, no installation date", 97 PrevInstallationDate: nil, 98 UsersCreationDates: []int64{10000000000, 30000000000, 20000000000}, 99 ExpectedInstallationDate: model.NewInt64(10000000000), 100 }, 101 { 102 Name: "New installation, second run: no users, installation date", 103 PrevInstallationDate: model.NewInt64(80000000000), 104 UsersCreationDates: []int64{10000000000, 30000000000, 20000000000}, 105 ExpectedInstallationDate: model.NewInt64(80000000000), 106 }, 107 { 108 Name: "Old installation already updated: users, installation date", 109 PrevInstallationDate: model.NewInt64(90000000000), 110 UsersCreationDates: []int64{10000000000, 30000000000, 20000000000}, 111 ExpectedInstallationDate: model.NewInt64(90000000000), 112 }, 113 } 114 115 for _, tc := range tt { 116 t.Run(tc.Name, func(t *testing.T) { 117 sqlStore := th.App.Srv.Store.User().(*sqlstore.SqlUserStore) 118 sqlStore.GetMaster().Exec("DELETE FROM Users") 119 120 for _, createAt := range tc.UsersCreationDates { 121 user := th.CreateUser() 122 user.CreateAt = createAt 123 sqlStore.GetMaster().Exec("UPDATE Users SET CreateAt = :CreateAt WHERE Id = :UserId", map[string]interface{}{"CreateAt": createAt, "UserId": user.Id}) 124 } 125 126 if tc.PrevInstallationDate == nil { 127 <-th.App.Srv.Store.System().PermanentDeleteByName(model.SYSTEM_INSTALLATION_DATE_KEY) 128 } else { 129 <-th.App.Srv.Store.System().SaveOrUpdate(&model.System{ 130 Name: model.SYSTEM_INSTALLATION_DATE_KEY, 131 Value: strconv.FormatInt(*tc.PrevInstallationDate, 10), 132 }) 133 } 134 135 err := th.App.ensureInstallationDate() 136 137 if tc.ExpectedInstallationDate == nil { 138 assert.Error(t, err) 139 } else { 140 assert.NoError(t, err) 141 142 result := <-th.App.Srv.Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY) 143 assert.Nil(t, result.Err) 144 data, _ := result.Data.(*model.System) 145 value, _ := strconv.ParseInt(data.Value, 10, 64) 146 assert.True(t, *tc.ExpectedInstallationDate <= value && *tc.ExpectedInstallationDate+1000 >= value) 147 } 148 149 sqlStore.GetMaster().Exec("DELETE FROM Users") 150 }) 151 } 152 }