Only send test email when changing smtp values (#4394)

* Only send test email when changing smtp values

* Update comment
This commit is contained in:
Tomas Touceda 2022-02-28 18:28:51 -03:00 committed by GitHub
parent 33c5f0651c
commit 6cffd5438f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View file

@ -0,0 +1 @@
* Only send test email when changing smtp values

View file

@ -152,6 +152,8 @@ func (svc *Service) ModifyAppConfig(ctx context.Context, p []byte) (*fleet.AppCo
return nil, err
}
oldSmtpSettings := appConfig.SMTPSettings
// TODO(mna): this ports the validations from the old validationMiddleware
// correctly, but this could be optimized so that we don't unmarshal the
// incoming bytes twice.
@ -172,9 +174,16 @@ func (svc *Service) ModifyAppConfig(ctx context.Context, p []byte) (*fleet.AppCo
return nil, &badRequestError{message: err.Error()}
}
if appConfig.SMTPSettings.SMTPEnabled || appConfig.SMTPSettings.SMTPConfigured {
if err = svc.sendTestEmail(ctx, appConfig); err != nil {
return nil, err
// ignore the values for SMTPEnabled and SMTPConfigured
oldSmtpSettings.SMTPEnabled = appConfig.SMTPSettings.SMTPEnabled
oldSmtpSettings.SMTPConfigured = appConfig.SMTPSettings.SMTPConfigured
// if we enable SMTP and the settings have changed, then we send a test email
if appConfig.SMTPSettings.SMTPEnabled {
if oldSmtpSettings != appConfig.SMTPSettings || !appConfig.SMTPSettings.SMTPConfigured {
if err = svc.sendTestEmail(ctx, appConfig); err != nil {
return nil, err
}
}
appConfig.SMTPSettings.SMTPConfigured = true
} else if appConfig.SMTPSettings.SMTPEnabled {