github.com/volatiletech/authboss@v2.4.1+incompatible/defaults/log_mailer_test.go (about)

     1  package defaults
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"math/rand"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/volatiletech/authboss"
    11  )
    12  
    13  func TestMailer(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	mailServer := &bytes.Buffer{}
    17  	mailer := NewLogMailer(mailServer)
    18  
    19  	err := mailer.Send(context.Background(), authboss.Email{
    20  		To:       []string{"some@email.com", "a@a.com"},
    21  		ToNames:  []string{"Jake", "Noname"},
    22  		From:     "some@guy.com",
    23  		FromName: "Joseph",
    24  		ReplyTo:  "an@email.com",
    25  		Subject:  "Email!",
    26  		TextBody: "No html here",
    27  		HTMLBody: "<html>body</html>",
    28  	})
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  
    33  	if mailServer.Len() == 0 {
    34  		t.Error("It should have logged the e-mail.")
    35  	}
    36  
    37  	str := mailServer.String()
    38  	if !strings.Contains(str, "From: Joseph <some@guy.com>") {
    39  		t.Error("From line not present.")
    40  	}
    41  
    42  	if !strings.Contains(str, "To: Jake <some@email.com>, Noname <a@a.com>") {
    43  		t.Error("To line not present.")
    44  	}
    45  
    46  	if !strings.Contains(str, "No html here") {
    47  		t.Error("Text body not present.")
    48  	}
    49  
    50  	if !strings.Contains(str, "<html>body</html>") {
    51  		t.Error("Html body not present.")
    52  	}
    53  
    54  	if t.Failed() {
    55  		t.Log(str)
    56  	}
    57  }
    58  
    59  func TestBoundary(t *testing.T) {
    60  	t.Parallel()
    61  
    62  	mailer := &SMTPMailer{"server", nil, rand.New(rand.NewSource(3))}
    63  	if got := mailer.boundary(); got != "fe3fhpsm69lx8jvnrnju0wr" {
    64  		t.Error("boundary was wrong", got)
    65  	}
    66  }