github.com/amar224/phishing-tool@v0.9.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/gophish/gophish/config"
    10  	"github.com/jordan-wright/email"
    11  	check "gopkg.in/check.v1"
    12  )
    13  
    14  func (s *ModelsSuite) TestEmailNotPresent(ch *check.C) {
    15  	req := &EmailRequest{}
    16  	ch.Assert(req.Validate(), check.Equals, ErrEmailNotSpecified)
    17  	req.Email = "test@example.com"
    18  	ch.Assert(req.Validate(), check.Equals, ErrFromAddressNotSpecified)
    19  	req.FromAddress = "from@example.com"
    20  	ch.Assert(req.Validate(), check.Equals, nil)
    21  }
    22  
    23  func (s *ModelsSuite) TestEmailRequestBackoff(ch *check.C) {
    24  	req := &EmailRequest{
    25  		ErrorChan: make(chan error),
    26  	}
    27  	expected := errors.New("Temporary Error")
    28  	go func() {
    29  		err := req.Backoff(expected)
    30  		ch.Assert(err, check.Equals, nil)
    31  	}()
    32  	ch.Assert(<-req.ErrorChan, check.Equals, expected)
    33  }
    34  
    35  func (s *ModelsSuite) TestEmailRequestError(ch *check.C) {
    36  	req := &EmailRequest{
    37  		ErrorChan: make(chan error),
    38  	}
    39  	expected := errors.New("Temporary Error")
    40  	go func() {
    41  		err := req.Error(expected)
    42  		ch.Assert(err, check.Equals, nil)
    43  	}()
    44  	ch.Assert(<-req.ErrorChan, check.Equals, expected)
    45  }
    46  
    47  func (s *ModelsSuite) TestEmailRequestSuccess(ch *check.C) {
    48  	req := &EmailRequest{
    49  		ErrorChan: make(chan error),
    50  	}
    51  	go func() {
    52  		err := req.Success()
    53  		ch.Assert(err, check.Equals, nil)
    54  	}()
    55  	ch.Assert(<-req.ErrorChan, check.Equals, nil)
    56  }
    57  
    58  func (s *ModelsSuite) TestEmailRequestGenerate(ch *check.C) {
    59  	smtp := SMTP{
    60  		FromAddress: "from@example.com",
    61  	}
    62  	template := Template{
    63  		Name:    "Test Template",
    64  		Subject: "{{.FirstName}} - Subject",
    65  		Text:    "{{.Email}} - Text",
    66  		HTML:    "{{.Email}} - HTML",
    67  	}
    68  	req := &EmailRequest{
    69  		SMTP:     smtp,
    70  		Template: template,
    71  		BaseRecipient: BaseRecipient{
    72  			FirstName: "First",
    73  			LastName:  "Last",
    74  			Email:     "firstlast@example.com",
    75  		},
    76  		FromAddress: smtp.FromAddress,
    77  	}
    78  
    79  	s.config.ContactAddress = "test@test.com"
    80  	expectedHeaders := map[string]string{
    81  		"X-Mailer":          config.ServerName,
    82  		"X-Gophish-Contact": s.config.ContactAddress,
    83  	}
    84  
    85  	msg := gomail.NewMessage()
    86  	err := req.Generate(msg)
    87  	ch.Assert(err, check.Equals, nil)
    88  
    89  	expected := &email.Email{
    90  		Subject: fmt.Sprintf("%s - Subject", req.FirstName),
    91  		Text:    []byte(fmt.Sprintf("%s - Text", req.Email)),
    92  		HTML:    []byte(fmt.Sprintf("%s - HTML", req.Email)),
    93  	}
    94  
    95  	msgBuff := &bytes.Buffer{}
    96  	_, err = msg.WriteTo(msgBuff)
    97  	ch.Assert(err, check.Equals, nil)
    98  
    99  	got, err := email.NewEmailFromReader(msgBuff)
   100  	ch.Assert(err, check.Equals, nil)
   101  	ch.Assert(got.Subject, check.Equals, expected.Subject)
   102  	ch.Assert(string(got.Text), check.Equals, string(expected.Text))
   103  	ch.Assert(string(got.HTML), check.Equals, string(expected.HTML))
   104  	for k, v := range expectedHeaders {
   105  		ch.Assert(got.Headers.Get(k), check.Equals, v)
   106  	}
   107  }
   108  
   109  func (s *ModelsSuite) TestEmailRequestURLTemplating(ch *check.C) {
   110  	smtp := SMTP{
   111  		FromAddress: "from@example.com",
   112  	}
   113  	template := Template{
   114  		Name:    "Test Template",
   115  		Subject: "{{.URL}}",
   116  		Text:    "{{.URL}}",
   117  		HTML:    "{{.URL}}",
   118  	}
   119  	req := &EmailRequest{
   120  		SMTP:     smtp,
   121  		Template: template,
   122  		URL:      "http://127.0.0.1/{{.Email}}",
   123  		BaseRecipient: BaseRecipient{
   124  			FirstName: "First",
   125  			LastName:  "Last",
   126  			Email:     "firstlast@example.com",
   127  		},
   128  		FromAddress: smtp.FromAddress,
   129  		RId:         fmt.Sprintf("%s-foobar", PreviewPrefix),
   130  	}
   131  
   132  	msg := gomail.NewMessage()
   133  	err := req.Generate(msg)
   134  	ch.Assert(err, check.Equals, nil)
   135  
   136  	expectedURL := fmt.Sprintf("http://127.0.0.1/%s?%s=%s", req.Email, RecipientParameter, req.RId)
   137  
   138  	msgBuff := &bytes.Buffer{}
   139  	_, err = msg.WriteTo(msgBuff)
   140  	ch.Assert(err, check.Equals, nil)
   141  
   142  	got, err := email.NewEmailFromReader(msgBuff)
   143  	ch.Assert(err, check.Equals, nil)
   144  	ch.Assert(got.Subject, check.Equals, expectedURL)
   145  	ch.Assert(string(got.Text), check.Equals, expectedURL)
   146  	ch.Assert(string(got.HTML), check.Equals, expectedURL)
   147  }
   148  func (s *ModelsSuite) TestEmailRequestGenerateEmptySubject(ch *check.C) {
   149  	smtp := SMTP{
   150  		FromAddress: "from@example.com",
   151  	}
   152  	template := Template{
   153  		Name:    "Test Template",
   154  		Subject: "",
   155  		Text:    "{{.Email}} - Text",
   156  		HTML:    "{{.Email}} - HTML",
   157  	}
   158  	req := &EmailRequest{
   159  		SMTP:     smtp,
   160  		Template: template,
   161  		BaseRecipient: BaseRecipient{
   162  			FirstName: "First",
   163  			LastName:  "Last",
   164  			Email:     "firstlast@example.com",
   165  		},
   166  		FromAddress: smtp.FromAddress,
   167  	}
   168  
   169  	msg := gomail.NewMessage()
   170  	err := req.Generate(msg)
   171  	ch.Assert(err, check.Equals, nil)
   172  
   173  	expected := &email.Email{
   174  		Subject: "",
   175  		Text:    []byte(fmt.Sprintf("%s - Text", req.Email)),
   176  		HTML:    []byte(fmt.Sprintf("%s - HTML", req.Email)),
   177  	}
   178  
   179  	msgBuff := &bytes.Buffer{}
   180  	_, err = msg.WriteTo(msgBuff)
   181  	ch.Assert(err, check.Equals, nil)
   182  
   183  	got, err := email.NewEmailFromReader(msgBuff)
   184  	ch.Assert(err, check.Equals, nil)
   185  	ch.Assert(got.Subject, check.Equals, expected.Subject)
   186  }
   187  
   188  func (s *ModelsSuite) TestPostSendTestEmailRequest(ch *check.C) {
   189  	smtp := SMTP{
   190  		FromAddress: "from@example.com",
   191  	}
   192  	template := Template{
   193  		Name:    "Test Template",
   194  		Subject: "",
   195  		Text:    "{{.Email}} - Text",
   196  		HTML:    "{{.Email}} - HTML",
   197  		UserId:  1,
   198  	}
   199  	err := PostTemplate(&template)
   200  	ch.Assert(err, check.Equals, nil)
   201  
   202  	page := Page{
   203  		Name:   "Test Page",
   204  		HTML:   "test",
   205  		UserId: 1,
   206  	}
   207  	err = PostPage(&page)
   208  	ch.Assert(err, check.Equals, nil)
   209  
   210  	req := &EmailRequest{
   211  		SMTP:       smtp,
   212  		TemplateId: template.Id,
   213  		PageId:     page.Id,
   214  		BaseRecipient: BaseRecipient{
   215  			FirstName: "First",
   216  			LastName:  "Last",
   217  			Email:     "firstlast@example.com",
   218  		},
   219  	}
   220  	err = PostEmailRequest(req)
   221  	ch.Assert(err, check.Equals, nil)
   222  
   223  	got, err := GetEmailRequestByResultId(req.RId)
   224  	ch.Assert(err, check.Equals, nil)
   225  	ch.Assert(got.RId, check.Equals, req.RId)
   226  	ch.Assert(got.Email, check.Equals, req.Email)
   227  }