summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorJonathan <jonfritz@gmail.com>2018-03-08 06:45:36 -0500
committerJoram Wilander <jwawilander@gmail.com>2018-03-08 06:45:36 -0500
commit5ed3b42629fa9ac4d1a9c237292afcf98e61b994 (patch)
tree8d774b8dc9cab94c02e676b09da9fd0bde4a4085 /model
parentb2dd00dd5b83fc7e8b311a55f5a2536e4f3d45a5 (diff)
downloadchat-5ed3b42629fa9ac4d1a9c237292afcf98e61b994.tar.gz
chat-5ed3b42629fa9ac4d1a9c237292afcf98e61b994.tar.bz2
chat-5ed3b42629fa9ac4d1a9c237292afcf98e61b994.zip
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
Diffstat (limited to 'model')
-rw-r--r--model/config.go47
-rw-r--r--model/config_test.go120
2 files changed, 153 insertions, 14 deletions
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) {