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) {
|
2015-08-15 09:34:41 +00:00
|
|
|
config.Conf.DBPath = ":memory:"
|
2016-01-19 03:13:32 +00:00
|
|
|
config.Conf.MigrationsPath = "../db/migrations/"
|
2014-03-27 02:42:07 +00:00
|
|
|
err := Setup()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed creating database: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 07:27:32 +00:00
|
|
|
func (s *ModelsSuite) TestGetUser(c *check.C) {
|
2014-03-27 02:42:07 +00:00
|
|
|
u, err := GetUser(1)
|
2015-02-21 07:27:32 +00:00
|
|
|
c.Assert(err, check.Equals, nil)
|
|
|
|
c.Assert(u.Username, check.Equals, "admin")
|
2014-03-27 02:42:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 02:52:13 +00:00
|
|
|
func (s *ModelsSuite) TestGeneratedAPIKey(c *check.C) {
|
|
|
|
u, err := GetUser(1)
|
|
|
|
c.Assert(err, check.Equals, nil)
|
|
|
|
c.Assert(u.ApiKey, check.Not(check.Equals), "12345678901234567890123456789012")
|
|
|
|
}
|
|
|
|
|
2015-02-21 07:27:32 +00:00
|
|
|
func (s *ModelsSuite) TestPostGroup(c *check.C) {
|
|
|
|
g := Group{Name: "Test Group"}
|
|
|
|
g.Targets = []Target{Target{Email: "test@example.com"}}
|
|
|
|
g.UserId = 1
|
|
|
|
err := PostGroup(&g)
|
|
|
|
c.Assert(err, check.Equals, nil)
|
|
|
|
c.Assert(g.Name, check.Equals, "Test Group")
|
|
|
|
c.Assert(g.Targets[0].Email, check.Equals, "test@example.com")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestPostGroupNoName(c *check.C) {
|
|
|
|
g := Group{Name: ""}
|
|
|
|
g.Targets = []Target{Target{Email: "test@example.com"}}
|
|
|
|
g.UserId = 1
|
|
|
|
err := PostGroup(&g)
|
|
|
|
c.Assert(err, check.Equals, ErrGroupNameNotSpecified)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestPostGroupNoTargets(c *check.C) {
|
|
|
|
g := Group{Name: "No Target Group"}
|
|
|
|
g.Targets = []Target{}
|
|
|
|
g.UserId = 1
|
|
|
|
err := PostGroup(&g)
|
|
|
|
c.Assert(err, check.Equals, ErrNoTargetsSpecified)
|
|
|
|
}
|
|
|
|
|
2016-02-22 04:13:06 +00:00
|
|
|
func (s *ModelsSuite) TestPostSMTP(c *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
Name: "Test SMTP",
|
|
|
|
Host: "1.1.1.1:25",
|
|
|
|
FromAddress: "Foo Bar <foo@example.com>",
|
|
|
|
UserId: 1,
|
|
|
|
}
|
|
|
|
err = PostSMTP(&smtp)
|
|
|
|
c.Assert(err, check.Equals, nil)
|
|
|
|
ss, err := GetSMTPs(1)
|
|
|
|
c.Assert(err, check.Equals, nil)
|
|
|
|
c.Assert(len(ss), check.Equals, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestPostSMTPNoHost(c *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
Name: "Test SMTP",
|
|
|
|
FromAddress: "Foo Bar <foo@example.com>",
|
|
|
|
UserId: 1,
|
|
|
|
}
|
|
|
|
err = PostSMTP(&smtp)
|
|
|
|
c.Assert(err, check.Equals, ErrHostNotSpecified)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestPostSMTPNoFrom(c *check.C) {
|
|
|
|
smtp := SMTP{
|
|
|
|
Name: "Test SMTP",
|
|
|
|
UserId: 1,
|
|
|
|
Host: "1.1.1.1:25",
|
|
|
|
}
|
|
|
|
err = PostSMTP(&smtp)
|
|
|
|
c.Assert(err, check.Equals, ErrFromAddressNotSpecified)
|
|
|
|
}
|
|
|
|
|
2015-02-21 07:27:32 +00:00
|
|
|
func (s *ModelsSuite) TestPutUser(c *check.C) {
|
2014-03-27 18:19:57 +00:00
|
|
|
u, err := GetUser(1)
|
|
|
|
u.Username = "admin_changed"
|
|
|
|
err = PutUser(&u)
|
2015-02-21 07:27:32 +00:00
|
|
|
c.Assert(err, check.Equals, nil)
|
2014-03-27 18:19:57 +00:00
|
|
|
u, err = GetUser(1)
|
2015-02-21 07:27:32 +00:00
|
|
|
c.Assert(u.Username, check.Equals, "admin_changed")
|
2014-03-27 18:19:57 +00:00
|
|
|
}
|