From e028806b5a7e9e29c4be5e7efb18b8d160ffef4b Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Fri, 15 Jul 2016 07:28:11 -0400 Subject: [PATCH 1/6] Add cleanup between tests for models --- models/models_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/models/models_test.go b/models/models_test.go index 212cc57b..25358e60 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -25,6 +25,20 @@ func (s *ModelsSuite) SetUpSuite(c *check.C) { } } +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{}) + + // Reset users table to default state. + db.Not("id", 1).Delete(User{}) + db.Model(User{}).Update("username", "admin") +} + func (s *ModelsSuite) TestGetUser(c *check.C) { u, err := GetUser(1) c.Assert(err, check.Equals, nil) From bc678878e271348422fd2b5f5cd074b9cde90b10 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Fri, 15 Jul 2016 07:31:44 -0400 Subject: [PATCH 2/6] Put User tests together --- models/models_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/models/models_test.go b/models/models_test.go index 25358e60..dd6b8988 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -45,6 +45,15 @@ func (s *ModelsSuite) TestGetUser(c *check.C) { c.Assert(u.Username, check.Equals, "admin") } +func (s *ModelsSuite) TestPutUser(c *check.C) { + u, err := GetUser(1) + u.Username = "admin_changed" + err = PutUser(&u) + c.Assert(err, check.Equals, nil) + u, err = GetUser(1) + c.Assert(u.Username, check.Equals, "admin_changed") +} + func (s *ModelsSuite) TestGeneratedAPIKey(c *check.C) { u, err := GetUser(1) c.Assert(err, check.Equals, nil) @@ -237,12 +246,3 @@ func (s *ModelsSuite) TestPostPage(c *check.C) { c.Assert(ok, check.Equals, false) }) } - -func (s *ModelsSuite) TestPutUser(c *check.C) { - u, err := GetUser(1) - u.Username = "admin_changed" - err = PutUser(&u) - c.Assert(err, check.Equals, nil) - u, err = GetUser(1) - c.Assert(u.Username, check.Equals, "admin_changed") -} From b5b0f0ddf8fbeff581481a71807f0a0df9d8d304 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Fri, 15 Jul 2016 07:37:16 -0400 Subject: [PATCH 3/6] Use generic names for Group tests --- models/models_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/models/models_test.go b/models/models_test.go index dd6b8988..17e5b3c4 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -88,7 +88,7 @@ func (s *ModelsSuite) TestPostGroupNoTargets(c *check.C) { func (s *ModelsSuite) TestPutGroup(c *check.C) { // Add test group. - group := Group{Name: "Put Test Group"} + group := Group{Name: "Test Group"} group.Targets = []Target{ Target{Email: "test1@example.com", FirstName: "First", LastName: "Example"}, Target{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}, @@ -97,14 +97,14 @@ func (s *ModelsSuite) TestPutGroup(c *check.C) { PostGroup(&group) // Update one of group's targets. - group.Targets[0].FirstName = "New" + group.Targets[0].FirstName = "Updated" err := PutGroup(&group) c.Assert(err, check.Equals, nil) // Verify updated target information. targets, _ := GetTargets(group.Id) c.Assert(targets[0].Email, check.Equals, "test1@example.com") - c.Assert(targets[0].FirstName, check.Equals, "New") + c.Assert(targets[0].FirstName, check.Equals, "Updated") c.Assert(targets[0].LastName, check.Equals, "Example") c.Assert(targets[1].Email, check.Equals, "test2@example.com") c.Assert(targets[1].FirstName, check.Equals, "Second") @@ -113,10 +113,10 @@ func (s *ModelsSuite) TestPutGroup(c *check.C) { func (s *ModelsSuite) TestPutGroupEmptyAttribute(c *check.C) { // Add test group. - group := Group{Name: "Put Empty Attribute Test Group"} + group := Group{Name: "Test Group"} group.Targets = []Target{ - Target{Email: "test3@example.com", FirstName: "Third", LastName: "Example"}, - Target{Email: "test4@example.com", FirstName: "Fourth", LastName: "Example"}, + Target{Email: "test1@example.com", FirstName: "First", LastName: "Example"}, + Target{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}, } group.UserId = 1 PostGroup(&group) @@ -128,11 +128,11 @@ func (s *ModelsSuite) TestPutGroupEmptyAttribute(c *check.C) { // Verify updated empty attribute was saved. targets, _ := GetTargets(group.Id) - c.Assert(targets[0].Email, check.Equals, "test3@example.com") + c.Assert(targets[0].Email, check.Equals, "test1@example.com") c.Assert(targets[0].FirstName, check.Equals, "") c.Assert(targets[0].LastName, check.Equals, "Example") - c.Assert(targets[1].Email, check.Equals, "test4@example.com") - c.Assert(targets[1].FirstName, check.Equals, "Fourth") + c.Assert(targets[1].Email, check.Equals, "test2@example.com") + c.Assert(targets[1].FirstName, check.Equals, "Second") c.Assert(targets[1].LastName, check.Equals, "Example") } From 3a9bc91415848e8e43f8b77c3a7ab490442c8ed1 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Sat, 16 Jul 2016 07:53:56 -0400 Subject: [PATCH 4/6] Add tests for getting group(s) --- models/models_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/models/models_test.go b/models/models_test.go index 17e5b3c4..74a1f86d 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -86,6 +86,63 @@ func (s *ModelsSuite) TestPostGroupNoTargets(c *check.C) { c.Assert(err, check.Equals, ErrNoTargetsSpecified) } +func (s *ModelsSuite) TestGetGroups(c *check.C) { + // Add groups. + PostGroup(&Group{ + Name: "Test Group 1", + Targets: []Target{Target{Email: "test1@example.com"}}, + UserId: 1, + }) + PostGroup(&Group{ + Name: "Test Group 2", + Targets: []Target{Target{Email: "test2@example.com"}}, + UserId: 1, + }) + + // Get groups and test result. + groups, err := GetGroups(1) + c.Assert(err, check.Equals, nil) + c.Assert(len(groups), check.Equals, 2) + c.Assert(len(groups[0].Targets), check.Equals, 1) + c.Assert(len(groups[1].Targets), check.Equals, 1) + c.Assert(groups[0].Name, check.Equals, "Test Group 1") + c.Assert(groups[1].Name, check.Equals, "Test Group 2") + c.Assert(groups[0].Targets[0].Email, check.Equals, "test1@example.com") + c.Assert(groups[1].Targets[0].Email, check.Equals, "test2@example.com") +} + +func (s *ModelsSuite) TestGetGroup(c *check.C) { + // Add group. + PostGroup(&Group{ + Name: "Test Group", + Targets: []Target{Target{Email: "test@example.com"}}, + UserId: 1, + }) + + // Get group and test result. + group, err := GetGroup(1, 1) + c.Assert(err, check.Equals, nil) + c.Assert(len(group.Targets), check.Equals, 1) + c.Assert(group.Name, check.Equals, "Test Group") + c.Assert(group.Targets[0].Email, check.Equals, "test@example.com") +} + +func (s *ModelsSuite) TestGetGroupByName(c *check.C) { + // Add group. + PostGroup(&Group{ + Name: "Test Group", + Targets: []Target{Target{Email: "test@example.com"}}, + UserId: 1, + }) + + // Get group and test result. + group, err := GetGroupByName("Test Group", 1) + c.Assert(err, check.Equals, nil) + c.Assert(len(group.Targets), check.Equals, 1) + c.Assert(group.Name, check.Equals, "Test Group") + c.Assert(group.Targets[0].Email, check.Equals, "test@example.com") +} + func (s *ModelsSuite) TestPutGroup(c *check.C) { // Add test group. group := Group{Name: "Test Group"} From f9a15cfe3d77e3dff37da6b94a5b2eb1c5929e3e Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Sat, 16 Jul 2016 08:38:27 -0400 Subject: [PATCH 5/6] Add more tests for getting group(s) --- models/models_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/models/models_test.go b/models/models_test.go index 74a1f86d..af01b4e4 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -111,6 +111,12 @@ func (s *ModelsSuite) TestGetGroups(c *check.C) { c.Assert(groups[1].Targets[0].Email, check.Equals, "test2@example.com") } +func (s *ModelsSuite) TestGetGroupsNoGroups(c *check.C) { + groups, err := GetGroups(1) + c.Assert(err, check.Equals, nil) + c.Assert(len(groups), check.Equals, 0) +} + func (s *ModelsSuite) TestGetGroup(c *check.C) { // Add group. PostGroup(&Group{ @@ -127,6 +133,12 @@ func (s *ModelsSuite) TestGetGroup(c *check.C) { c.Assert(group.Targets[0].Email, check.Equals, "test@example.com") } +func (s *ModelsSuite) TestGetGroupNoGroups(c *check.C) { + group, err := GetGroup(1, 1) + c.Assert(err, check.Not(check.Equals), nil) + c.Assert(group.Name, check.Not(check.Equals), "Test Group") +} + func (s *ModelsSuite) TestGetGroupByName(c *check.C) { // Add group. PostGroup(&Group{ @@ -143,6 +155,12 @@ func (s *ModelsSuite) TestGetGroupByName(c *check.C) { c.Assert(group.Targets[0].Email, check.Equals, "test@example.com") } +func (s *ModelsSuite) TestGetGroupByNameNoGroups(c *check.C) { + group, err := GetGroupByName("Test Group", 1) + c.Assert(err, check.Not(check.Equals), nil) + c.Assert(group.Name, check.Not(check.Equals), "Test Group") +} + func (s *ModelsSuite) TestPutGroup(c *check.C) { // Add test group. group := Group{Name: "Test Group"} From 2d4a755c2c4e23f21dc689bdd2c18b71fb9182a6 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Mon, 18 Jul 2016 18:47:28 -0400 Subject: [PATCH 6/6] Check for specific error in group tests --- models/models_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/models/models_test.go b/models/models_test.go index af01b4e4..3b5dd6c0 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -6,6 +6,7 @@ import ( "github.com/PuerkitoBio/goquery" "github.com/gophish/gophish/config" + "github.com/jinzhu/gorm" "gopkg.in/check.v1" ) @@ -134,9 +135,8 @@ func (s *ModelsSuite) TestGetGroup(c *check.C) { } func (s *ModelsSuite) TestGetGroupNoGroups(c *check.C) { - group, err := GetGroup(1, 1) - c.Assert(err, check.Not(check.Equals), nil) - c.Assert(group.Name, check.Not(check.Equals), "Test Group") + _, err := GetGroup(1, 1) + c.Assert(err, check.Equals, gorm.ErrRecordNotFound) } func (s *ModelsSuite) TestGetGroupByName(c *check.C) { @@ -156,9 +156,8 @@ func (s *ModelsSuite) TestGetGroupByName(c *check.C) { } func (s *ModelsSuite) TestGetGroupByNameNoGroups(c *check.C) { - group, err := GetGroupByName("Test Group", 1) - c.Assert(err, check.Not(check.Equals), nil) - c.Assert(group.Name, check.Not(check.Equals), "Test Group") + _, err := GetGroupByName("Test Group", 1) + c.Assert(err, check.Equals, gorm.ErrRecordNotFound) } func (s *ModelsSuite) TestPutGroup(c *check.C) {