From 5ed3b42629fa9ac4d1a9c237292afcf98e61b994 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 8 Mar 2018 06:45:36 -0500 Subject: MM-8840: Add GlobalRelay Configuration Settings (#8304) * Added new GlobalRelay config settings * Adding default values to global relay config * Migrate global relay config tests to table driven tests --- i18n/en.json | 24 +++++++++++ model/config.go | 47 +++++++++++++++++--- model/config_test.go | 120 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 177 insertions(+), 14 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 2efb13375..a6f0eb30b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -4946,6 +4946,30 @@ "id": "model.config.is_valid.message_export.batch_size.app_error", "translation": "Message export job BatchSize must be a positive integer" }, + { + "id": "model.config.is_valid.message_export.export_type.app_error", + "translation": "Message export job ExportFormat must be one of either 'actiance' or 'globalrelay'" + }, + { + "id": "model.config.is_valid.message_export.global_relay.config_missing.app_error", + "translation": "Message export job ExportFormat is set to 'globalrelay', but GlobalRelaySettings are missing" + }, + { + "id": "model.config.is_valid.message_export.global_relay.customer_type.app_error", + "translation": "Message export GlobalRelaySettings.CustomerType must be set to one of either 'A9' or 'A10'" + }, + { + "id": "model.config.is_valid.message_export.global_relay.email_address.app_error", + "translation": "Message export job GlobalRelaySettings.EmailAddress must be set to a valid email address" + }, + { + "id": "model.config.is_valid.message_export.global_relay.smtp_username.app_error", + "translation": "Message export job GlobalRelaySettings.SmtpUsername must be set" + }, + { + "id": "model.config.is_valid.message_export.global_relay.smtp_password.app_error", + "translation": "Message export job GlobalRelaySettings.SmtpPassword must be set" + }, { "id": "model.config.is_valid.message_export.daily_runtime.app_error", "translation": "Message export job DailyRuntime must be a 24-hour time stamp in the form HH:MM." diff --git a/model/config.go b/model/config.go index 8e1dfce0a..98e331f10 100644 --- a/model/config.go +++ b/model/config.go @@ -154,6 +154,8 @@ const ( COMPLIANCE_EXPORT_TYPE_ACTIANCE = "actiance" COMPLIANCE_EXPORT_TYPE_GLOBALRELAY = "globalrelay" + GLOBALRELAY_CUSTOMER_TYPE_A9 = "A9" + GLOBALRELAY_CUSTOMER_TYPE_A10 = "A10" ) type ServiceSettings struct { @@ -1627,6 +1629,28 @@ func (s *PluginSettings) SetDefaults() { } } +type GlobalRelayMessageExportSettings struct { + CustomerType *string // must be either A9 or A10, dictates SMTP server url + SmtpUsername *string + SmtpPassword *string + EmailAddress *string // the address to send messages to +} + +func (s *GlobalRelayMessageExportSettings) SetDefaults() { + if s.CustomerType == nil { + s.CustomerType = NewString(GLOBALRELAY_CUSTOMER_TYPE_A9) + } + if s.SmtpUsername == nil { + s.SmtpUsername = NewString("") + } + if s.SmtpPassword == nil { + s.SmtpPassword = NewString("") + } + if s.EmailAddress == nil { + s.EmailAddress = NewString("") + } +} + type MessageExportSettings struct { EnableExport *bool ExportFormat *string @@ -1635,7 +1659,7 @@ type MessageExportSettings struct { BatchSize *int // formatter-specific settings - these are only expected to be non-nil if ExportFormat is set to the associated format - GlobalRelayEmailAddress *string + GlobalRelaySettings *GlobalRelayMessageExportSettings } func (s *MessageExportSettings) SetDefaults() { @@ -1666,6 +1690,11 @@ func (s *MessageExportSettings) SetDefaults() { if s.BatchSize == nil { s.BatchSize = NewInt(10000) } + + if s.GlobalRelaySettings == nil { + s.GlobalRelaySettings = &GlobalRelayMessageExportSettings{} + s.GlobalRelaySettings.SetDefaults() + } } type ConfigFunc func() *Config @@ -2199,10 +2228,18 @@ func (mes *MessageExportSettings) isValid(fs FileSettings) *AppError { } if *mes.ExportFormat == COMPLIANCE_EXPORT_TYPE_GLOBALRELAY { - // validating email addresses is hard - just make sure it contains an '@' sign - // see https://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address - if mes.GlobalRelayEmailAddress == nil || !strings.Contains(*mes.GlobalRelayEmailAddress, "@") { - return NewAppError("Config.IsValid", "model.config.is_valid.message_export.global_relay_email_address.app_error", nil, "", http.StatusBadRequest) + if mes.GlobalRelaySettings == nil { + return NewAppError("Config.IsValid", "model.config.is_valid.message_export.global_relay.config_missing.app_error", nil, "", http.StatusBadRequest) + } else if mes.GlobalRelaySettings.CustomerType == nil || (*mes.GlobalRelaySettings.CustomerType != GLOBALRELAY_CUSTOMER_TYPE_A9 && *mes.GlobalRelaySettings.CustomerType != GLOBALRELAY_CUSTOMER_TYPE_A10) { + return NewAppError("Config.IsValid", "model.config.is_valid.message_export.global_relay.customer_type.app_error", nil, "", http.StatusBadRequest) + } else if mes.GlobalRelaySettings.EmailAddress == nil || !strings.Contains(*mes.GlobalRelaySettings.EmailAddress, "@") { + // validating email addresses is hard - just make sure it contains an '@' sign + // see https://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address + return NewAppError("Config.IsValid", "model.config.is_valid.message_export.global_relay.email_address.app_error", nil, "", http.StatusBadRequest) + } else if mes.GlobalRelaySettings.SmtpUsername == nil || *mes.GlobalRelaySettings.SmtpUsername == "" { + return NewAppError("Config.IsValid", "model.config.is_valid.message_export.global_relay.smtp_username.app_error", nil, "", http.StatusBadRequest) + } else if mes.GlobalRelaySettings.SmtpPassword == nil || *mes.GlobalRelaySettings.SmtpPassword == "" { + return NewAppError("Config.IsValid", "model.config.is_valid.message_export.global_relay.smtp_password.app_error", nil, "", http.StatusBadRequest) } } } diff --git a/model/config_test.go b/model/config_test.go index 919f73fd7..1f917af27 100644 --- a/model/config_test.go +++ b/model/config_test.go @@ -183,21 +183,123 @@ func TestMessageExportSettingsIsValidActiance(t *testing.T) { require.Nil(t, mes.isValid(*fs)) } -func TestMessageExportSettingsIsValidGlobalRelay(t *testing.T) { +func TestMessageExportSettingsIsValidGlobalRelaySettingsMissing(t *testing.T) { fs := &FileSettings{ DriverName: NewString("foo"), // bypass file location check } mes := &MessageExportSettings{ - EnableExport: NewBool(true), - ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), - ExportFromTimestamp: NewInt64(0), - DailyRunTime: NewString("15:04"), - BatchSize: NewInt(100), - GlobalRelayEmailAddress: NewString("test@mattermost.com"), + EnableExport: NewBool(true), + ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), + ExportFromTimestamp: NewInt64(0), + DailyRunTime: NewString("15:04"), + BatchSize: NewInt(100), } - // should pass because everything is valid - require.Nil(t, mes.isValid(*fs)) + // should fail because globalrelay settings are missing + require.Error(t, mes.isValid(*fs)) +} + +func TestMessageExportSettingsIsValidGlobalRelaySettingsInvalidCustomerType(t *testing.T) { + fs := &FileSettings{ + DriverName: NewString("foo"), // bypass file location check + } + mes := &MessageExportSettings{ + EnableExport: NewBool(true), + ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), + ExportFromTimestamp: NewInt64(0), + DailyRunTime: NewString("15:04"), + BatchSize: NewInt(100), + GlobalRelaySettings: &GlobalRelayMessageExportSettings{ + CustomerType: NewString("Invalid"), + EmailAddress: NewString("valid@mattermost.com"), + SmtpUsername: NewString("SomeUsername"), + SmtpPassword: NewString("SomePassword"), + }, + } + + // should fail because customer type is invalid + require.Error(t, mes.isValid(*fs)) +} + +// func TestMessageExportSettingsIsValidGlobalRelaySettingsInvalidEmailAddress(t *testing.T) { +func TestMessageExportSettingsGlobalRelaySettings(t *testing.T) { + fs := &FileSettings{ + DriverName: NewString("foo"), // bypass file location check + } + tests := []struct { + name string + value *GlobalRelayMessageExportSettings + success bool + }{ + { + "Invalid email address", + &GlobalRelayMessageExportSettings{ + CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A9), + EmailAddress: NewString("invalidEmailAddress"), + SmtpUsername: NewString("SomeUsername"), + SmtpPassword: NewString("SomePassword"), + }, + false, + }, + { + "Missing smtp username", + &GlobalRelayMessageExportSettings{ + CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A10), + EmailAddress: NewString("valid@mattermost.com"), + SmtpPassword: NewString("SomePassword"), + }, + false, + }, + { + "Invalid smtp username", + &GlobalRelayMessageExportSettings{ + CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A10), + EmailAddress: NewString("valid@mattermost.com"), + SmtpUsername: NewString(""), + SmtpPassword: NewString("SomePassword"), + }, + false, + }, + { + "Invalid smtp password", + &GlobalRelayMessageExportSettings{ + CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A10), + EmailAddress: NewString("valid@mattermost.com"), + SmtpUsername: NewString("SomeUsername"), + SmtpPassword: NewString(""), + }, + false, + }, + { + "Valid data", + &GlobalRelayMessageExportSettings{ + CustomerType: NewString(GLOBALRELAY_CUSTOMER_TYPE_A9), + EmailAddress: NewString("valid@mattermost.com"), + SmtpUsername: NewString("SomeUsername"), + SmtpPassword: NewString("SomePassword"), + }, + true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mes := &MessageExportSettings{ + EnableExport: NewBool(true), + ExportFormat: NewString(COMPLIANCE_EXPORT_TYPE_GLOBALRELAY), + ExportFromTimestamp: NewInt64(0), + DailyRunTime: NewString("15:04"), + BatchSize: NewInt(100), + GlobalRelaySettings: tt.value, + } + + if tt.success { + require.Nil(t, mes.isValid(*fs)) + } else { + require.Error(t, mes.isValid(*fs)) + } + }) + } } func TestMessageExportSetDefaults(t *testing.T) { -- cgit v1.2.3-1-g7c22