github.com/olivierlemoal/gophish@v0.9.0/worker/worker_test.go (about)

     1  package worker
     2  
     3  import (
     4  	"github.com/gophish/gophish/config"
     5  	"github.com/gophish/gophish/models"
     6  	"github.com/stretchr/testify/suite"
     7  )
     8  
     9  // WorkerSuite is a suite of tests to cover API related functions
    10  type WorkerSuite struct {
    11  	suite.Suite
    12  	config *config.Config
    13  }
    14  
    15  func (s *WorkerSuite) SetupSuite() {
    16  	conf := &config.Config{
    17  		DBName:         "sqlite3",
    18  		DBPath:         ":memory:",
    19  		MigrationsPath: "../db/db_sqlite3/migrations/",
    20  	}
    21  	err := models.Setup(conf)
    22  	if err != nil {
    23  		s.T().Fatalf("Failed creating database: %v", err)
    24  	}
    25  	s.config = conf
    26  	s.Nil(err)
    27  }
    28  
    29  func (s *WorkerSuite) TearDownTest() {
    30  	campaigns, _ := models.GetCampaigns(1)
    31  	for _, campaign := range campaigns {
    32  		models.DeleteCampaign(campaign.Id)
    33  	}
    34  }
    35  
    36  func (s *WorkerSuite) SetupTest() {
    37  	s.config.TestFlag = true
    38  	// Add a group
    39  	group := models.Group{Name: "Test Group"}
    40  	group.Targets = []models.Target{
    41  		models.Target{BaseRecipient: models.BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}},
    42  		models.Target{BaseRecipient: models.BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}},
    43  	}
    44  	group.UserId = 1
    45  	models.PostGroup(&group)
    46  
    47  	// Add a template
    48  	t := models.Template{Name: "Test Template"}
    49  	t.Subject = "Test subject"
    50  	t.Text = "Text text"
    51  	t.HTML = "<html>Test</html>"
    52  	t.UserId = 1
    53  	models.PostTemplate(&t)
    54  
    55  	// Add a landing page
    56  	p := models.Page{Name: "Test Page"}
    57  	p.HTML = "<html>Test</html>"
    58  	p.UserId = 1
    59  	models.PostPage(&p)
    60  
    61  	// Add a sending profile
    62  	smtp := models.SMTP{Name: "Test Page"}
    63  	smtp.UserId = 1
    64  	smtp.Host = "example.com"
    65  	smtp.FromAddress = "test@test.com"
    66  	models.PostSMTP(&smtp)
    67  
    68  	// Setup and "launch" our campaign
    69  	// Set the status such that no emails are attempted
    70  	c := models.Campaign{Name: "Test campaign"}
    71  	c.UserId = 1
    72  	c.Template = t
    73  	c.Page = p
    74  	c.SMTP = smtp
    75  	c.Groups = []models.Group{group}
    76  	models.PostCampaign(&c, c.UserId)
    77  	c.UpdateStatus(models.CampaignEmailsSent)
    78  }