mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-24 08:42:17 +02:00
e9c5d3e13c
- Allow users to test the smtp mailer by sending themselves e-mails - Add a protection to ensure that either the HTML or the Text body exists so we never send blank e-mails. - Fix a bug where if the html body was blank, the mime section for it was still set and therefore gmail and (probably) other clients would show a blank e-mail instead of the content in the text e-mails.
93 lines
1.9 KiB
Go
93 lines
1.9 KiB
Go
package defaults
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/smtp"
|
|
"testing"
|
|
|
|
"github.com/volatiletech/authboss"
|
|
)
|
|
|
|
var (
|
|
flagTestSMTPMailer = flag.Bool("test-smtp-mailer", false, "Test the smtp mailer")
|
|
)
|
|
|
|
func TestSMTPMailer(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if !*flagTestSMTPMailer {
|
|
t.Skip("SMTP Mailer Testing not enabled (-test-smtp-mailer flag)")
|
|
}
|
|
|
|
creds := struct {
|
|
Server string `json:"server,omitempty"`
|
|
Port int `json:"port,omitempty"`
|
|
Email string `json:"email,omitempty"`
|
|
Password string `json:"password,omitempty"`
|
|
}{}
|
|
|
|
b, err := ioutil.ReadFile("smtp_mailer_test.json")
|
|
if err != nil {
|
|
t.Fatal(`error reading file: "smtp_mailer_test.json`, err)
|
|
}
|
|
|
|
if err = json.Unmarshal(b, &creds); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
server := fmt.Sprintf("%s:%d", creds.Server, creds.Port)
|
|
mailer := NewSMTPMailer(server, smtp.PlainAuth("", creds.Email, creds.Password, creds.Server))
|
|
|
|
mail := authboss.Email{
|
|
From: creds.Email,
|
|
To: []string{creds.Email},
|
|
Subject: "Authboss Test SMTP Mailer",
|
|
}
|
|
|
|
txtOnly := mail
|
|
txtOnly.Subject += ": Text Content"
|
|
txtOnly.TextBody = "Authboss\nSMTP\nTest\nWith\nNewlines"
|
|
|
|
if err = mailer.Send(context.Background(), txtOnly); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
htmlOnly := mail
|
|
htmlOnly.Subject += ": HTML Content"
|
|
htmlOnly.HTMLBody = "Authboss<br>Test<br>\nWith<br>Newlines\nand<br>breaks"
|
|
|
|
if err = mailer.Send(context.Background(), htmlOnly); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
mixed := mail
|
|
mixed.Subject += ": Mixed Content"
|
|
mixed.HTMLBody = htmlOnly.HTMLBody
|
|
mixed.TextBody = txtOnly.TextBody
|
|
|
|
if err = mailer.Send(context.Background(), mixed); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestSMTPMailerPanic(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_ = NewSMTPMailer("server", nil)
|
|
|
|
recovered := false
|
|
defer func() {
|
|
recovered = recover() != nil
|
|
}()
|
|
|
|
NewSMTPMailer("", nil)
|
|
|
|
if !recovered {
|
|
t.Error("Should have panicked.")
|
|
}
|
|
}
|