github.com/topsteplocal/gophish@v0.6.0/models/email_request_test.go (about)

     1  package models
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/gophish/gomail"
     9  	"github.com/jordan-wright/email"
    10  	check "gopkg.in/check.v1"
    11  )
    12  
    13  func (s *ModelsSuite) TestEmailNotPresent(ch *check.C) {
    14  	req := &SendTestEmailRequest{}
    15  	ch.Assert(req.Validate(), check.Equals, ErrEmailNotSpecified)
    16  	req.Email = "test@example.com"
    17  	ch.Assert(req.Validate(), check.Equals, nil)
    18  }
    19  
    20  func (s *ModelsSuite) TestEmailRequestBackoff(ch *check.C) {
    21  	req := &SendTestEmailRequest{
    22  		ErrorChan: make(chan error),
    23  	}
    24  	expected := errors.New("Temporary Error")
    25  	go func() {
    26  		err = req.Backoff(expected)
    27  		ch.Assert(err, check.Equals, nil)
    28  	}()
    29  	ch.Assert(<-req.ErrorChan, check.Equals, expected)
    30  }
    31  
    32  func (s *ModelsSuite) TestEmailRequestError(ch *check.C) {
    33  	req := &SendTestEmailRequest{
    34  		ErrorChan: make(chan error),
    35  	}
    36  	expected := errors.New("Temporary Error")
    37  	go func() {
    38  		err = req.Error(expected)
    39  		ch.Assert(err, check.Equals, nil)
    40  	}()
    41  	ch.Assert(<-req.ErrorChan, check.Equals, expected)
    42  }
    43  
    44  func (s *ModelsSuite) TestEmailRequestSuccess(ch *check.C) {
    45  	req := &SendTestEmailRequest{
    46  		ErrorChan: make(chan error),
    47  	}
    48  	go func() {
    49  		err = req.Success()
    50  		ch.Assert(err, check.Equals, nil)
    51  	}()
    52  	ch.Assert(<-req.ErrorChan, check.Equals, nil)
    53  }
    54  
    55  func (s *ModelsSuite) TestEmailRequestGenerate(ch *check.C) {
    56  	smtp := SMTP{
    57  		FromAddress: "from@example.com",
    58  	}
    59  	template := Template{
    60  		Name:    "Test Template",
    61  		Subject: "{{.FirstName}} - Subject",
    62  		Text:    "{{.Email}} - Text",
    63  		HTML:    "{{.Email}} - HTML",
    64  	}
    65  	target := Target{
    66  		FirstName: "First",
    67  		LastName:  "Last",
    68  		Email:     "firstlast@example.com",
    69  	}
    70  	req := &SendTestEmailRequest{
    71  		SMTP:     smtp,
    72  		Template: template,
    73  		Target:   target,
    74  	}
    75  
    76  	msg := gomail.NewMessage()
    77  	err = req.Generate(msg)
    78  	ch.Assert(err, check.Equals, nil)
    79  
    80  	expected := &email.Email{
    81  		Subject: fmt.Sprintf("%s - Subject", req.FirstName),
    82  		Text:    []byte(fmt.Sprintf("%s - Text", req.Email)),
    83  		HTML:    []byte(fmt.Sprintf("%s - HTML", req.Email)),
    84  	}
    85  
    86  	msgBuff := &bytes.Buffer{}
    87  	_, err = msg.WriteTo(msgBuff)
    88  	ch.Assert(err, check.Equals, nil)
    89  
    90  	got, err := email.NewEmailFromReader(msgBuff)
    91  	ch.Assert(err, check.Equals, nil)
    92  	ch.Assert(got.Subject, check.Equals, expected.Subject)
    93  	ch.Assert(string(got.Text), check.Equals, string(expected.Text))
    94  	ch.Assert(string(got.HTML), check.Equals, string(expected.HTML))
    95  }
    96  
    97  func (s *ModelsSuite) TestEmailRequestURLTemplating(ch *check.C) {
    98  	smtp := SMTP{
    99  		FromAddress: "from@example.com",
   100  	}
   101  	template := Template{
   102  		Name:    "Test Template",
   103  		Subject: "{{.URL}}",
   104  		Text:    "{{.URL}}",
   105  		HTML:    "{{.URL}}",
   106  	}
   107  	target := Target{
   108  		FirstName: "First",
   109  		LastName:  "Last",
   110  		Email:     "firstlast@example.com",
   111  	}
   112  	req := &SendTestEmailRequest{
   113  		SMTP:     smtp,
   114  		Template: template,
   115  		Target:   target,
   116  		URL: "http://127.0.0.1/{{.Email}}",
   117  	}
   118  
   119  	msg := gomail.NewMessage()
   120  	err = req.Generate(msg)
   121  	ch.Assert(err, check.Equals, nil)
   122  
   123  	expectedURL := fmt.Sprintf("http://127.0.0.1/%s", target.Email)
   124  
   125  	msgBuff := &bytes.Buffer{}
   126  	_, err = msg.WriteTo(msgBuff)
   127  	ch.Assert(err, check.Equals, nil)
   128  
   129  	got, err := email.NewEmailFromReader(msgBuff)
   130  	ch.Assert(err, check.Equals, nil)
   131  	ch.Assert(got.Subject, check.Equals, expectedURL)
   132  	ch.Assert(string(got.Text), check.Equals, expectedURL)
   133  	ch.Assert(string(got.HTML), check.Equals, expectedURL)
   134  }
   135  func (s *ModelsSuite) TestEmailRequestGenerateEmptySubject(ch *check.C) {
   136  	smtp := SMTP{
   137  		FromAddress: "from@example.com",
   138  	}
   139  	template := Template{
   140  		Name:    "Test Template",
   141  		Subject: "",
   142  		Text:    "{{.Email}} - Text",
   143  		HTML:    "{{.Email}} - HTML",
   144  	}
   145  	target := Target{
   146  		FirstName: "First",
   147  		LastName:  "Last",
   148  		Email:     "firstlast@example.com",
   149  	}
   150  	req := &SendTestEmailRequest{
   151  		SMTP:     smtp,
   152  		Template: template,
   153  		Target:   target,
   154  	}
   155  
   156  	msg := gomail.NewMessage()
   157  	err = req.Generate(msg)
   158  	ch.Assert(err, check.Equals, nil)
   159  
   160  	expected := &email.Email{
   161  		Subject: "",
   162  		Text:    []byte(fmt.Sprintf("%s - Text", req.Email)),
   163  		HTML:    []byte(fmt.Sprintf("%s - HTML", req.Email)),
   164  	}
   165  
   166  	msgBuff := &bytes.Buffer{}
   167  	_, err = msg.WriteTo(msgBuff)
   168  	ch.Assert(err, check.Equals, nil)
   169  
   170  	got, err := email.NewEmailFromReader(msgBuff)
   171  	ch.Assert(err, check.Equals, nil)
   172  	ch.Assert(got.Subject, check.Equals, expected.Subject)
   173  }