2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-07-24 14:56:19 +08:00
|
|
|
func smtpVarsClean() {
|
2018-06-13 03:03:42 +08:00
|
|
|
for _, env := range []string{"SMTP_USERNAME", "SMTP_PASSWORD", "SMTP_HOST", "SMTP_PORT", "SMTP_FROM_ADDRESS"} {
|
2018-05-27 22:40:42 +08:00
|
|
|
os.Setenv(env, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSmtpConfigureBasics(t *testing.T) {
|
|
|
|
failTestOnError(t, setupTestEnv())
|
2018-07-24 14:56:19 +08:00
|
|
|
smtpVarsClean()
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
os.Setenv("SMTP_USERNAME", "test@example.com")
|
|
|
|
os.Setenv("SMTP_PASSWORD", "hunter2")
|
|
|
|
os.Setenv("SMTP_HOST", "smtp.commento.io")
|
|
|
|
os.Setenv("SMTP_FROM_ADDRESS", "no-reply@commento.io")
|
|
|
|
|
|
|
|
if err := smtpConfigure(); err != nil {
|
|
|
|
t.Errorf("unexpected error when configuring SMTP: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSmtpConfigureEmptyHost(t *testing.T) {
|
|
|
|
failTestOnError(t, setupTestEnv())
|
2018-07-24 14:56:19 +08:00
|
|
|
smtpVarsClean()
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
os.Setenv("SMTP_USERNAME", "test@example.com")
|
|
|
|
os.Setenv("SMTP_PASSWORD", "hunter2")
|
|
|
|
os.Setenv("SMTP_FROM_ADDRESS", "no-reply@commento.io")
|
|
|
|
|
|
|
|
if err := smtpConfigure(); err != nil {
|
|
|
|
t.Errorf("unexpected error when configuring SMTP: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if smtpConfigured {
|
2018-06-13 02:52:06 +08:00
|
|
|
t.Errorf("SMTP configured when it should not be due to empty COMMENTO_SMTP_HOST")
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSmtpConfigureEmptyAddress(t *testing.T) {
|
|
|
|
failTestOnError(t, setupTestEnv())
|
2018-07-24 14:56:19 +08:00
|
|
|
smtpVarsClean()
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
os.Setenv("SMTP_USERNAME", "test@example.com")
|
|
|
|
os.Setenv("SMTP_PASSWORD", "hunter2")
|
|
|
|
os.Setenv("SMTP_HOST", "smtp.commento.io")
|
2018-06-09 17:52:49 +08:00
|
|
|
os.Setenv("SMTP_PORT", "25")
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
if err := smtpConfigure(); err == nil {
|
2018-06-13 02:52:06 +08:00
|
|
|
t.Errorf("expected error not found; SMTP should not be configured when COMMENTO_SMTP_FROM_ADDRESS is empty")
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|