2017-12-09 21:42:07 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gophish/gomail"
|
2018-06-19 02:37:59 +00:00
|
|
|
"github.com/gophish/gophish/config"
|
2017-12-09 21:42:07 +00:00
|
|
|
"github.com/jordan-wright/email"
|
|
|
|
check "gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestEmailNotPresent(ch *check.C) {
|
2018-06-09 02:20:52 +00:00
|
|
|
req := &EmailRequest{}
|
2017-12-09 21:42:07 +00:00
|
|
|
ch.Assert(req.Validate(), check.Equals, ErrEmailNotSpecified)
|
|
|
|
req.Email = "test@example.com"
|
2018-06-09 02:20:52 +00:00
|
|
|
ch.Assert(req.Validate(), check.Equals, ErrFromAddressNotSpecified)
|
|
|
|
req.FromAddress = "from@example.com"
|
2017-12-09 21:42:07 +00:00
|
|
|
ch.Assert(req.Validate(), check.Equals, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestEmailRequestBackoff(ch *check.C) {
|
2018-06-09 02:20:52 +00:00
|
|
|
req := &EmailRequest{
|
2017-12-09 21:42:07 +00:00
|
|
|
ErrorChan: make(chan error),
|
|
|
|
}
|
|
|
|
expected := errors.New("Temporary Error")
|
|
|
|
go func() {
|
2018-12-15 21:42:32 +00:00
|
|
|
err := req.Backoff(expected)
|
2017-12-09 21:42:07 +00:00
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
}()
|
|
|
|
ch.Assert(<-req.ErrorChan, check.Equals, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestEmailRequestError(ch *check.C) {
|
2018-06-09 02:20:52 +00:00
|
|
|
req := &EmailRequest{
|
2017-12-09 21:42:07 +00:00
|
|
|
ErrorChan: make(chan error),
|
|
|
|
}
|
|
|
|
expected := errors.New("Temporary Error")
|
|
|
|
go func() {
|
2018-12-15 21:42:32 +00:00
|
|
|
err := req.Error(expected)
|
2017-12-09 21:42:07 +00:00
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
}()
|
|
|
|
ch.Assert(<-req.ErrorChan, check.Equals, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestEmailRequestSuccess(ch *check.C) {
|
2018-06-09 02:20:52 +00:00
|
|
|
req := &EmailRequest{
|
2017-12-09 21:42:07 +00:00
|
|
|
ErrorChan: make(chan error),
|
|
|
|
}
|
|
|
|
go func() {
|
2018-12-15 21:42:32 +00:00
|
|
|
err := req.Success()
|
2017-12-09 21:42:07 +00:00
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
}()
|
|
|
|
ch.Assert(<-req.ErrorChan, check.Equals, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestEmailRequestGenerate(ch *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
FromAddress: "from@example.com",
|
|
|
|
}
|
|
|
|
template := Template{
|
|
|
|
Name: "Test Template",
|
|
|
|
Subject: "{{.FirstName}} - Subject",
|
|
|
|
Text: "{{.Email}} - Text",
|
|
|
|
HTML: "{{.Email}} - HTML",
|
|
|
|
}
|
2018-06-09 02:20:52 +00:00
|
|
|
req := &EmailRequest{
|
2017-12-09 21:42:07 +00:00
|
|
|
SMTP: smtp,
|
|
|
|
Template: template,
|
2018-06-09 02:20:52 +00:00
|
|
|
BaseRecipient: BaseRecipient{
|
|
|
|
FirstName: "First",
|
|
|
|
LastName: "Last",
|
|
|
|
Email: "firstlast@example.com",
|
|
|
|
},
|
|
|
|
FromAddress: smtp.FromAddress,
|
2017-12-09 21:42:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 21:42:32 +00:00
|
|
|
s.config.ContactAddress = "test@test.com"
|
2018-06-19 02:37:59 +00:00
|
|
|
expectedHeaders := map[string]string{
|
|
|
|
"X-Mailer": config.ServerName,
|
2018-12-15 21:42:32 +00:00
|
|
|
"X-Gophish-Contact": s.config.ContactAddress,
|
2018-06-19 02:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 21:42:07 +00:00
|
|
|
msg := gomail.NewMessage()
|
2018-12-15 21:42:32 +00:00
|
|
|
err := req.Generate(msg)
|
2017-12-09 21:42:07 +00:00
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
expected := &email.Email{
|
|
|
|
Subject: fmt.Sprintf("%s - Subject", req.FirstName),
|
|
|
|
Text: []byte(fmt.Sprintf("%s - Text", req.Email)),
|
|
|
|
HTML: []byte(fmt.Sprintf("%s - HTML", req.Email)),
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBuff := &bytes.Buffer{}
|
|
|
|
_, err = msg.WriteTo(msgBuff)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
got, err := email.NewEmailFromReader(msgBuff)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
ch.Assert(got.Subject, check.Equals, expected.Subject)
|
|
|
|
ch.Assert(string(got.Text), check.Equals, string(expected.Text))
|
|
|
|
ch.Assert(string(got.HTML), check.Equals, string(expected.HTML))
|
2018-06-19 02:37:59 +00:00
|
|
|
for k, v := range expectedHeaders {
|
|
|
|
ch.Assert(got.Headers.Get(k), check.Equals, v)
|
|
|
|
}
|
2017-12-09 21:42:07 +00:00
|
|
|
}
|
2018-01-13 23:49:42 +00:00
|
|
|
|
2022-03-25 15:24:49 +00:00
|
|
|
func (s *ModelsSuite) TestGetSmtpFrom(ch *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
FromAddress: "from@example.com",
|
|
|
|
}
|
|
|
|
template := Template{
|
|
|
|
Name: "Test Template",
|
|
|
|
Subject: "{{.FirstName}} - Subject",
|
|
|
|
Text: "{{.Email}} - Text",
|
|
|
|
HTML: "{{.Email}} - HTML",
|
|
|
|
}
|
|
|
|
req := &EmailRequest{
|
|
|
|
SMTP: smtp,
|
|
|
|
Template: template,
|
|
|
|
URL: "http://127.0.0.1/{{.Email}}",
|
|
|
|
BaseRecipient: BaseRecipient{
|
|
|
|
FirstName: "First",
|
|
|
|
LastName: "Last",
|
|
|
|
Email: "firstlast@example.com",
|
|
|
|
},
|
|
|
|
FromAddress: smtp.FromAddress,
|
|
|
|
RId: fmt.Sprintf("%s-foobar", PreviewPrefix),
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := gomail.NewMessage()
|
|
|
|
err := req.Generate(msg)
|
|
|
|
smtp_from, err := req.GetSmtpFrom()
|
|
|
|
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
ch.Assert(smtp_from, check.Equals, "from@example.com")
|
|
|
|
}
|
|
|
|
|
2018-01-13 23:49:42 +00:00
|
|
|
func (s *ModelsSuite) TestEmailRequestURLTemplating(ch *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
FromAddress: "from@example.com",
|
|
|
|
}
|
|
|
|
template := Template{
|
|
|
|
Name: "Test Template",
|
|
|
|
Subject: "{{.URL}}",
|
|
|
|
Text: "{{.URL}}",
|
|
|
|
HTML: "{{.URL}}",
|
|
|
|
}
|
2018-06-09 02:20:52 +00:00
|
|
|
req := &EmailRequest{
|
2018-01-13 23:49:42 +00:00
|
|
|
SMTP: smtp,
|
|
|
|
Template: template,
|
2018-06-09 02:20:52 +00:00
|
|
|
URL: "http://127.0.0.1/{{.Email}}",
|
|
|
|
BaseRecipient: BaseRecipient{
|
|
|
|
FirstName: "First",
|
|
|
|
LastName: "Last",
|
|
|
|
Email: "firstlast@example.com",
|
|
|
|
},
|
|
|
|
FromAddress: smtp.FromAddress,
|
|
|
|
RId: fmt.Sprintf("%s-foobar", PreviewPrefix),
|
2018-01-13 23:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := gomail.NewMessage()
|
2018-12-15 21:42:32 +00:00
|
|
|
err := req.Generate(msg)
|
2018-01-13 23:49:42 +00:00
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
2018-06-09 02:20:52 +00:00
|
|
|
expectedURL := fmt.Sprintf("http://127.0.0.1/%s?%s=%s", req.Email, RecipientParameter, req.RId)
|
2018-01-13 23:49:42 +00:00
|
|
|
|
|
|
|
msgBuff := &bytes.Buffer{}
|
|
|
|
_, err = msg.WriteTo(msgBuff)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
got, err := email.NewEmailFromReader(msgBuff)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
ch.Assert(got.Subject, check.Equals, expectedURL)
|
|
|
|
ch.Assert(string(got.Text), check.Equals, expectedURL)
|
|
|
|
ch.Assert(string(got.HTML), check.Equals, expectedURL)
|
2018-02-10 19:46:08 +00:00
|
|
|
}
|
|
|
|
func (s *ModelsSuite) TestEmailRequestGenerateEmptySubject(ch *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
FromAddress: "from@example.com",
|
|
|
|
}
|
|
|
|
template := Template{
|
|
|
|
Name: "Test Template",
|
|
|
|
Subject: "",
|
|
|
|
Text: "{{.Email}} - Text",
|
|
|
|
HTML: "{{.Email}} - HTML",
|
|
|
|
}
|
2018-06-09 02:20:52 +00:00
|
|
|
req := &EmailRequest{
|
2018-02-10 19:46:08 +00:00
|
|
|
SMTP: smtp,
|
|
|
|
Template: template,
|
2018-06-09 02:20:52 +00:00
|
|
|
BaseRecipient: BaseRecipient{
|
|
|
|
FirstName: "First",
|
|
|
|
LastName: "Last",
|
|
|
|
Email: "firstlast@example.com",
|
|
|
|
},
|
|
|
|
FromAddress: smtp.FromAddress,
|
2018-02-10 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := gomail.NewMessage()
|
2018-12-15 21:42:32 +00:00
|
|
|
err := req.Generate(msg)
|
2018-02-10 19:46:08 +00:00
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
expected := &email.Email{
|
|
|
|
Subject: "",
|
|
|
|
Text: []byte(fmt.Sprintf("%s - Text", req.Email)),
|
|
|
|
HTML: []byte(fmt.Sprintf("%s - HTML", req.Email)),
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBuff := &bytes.Buffer{}
|
|
|
|
_, err = msg.WriteTo(msgBuff)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
got, err := email.NewEmailFromReader(msgBuff)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
ch.Assert(got.Subject, check.Equals, expected.Subject)
|
|
|
|
}
|
2018-06-09 02:20:52 +00:00
|
|
|
|
|
|
|
func (s *ModelsSuite) TestPostSendTestEmailRequest(ch *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
FromAddress: "from@example.com",
|
|
|
|
}
|
|
|
|
template := Template{
|
|
|
|
Name: "Test Template",
|
|
|
|
Subject: "",
|
|
|
|
Text: "{{.Email}} - Text",
|
|
|
|
HTML: "{{.Email}} - HTML",
|
|
|
|
UserId: 1,
|
|
|
|
}
|
|
|
|
err := PostTemplate(&template)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
page := Page{
|
|
|
|
Name: "Test Page",
|
|
|
|
HTML: "test",
|
|
|
|
UserId: 1,
|
|
|
|
}
|
|
|
|
err = PostPage(&page)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
req := &EmailRequest{
|
|
|
|
SMTP: smtp,
|
|
|
|
TemplateId: template.Id,
|
|
|
|
PageId: page.Id,
|
|
|
|
BaseRecipient: BaseRecipient{
|
|
|
|
FirstName: "First",
|
|
|
|
LastName: "Last",
|
|
|
|
Email: "firstlast@example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = PostEmailRequest(req)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
|
|
|
|
got, err := GetEmailRequestByResultId(req.RId)
|
|
|
|
ch.Assert(err, check.Equals, nil)
|
|
|
|
ch.Assert(got.RId, check.Equals, req.RId)
|
|
|
|
ch.Assert(got.Email, check.Equals, req.Email)
|
|
|
|
}
|