github.com/amar224/phishing-tool@v0.9.0/models/result_test.go (about)

     1  package models
     2  
     3  import (
     4  	"net/mail"
     5  	"regexp"
     6  	"time"
     7  
     8  	"gopkg.in/check.v1"
     9  )
    10  
    11  func (s *ModelsSuite) TestGenerateResultId(c *check.C) {
    12  	r := Result{}
    13  	r.GenerateId(db)
    14  	match, err := regexp.Match("[a-zA-Z0-9]{7}", []byte(r.RId))
    15  	c.Assert(err, check.Equals, nil)
    16  	c.Assert(match, check.Equals, true)
    17  }
    18  
    19  func (s *ModelsSuite) TestFormatAddress(c *check.C) {
    20  	r := Result{
    21  		BaseRecipient: BaseRecipient{
    22  			FirstName: "John",
    23  			LastName:  "Doe",
    24  			Email:     "johndoe@example.com",
    25  		},
    26  	}
    27  	expected := &mail.Address{
    28  		Name:    "John Doe",
    29  		Address: "johndoe@example.com",
    30  	}
    31  	c.Assert(r.FormatAddress(), check.Equals, expected.String())
    32  
    33  	r = Result{
    34  		BaseRecipient: BaseRecipient{Email: "johndoe@example.com"},
    35  	}
    36  	c.Assert(r.FormatAddress(), check.Equals, r.Email)
    37  }
    38  
    39  func (s *ModelsSuite) TestResultSendingStatus(ch *check.C) {
    40  	c := s.createCampaignDependencies(ch)
    41  	ch.Assert(PostCampaign(&c, c.UserId), check.Equals, nil)
    42  	// This campaign wasn't scheduled, so we expect the status to
    43  	// be sending
    44  	for _, r := range c.Results {
    45  		ch.Assert(r.Status, check.Equals, StatusSending)
    46  		ch.Assert(r.ModifiedDate, check.Equals, c.CreatedDate)
    47  	}
    48  }
    49  func (s *ModelsSuite) TestResultScheduledStatus(ch *check.C) {
    50  	c := s.createCampaignDependencies(ch)
    51  	c.LaunchDate = time.Now().UTC().Add(time.Hour * time.Duration(1))
    52  	ch.Assert(PostCampaign(&c, c.UserId), check.Equals, nil)
    53  	// This campaign wasn't scheduled, so we expect the status to
    54  	// be sending
    55  	for _, r := range c.Results {
    56  		ch.Assert(r.Status, check.Equals, StatusScheduled)
    57  		ch.Assert(r.ModifiedDate, check.Equals, c.CreatedDate)
    58  	}
    59  }
    60  
    61  func (s *ModelsSuite) TestResultVariableStatus(ch *check.C) {
    62  	c := s.createCampaignDependencies(ch)
    63  	c.LaunchDate = time.Now().UTC()
    64  	c.SendByDate = c.LaunchDate.Add(2 * time.Minute)
    65  	ch.Assert(PostCampaign(&c, c.UserId), check.Equals, nil)
    66  
    67  	// The campaign has a window smaller than our group size, so we expect some
    68  	// emails to be sent immediately, while others will be scheduled
    69  	for _, r := range c.Results {
    70  		if r.SendDate.Before(c.CreatedDate) || r.SendDate.Equal(c.CreatedDate) {
    71  			ch.Assert(r.Status, check.Equals, StatusSending)
    72  		} else {
    73  			ch.Assert(r.Status, check.Equals, StatusScheduled)
    74  		}
    75  	}
    76  }
    77  
    78  func (s *ModelsSuite) TestDuplicateResults(ch *check.C) {
    79  	group := Group{Name: "Test Group"}
    80  	group.Targets = []Target{
    81  		Target{BaseRecipient: BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}},
    82  		Target{BaseRecipient: BaseRecipient{Email: "test1@example.com", FirstName: "Duplicate", LastName: "Duplicate"}},
    83  		Target{BaseRecipient: BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}},
    84  	}
    85  	group.UserId = 1
    86  	ch.Assert(PostGroup(&group), check.Equals, nil)
    87  
    88  	// Add a template
    89  	t := Template{Name: "Test Template"}
    90  	t.Subject = "{{.RId}} - Subject"
    91  	t.Text = "{{.RId}} - Text"
    92  	t.HTML = "{{.RId}} - HTML"
    93  	t.UserId = 1
    94  	ch.Assert(PostTemplate(&t), check.Equals, nil)
    95  
    96  	// Add a landing page
    97  	p := Page{Name: "Test Page"}
    98  	p.HTML = "<html>Test</html>"
    99  	p.UserId = 1
   100  	ch.Assert(PostPage(&p), check.Equals, nil)
   101  
   102  	// Add a sending profile
   103  	smtp := SMTP{Name: "Test Page"}
   104  	smtp.UserId = 1
   105  	smtp.Host = "example.com"
   106  	smtp.FromAddress = "test@test.com"
   107  	ch.Assert(PostSMTP(&smtp), check.Equals, nil)
   108  
   109  	c := Campaign{Name: "Test campaign"}
   110  	c.UserId = 1
   111  	c.Template = t
   112  	c.Page = p
   113  	c.SMTP = smtp
   114  	c.Groups = []Group{group}
   115  
   116  	ch.Assert(PostCampaign(&c, c.UserId), check.Equals, nil)
   117  	ch.Assert(len(c.Results), check.Equals, 2)
   118  	ch.Assert(c.Results[0].Email, check.Equals, group.Targets[0].Email)
   119  	ch.Assert(c.Results[1].Email, check.Equals, group.Targets[2].Email)
   120  }