2014-03-27 02:42:07 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/config"
|
2015-02-21 07:27:32 +00:00
|
|
|
"gopkg.in/check.v1"
|
2014-03-27 02:42:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
2015-02-21 07:27:32 +00:00
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
2014-03-27 02:42:07 +00:00
|
|
|
|
|
|
|
type ModelsSuite struct{}
|
|
|
|
|
2015-02-21 07:27:32 +00:00
|
|
|
var _ = check.Suite(&ModelsSuite{})
|
2014-03-27 02:42:07 +00:00
|
|
|
|
2015-02-21 07:27:32 +00:00
|
|
|
func (s *ModelsSuite) SetUpSuite(c *check.C) {
|
2016-11-19 16:37:22 +00:00
|
|
|
config.Conf.DBName = "sqlite3"
|
2015-08-15 09:34:41 +00:00
|
|
|
config.Conf.DBPath = ":memory:"
|
2016-11-19 16:37:22 +00:00
|
|
|
config.Conf.MigrationsPath = "../db/db_sqlite3/migrations/"
|
2014-03-27 02:42:07 +00:00
|
|
|
err := Setup()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed creating database: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 11:28:11 +00:00
|
|
|
func (s *ModelsSuite) TearDownTest(c *check.C) {
|
|
|
|
// Clear database tables between each test. If new tables are
|
|
|
|
// used in this test suite they will need to be cleaned up here.
|
|
|
|
db.Delete(Group{})
|
|
|
|
db.Delete(Target{})
|
|
|
|
db.Delete(GroupTarget{})
|
|
|
|
db.Delete(SMTP{})
|
|
|
|
db.Delete(Page{})
|
2017-12-09 21:42:07 +00:00
|
|
|
db.Delete(Result{})
|
|
|
|
db.Delete(MailLog{})
|
|
|
|
db.Delete(Campaign{})
|
2016-07-15 11:28:11 +00:00
|
|
|
|
|
|
|
// Reset users table to default state.
|
|
|
|
db.Not("id", 1).Delete(User{})
|
|
|
|
db.Model(User{}).Update("username", "admin")
|
|
|
|
}
|
|
|
|
|
2018-02-10 19:46:08 +00:00
|
|
|
func (s *ModelsSuite) createCampaignDependencies(ch *check.C, optional ...string) Campaign {
|
|
|
|
// we use the optional parameter to pass an alternative subject
|
2017-12-09 21:42:07 +00:00
|
|
|
group := Group{Name: "Test Group"}
|
|
|
|
group.Targets = []Target{
|
2018-06-09 02:20:52 +00:00
|
|
|
Target{BaseRecipient: BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}},
|
|
|
|
Target{BaseRecipient: BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}},
|
2017-12-09 21:42:07 +00:00
|
|
|
}
|
|
|
|
group.UserId = 1
|
|
|
|
ch.Assert(PostGroup(&group), check.Equals, nil)
|
|
|
|
|
|
|
|
// Add a template
|
|
|
|
t := Template{Name: "Test Template"}
|
2018-02-10 19:46:08 +00:00
|
|
|
if len(optional) > 0 {
|
|
|
|
t.Subject = optional[0]
|
|
|
|
} else {
|
|
|
|
t.Subject = "{{.RId}} - Subject"
|
|
|
|
}
|
2017-12-09 21:42:07 +00:00
|
|
|
t.Text = "{{.RId}} - Text"
|
|
|
|
t.HTML = "{{.RId}} - HTML"
|
|
|
|
t.UserId = 1
|
|
|
|
ch.Assert(PostTemplate(&t), check.Equals, nil)
|
|
|
|
|
|
|
|
// Add a landing page
|
|
|
|
p := Page{Name: "Test Page"}
|
|
|
|
p.HTML = "<html>Test</html>"
|
|
|
|
p.UserId = 1
|
|
|
|
ch.Assert(PostPage(&p), check.Equals, nil)
|
|
|
|
|
|
|
|
// Add a sending profile
|
|
|
|
smtp := SMTP{Name: "Test Page"}
|
|
|
|
smtp.UserId = 1
|
|
|
|
smtp.Host = "example.com"
|
|
|
|
smtp.FromAddress = "test@test.com"
|
|
|
|
ch.Assert(PostSMTP(&smtp), check.Equals, nil)
|
|
|
|
|
|
|
|
c := Campaign{Name: "Test campaign"}
|
|
|
|
c.UserId = 1
|
|
|
|
c.Template = t
|
|
|
|
c.Page = p
|
|
|
|
c.SMTP = smtp
|
|
|
|
c.Groups = []Group{group}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) createCampaign(ch *check.C) Campaign {
|
|
|
|
c := s.createCampaignDependencies(ch)
|
|
|
|
// Setup and "launch" our campaign
|
|
|
|
ch.Assert(PostCampaign(&c, c.UserId), check.Equals, nil)
|
|
|
|
return c
|
|
|
|
}
|