commento/api/smtp_configure_test.go
Adhityaa 425312034f smtp_configure_test.go: clean env before test
Sometimes the tests end before they reach the end.
2018-06-13 00:32:32 +05:30

62 lines
1.5 KiB
Go

package main
import (
"os"
"testing"
)
func cleanSmtpVars() {
for _, env := range []string{"SMTP_USERNAME", "SMTP_PASSWORD", "SMTP_HOST", "SMTP_FROM_ADDRESS"} {
os.Setenv(env, "")
}
}
func TestSmtpConfigureBasics(t *testing.T) {
failTestOnError(t, setupTestEnv())
cleanSmtpVars()
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())
cleanSmtpVars()
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 {
t.Errorf("SMTP configured when it should not be due to empty COMMENTO_SMTP_HOST")
return
}
}
func TestSmtpConfigureEmptyAddress(t *testing.T) {
failTestOnError(t, setupTestEnv())
cleanSmtpVars()
os.Setenv("SMTP_USERNAME", "test@example.com")
os.Setenv("SMTP_PASSWORD", "hunter2")
os.Setenv("SMTP_HOST", "smtp.commento.io")
os.Setenv("SMTP_PORT", "25")
if err := smtpConfigure(); err == nil {
t.Errorf("expected error not found; SMTP should not be configured when COMMENTO_SMTP_FROM_ADDRESS is empty")
return
}
}