mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-13 07:59:52 +00:00
test: add unit test case for email notifier
This commit is contained in:
parent
1bedb31a3c
commit
2994cb5c65
51
internal/pkg/core/notifier/providers/email/email_test.go
Normal file
51
internal/pkg/core/notifier/providers/email/email_test.go
Normal file
@ -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)
|
||||||
|
}
|
@ -154,10 +154,8 @@ func GetValueOrDefaultAsBool(dict map[string]any, key string, defaultValue bool)
|
|||||||
|
|
||||||
// 兼容字符串类型的值
|
// 兼容字符串类型的值
|
||||||
if str, ok := value.(string); ok {
|
if str, ok := value.(string); ok {
|
||||||
if str == "true" || str == "True" || str == "1" {
|
if result, err := strconv.ParseBool(str); err == nil {
|
||||||
return true
|
return result
|
||||||
} else if str == "false" || str == "False" || str == "0" {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
"settings.notification.email.smtp_host.placeholder": "Please enter SMTP host",
|
"settings.notification.email.smtp_host.placeholder": "Please enter SMTP host",
|
||||||
"settings.notification.email.smtp_port.label": "SMTP Port",
|
"settings.notification.email.smtp_port.label": "SMTP Port",
|
||||||
"settings.notification.email.smtp_port.placeholder": "Please enter 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.label": "Username",
|
||||||
"settings.notification.email.username.placeholder": "please enter username",
|
"settings.notification.email.username.placeholder": "please enter username",
|
||||||
"settings.notification.email.password.label": "Password",
|
"settings.notification.email.password.label": "Password",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user