diff --git a/internal/configuration/decode_hooks.go b/internal/configuration/decode_hooks.go index b2012d9ff..513f7fd37 100644 --- a/internal/configuration/decode_hooks.go +++ b/internal/configuration/decode_hooks.go @@ -10,8 +10,8 @@ import ( // StringToMailAddressFunc decodes a string into a mail.Address. func StringToMailAddressFunc() mapstructure.DecodeHookFunc { - return func(f reflect.Kind, t reflect.Kind, data interface{}) (value interface{}, err error) { - if f != reflect.String || t != reflect.TypeOf(mail.Address{}).Kind() { + return func(f reflect.Type, t reflect.Type, data interface{}) (value interface{}, err error) { + if f.Kind() != reflect.String || t != reflect.TypeOf(mail.Address{}) { return data, nil } diff --git a/internal/configuration/provider_test.go b/internal/configuration/provider_test.go index e708a859f..961b15d41 100644 --- a/internal/configuration/provider_test.go +++ b/internal/configuration/provider_test.go @@ -254,6 +254,47 @@ func TestShouldHandleErrInvalidatorWhenSMTPSenderBlank(t *testing.T) { assert.Equal(t, "", config.Notifier.SMTP.Sender.Name) assert.Equal(t, "", config.Notifier.SMTP.Sender.Address) + + validator.ValidateNotifier(config.Notifier, val) + + require.Len(t, val.Errors(), 1) + assert.Len(t, val.Warnings(), 0) + + assert.EqualError(t, val.Errors()[0], "smtp notifier: the 'sender' must be configured") +} + +func TestShouldDecodeSMTPSenderWithoutName(t *testing.T) { + testReset() + + val := schema.NewStructValidator() + keys, config, err := Load(val, NewDefaultSources([]string{"./test_resources/config.yml"}, DefaultEnvPrefix, DefaultEnvDelimiter)...) + + assert.NoError(t, err) + + validator.ValidateKeys(keys, DefaultEnvPrefix, val) + + assert.Len(t, val.Errors(), 0) + assert.Len(t, val.Warnings(), 0) + + assert.Equal(t, "", config.Notifier.SMTP.Sender.Name) + assert.Equal(t, "admin@example.com", config.Notifier.SMTP.Sender.Address) +} + +func TestShouldDecodeSMTPSenderWithName(t *testing.T) { + testReset() + + val := schema.NewStructValidator() + keys, config, err := Load(val, NewDefaultSources([]string{"./test_resources/config_alt.yml"}, DefaultEnvPrefix, DefaultEnvDelimiter)...) + + assert.NoError(t, err) + + validator.ValidateKeys(keys, DefaultEnvPrefix, val) + + assert.Len(t, val.Errors(), 0) + assert.Len(t, val.Warnings(), 0) + + assert.Equal(t, "Admin", config.Notifier.SMTP.Sender.Name) + assert.Equal(t, "admin@example.com", config.Notifier.SMTP.Sender.Address) } func TestShouldNotReadConfigurationOnFSAccessDenied(t *testing.T) { diff --git a/internal/configuration/test_resources/config_alt.yml b/internal/configuration/test_resources/config_alt.yml index c97198439..72776ab6d 100644 --- a/internal/configuration/test_resources/config_alt.yml +++ b/internal/configuration/test_resources/config_alt.yml @@ -119,6 +119,6 @@ notifier: username: test host: 127.0.0.1 port: 1025 - sender: admin@example.com + sender: Admin disable_require_tls: true ...