diff --git a/internal/pkg/core/notifier/providers/email/email_test.go b/internal/pkg/core/notifier/providers/email/email_test.go new file mode 100644 index 00000000..1197a1a6 --- /dev/null +++ b/internal/pkg/core/notifier/providers/email/email_test.go @@ -0,0 +1,51 @@ +package email_test + +import ( + "os" + "strconv" + "testing" + + notifierEmail "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/email" +) + +/* +Shell command to run this test: + + CERTIMATE_NOTIFIER_EMAIL_SMTPPORT=465 \ + CERTIMATE_NOTIFIER_EMAIL_SMTPTLS=true \ + CERTIMATE_NOTIFIER_EMAIL_SMTPHOST="smtp.example.com" \ + CERTIMATE_NOTIFIER_EMAIL_USERNAME="your-username" \ + CERTIMATE_NOTIFIER_EMAIL_PASSWORD="your-password" \ + CERTIMATE_NOTIFIER_EMAIL_SENDERADDRESS="sender@example.com" \ + CERTIMATE_NOTIFIER_EMAIL_RECEIVERADDRESS="receiver@example.com" \ + go test -v -run TestNotify email_test.go +*/ +func TestNotify(t *testing.T) { + smtpPort, err := strconv.ParseInt(os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SMTPPORT"), 10, 32) + if err != nil { + t.Errorf("invalid envvar: %+v", err) + panic(err) + } + + smtpTLS, err := strconv.ParseBool(os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SMTPTLS")) + if err != nil { + t.Errorf("invalid envvar: %+v", err) + panic(err) + } + + res, err := notifierEmail.New(¬ifierEmail.EmailNotifierConfig{ + SmtpHost: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SMTPHOST"), + SmtpPort: int32(smtpPort), + SmtpTLS: smtpTLS, + Username: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_USERNAME"), + Password: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_PASSWORD"), + SenderAddress: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SENDERADDRESS"), + ReceiverAddress: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_RECEIVERADDRESS"), + }) + if err != nil { + t.Errorf("invalid envvar: %+v", err) + panic(err) + } + + t.Logf("notify result: %v", res) +} diff --git a/internal/pkg/utils/maps/maps.go b/internal/pkg/utils/maps/maps.go index b475f09d..6f3c6fe6 100644 --- a/internal/pkg/utils/maps/maps.go +++ b/internal/pkg/utils/maps/maps.go @@ -154,10 +154,8 @@ func GetValueOrDefaultAsBool(dict map[string]any, key string, defaultValue bool) // 兼容字符串类型的值 if str, ok := value.(string); ok { - if str == "true" || str == "True" || str == "1" { - return true - } else if str == "false" || str == "False" || str == "0" { - return false + if result, err := strconv.ParseBool(str); err == nil { + return result } } } diff --git a/ui/src/i18n/locales/en/nls.settings.json b/ui/src/i18n/locales/en/nls.settings.json index b427a02a..580c4ef1 100644 --- a/ui/src/i18n/locales/en/nls.settings.json +++ b/ui/src/i18n/locales/en/nls.settings.json @@ -37,7 +37,7 @@ "settings.notification.email.smtp_host.placeholder": "Please enter SMTP host", "settings.notification.email.smtp_port.label": "SMTP Port", "settings.notification.email.smtp_port.placeholder": "Please enter SMTP port", - "settings.notification.email.smtp_tls.label": "TLS/SSL connection", + "settings.notification.email.smtp_tls.label": "Use TLS/SSL", "settings.notification.email.username.label": "Username", "settings.notification.email.username.placeholder": "please enter username", "settings.notification.email.password.label": "Password",