github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/config_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestConfigDefaultFileSettingsDirectory(t *testing.T) { 13 c1 := Config{} 14 c1.SetDefaults() 15 16 if c1.FileSettings.Directory != "./data/" { 17 t.Fatal("FileSettings.Directory should default to './data/'") 18 } 19 } 20 21 func TestConfigDefaultEmailNotificationContentsType(t *testing.T) { 22 c1 := Config{} 23 c1.SetDefaults() 24 25 if *c1.EmailSettings.EmailNotificationContentsType != EMAIL_NOTIFICATION_CONTENTS_FULL { 26 t.Fatal("EmailSettings.EmailNotificationContentsType should default to 'full'") 27 } 28 } 29 30 func TestConfigDefaultFileSettingsS3SSE(t *testing.T) { 31 c1 := Config{} 32 c1.SetDefaults() 33 34 if *c1.FileSettings.AmazonS3SSE { 35 t.Fatal("FileSettings.AmazonS3SSE should default to false") 36 } 37 } 38 39 func TestConfigDefaultServiceSettingsExperimentalGroupUnreadChannels(t *testing.T) { 40 c1 := Config{} 41 c1.SetDefaults() 42 43 if *c1.ServiceSettings.ExperimentalGroupUnreadChannels != GROUP_UNREAD_CHANNELS_DISABLED { 44 t.Fatal("ServiceSettings.ExperimentalGroupUnreadChannels should default to 'disabled'") 45 } 46 47 // This setting was briefly a boolean, so ensure that those values still work as expected 48 c1 = Config{ 49 ServiceSettings: ServiceSettings{ 50 ExperimentalGroupUnreadChannels: NewString("1"), 51 }, 52 } 53 c1.SetDefaults() 54 55 if *c1.ServiceSettings.ExperimentalGroupUnreadChannels != GROUP_UNREAD_CHANNELS_DEFAULT_ON { 56 t.Fatal("ServiceSettings.ExperimentalGroupUnreadChannels should set true to 'default on'") 57 } 58 59 c1 = Config{ 60 ServiceSettings: ServiceSettings{ 61 ExperimentalGroupUnreadChannels: NewString("0"), 62 }, 63 } 64 c1.SetDefaults() 65 66 if *c1.ServiceSettings.ExperimentalGroupUnreadChannels != GROUP_UNREAD_CHANNELS_DISABLED { 67 t.Fatal("ServiceSettings.ExperimentalGroupUnreadChannels should set false to 'disabled'") 68 } 69 } 70 71 func TestMessageExportSettingsIsValidEnableExportNotSet(t *testing.T) { 72 fs := &FileSettings{} 73 mes := &MessageExportSettings{} 74 75 // should fail fast because mes.EnableExport is not set 76 require.Error(t, mes.isValid(*fs)) 77 } 78 79 func TestMessageExportSettingsIsValidEnableExportFalse(t *testing.T) { 80 fs := &FileSettings{} 81 mes := &MessageExportSettings{ 82 EnableExport: NewBool(false), 83 } 84 85 // should fail fast because message export isn't enabled 86 require.Nil(t, mes.isValid(*fs)) 87 } 88 89 func TestMessageExportSettingsIsValidExportFromTimestampInvalid(t *testing.T) { 90 fs := &FileSettings{} 91 mes := &MessageExportSettings{ 92 EnableExport: NewBool(true), 93 } 94 95 // should fail fast because export from timestamp isn't set 96 require.Error(t, mes.isValid(*fs)) 97 98 mes.ExportFromTimestamp = NewInt64(-1) 99 100 // should fail fast because export from timestamp isn't valid 101 require.Error(t, mes.isValid(*fs)) 102 103 mes.ExportFromTimestamp = NewInt64(GetMillis() + 10000) 104 105 // should fail fast because export from timestamp is greater than current time 106 require.Error(t, mes.isValid(*fs)) 107 } 108 109 func TestMessageExportSettingsIsValidDailyRunTimeInvalid(t *testing.T) { 110 fs := &FileSettings{} 111 mes := &MessageExportSettings{ 112 EnableExport: NewBool(true), 113 ExportFromTimestamp: NewInt64(0), 114 } 115 116 // should fail fast because daily runtime isn't set 117 require.Error(t, mes.isValid(*fs)) 118 119 mes.DailyRunTime = NewString("33:33:33") 120 121 // should fail fast because daily runtime is invalid format 122 require.Error(t, mes.isValid(*fs)) 123 } 124 125 func TestMessageExportSettingsIsValidBatchSizeInvalid(t *testing.T) { 126 fs := &FileSettings{ 127 DriverName: NewString("foo"), // bypass file location check 128 } 129 mes := &MessageExportSettings{ 130 EnableExport: NewBool(true), 131 ExportFromTimestamp: NewInt64(0), 132 DailyRunTime: NewString("15:04"), 133 } 134 135 // should fail fast because batch size isn't set 136 require.Error(t, mes.isValid(*fs)) 137 } 138 139 func TestMessageExportSettingsIsValidExportFormatInvalid(t *testing.T) { 140 fs := &FileSettings{ 141 DriverName: NewString("foo"), // bypass file location check 142 } 143 mes := &MessageExportSettings{ 144 EnableExport: NewBool(true), 145 ExportFromTimestamp: NewInt64(0), 146 DailyRunTime: NewString("15:04"), 147 BatchSize: NewInt(100), 148 } 149 150 // should fail fast because export format isn't set 151 require.Error(t, mes.isValid(*fs)) 152 } 153 154 func TestMessageExportSettingsIsValidGlobalRelayEmailAddressInvalid(t *testing.T) { 155 fs := &FileSettings{ 156 DriverName: NewString("foo"), // bypass file location check 157 } 158 mes := &MessageExportSettings{ 159 EnableExport: NewBool(true), 160 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), 161 ExportFromTimestamp: NewInt64(0), 162 DailyRunTime: NewString("15:04"), 163 BatchSize: NewInt(100), 164 } 165 166 // should fail fast because global relay email address isn't set 167 require.Error(t, mes.isValid(*fs)) 168 } 169 170 func TestMessageExportSettingsIsValidActiance(t *testing.T) { 171 fs := &FileSettings{ 172 DriverName: NewString("foo"), // bypass file location check 173 } 174 mes := &MessageExportSettings{ 175 EnableExport: NewBool(true), 176 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_ACTIANCE), 177 ExportFromTimestamp: NewInt64(0), 178 DailyRunTime: NewString("15:04"), 179 BatchSize: NewInt(100), 180 } 181 182 // should pass because everything is valid 183 require.Nil(t, mes.isValid(*fs)) 184 } 185 186 func TestMessageExportSettingsIsValidGlobalRelay(t *testing.T) { 187 fs := &FileSettings{ 188 DriverName: NewString("foo"), // bypass file location check 189 } 190 mes := &MessageExportSettings{ 191 EnableExport: NewBool(true), 192 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), 193 ExportFromTimestamp: NewInt64(0), 194 DailyRunTime: NewString("15:04"), 195 BatchSize: NewInt(100), 196 GlobalRelayEmailAddress: NewString("test@mattermost.com"), 197 } 198 199 // should pass because everything is valid 200 require.Nil(t, mes.isValid(*fs)) 201 } 202 203 func TestMessageExportSetDefaults(t *testing.T) { 204 mes := &MessageExportSettings{} 205 mes.SetDefaults() 206 207 require.False(t, *mes.EnableExport) 208 require.Equal(t, "01:00", *mes.DailyRunTime) 209 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 210 require.Equal(t, 10000, *mes.BatchSize) 211 require.Equal(t, COMPLIANCE_EXPORT_TYPE_ACTIANCE, *mes.ExportFormat) 212 } 213 214 func TestMessageExportSetDefaultsExportEnabledExportFromTimestampNil(t *testing.T) { 215 mes := &MessageExportSettings{ 216 EnableExport: NewBool(true), 217 } 218 mes.SetDefaults() 219 220 require.True(t, *mes.EnableExport) 221 require.Equal(t, "01:00", *mes.DailyRunTime) 222 require.NotEqual(t, int64(0), *mes.ExportFromTimestamp) 223 require.True(t, *mes.ExportFromTimestamp <= GetMillis()) 224 require.Equal(t, 10000, *mes.BatchSize) 225 } 226 227 func TestMessageExportSetDefaultsExportEnabledExportFromTimestampZero(t *testing.T) { 228 mes := &MessageExportSettings{ 229 EnableExport: NewBool(true), 230 ExportFromTimestamp: NewInt64(0), 231 } 232 mes.SetDefaults() 233 234 require.True(t, *mes.EnableExport) 235 require.Equal(t, "01:00", *mes.DailyRunTime) 236 require.NotEqual(t, int64(0), *mes.ExportFromTimestamp) 237 require.True(t, *mes.ExportFromTimestamp <= GetMillis()) 238 require.Equal(t, 10000, *mes.BatchSize) 239 } 240 241 func TestMessageExportSetDefaultsExportEnabledExportFromTimestampNonZero(t *testing.T) { 242 mes := &MessageExportSettings{ 243 EnableExport: NewBool(true), 244 ExportFromTimestamp: NewInt64(12345), 245 } 246 mes.SetDefaults() 247 248 require.True(t, *mes.EnableExport) 249 require.Equal(t, "01:00", *mes.DailyRunTime) 250 require.Equal(t, int64(12345), *mes.ExportFromTimestamp) 251 require.Equal(t, 10000, *mes.BatchSize) 252 } 253 254 func TestMessageExportSetDefaultsExportDisabledExportFromTimestampNil(t *testing.T) { 255 mes := &MessageExportSettings{ 256 EnableExport: NewBool(false), 257 } 258 mes.SetDefaults() 259 260 require.False(t, *mes.EnableExport) 261 require.Equal(t, "01:00", *mes.DailyRunTime) 262 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 263 require.Equal(t, 10000, *mes.BatchSize) 264 } 265 266 func TestMessageExportSetDefaultsExportDisabledExportFromTimestampZero(t *testing.T) { 267 mes := &MessageExportSettings{ 268 EnableExport: NewBool(false), 269 ExportFromTimestamp: NewInt64(0), 270 } 271 mes.SetDefaults() 272 273 require.False(t, *mes.EnableExport) 274 require.Equal(t, "01:00", *mes.DailyRunTime) 275 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 276 require.Equal(t, 10000, *mes.BatchSize) 277 } 278 279 func TestMessageExportSetDefaultsExportDisabledExportFromTimestampNonZero(t *testing.T) { 280 mes := &MessageExportSettings{ 281 EnableExport: NewBool(false), 282 ExportFromTimestamp: NewInt64(12345), 283 } 284 mes.SetDefaults() 285 286 require.False(t, *mes.EnableExport) 287 require.Equal(t, "01:00", *mes.DailyRunTime) 288 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 289 require.Equal(t, 10000, *mes.BatchSize) 290 }