fleet/server/mail/mail_test.go
Zachary Wasserman adf87140a7
Add ability to prefix Fleet URLs (#2112)
- Add the server_url_prefix flag for configuring this functionality
- Add prefix handling to the server routes
- Refactor JS to use appropriate paths from modules
- Use JS template to get URL prefix into JS environment
- Update webpack config to support prefixing

Thanks to securityonion.net for sponsoring the development of this feature.

Closes #1661
2019-10-16 16:40:45 -07:00

146 lines
3.5 KiB
Go

package mail
import (
"os"
"reflect"
"runtime"
"strings"
"testing"
"github.com/kolide/fleet/server/kolide"
"github.com/stretchr/testify/assert"
)
type mockMailer struct{}
func (m *mockMailer) SendEmail(e kolide.Email) error {
return nil
}
func getMailer() kolide.MailService {
if os.Getenv("MAIL_TEST") == "" {
return &mockMailer{}
}
return NewService()
}
func functionName(f func(*testing.T, kolide.MailService)) string {
fullName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
elements := strings.Split(fullName, ".")
return elements[len(elements)-1]
}
var testFunctions = [...]func(*testing.T, kolide.MailService){
testSMTPPlainAuth,
testSMTPSkipVerify,
testSMTPNoAuth,
testMailTest,
}
func TestMail(t *testing.T) {
for _, f := range testFunctions {
r := getMailer()
t.Run(functionName(f), func(t *testing.T) {
f(t, r)
})
}
}
func testSMTPPlainAuth(t *testing.T, mailer kolide.MailService) {
mail := kolide.Email{
Subject: "smtp plain auth",
To: []string{"john@kolide.co"},
Config: &kolide.AppConfig{
SMTPConfigured: true,
SMTPAuthenticationType: kolide.AuthTypeUserNamePassword,
SMTPAuthenticationMethod: kolide.AuthMethodPlain,
SMTPUserName: "bob",
SMTPPassword: "secret",
SMTPEnableTLS: true,
SMTPVerifySSLCerts: true,
SMTPEnableStartTLS: true,
SMTPPort: 1025,
SMTPServer: "localhost",
SMTPSenderAddress: "kolide@kolide.com",
},
Mailer: &kolide.SMTPTestMailer{
BaseURL: "https://localhost:8080",
},
}
err := mailer.SendEmail(mail)
assert.Nil(t, err)
}
func testSMTPSkipVerify(t *testing.T, mailer kolide.MailService) {
mail := kolide.Email{
Subject: "skip verify",
To: []string{"john@kolide.co"},
Config: &kolide.AppConfig{
SMTPConfigured: true,
SMTPAuthenticationType: kolide.AuthTypeUserNamePassword,
SMTPAuthenticationMethod: kolide.AuthMethodPlain,
SMTPUserName: "bob",
SMTPPassword: "secret",
SMTPEnableTLS: true,
SMTPVerifySSLCerts: false,
SMTPEnableStartTLS: true,
SMTPPort: 1025,
SMTPServer: "localhost",
SMTPSenderAddress: "kolide@kolide.com",
},
Mailer: &kolide.SMTPTestMailer{
BaseURL: "https://localhost:8080",
},
}
err := mailer.SendEmail(mail)
assert.Nil(t, err)
}
func testSMTPNoAuth(t *testing.T, mailer kolide.MailService) {
mail := kolide.Email{
Subject: "no auth",
To: []string{"bob@foo.com"},
Config: &kolide.AppConfig{
SMTPConfigured: true,
SMTPAuthenticationType: kolide.AuthTypeNone,
SMTPEnableTLS: true,
SMTPVerifySSLCerts: true,
SMTPPort: 1025,
SMTPServer: "localhost",
SMTPSenderAddress: "kolide@kolide.com",
},
Mailer: &kolide.SMTPTestMailer{
BaseURL: "https://localhost:8080",
},
}
err := mailer.SendEmail(mail)
assert.Nil(t, err)
}
func testMailTest(t *testing.T, mailer kolide.MailService) {
mail := kolide.Email{
Subject: "test tester",
To: []string{"bob@foo.com"},
Config: &kolide.AppConfig{
SMTPConfigured: true,
SMTPAuthenticationType: kolide.AuthTypeNone,
SMTPEnableTLS: true,
SMTPVerifySSLCerts: true,
SMTPPort: 1025,
SMTPServer: "localhost",
SMTPSenderAddress: "kolide@kolide.com",
},
Mailer: &kolide.SMTPTestMailer{
BaseURL: "https://localhost:8080",
},
}
err := Test(mailer, mail)
assert.Nil(t, err)
}