2015-01-26 01:40:57 +02:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-02-22 01:04:30 +02:00
|
|
|
"context"
|
2015-03-30 18:55:03 +02:00
|
|
|
"io/ioutil"
|
2017-02-21 01:56:26 +02:00
|
|
|
"math/rand"
|
2015-01-26 01:40:57 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMailer(t *testing.T) {
|
2015-03-31 21:34:03 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ab := New()
|
2015-01-26 01:40:57 +02:00
|
|
|
mailServer := &bytes.Buffer{}
|
|
|
|
|
2015-03-31 21:34:03 +02:00
|
|
|
ab.Mailer = LogMailer(mailServer)
|
2017-02-22 01:04:30 +02:00
|
|
|
ab.StoreLoader = mockStoreLoader{}
|
2015-03-31 21:34:03 +02:00
|
|
|
ab.LogWriter = ioutil.Discard
|
2015-01-26 01:40:57 +02:00
|
|
|
|
2017-02-22 01:04:30 +02:00
|
|
|
err := ab.SendMail(context.TODO(), Email{
|
2015-01-26 01:40:57 +02:00
|
|
|
To: []string{"some@email.com", "a@a.com"},
|
|
|
|
ToNames: []string{"Jake", "Noname"},
|
|
|
|
From: "some@guy.com",
|
|
|
|
FromName: "Joseph",
|
|
|
|
ReplyTo: "an@email.com",
|
|
|
|
Subject: "Email!",
|
|
|
|
TextBody: "No html here",
|
|
|
|
HTMLBody: "<html>body</html>",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mailServer.Len() == 0 {
|
|
|
|
t.Error("It should have logged the e-mail.")
|
|
|
|
}
|
|
|
|
|
|
|
|
str := mailServer.String()
|
|
|
|
if !strings.Contains(str, "From: Joseph <some@guy.com>") {
|
|
|
|
t.Error("From line not present.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(str, "To: Jake <some@email.com>, Noname <a@a.com>") {
|
|
|
|
t.Error("To line not present.")
|
|
|
|
}
|
2015-01-26 02:55:30 +02:00
|
|
|
|
|
|
|
if !strings.Contains(str, "No html here") {
|
|
|
|
t.Error("Text body not present.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(str, "<html>body</html>") {
|
|
|
|
t.Error("Html body not present.")
|
|
|
|
}
|
2015-01-26 01:40:57 +02:00
|
|
|
}
|
2015-02-24 23:03:06 +02:00
|
|
|
|
|
|
|
func TestSMTPMailer(t *testing.T) {
|
2015-03-31 21:34:03 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-02-24 23:03:06 +02:00
|
|
|
var _ Mailer = SMTPMailer("server", nil)
|
|
|
|
|
|
|
|
recovered := false
|
|
|
|
defer func() {
|
|
|
|
recovered = recover() != nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
SMTPMailer("", nil)
|
|
|
|
|
|
|
|
if !recovered {
|
|
|
|
t.Error("Should have panicd.")
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 01:56:26 +02:00
|
|
|
|
|
|
|
func TestBoundary(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-02-22 01:04:30 +02:00
|
|
|
mailer := smtpMailer{"server", nil, rand.New(rand.NewSource(3))}
|
2017-02-24 02:13:25 +02:00
|
|
|
if got := mailer.boundary(); got != "fe3fhpsm69lx8jvnrnju0wr" {
|
2017-02-21 01:56:26 +02:00
|
|
|
t.Error("boundary was wrong", got)
|
|
|
|
}
|
|
|
|
}
|