github.com/amar224/phishing-tool@v0.9.0/models/group_test.go (about) 1 package models 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/jinzhu/gorm" 8 "gopkg.in/check.v1" 9 ) 10 11 func (s *ModelsSuite) TestPostGroup(c *check.C) { 12 g := Group{Name: "Test Group"} 13 g.Targets = []Target{Target{BaseRecipient: BaseRecipient{Email: "test@example.com"}}} 14 g.UserId = 1 15 err := PostGroup(&g) 16 c.Assert(err, check.Equals, nil) 17 c.Assert(g.Name, check.Equals, "Test Group") 18 c.Assert(g.Targets[0].Email, check.Equals, "test@example.com") 19 } 20 21 func (s *ModelsSuite) TestPostGroupNoName(c *check.C) { 22 g := Group{Name: ""} 23 g.Targets = []Target{Target{BaseRecipient: BaseRecipient{Email: "test@example.com"}}} 24 g.UserId = 1 25 err := PostGroup(&g) 26 c.Assert(err, check.Equals, ErrGroupNameNotSpecified) 27 } 28 29 func (s *ModelsSuite) TestPostGroupNoTargets(c *check.C) { 30 g := Group{Name: "No Target Group"} 31 g.Targets = []Target{} 32 g.UserId = 1 33 err := PostGroup(&g) 34 c.Assert(err, check.Equals, ErrNoTargetsSpecified) 35 } 36 37 func (s *ModelsSuite) TestGetGroups(c *check.C) { 38 // Add groups. 39 PostGroup(&Group{ 40 Name: "Test Group 1", 41 Targets: []Target{ 42 Target{ 43 BaseRecipient: BaseRecipient{Email: "test1@example.com"}, 44 }, 45 }, 46 UserId: 1, 47 }) 48 PostGroup(&Group{ 49 Name: "Test Group 2", 50 Targets: []Target{ 51 Target{ 52 BaseRecipient: BaseRecipient{Email: "test2@example.com"}, 53 }, 54 }, 55 UserId: 1, 56 }) 57 58 // Get groups and test result. 59 groups, err := GetGroups(1) 60 c.Assert(err, check.Equals, nil) 61 c.Assert(len(groups), check.Equals, 2) 62 c.Assert(len(groups[0].Targets), check.Equals, 1) 63 c.Assert(len(groups[1].Targets), check.Equals, 1) 64 c.Assert(groups[0].Name, check.Equals, "Test Group 1") 65 c.Assert(groups[1].Name, check.Equals, "Test Group 2") 66 c.Assert(groups[0].Targets[0].Email, check.Equals, "test1@example.com") 67 c.Assert(groups[1].Targets[0].Email, check.Equals, "test2@example.com") 68 } 69 70 func (s *ModelsSuite) TestGetGroupsNoGroups(c *check.C) { 71 groups, err := GetGroups(1) 72 c.Assert(err, check.Equals, nil) 73 c.Assert(len(groups), check.Equals, 0) 74 } 75 76 func (s *ModelsSuite) TestGetGroup(c *check.C) { 77 // Add group. 78 originalGroup := &Group{ 79 Name: "Test Group", 80 Targets: []Target{ 81 Target{ 82 BaseRecipient: BaseRecipient{Email: "test@example.com"}, 83 }, 84 }, 85 UserId: 1, 86 } 87 c.Assert(PostGroup(originalGroup), check.Equals, nil) 88 89 // Get group and test result. 90 group, err := GetGroup(originalGroup.Id, 1) 91 c.Assert(err, check.Equals, nil) 92 c.Assert(len(group.Targets), check.Equals, 1) 93 c.Assert(group.Name, check.Equals, "Test Group") 94 c.Assert(group.Targets[0].Email, check.Equals, "test@example.com") 95 } 96 97 func (s *ModelsSuite) TestGetGroupNoGroups(c *check.C) { 98 _, err := GetGroup(1, 1) 99 c.Assert(err, check.Equals, gorm.ErrRecordNotFound) 100 } 101 102 func (s *ModelsSuite) TestGetGroupByName(c *check.C) { 103 // Add group. 104 PostGroup(&Group{ 105 Name: "Test Group", 106 Targets: []Target{ 107 Target{ 108 BaseRecipient: BaseRecipient{Email: "test@example.com"}, 109 }, 110 }, 111 UserId: 1, 112 }) 113 114 // Get group and test result. 115 group, err := GetGroupByName("Test Group", 1) 116 c.Assert(err, check.Equals, nil) 117 c.Assert(len(group.Targets), check.Equals, 1) 118 c.Assert(group.Name, check.Equals, "Test Group") 119 c.Assert(group.Targets[0].Email, check.Equals, "test@example.com") 120 } 121 122 func (s *ModelsSuite) TestGetGroupByNameNoGroups(c *check.C) { 123 _, err := GetGroupByName("Test Group", 1) 124 c.Assert(err, check.Equals, gorm.ErrRecordNotFound) 125 } 126 127 func (s *ModelsSuite) TestPutGroup(c *check.C) { 128 // Add test group. 129 group := Group{Name: "Test Group"} 130 group.Targets = []Target{ 131 Target{BaseRecipient: BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}}, 132 Target{BaseRecipient: BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}}, 133 } 134 group.UserId = 1 135 PostGroup(&group) 136 137 // Update one of group's targets. 138 group.Targets[0].FirstName = "Updated" 139 err := PutGroup(&group) 140 c.Assert(err, check.Equals, nil) 141 142 // Verify updated target information. 143 targets, _ := GetTargets(group.Id) 144 c.Assert(targets[0].Email, check.Equals, "test1@example.com") 145 c.Assert(targets[0].FirstName, check.Equals, "Updated") 146 c.Assert(targets[0].LastName, check.Equals, "Example") 147 c.Assert(targets[1].Email, check.Equals, "test2@example.com") 148 c.Assert(targets[1].FirstName, check.Equals, "Second") 149 c.Assert(targets[1].LastName, check.Equals, "Example") 150 } 151 152 func (s *ModelsSuite) TestPutGroupEmptyAttribute(c *check.C) { 153 // Add test group. 154 group := Group{Name: "Test Group"} 155 group.Targets = []Target{ 156 Target{BaseRecipient: BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}}, 157 Target{BaseRecipient: BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}}, 158 } 159 group.UserId = 1 160 PostGroup(&group) 161 162 // Update one of group's targets. 163 group.Targets[0].FirstName = "" 164 err := PutGroup(&group) 165 c.Assert(err, check.Equals, nil) 166 167 // Verify updated empty attribute was saved. 168 targets, _ := GetTargets(group.Id) 169 c.Assert(targets[0].Email, check.Equals, "test1@example.com") 170 c.Assert(targets[0].FirstName, check.Equals, "") 171 c.Assert(targets[0].LastName, check.Equals, "Example") 172 c.Assert(targets[1].Email, check.Equals, "test2@example.com") 173 c.Assert(targets[1].FirstName, check.Equals, "Second") 174 c.Assert(targets[1].LastName, check.Equals, "Example") 175 } 176 177 func benchmarkPostGroup(b *testing.B, iter, size int) { 178 b.StopTimer() 179 g := &Group{ 180 Name: fmt.Sprintf("Group-%d", iter), 181 } 182 for i := 0; i < size; i++ { 183 g.Targets = append(g.Targets, Target{ 184 BaseRecipient: BaseRecipient{ 185 FirstName: "User", 186 LastName: fmt.Sprintf("%d", i), 187 Email: fmt.Sprintf("test-%d@test.com", i), 188 }, 189 }) 190 } 191 b.StartTimer() 192 err := PostGroup(g) 193 if err != nil { 194 b.Fatalf("error posting group: %v", err) 195 } 196 } 197 198 // benchmarkPutGroup modifies half of the group to simulate a large change 199 func benchmarkPutGroup(b *testing.B, iter, size int) { 200 b.StopTimer() 201 // First, we need to create the group 202 g := &Group{ 203 Name: fmt.Sprintf("Group-%d", iter), 204 } 205 for i := 0; i < size; i++ { 206 g.Targets = append(g.Targets, Target{ 207 BaseRecipient: BaseRecipient{ 208 FirstName: "User", 209 LastName: fmt.Sprintf("%d", i), 210 Email: fmt.Sprintf("test-%d@test.com", i), 211 }, 212 }) 213 } 214 err := PostGroup(g) 215 if err != nil { 216 b.Fatalf("error posting group: %v", err) 217 } 218 // Now we need to change half of the group. 219 for i := 0; i < size/2; i++ { 220 g.Targets[i].Email = fmt.Sprintf("test-modified-%d@test.com", i) 221 } 222 b.StartTimer() 223 err = PutGroup(g) 224 if err != nil { 225 b.Fatalf("error modifying group: %v", err) 226 } 227 } 228 229 func BenchmarkPostGroup100(b *testing.B) { 230 setupBenchmark(b) 231 b.ResetTimer() 232 for i := 0; i < b.N; i++ { 233 benchmarkPostGroup(b, i, 100) 234 b.StopTimer() 235 resetBenchmark(b) 236 } 237 tearDownBenchmark(b) 238 } 239 240 func BenchmarkPostGroup1000(b *testing.B) { 241 setupBenchmark(b) 242 b.ResetTimer() 243 for i := 0; i < b.N; i++ { 244 benchmarkPostGroup(b, i, 1000) 245 b.StopTimer() 246 resetBenchmark(b) 247 } 248 tearDownBenchmark(b) 249 } 250 251 func BenchmarkPostGroup10000(b *testing.B) { 252 setupBenchmark(b) 253 b.ResetTimer() 254 for i := 0; i < b.N; i++ { 255 benchmarkPostGroup(b, i, 10000) 256 b.StopTimer() 257 resetBenchmark(b) 258 } 259 tearDownBenchmark(b) 260 } 261 262 func BenchmarkPutGroup100(b *testing.B) { 263 setupBenchmark(b) 264 b.ResetTimer() 265 for i := 0; i < b.N; i++ { 266 benchmarkPutGroup(b, i, 100) 267 b.StopTimer() 268 resetBenchmark(b) 269 } 270 tearDownBenchmark(b) 271 } 272 273 func BenchmarkPutGroup1000(b *testing.B) { 274 setupBenchmark(b) 275 b.ResetTimer() 276 for i := 0; i < b.N; i++ { 277 benchmarkPutGroup(b, i, 1000) 278 b.StopTimer() 279 resetBenchmark(b) 280 } 281 tearDownBenchmark(b) 282 } 283 284 func BenchmarkPutGroup10000(b *testing.B) { 285 setupBenchmark(b) 286 b.ResetTimer() 287 for i := 0; i < b.N; i++ { 288 benchmarkPutGroup(b, i, 10000) 289 b.StopTimer() 290 resetBenchmark(b) 291 } 292 tearDownBenchmark(b) 293 }