2016-12-20 21:54:30 +00:00
|
|
|
package mail
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
2016-12-20 21:54:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-30 02:22:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-20 21:54:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type mockMailer struct{}
|
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (m *mockMailer) SendEmail(e fleet.Email) error {
|
2016-12-20 21:54:30 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func getMailer() fleet.MailService {
|
2016-12-20 21:54:30 +00:00
|
|
|
|
|
|
|
|
if os.Getenv("MAIL_TEST") == "" {
|
|
|
|
|
return &mockMailer{}
|
|
|
|
|
}
|
|
|
|
|
return NewService()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
var testFunctions = [...]func(*testing.T, fleet.MailService){
|
2016-12-20 21:54:30 +00:00
|
|
|
testSMTPPlainAuth,
|
|
|
|
|
testSMTPSkipVerify,
|
|
|
|
|
testSMTPNoAuth,
|
2016-12-22 14:12:34 +00:00
|
|
|
testMailTest,
|
2016-12-20 21:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMail(t *testing.T) {
|
|
|
|
|
for _, f := range testFunctions {
|
|
|
|
|
r := getMailer()
|
|
|
|
|
|
2020-04-21 00:29:02 +00:00
|
|
|
t.Run(test.FunctionName(f), func(t *testing.T) {
|
2016-12-20 21:54:30 +00:00
|
|
|
f(t, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testSMTPPlainAuth(t *testing.T, mailer fleet.MailService) {
|
|
|
|
|
mail := fleet.Email{
|
2016-12-20 21:54:30 +00:00
|
|
|
Subject: "smtp plain auth",
|
2021-06-06 22:07:29 +00:00
|
|
|
To: []string{"john@fleet.co"},
|
|
|
|
|
Config: &fleet.AppConfig{
|
2021-08-20 15:27:41 +00:00
|
|
|
SMTPSettings: fleet.SMTPSettings{
|
|
|
|
|
SMTPConfigured: true,
|
|
|
|
|
SMTPAuthenticationType: fleet.AuthTypeNameUserNamePassword,
|
|
|
|
|
SMTPAuthenticationMethod: fleet.AuthMethodNamePlain,
|
|
|
|
|
SMTPUserName: "bob",
|
|
|
|
|
SMTPPassword: "secret",
|
|
|
|
|
SMTPEnableTLS: true,
|
|
|
|
|
SMTPVerifySSLCerts: true,
|
|
|
|
|
SMTPEnableStartTLS: true,
|
|
|
|
|
SMTPPort: 1025,
|
|
|
|
|
SMTPServer: "localhost",
|
|
|
|
|
SMTPSenderAddress: "test@example.com",
|
|
|
|
|
},
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
2020-03-30 02:22:04 +00:00
|
|
|
Mailer: &SMTPTestMailer{
|
2019-10-16 23:40:45 +00:00
|
|
|
BaseURL: "https://localhost:8080",
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := mailer.SendEmail(mail)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testSMTPSkipVerify(t *testing.T, mailer fleet.MailService) {
|
|
|
|
|
mail := fleet.Email{
|
2016-12-20 21:54:30 +00:00
|
|
|
Subject: "skip verify",
|
2021-06-06 22:07:29 +00:00
|
|
|
To: []string{"john@fleet.co"},
|
|
|
|
|
Config: &fleet.AppConfig{
|
2021-08-20 15:27:41 +00:00
|
|
|
SMTPSettings: fleet.SMTPSettings{
|
|
|
|
|
SMTPConfigured: true,
|
|
|
|
|
SMTPAuthenticationType: fleet.AuthTypeNameUserNamePassword,
|
|
|
|
|
SMTPAuthenticationMethod: fleet.AuthMethodNamePlain,
|
|
|
|
|
SMTPUserName: "bob",
|
|
|
|
|
SMTPPassword: "secret",
|
|
|
|
|
SMTPEnableTLS: true,
|
|
|
|
|
SMTPVerifySSLCerts: false,
|
|
|
|
|
SMTPEnableStartTLS: true,
|
|
|
|
|
SMTPPort: 1025,
|
|
|
|
|
SMTPServer: "localhost",
|
|
|
|
|
SMTPSenderAddress: "test@example.com",
|
|
|
|
|
},
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
2020-03-30 02:22:04 +00:00
|
|
|
Mailer: &SMTPTestMailer{
|
2019-10-16 23:40:45 +00:00
|
|
|
BaseURL: "https://localhost:8080",
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := mailer.SendEmail(mail)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testSMTPNoAuth(t *testing.T, mailer fleet.MailService) {
|
|
|
|
|
mail := fleet.Email{
|
2016-12-20 21:54:30 +00:00
|
|
|
Subject: "no auth",
|
|
|
|
|
To: []string{"bob@foo.com"},
|
2021-06-06 22:07:29 +00:00
|
|
|
Config: &fleet.AppConfig{
|
2021-08-20 15:27:41 +00:00
|
|
|
SMTPSettings: fleet.SMTPSettings{
|
|
|
|
|
SMTPConfigured: true,
|
|
|
|
|
SMTPAuthenticationType: fleet.AuthTypeNameNone,
|
|
|
|
|
SMTPEnableTLS: true,
|
|
|
|
|
SMTPVerifySSLCerts: true,
|
|
|
|
|
SMTPPort: 1025,
|
|
|
|
|
SMTPServer: "localhost",
|
|
|
|
|
SMTPSenderAddress: "test@example.com",
|
|
|
|
|
},
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
2020-03-30 02:22:04 +00:00
|
|
|
Mailer: &SMTPTestMailer{
|
2019-10-16 23:40:45 +00:00
|
|
|
BaseURL: "https://localhost:8080",
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := mailer.SendEmail(mail)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
}
|
2016-12-22 14:12:34 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testMailTest(t *testing.T, mailer fleet.MailService) {
|
|
|
|
|
mail := fleet.Email{
|
2016-12-22 14:12:34 +00:00
|
|
|
Subject: "test tester",
|
|
|
|
|
To: []string{"bob@foo.com"},
|
2021-06-06 22:07:29 +00:00
|
|
|
Config: &fleet.AppConfig{
|
2021-08-20 15:27:41 +00:00
|
|
|
SMTPSettings: fleet.SMTPSettings{
|
|
|
|
|
SMTPConfigured: true,
|
|
|
|
|
SMTPAuthenticationType: fleet.AuthTypeNameNone,
|
|
|
|
|
SMTPEnableTLS: true,
|
|
|
|
|
SMTPVerifySSLCerts: true,
|
|
|
|
|
SMTPPort: 1025,
|
|
|
|
|
SMTPServer: "localhost",
|
|
|
|
|
SMTPSenderAddress: "test@example.com",
|
|
|
|
|
},
|
2016-12-22 14:12:34 +00:00
|
|
|
},
|
2020-03-30 02:22:04 +00:00
|
|
|
Mailer: &SMTPTestMailer{
|
2019-10-16 23:40:45 +00:00
|
|
|
BaseURL: "https://localhost:8080",
|
2016-12-22 14:12:34 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
err := Test(mailer, mail)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
}
|
2020-03-30 02:22:04 +00:00
|
|
|
|
|
|
|
|
func TestTemplateProcessor(t *testing.T) {
|
|
|
|
|
mailer := PasswordResetMailer{
|
|
|
|
|
BaseURL: "https://localhost.com:8080",
|
|
|
|
|
Token: "12345",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := mailer.Message()
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
|
}
|