github.com/8samaka/gophish@v0.9.0/controllers/controllers_test.go (about) 1 package controllers 2 3 import ( 4 "net/http/httptest" 5 "os" 6 "testing" 7 8 "github.com/gophish/gophish/config" 9 "github.com/gophish/gophish/models" 10 "github.com/stretchr/testify/suite" 11 ) 12 13 // ControllersSuite is a suite of tests to cover API related functions 14 type ControllersSuite struct { 15 suite.Suite 16 apiKey string 17 config *config.Config 18 adminServer *httptest.Server 19 phishServer *httptest.Server 20 } 21 22 func (s *ControllersSuite) SetupSuite() { 23 conf := &config.Config{ 24 DBName: "sqlite3", 25 DBPath: ":memory:", 26 MigrationsPath: "../db/db_sqlite3/migrations/", 27 } 28 err := models.Setup(conf) 29 if err != nil { 30 s.T().Fatalf("Failed creating database: %v", err) 31 } 32 s.config = conf 33 s.Nil(err) 34 // Setup the admin server for use in testing 35 s.adminServer = httptest.NewUnstartedServer(NewAdminServer(s.config.AdminConf).server.Handler) 36 s.adminServer.Config.Addr = s.config.AdminConf.ListenURL 37 s.adminServer.Start() 38 // Get the API key to use for these tests 39 u, err := models.GetUser(1) 40 s.Nil(err) 41 s.apiKey = u.ApiKey 42 // Start the phishing server 43 s.phishServer = httptest.NewUnstartedServer(NewPhishingServer(s.config.PhishConf).server.Handler) 44 s.phishServer.Config.Addr = s.config.PhishConf.ListenURL 45 s.phishServer.Start() 46 // Move our cwd up to the project root for help with resolving 47 // static assets 48 err = os.Chdir("../") 49 s.Nil(err) 50 } 51 52 func (s *ControllersSuite) TearDownTest() { 53 campaigns, _ := models.GetCampaigns(1) 54 for _, campaign := range campaigns { 55 models.DeleteCampaign(campaign.Id) 56 } 57 } 58 59 func (s *ControllersSuite) SetupTest() { 60 // Add a group 61 group := models.Group{Name: "Test Group"} 62 group.Targets = []models.Target{ 63 models.Target{BaseRecipient: models.BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}}, 64 models.Target{BaseRecipient: models.BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}}, 65 } 66 group.UserId = 1 67 models.PostGroup(&group) 68 69 // Add a template 70 t := models.Template{Name: "Test Template"} 71 t.Subject = "Test subject" 72 t.Text = "Text text" 73 t.HTML = "<html>Test</html>" 74 t.UserId = 1 75 models.PostTemplate(&t) 76 77 // Add a landing page 78 p := models.Page{Name: "Test Page"} 79 p.HTML = "<html>Test</html>" 80 p.UserId = 1 81 models.PostPage(&p) 82 83 // Add a sending profile 84 smtp := models.SMTP{Name: "Test Page"} 85 smtp.UserId = 1 86 smtp.Host = "example.com" 87 smtp.FromAddress = "test@test.com" 88 models.PostSMTP(&smtp) 89 90 // Setup and "launch" our campaign 91 // Set the status such that no emails are attempted 92 c := models.Campaign{Name: "Test campaign"} 93 c.UserId = 1 94 c.Template = t 95 c.Page = p 96 c.SMTP = smtp 97 c.Groups = []models.Group{group} 98 models.PostCampaign(&c, c.UserId) 99 c.UpdateStatus(models.CampaignEmailsSent) 100 } 101 102 func (s *ControllersSuite) TearDownSuite() { 103 // Tear down the admin and phishing servers 104 s.adminServer.Close() 105 s.phishServer.Close() 106 } 107 108 func TestControllerSuite(t *testing.T) { 109 suite.Run(t, new(ControllersSuite)) 110 }