gophish/models/campaign_test.go

136 lines
4.5 KiB
Go

package models
import (
"time"
check "gopkg.in/check.v1"
)
func (s *ModelsSuite) TestGenerateSendDate(c *check.C) {
campaign := s.createCampaignDependencies(c)
// Test that if no launch date is provided, the campaign's creation date
// is used.
err := PostCampaign(&campaign, campaign.UserId)
c.Assert(err, check.Equals, nil)
c.Assert(campaign.LaunchDate, check.Equals, campaign.CreatedDate)
ms, err := GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
for _, m := range ms {
c.Assert(m.SendDate, check.Equals, campaign.CreatedDate)
}
// Test that if no send date is provided, all the emails are sent at the
// campaign's launch date
campaign = s.createCampaignDependencies(c)
campaign.LaunchDate = time.Now().UTC()
err = PostCampaign(&campaign, campaign.UserId)
c.Assert(err, check.Equals, nil)
ms, err = GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
for _, m := range ms {
c.Assert(m.SendDate, check.Equals, campaign.LaunchDate)
}
// Finally, test that if a send date is provided, the emails are staggered
// correctly.
campaign = s.createCampaignDependencies(c)
campaign.LaunchDate = time.Now().UTC()
campaign.SendByDate = campaign.LaunchDate.Add(2 * time.Minute)
err = PostCampaign(&campaign, campaign.UserId)
c.Assert(err, check.Equals, nil)
ms, err = GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
sendingOffset := 2 / float64(len(ms))
for i, m := range ms {
expectedOffset := int(sendingOffset * float64(i))
expectedDate := campaign.LaunchDate.Add(time.Duration(expectedOffset) * time.Minute)
c.Assert(m.SendDate, check.Equals, expectedDate)
}
}
func (s *ModelsSuite) TestCampaignDateValidation(c *check.C) {
campaign := s.createCampaignDependencies(c)
// If both are zero, then the campaign should start immediately with no
// send by date
err := campaign.Validate()
c.Assert(err, check.Equals, nil)
// If the launch date is specified, then the send date is optional
campaign = s.createCampaignDependencies(c)
campaign.LaunchDate = time.Now().UTC()
err = campaign.Validate()
c.Assert(err, check.Equals, nil)
// If the send date is greater than the launch date, then there's no
//problem
campaign = s.createCampaignDependencies(c)
campaign.LaunchDate = time.Now().UTC()
campaign.SendByDate = campaign.LaunchDate.Add(1 * time.Minute)
err = campaign.Validate()
c.Assert(err, check.Equals, nil)
// If the send date is less than the launch date, then there's an issue
campaign = s.createCampaignDependencies(c)
campaign.LaunchDate = time.Now().UTC()
campaign.SendByDate = campaign.LaunchDate.Add(-1 * time.Minute)
err = campaign.Validate()
c.Assert(err, check.Equals, ErrInvalidSendByDate)
}
func (s *ModelsSuite) TestLaunchCampaignMaillogStatus(c *check.C) {
// For the first test, ensure that campaigns created with the zero date
// (and therefore are set to launch immediately) have maillogs that are
// locked to prevent race conditions.
campaign := s.createCampaign(c)
ms, err := GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
for _, m := range ms {
c.Assert(m.Processing, check.Equals, true)
}
// Next, verify that campaigns scheduled in the future do not lock the
// maillogs so that they can be picked up by the background worker.
campaign = s.createCampaignDependencies(c)
campaign.Name = "New Campaign"
campaign.LaunchDate = time.Now().Add(1 * time.Hour)
c.Assert(PostCampaign(&campaign, campaign.UserId), check.Equals, nil)
ms, err = GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
for _, m := range ms {
c.Assert(m.Processing, check.Equals, false)
}
}
func (s *ModelsSuite) TestDeleteCampaignAlsoDeletesMailLogs(c *check.C) {
campaign := s.createCampaign(c)
ms, err := GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
c.Assert(len(ms), check.Equals, len(campaign.Results))
err = DeleteCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
ms, err = GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
c.Assert(len(ms), check.Equals, 0)
}
func (s *ModelsSuite) TestCompleteCampaignAlsoDeletesMailLogs(c *check.C) {
campaign := s.createCampaign(c)
ms, err := GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
c.Assert(len(ms), check.Equals, len(campaign.Results))
err = CompleteCampaign(campaign.Id, campaign.UserId)
c.Assert(err, check.Equals, nil)
ms, err = GetMailLogsByCampaign(campaign.Id)
c.Assert(err, check.Equals, nil)
c.Assert(len(ms), check.Equals, 0)
}