github.com/rajatvaryani/mattermost-server@v5.11.1+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 "fmt" 8 "reflect" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestConfigDefaults(t *testing.T) { 16 t.Parallel() 17 18 t.Run("somewhere nil when uninitialized", func(t *testing.T) { 19 c := Config{} 20 require.False(t, checkNowhereNil(t, "config", c)) 21 }) 22 23 t.Run("nowhere nil when initialized", func(t *testing.T) { 24 c := Config{} 25 c.SetDefaults() 26 require.True(t, checkNowhereNil(t, "config", c)) 27 }) 28 29 t.Run("nowhere nil when partially initialized", func(t *testing.T) { 30 var recursivelyUninitialize func(*Config, string, reflect.Value) 31 recursivelyUninitialize = func(config *Config, name string, v reflect.Value) { 32 if v.Type().Kind() == reflect.Ptr { 33 // Set every pointer we find in the tree to nil 34 v.Set(reflect.Zero(v.Type())) 35 require.True(t, v.IsNil()) 36 37 // SetDefaults on the root config should make it non-nil, otherwise 38 // it means that SetDefaults isn't being called recursively in 39 // all cases. 40 config.SetDefaults() 41 if assert.False(t, v.IsNil(), "%s should be non-nil after SetDefaults()", name) { 42 recursivelyUninitialize(config, fmt.Sprintf("(*%s)", name), v.Elem()) 43 } 44 45 } else if v.Type().Kind() == reflect.Struct { 46 for i := 0; i < v.NumField(); i++ { 47 recursivelyUninitialize(config, fmt.Sprintf("%s.%s", name, v.Type().Field(i).Name), v.Field(i)) 48 } 49 } 50 } 51 52 c := Config{} 53 c.SetDefaults() 54 recursivelyUninitialize(&c, "config", reflect.ValueOf(&c).Elem()) 55 }) 56 } 57 58 func TestConfigEmptySiteName(t *testing.T) { 59 c1 := Config{ 60 TeamSettings: TeamSettings{ 61 SiteName: NewString(""), 62 }, 63 } 64 c1.SetDefaults() 65 66 if *c1.TeamSettings.SiteName != TEAM_SETTINGS_DEFAULT_SITE_NAME { 67 t.Fatal("TeamSettings.SiteName should default to " + TEAM_SETTINGS_DEFAULT_SITE_NAME) 68 } 69 } 70 71 func TestConfigDefaultFileSettingsDirectory(t *testing.T) { 72 c1 := Config{} 73 c1.SetDefaults() 74 75 if *c1.FileSettings.Directory != "./data/" { 76 t.Fatal("FileSettings.Directory should default to './data/'") 77 } 78 } 79 80 func TestConfigDefaultEmailNotificationContentsType(t *testing.T) { 81 c1 := Config{} 82 c1.SetDefaults() 83 84 if *c1.EmailSettings.EmailNotificationContentsType != EMAIL_NOTIFICATION_CONTENTS_FULL { 85 t.Fatal("EmailSettings.EmailNotificationContentsType should default to 'full'") 86 } 87 } 88 89 func TestConfigDefaultFileSettingsS3SSE(t *testing.T) { 90 c1 := Config{} 91 c1.SetDefaults() 92 93 if *c1.FileSettings.AmazonS3SSE { 94 t.Fatal("FileSettings.AmazonS3SSE should default to false") 95 } 96 } 97 98 func TestConfigDefaultServiceSettingsExperimentalGroupUnreadChannels(t *testing.T) { 99 c1 := Config{} 100 c1.SetDefaults() 101 102 if *c1.ServiceSettings.ExperimentalGroupUnreadChannels != GROUP_UNREAD_CHANNELS_DISABLED { 103 t.Fatal("ServiceSettings.ExperimentalGroupUnreadChannels should default to 'disabled'") 104 } 105 106 // This setting was briefly a boolean, so ensure that those values still work as expected 107 c1 = Config{ 108 ServiceSettings: ServiceSettings{ 109 ExperimentalGroupUnreadChannels: NewString("1"), 110 }, 111 } 112 c1.SetDefaults() 113 114 if *c1.ServiceSettings.ExperimentalGroupUnreadChannels != GROUP_UNREAD_CHANNELS_DEFAULT_ON { 115 t.Fatal("ServiceSettings.ExperimentalGroupUnreadChannels should set true to 'default on'") 116 } 117 118 c1 = Config{ 119 ServiceSettings: ServiceSettings{ 120 ExperimentalGroupUnreadChannels: NewString("0"), 121 }, 122 } 123 c1.SetDefaults() 124 125 if *c1.ServiceSettings.ExperimentalGroupUnreadChannels != GROUP_UNREAD_CHANNELS_DISABLED { 126 t.Fatal("ServiceSettings.ExperimentalGroupUnreadChannels should set false to 'disabled'") 127 } 128 } 129 130 func TestTeamSettingsIsValidSiteNameEmpty(t *testing.T) { 131 c1 := Config{} 132 c1.SetDefaults() 133 c1.TeamSettings.SiteName = NewString("") 134 135 // should fail fast because ts.SiteName is not set 136 err := c1.TeamSettings.isValid() 137 if err == nil { 138 t.Fatal("TeamSettings validation should fail with an empty SiteName") 139 } 140 } 141 142 func TestMessageExportSettingsIsValidEnableExportNotSet(t *testing.T) { 143 fs := &FileSettings{} 144 mes := &MessageExportSettings{} 145 146 // should fail fast because mes.EnableExport is not set 147 require.Error(t, mes.isValid(*fs)) 148 } 149 150 func TestMessageExportSettingsIsValidEnableExportFalse(t *testing.T) { 151 fs := &FileSettings{} 152 mes := &MessageExportSettings{ 153 EnableExport: NewBool(false), 154 } 155 156 // should fail fast because message export isn't enabled 157 require.Nil(t, mes.isValid(*fs)) 158 } 159 160 func TestMessageExportSettingsIsValidExportFromTimestampInvalid(t *testing.T) { 161 fs := &FileSettings{} 162 mes := &MessageExportSettings{ 163 EnableExport: NewBool(true), 164 } 165 166 // should fail fast because export from timestamp isn't set 167 require.Error(t, mes.isValid(*fs)) 168 169 mes.ExportFromTimestamp = NewInt64(-1) 170 171 // should fail fast because export from timestamp isn't valid 172 require.Error(t, mes.isValid(*fs)) 173 174 mes.ExportFromTimestamp = NewInt64(GetMillis() + 10000) 175 176 // should fail fast because export from timestamp is greater than current time 177 require.Error(t, mes.isValid(*fs)) 178 } 179 180 func TestMessageExportSettingsIsValidDailyRunTimeInvalid(t *testing.T) { 181 fs := &FileSettings{} 182 mes := &MessageExportSettings{ 183 EnableExport: NewBool(true), 184 ExportFromTimestamp: NewInt64(0), 185 } 186 187 // should fail fast because daily runtime isn't set 188 require.Error(t, mes.isValid(*fs)) 189 190 mes.DailyRunTime = NewString("33:33:33") 191 192 // should fail fast because daily runtime is invalid format 193 require.Error(t, mes.isValid(*fs)) 194 } 195 196 func TestMessageExportSettingsIsValidBatchSizeInvalid(t *testing.T) { 197 fs := &FileSettings{ 198 DriverName: NewString("foo"), // bypass file location check 199 } 200 mes := &MessageExportSettings{ 201 EnableExport: NewBool(true), 202 ExportFromTimestamp: NewInt64(0), 203 DailyRunTime: NewString("15:04"), 204 } 205 206 // should fail fast because batch size isn't set 207 require.Error(t, mes.isValid(*fs)) 208 } 209 210 func TestMessageExportSettingsIsValidExportFormatInvalid(t *testing.T) { 211 fs := &FileSettings{ 212 DriverName: NewString("foo"), // bypass file location check 213 } 214 mes := &MessageExportSettings{ 215 EnableExport: NewBool(true), 216 ExportFromTimestamp: NewInt64(0), 217 DailyRunTime: NewString("15:04"), 218 BatchSize: NewInt(100), 219 } 220 221 // should fail fast because export format isn't set 222 require.Error(t, mes.isValid(*fs)) 223 } 224 225 func TestMessageExportSettingsIsValidGlobalRelayEmailAddressInvalid(t *testing.T) { 226 fs := &FileSettings{ 227 DriverName: NewString("foo"), // bypass file location check 228 } 229 mes := &MessageExportSettings{ 230 EnableExport: NewBool(true), 231 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), 232 ExportFromTimestamp: NewInt64(0), 233 DailyRunTime: NewString("15:04"), 234 BatchSize: NewInt(100), 235 } 236 237 // should fail fast because global relay email address isn't set 238 require.Error(t, mes.isValid(*fs)) 239 } 240 241 func TestMessageExportSettingsIsValidActiance(t *testing.T) { 242 fs := &FileSettings{ 243 DriverName: NewString("foo"), // bypass file location check 244 } 245 mes := &MessageExportSettings{ 246 EnableExport: NewBool(true), 247 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_ACTIANCE), 248 ExportFromTimestamp: NewInt64(0), 249 DailyRunTime: NewString("15:04"), 250 BatchSize: NewInt(100), 251 } 252 253 // should pass because everything is valid 254 require.Nil(t, mes.isValid(*fs)) 255 } 256 257 func TestMessageExportSettingsIsValidGlobalRelaySettingsMissing(t *testing.T) { 258 fs := &FileSettings{ 259 DriverName: NewString("foo"), // bypass file location check 260 } 261 mes := &MessageExportSettings{ 262 EnableExport: NewBool(true), 263 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), 264 ExportFromTimestamp: NewInt64(0), 265 DailyRunTime: NewString("15:04"), 266 BatchSize: NewInt(100), 267 } 268 269 // should fail because globalrelay settings are missing 270 require.Error(t, mes.isValid(*fs)) 271 } 272 273 func TestMessageExportSettingsIsValidGlobalRelaySettingsInvalidCustomerType(t *testing.T) { 274 fs := &FileSettings{ 275 DriverName: NewString("foo"), // bypass file location check 276 } 277 mes := &MessageExportSettings{ 278 EnableExport: NewBool(true), 279 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), 280 ExportFromTimestamp: NewInt64(0), 281 DailyRunTime: NewString("15:04"), 282 BatchSize: NewInt(100), 283 GlobalRelaySettings: &GlobalRelayMessageExportSettings{ 284 CustomerType: NewString("Invalid"), 285 EmailAddress: NewString("valid@mattermost.com"), 286 SmtpUsername: NewString("SomeUsername"), 287 SmtpPassword: NewString("SomePassword"), 288 }, 289 } 290 291 // should fail because customer type is invalid 292 require.Error(t, mes.isValid(*fs)) 293 } 294 295 // func TestMessageExportSettingsIsValidGlobalRelaySettingsInvalidEmailAddress(t *testing.T) { 296 func TestMessageExportSettingsGlobalRelaySettings(t *testing.T) { 297 fs := &FileSettings{ 298 DriverName: NewString("foo"), // bypass file location check 299 } 300 tests := []struct { 301 name string 302 value *GlobalRelayMessageExportSettings 303 success bool 304 }{ 305 { 306 "Invalid email address", 307 &GlobalRelayMessageExportSettings{ 308 CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A9), 309 EmailAddress: NewString("invalidEmailAddress"), 310 SmtpUsername: NewString("SomeUsername"), 311 SmtpPassword: NewString("SomePassword"), 312 }, 313 false, 314 }, 315 { 316 "Missing smtp username", 317 &GlobalRelayMessageExportSettings{ 318 CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A10), 319 EmailAddress: NewString("valid@mattermost.com"), 320 SmtpPassword: NewString("SomePassword"), 321 }, 322 false, 323 }, 324 { 325 "Invalid smtp username", 326 &GlobalRelayMessageExportSettings{ 327 CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A10), 328 EmailAddress: NewString("valid@mattermost.com"), 329 SmtpUsername: NewString(""), 330 SmtpPassword: NewString("SomePassword"), 331 }, 332 false, 333 }, 334 { 335 "Invalid smtp password", 336 &GlobalRelayMessageExportSettings{ 337 CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A10), 338 EmailAddress: NewString("valid@mattermost.com"), 339 SmtpUsername: NewString("SomeUsername"), 340 SmtpPassword: NewString(""), 341 }, 342 false, 343 }, 344 { 345 "Valid data", 346 &GlobalRelayMessageExportSettings{ 347 CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A9), 348 EmailAddress: NewString("valid@mattermost.com"), 349 SmtpUsername: NewString("SomeUsername"), 350 SmtpPassword: NewString("SomePassword"), 351 }, 352 true, 353 }, 354 } 355 356 for _, tt := range tests { 357 t.Run(tt.name, func(t *testing.T) { 358 mes := &MessageExportSettings{ 359 EnableExport: NewBool(true), 360 ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), 361 ExportFromTimestamp: NewInt64(0), 362 DailyRunTime: NewString("15:04"), 363 BatchSize: NewInt(100), 364 GlobalRelaySettings: tt.value, 365 } 366 367 if tt.success { 368 require.Nil(t, mes.isValid(*fs)) 369 } else { 370 require.Error(t, mes.isValid(*fs)) 371 } 372 }) 373 } 374 } 375 376 func TestMessageExportSetDefaults(t *testing.T) { 377 mes := &MessageExportSettings{} 378 mes.SetDefaults() 379 380 require.False(t, *mes.EnableExport) 381 require.Equal(t, "01:00", *mes.DailyRunTime) 382 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 383 require.Equal(t, 10000, *mes.BatchSize) 384 require.Equal(t, COMPLIANCE_EXPORT_TYPE_ACTIANCE, *mes.ExportFormat) 385 } 386 387 func TestMessageExportSetDefaultsExportEnabledExportFromTimestampNil(t *testing.T) { 388 // Test retained as protection against regression of MM-13185 389 mes := &MessageExportSettings{ 390 EnableExport: NewBool(true), 391 } 392 mes.SetDefaults() 393 394 require.True(t, *mes.EnableExport) 395 require.Equal(t, "01:00", *mes.DailyRunTime) 396 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 397 require.True(t, *mes.ExportFromTimestamp <= GetMillis()) 398 require.Equal(t, 10000, *mes.BatchSize) 399 } 400 401 func TestMessageExportSetDefaultsExportEnabledExportFromTimestampZero(t *testing.T) { 402 // Test retained as protection against regression of MM-13185 403 mes := &MessageExportSettings{ 404 EnableExport: NewBool(true), 405 ExportFromTimestamp: NewInt64(0), 406 } 407 mes.SetDefaults() 408 409 require.True(t, *mes.EnableExport) 410 require.Equal(t, "01:00", *mes.DailyRunTime) 411 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 412 require.True(t, *mes.ExportFromTimestamp <= GetMillis()) 413 require.Equal(t, 10000, *mes.BatchSize) 414 } 415 416 func TestMessageExportSetDefaultsExportEnabledExportFromTimestampNonZero(t *testing.T) { 417 mes := &MessageExportSettings{ 418 EnableExport: NewBool(true), 419 ExportFromTimestamp: NewInt64(12345), 420 } 421 mes.SetDefaults() 422 423 require.True(t, *mes.EnableExport) 424 require.Equal(t, "01:00", *mes.DailyRunTime) 425 require.Equal(t, int64(12345), *mes.ExportFromTimestamp) 426 require.Equal(t, 10000, *mes.BatchSize) 427 } 428 429 func TestMessageExportSetDefaultsExportDisabledExportFromTimestampNil(t *testing.T) { 430 mes := &MessageExportSettings{ 431 EnableExport: NewBool(false), 432 } 433 mes.SetDefaults() 434 435 require.False(t, *mes.EnableExport) 436 require.Equal(t, "01:00", *mes.DailyRunTime) 437 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 438 require.Equal(t, 10000, *mes.BatchSize) 439 } 440 441 func TestMessageExportSetDefaultsExportDisabledExportFromTimestampZero(t *testing.T) { 442 mes := &MessageExportSettings{ 443 EnableExport: NewBool(false), 444 ExportFromTimestamp: NewInt64(0), 445 } 446 mes.SetDefaults() 447 448 require.False(t, *mes.EnableExport) 449 require.Equal(t, "01:00", *mes.DailyRunTime) 450 require.Equal(t, int64(0), *mes.ExportFromTimestamp) 451 require.Equal(t, 10000, *mes.BatchSize) 452 } 453 454 func TestMessageExportSetDefaultsExportDisabledExportFromTimestampNonZero(t *testing.T) { 455 // Test retained as protection against regression of MM-13185 456 mes := &MessageExportSettings{ 457 EnableExport: NewBool(false), 458 ExportFromTimestamp: NewInt64(12345), 459 } 460 mes.SetDefaults() 461 462 require.False(t, *mes.EnableExport) 463 require.Equal(t, "01:00", *mes.DailyRunTime) 464 require.Equal(t, int64(12345), *mes.ExportFromTimestamp) 465 require.Equal(t, 10000, *mes.BatchSize) 466 } 467 468 func TestDisplaySettingsIsValidCustomUrlSchemes(t *testing.T) { 469 tests := []struct { 470 name string 471 value []string 472 valid bool 473 }{ 474 { 475 name: "empty", 476 value: []string{}, 477 valid: true, 478 }, 479 { 480 name: "custom protocol", 481 value: []string{"steam"}, 482 valid: true, 483 }, 484 { 485 name: "multiple custom protocols", 486 value: []string{"bitcoin", "rss", "redis"}, 487 valid: true, 488 }, 489 { 490 name: "containing numbers", 491 value: []string{"ut2004", "ts3server", "h323"}, 492 valid: true, 493 }, 494 { 495 name: "containing period", 496 value: []string{"iris.beep"}, 497 valid: false, // should technically be true, but client doesn't support it 498 }, 499 { 500 name: "containing hyphen", 501 value: []string{"ms-excel"}, 502 valid: true, 503 }, 504 { 505 name: "containing plus", 506 value: []string{"coap+tcp", "coap+ws"}, 507 valid: false, // should technically be true, but client doesn't support it 508 }, 509 { 510 name: "starting with number", 511 value: []string{"4four"}, 512 valid: false, 513 }, 514 { 515 name: "starting with period", 516 value: []string{"data", ".dot"}, 517 valid: false, 518 }, 519 { 520 name: "starting with hyphen", 521 value: []string{"-hyphen", "dns"}, 522 valid: false, 523 }, 524 { 525 name: "invalid symbols", 526 value: []string{"!!fun!!"}, 527 valid: false, 528 }, 529 { 530 name: "invalid letters", 531 value: []string{"école"}, 532 valid: false, 533 }, 534 } 535 for _, test := range tests { 536 t.Run(test.name, func(t *testing.T) { 537 ds := &DisplaySettings{} 538 ds.SetDefaults() 539 540 ds.CustomUrlSchemes = test.value 541 542 if err := ds.isValid(); err != nil && test.valid { 543 t.Error("Expected CustomUrlSchemes to be valid but got error:", err) 544 } else if err == nil && !test.valid { 545 t.Error("Expected CustomUrlSchemes to be invalid but got no error") 546 } 547 }) 548 } 549 } 550 551 func TestListenAddressIsValidated(t *testing.T) { 552 553 testValues := map[string]bool{ 554 ":8065": true, 555 ":9917": true, 556 "0.0.0.0:9917": true, 557 "[2001:db8::68]:9918": true, 558 "[::1]:8065": true, 559 "localhost:8065": true, 560 "test.com:8065": true, 561 ":0": true, 562 ":33147": true, 563 "123:8065": false, 564 "[::1]:99999": false, 565 "[::1]:-1": false, 566 "[::1]:8065a": false, 567 "0.0.0:9917": false, 568 "0.0.0.0:9917/": false, 569 "0..0.0:9917/": false, 570 "0.0.0222.0:9917/": false, 571 "http://0.0.0.0:9917/": false, 572 "http://0.0.0.0:9917": false, 573 "8065": false, 574 "[2001:db8::68]": false, 575 } 576 577 for key, expected := range testValues { 578 ss := &ServiceSettings{ 579 ListenAddress: NewString(key), 580 } 581 ss.SetDefaults() 582 if expected { 583 require.Nil(t, ss.isValid(), fmt.Sprintf("Got an error from '%v'.", key)) 584 } else { 585 err := ss.isValid() 586 require.NotNil(t, err, fmt.Sprintf("Expected '%v' to throw an error.", key)) 587 require.Equal(t, "model.config.is_valid.listen_address.app_error", err.Message) 588 } 589 } 590 591 } 592 593 func TestImageProxySettingsSetDefaults(t *testing.T) { 594 ss := ServiceSettings{ 595 DEPRECATED_DO_NOT_USE_ImageProxyType: NewString(IMAGE_PROXY_TYPE_ATMOS_CAMO), 596 DEPRECATED_DO_NOT_USE_ImageProxyURL: NewString("http://images.example.com"), 597 DEPRECATED_DO_NOT_USE_ImageProxyOptions: NewString("1234abcd"), 598 } 599 600 t.Run("default, no old settings", func(t *testing.T) { 601 ips := ImageProxySettings{} 602 ips.SetDefaults(ServiceSettings{}) 603 604 assert.Equal(t, false, *ips.Enable) 605 assert.Equal(t, IMAGE_PROXY_TYPE_LOCAL, *ips.ImageProxyType) 606 assert.Equal(t, "", *ips.RemoteImageProxyURL) 607 assert.Equal(t, "", *ips.RemoteImageProxyOptions) 608 }) 609 610 t.Run("default, old settings", func(t *testing.T) { 611 ips := ImageProxySettings{} 612 ips.SetDefaults(ss) 613 614 assert.Equal(t, true, *ips.Enable) 615 assert.Equal(t, *ss.DEPRECATED_DO_NOT_USE_ImageProxyType, *ips.ImageProxyType) 616 assert.Equal(t, *ss.DEPRECATED_DO_NOT_USE_ImageProxyURL, *ips.RemoteImageProxyURL) 617 assert.Equal(t, *ss.DEPRECATED_DO_NOT_USE_ImageProxyOptions, *ips.RemoteImageProxyOptions) 618 }) 619 620 t.Run("not default, old settings", func(t *testing.T) { 621 url := "http://images.mattermost.com" 622 options := "aaaaaaaa" 623 624 ips := ImageProxySettings{ 625 Enable: NewBool(false), 626 ImageProxyType: NewString(IMAGE_PROXY_TYPE_LOCAL), 627 RemoteImageProxyURL: &url, 628 RemoteImageProxyOptions: &options, 629 } 630 ips.SetDefaults(ss) 631 632 assert.Equal(t, false, *ips.Enable) 633 assert.Equal(t, IMAGE_PROXY_TYPE_LOCAL, *ips.ImageProxyType) 634 assert.Equal(t, url, *ips.RemoteImageProxyURL) 635 assert.Equal(t, options, *ips.RemoteImageProxyOptions) 636 }) 637 } 638 639 func TestImageProxySettingsIsValid(t *testing.T) { 640 for _, test := range []struct { 641 Name string 642 Enable bool 643 ImageProxyType string 644 RemoteImageProxyURL string 645 RemoteImageProxyOptions string 646 ExpectError bool 647 }{ 648 { 649 Name: "disabled", 650 Enable: false, 651 ExpectError: false, 652 }, 653 { 654 Name: "disabled with bad values", 655 Enable: false, 656 ImageProxyType: "garbage", 657 RemoteImageProxyURL: "garbage", 658 RemoteImageProxyOptions: "garbage", 659 ExpectError: false, 660 }, 661 { 662 Name: "missing type", 663 Enable: true, 664 ImageProxyType: "", 665 ExpectError: true, 666 }, 667 { 668 Name: "local", 669 Enable: true, 670 ImageProxyType: "local", 671 RemoteImageProxyURL: "garbage", 672 RemoteImageProxyOptions: "garbage", 673 ExpectError: false, 674 }, 675 { 676 Name: "atmos/camo", 677 Enable: true, 678 ImageProxyType: IMAGE_PROXY_TYPE_ATMOS_CAMO, 679 RemoteImageProxyURL: "someurl", 680 RemoteImageProxyOptions: "someoptions", 681 ExpectError: false, 682 }, 683 { 684 Name: "atmos/camo, missing url", 685 Enable: true, 686 ImageProxyType: IMAGE_PROXY_TYPE_ATMOS_CAMO, 687 RemoteImageProxyURL: "", 688 RemoteImageProxyOptions: "garbage", 689 ExpectError: true, 690 }, 691 { 692 Name: "atmos/camo, missing options", 693 Enable: true, 694 ImageProxyType: IMAGE_PROXY_TYPE_ATMOS_CAMO, 695 RemoteImageProxyURL: "someurl", 696 RemoteImageProxyOptions: "", 697 ExpectError: true, 698 }, 699 } { 700 t.Run(test.Name, func(t *testing.T) { 701 ips := &ImageProxySettings{ 702 Enable: &test.Enable, 703 ImageProxyType: &test.ImageProxyType, 704 RemoteImageProxyURL: &test.RemoteImageProxyURL, 705 RemoteImageProxyOptions: &test.RemoteImageProxyOptions, 706 } 707 708 err := ips.isValid() 709 if test.ExpectError { 710 assert.NotNil(t, err) 711 } else { 712 assert.Nil(t, err) 713 } 714 }) 715 } 716 } 717 718 func TestLdapSettingsIsValid(t *testing.T) { 719 for _, test := range []struct { 720 Name string 721 LdapSettings LdapSettings 722 ExpectError bool 723 }{ 724 { 725 Name: "disabled", 726 LdapSettings: LdapSettings{ 727 Enable: NewBool(false), 728 }, 729 ExpectError: false, 730 }, 731 { 732 Name: "missing server", 733 LdapSettings: LdapSettings{ 734 Enable: NewBool(true), 735 LdapServer: NewString(""), 736 BaseDN: NewString("basedn"), 737 EmailAttribute: NewString("email"), 738 UsernameAttribute: NewString("username"), 739 IdAttribute: NewString("id"), 740 LoginIdAttribute: NewString("loginid"), 741 UserFilter: NewString(""), 742 }, 743 ExpectError: true, 744 }, 745 { 746 Name: "empty user filter", 747 LdapSettings: LdapSettings{ 748 Enable: NewBool(true), 749 LdapServer: NewString("server"), 750 BaseDN: NewString("basedn"), 751 EmailAttribute: NewString("email"), 752 UsernameAttribute: NewString("username"), 753 IdAttribute: NewString("id"), 754 LoginIdAttribute: NewString("loginid"), 755 UserFilter: NewString(""), 756 }, 757 ExpectError: false, 758 }, 759 { 760 Name: "valid user filter #1", 761 LdapSettings: LdapSettings{ 762 Enable: NewBool(true), 763 LdapServer: NewString("server"), 764 BaseDN: NewString("basedn"), 765 EmailAttribute: NewString("email"), 766 UsernameAttribute: NewString("username"), 767 IdAttribute: NewString("id"), 768 LoginIdAttribute: NewString("loginid"), 769 UserFilter: NewString("(property=value)"), 770 }, 771 ExpectError: false, 772 }, 773 { 774 Name: "invalid user filter #1", 775 LdapSettings: LdapSettings{ 776 Enable: NewBool(true), 777 LdapServer: NewString("server"), 778 BaseDN: NewString("basedn"), 779 EmailAttribute: NewString("email"), 780 UsernameAttribute: NewString("username"), 781 IdAttribute: NewString("id"), 782 LoginIdAttribute: NewString("loginid"), 783 UserFilter: NewString("("), 784 }, 785 ExpectError: true, 786 }, 787 { 788 Name: "invalid user filter #2", 789 LdapSettings: LdapSettings{ 790 Enable: NewBool(true), 791 LdapServer: NewString("server"), 792 BaseDN: NewString("basedn"), 793 EmailAttribute: NewString("email"), 794 UsernameAttribute: NewString("username"), 795 IdAttribute: NewString("id"), 796 LoginIdAttribute: NewString("loginid"), 797 UserFilter: NewString("()"), 798 }, 799 ExpectError: true, 800 }, 801 { 802 Name: "valid user filter #2", 803 LdapSettings: LdapSettings{ 804 Enable: NewBool(true), 805 LdapServer: NewString("server"), 806 BaseDN: NewString("basedn"), 807 EmailAttribute: NewString("email"), 808 UsernameAttribute: NewString("username"), 809 IdAttribute: NewString("id"), 810 LoginIdAttribute: NewString("loginid"), 811 UserFilter: NewString("(&(property=value)(otherthing=othervalue))"), 812 }, 813 ExpectError: false, 814 }, 815 { 816 Name: "valid user filter #3", 817 LdapSettings: LdapSettings{ 818 Enable: NewBool(true), 819 LdapServer: NewString("server"), 820 BaseDN: NewString("basedn"), 821 EmailAttribute: NewString("email"), 822 UsernameAttribute: NewString("username"), 823 IdAttribute: NewString("id"), 824 LoginIdAttribute: NewString("loginid"), 825 UserFilter: NewString("(&(property=value)(|(otherthing=othervalue)(other=thing)))"), 826 }, 827 ExpectError: false, 828 }, 829 { 830 Name: "invalid user filter #3", 831 LdapSettings: LdapSettings{ 832 Enable: NewBool(true), 833 LdapServer: NewString("server"), 834 BaseDN: NewString("basedn"), 835 EmailAttribute: NewString("email"), 836 UsernameAttribute: NewString("username"), 837 IdAttribute: NewString("id"), 838 LoginIdAttribute: NewString("loginid"), 839 UserFilter: NewString("(&(property=value)(|(otherthing=othervalue)(other=thing))"), 840 }, 841 ExpectError: true, 842 }, 843 { 844 Name: "invalid user filter #4", 845 LdapSettings: LdapSettings{ 846 Enable: NewBool(true), 847 LdapServer: NewString("server"), 848 BaseDN: NewString("basedn"), 849 EmailAttribute: NewString("email"), 850 UsernameAttribute: NewString("username"), 851 IdAttribute: NewString("id"), 852 LoginIdAttribute: NewString("loginid"), 853 UserFilter: NewString("(&(property=value)((otherthing=othervalue)(other=thing)))"), 854 }, 855 ExpectError: true, 856 }, 857 } { 858 t.Run(test.Name, func(t *testing.T) { 859 test.LdapSettings.SetDefaults() 860 861 err := test.LdapSettings.isValid() 862 if test.ExpectError { 863 assert.NotNil(t, err) 864 } else { 865 assert.Nil(t, err) 866 } 867 }) 868 } 869 }