github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/compliance_store.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package storetest 5 6 import ( 7 "encoding/json" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 14 "github.com/mattermost/mattermost-server/v5/model" 15 "github.com/mattermost/mattermost-server/v5/store" 16 ) 17 18 func cleanupStoreState(t *testing.T, ss store.Store) { 19 //remove existing users 20 allUsers, err := ss.User().GetAll() 21 require.NoError(t, err, "error cleaning all test users", err) 22 for _, u := range allUsers { 23 err = ss.User().PermanentDelete(u.Id) 24 require.NoError(t, err, "failed cleaning up test user %s", u.Username) 25 26 //remove all posts by this user 27 nErr := ss.Post().PermanentDeleteByUser(u.Id) 28 require.NoError(t, nErr, "failed cleaning all posts of test user %s", u.Username) 29 } 30 31 //remove existing channels 32 allChannels, nErr := ss.Channel().GetAllChannels(0, 100000, store.ChannelSearchOpts{IncludeDeleted: true}) 33 require.NoError(t, nErr, "error cleaning all test channels", nErr) 34 for _, channel := range *allChannels { 35 nErr = ss.Channel().PermanentDelete(channel.Id) 36 require.NoError(t, nErr, "failed cleaning up test channel %s", channel.Id) 37 } 38 39 //remove existing teams 40 allTeams, nErr := ss.Team().GetAll() 41 require.NoError(t, nErr, "error cleaning all test teams", nErr) 42 for _, team := range allTeams { 43 err := ss.Team().PermanentDelete(team.Id) 44 require.NoError(t, err, "failed cleaning up test team %s", team.Id) 45 } 46 } 47 48 func TestComplianceStore(t *testing.T, ss store.Store) { 49 t.Run("", func(t *testing.T) { testComplianceStore(t, ss) }) 50 t.Run("ComplianceExport", func(t *testing.T) { testComplianceExport(t, ss) }) 51 t.Run("ComplianceExportDirectMessages", func(t *testing.T) { testComplianceExportDirectMessages(t, ss) }) 52 t.Run("MessageExportPublicChannel", func(t *testing.T) { testMessageExportPublicChannel(t, ss) }) 53 t.Run("MessageExportPrivateChannel", func(t *testing.T) { testMessageExportPrivateChannel(t, ss) }) 54 t.Run("MessageExportDirectMessageChannel", func(t *testing.T) { testMessageExportDirectMessageChannel(t, ss) }) 55 t.Run("MessageExportGroupMessageChannel", func(t *testing.T) { testMessageExportGroupMessageChannel(t, ss) }) 56 t.Run("MessageEditExportMessage", func(t *testing.T) { testEditExportMessage(t, ss) }) 57 t.Run("MessageEditAfterExportMessage", func(t *testing.T) { testEditAfterExportMessage(t, ss) }) 58 t.Run("MessageDeleteExportMessage", func(t *testing.T) { testDeleteExportMessage(t, ss) }) 59 t.Run("MessageDeleteAfterExportMessage", func(t *testing.T) { testDeleteAfterExportMessage(t, ss) }) 60 } 61 62 func testComplianceStore(t *testing.T, ss store.Store) { 63 compliance1 := &model.Compliance{Desc: "Audit for federal subpoena case #22443", UserId: model.NewId(), Status: model.COMPLIANCE_STATUS_FAILED, StartAt: model.GetMillis() - 1, EndAt: model.GetMillis() + 1, Type: model.COMPLIANCE_TYPE_ADHOC} 64 _, err := ss.Compliance().Save(compliance1) 65 require.NoError(t, err) 66 time.Sleep(100 * time.Millisecond) 67 68 compliance2 := &model.Compliance{Desc: "Audit for federal subpoena case #11458", UserId: model.NewId(), Status: model.COMPLIANCE_STATUS_RUNNING, StartAt: model.GetMillis() - 1, EndAt: model.GetMillis() + 1, Type: model.COMPLIANCE_TYPE_ADHOC} 69 _, err = ss.Compliance().Save(compliance2) 70 require.NoError(t, err) 71 time.Sleep(100 * time.Millisecond) 72 73 compliances, _ := ss.Compliance().GetAll(0, 1000) 74 75 require.Equal(t, model.COMPLIANCE_STATUS_RUNNING, compliances[0].Status) 76 require.Equal(t, compliance2.Id, compliances[0].Id) 77 78 compliance2.Status = model.COMPLIANCE_STATUS_FAILED 79 _, err = ss.Compliance().Update(compliance2) 80 require.NoError(t, err) 81 82 compliances, _ = ss.Compliance().GetAll(0, 1000) 83 84 require.Equal(t, model.COMPLIANCE_STATUS_FAILED, compliances[0].Status) 85 require.Equal(t, compliance2.Id, compliances[0].Id) 86 87 compliances, _ = ss.Compliance().GetAll(0, 1) 88 89 require.Len(t, compliances, 1) 90 91 compliances, _ = ss.Compliance().GetAll(1, 1) 92 93 require.Len(t, compliances, 1) 94 95 rc2, _ := ss.Compliance().Get(compliance2.Id) 96 require.Equal(t, compliance2.Status, rc2.Status) 97 } 98 99 func testComplianceExport(t *testing.T, ss store.Store) { 100 time.Sleep(100 * time.Millisecond) 101 102 t1 := &model.Team{} 103 t1.DisplayName = "DisplayName" 104 t1.Name = "zz" + model.NewId() + "b" 105 t1.Email = MakeEmail() 106 t1.Type = model.TEAM_OPEN 107 t1, err := ss.Team().Save(t1) 108 require.NoError(t, err) 109 110 u1 := &model.User{} 111 u1.Email = MakeEmail() 112 u1.Username = model.NewId() 113 u1, err = ss.User().Save(u1) 114 require.NoError(t, err) 115 _, nErr := ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u1.Id}, -1) 116 require.NoError(t, nErr) 117 118 u2 := &model.User{} 119 u2.Email = MakeEmail() 120 u2.Username = model.NewId() 121 u2, err = ss.User().Save(u2) 122 require.NoError(t, err) 123 _, nErr = ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u2.Id}, -1) 124 require.NoError(t, nErr) 125 126 c1 := &model.Channel{} 127 c1.TeamId = t1.Id 128 c1.DisplayName = "Channel2" 129 c1.Name = "zz" + model.NewId() + "b" 130 c1.Type = model.CHANNEL_OPEN 131 c1, nErr = ss.Channel().Save(c1, -1) 132 require.NoError(t, nErr) 133 134 o1 := &model.Post{} 135 o1.ChannelId = c1.Id 136 o1.UserId = u1.Id 137 o1.CreateAt = model.GetMillis() 138 o1.Message = "zz" + model.NewId() + "b" 139 o1, nErr = ss.Post().Save(o1) 140 require.NoError(t, nErr) 141 142 o1a := &model.Post{} 143 o1a.ChannelId = c1.Id 144 o1a.UserId = u1.Id 145 o1a.CreateAt = o1.CreateAt + 10 146 o1a.Message = "zz" + model.NewId() + "b" 147 _, nErr = ss.Post().Save(o1a) 148 require.NoError(t, nErr) 149 150 o2 := &model.Post{} 151 o2.ChannelId = c1.Id 152 o2.UserId = u1.Id 153 o2.CreateAt = o1.CreateAt + 20 154 o2.Message = "zz" + model.NewId() + "b" 155 _, nErr = ss.Post().Save(o2) 156 require.NoError(t, nErr) 157 158 o2a := &model.Post{} 159 o2a.ChannelId = c1.Id 160 o2a.UserId = u2.Id 161 o2a.CreateAt = o1.CreateAt + 30 162 o2a.Message = "zz" + model.NewId() + "b" 163 o2a, nErr = ss.Post().Save(o2a) 164 require.NoError(t, nErr) 165 166 time.Sleep(100 * time.Millisecond) 167 168 cr1 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1} 169 cposts, nErr := ss.Compliance().ComplianceExport(cr1) 170 require.NoError(t, nErr) 171 assert.Len(t, cposts, 4) 172 assert.Equal(t, cposts[0].PostId, o1.Id) 173 assert.Equal(t, cposts[3].PostId, o2a.Id) 174 175 cr2 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email} 176 cposts, nErr = ss.Compliance().ComplianceExport(cr2) 177 require.NoError(t, nErr) 178 assert.Len(t, cposts, 1) 179 assert.Equal(t, cposts[0].PostId, o2a.Id) 180 181 cr3 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email + ", " + u1.Email} 182 cposts, nErr = ss.Compliance().ComplianceExport(cr3) 183 require.NoError(t, nErr) 184 assert.Len(t, cposts, 4) 185 assert.Equal(t, cposts[0].PostId, o1.Id) 186 assert.Equal(t, cposts[3].PostId, o2a.Id) 187 188 cr4 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Keywords: o2a.Message} 189 cposts, nErr = ss.Compliance().ComplianceExport(cr4) 190 require.NoError(t, nErr) 191 assert.Len(t, cposts, 1) 192 assert.Equal(t, cposts[0].PostId, o2a.Id) 193 194 cr5 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Keywords: o2a.Message + " " + o1.Message} 195 cposts, nErr = ss.Compliance().ComplianceExport(cr5) 196 require.NoError(t, nErr) 197 assert.Len(t, cposts, 2) 198 assert.Equal(t, cposts[0].PostId, o1.Id) 199 200 cr6 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email + ", " + u1.Email, Keywords: o2a.Message + " " + o1.Message} 201 cposts, nErr = ss.Compliance().ComplianceExport(cr6) 202 require.NoError(t, nErr) 203 assert.Len(t, cposts, 2) 204 assert.Equal(t, cposts[0].PostId, o1.Id) 205 assert.Equal(t, cposts[1].PostId, o2a.Id) 206 } 207 208 func testComplianceExportDirectMessages(t *testing.T, ss store.Store) { 209 time.Sleep(100 * time.Millisecond) 210 211 t1 := &model.Team{} 212 t1.DisplayName = "DisplayName" 213 t1.Name = "zz" + model.NewId() + "b" 214 t1.Email = MakeEmail() 215 t1.Type = model.TEAM_OPEN 216 t1, err := ss.Team().Save(t1) 217 require.NoError(t, err) 218 219 u1 := &model.User{} 220 u1.Email = MakeEmail() 221 u1.Username = model.NewId() 222 u1, err = ss.User().Save(u1) 223 require.NoError(t, err) 224 _, nErr := ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u1.Id}, -1) 225 require.NoError(t, nErr) 226 227 u2 := &model.User{} 228 u2.Email = MakeEmail() 229 u2.Username = model.NewId() 230 u2, err = ss.User().Save(u2) 231 require.NoError(t, err) 232 _, nErr = ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u2.Id}, -1) 233 require.NoError(t, nErr) 234 235 c1 := &model.Channel{} 236 c1.TeamId = t1.Id 237 c1.DisplayName = "Channel2" 238 c1.Name = "zz" + model.NewId() + "b" 239 c1.Type = model.CHANNEL_OPEN 240 c1, nErr = ss.Channel().Save(c1, -1) 241 require.NoError(t, nErr) 242 243 cDM, nErr := ss.Channel().CreateDirectChannel(u1, u2) 244 require.NoError(t, nErr) 245 o1 := &model.Post{} 246 o1.ChannelId = c1.Id 247 o1.UserId = u1.Id 248 o1.CreateAt = model.GetMillis() 249 o1.Message = "zz" + model.NewId() + "b" 250 o1, nErr = ss.Post().Save(o1) 251 require.NoError(t, nErr) 252 253 o1a := &model.Post{} 254 o1a.ChannelId = c1.Id 255 o1a.UserId = u1.Id 256 o1a.CreateAt = o1.CreateAt + 10 257 o1a.Message = "zz" + model.NewId() + "b" 258 _, nErr = ss.Post().Save(o1a) 259 require.NoError(t, nErr) 260 261 o2 := &model.Post{} 262 o2.ChannelId = c1.Id 263 o2.UserId = u1.Id 264 o2.CreateAt = o1.CreateAt + 20 265 o2.Message = "zz" + model.NewId() + "b" 266 _, nErr = ss.Post().Save(o2) 267 require.NoError(t, nErr) 268 269 o2a := &model.Post{} 270 o2a.ChannelId = c1.Id 271 o2a.UserId = u2.Id 272 o2a.CreateAt = o1.CreateAt + 30 273 o2a.Message = "zz" + model.NewId() + "b" 274 _, nErr = ss.Post().Save(o2a) 275 require.NoError(t, nErr) 276 277 o3 := &model.Post{} 278 o3.ChannelId = cDM.Id 279 o3.UserId = u1.Id 280 o3.CreateAt = o1.CreateAt + 40 281 o3.Message = "zz" + model.NewId() + "b" 282 o3, nErr = ss.Post().Save(o3) 283 require.NoError(t, nErr) 284 285 time.Sleep(100 * time.Millisecond) 286 287 cr1 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o3.CreateAt + 1, Emails: u1.Email} 288 cposts, nErr := ss.Compliance().ComplianceExport(cr1) 289 require.NoError(t, nErr) 290 assert.Len(t, cposts, 4) 291 assert.Equal(t, cposts[0].PostId, o1.Id) 292 assert.Equal(t, cposts[len(cposts)-1].PostId, o3.Id) 293 } 294 295 func testMessageExportPublicChannel(t *testing.T, ss store.Store) { 296 defer cleanupStoreState(t, ss) 297 298 // get the starting number of message export entries 299 startTime := model.GetMillis() 300 messages, err := ss.Compliance().MessageExport(startTime-10, 10) 301 require.NoError(t, err) 302 assert.Equal(t, 0, len(messages)) 303 304 // need a team 305 team := &model.Team{ 306 DisplayName: "DisplayName", 307 Name: "zz" + model.NewId() + "b", 308 Email: MakeEmail(), 309 Type: model.TEAM_OPEN, 310 } 311 team, err = ss.Team().Save(team) 312 require.NoError(t, err) 313 314 // and two users that are a part of that team 315 user1 := &model.User{ 316 Email: MakeEmail(), 317 Username: model.NewId(), 318 } 319 user1, err = ss.User().Save(user1) 320 require.NoError(t, err) 321 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 322 TeamId: team.Id, 323 UserId: user1.Id, 324 }, -1) 325 require.NoError(t, nErr) 326 327 user2 := &model.User{ 328 Email: MakeEmail(), 329 Username: model.NewId(), 330 } 331 user2, err = ss.User().Save(user2) 332 require.NoError(t, err) 333 _, nErr = ss.Team().SaveMember(&model.TeamMember{ 334 TeamId: team.Id, 335 UserId: user2.Id, 336 }, -1) 337 require.NoError(t, nErr) 338 339 // need a public channel 340 channel := &model.Channel{ 341 TeamId: team.Id, 342 Name: model.NewId(), 343 DisplayName: "Public Channel", 344 Type: model.CHANNEL_OPEN, 345 } 346 channel, nErr = ss.Channel().Save(channel, -1) 347 require.NoError(t, nErr) 348 349 // user1 posts twice in the public channel 350 post1 := &model.Post{ 351 ChannelId: channel.Id, 352 UserId: user1.Id, 353 CreateAt: startTime, 354 Message: "zz" + model.NewId() + "a", 355 } 356 post1, err = ss.Post().Save(post1) 357 require.NoError(t, err) 358 359 post2 := &model.Post{ 360 ChannelId: channel.Id, 361 UserId: user1.Id, 362 CreateAt: startTime + 10, 363 Message: "zz" + model.NewId() + "b", 364 } 365 post2, err = ss.Post().Save(post2) 366 require.NoError(t, err) 367 368 // fetch the message exports for both posts that user1 sent 369 messageExportMap := map[string]model.MessageExport{} 370 messages, err = ss.Compliance().MessageExport(startTime-10, 10) 371 require.NoError(t, err) 372 assert.Equal(t, 2, len(messages)) 373 374 for _, v := range messages { 375 messageExportMap[*v.PostId] = *v 376 } 377 378 // post1 was made by user1 in channel1 and team1 379 assert.Equal(t, post1.Id, *messageExportMap[post1.Id].PostId) 380 assert.Equal(t, post1.CreateAt, *messageExportMap[post1.Id].PostCreateAt) 381 assert.Equal(t, post1.Message, *messageExportMap[post1.Id].PostMessage) 382 assert.Equal(t, channel.Id, *messageExportMap[post1.Id].ChannelId) 383 assert.Equal(t, channel.DisplayName, *messageExportMap[post1.Id].ChannelDisplayName) 384 assert.Equal(t, user1.Id, *messageExportMap[post1.Id].UserId) 385 assert.Equal(t, user1.Email, *messageExportMap[post1.Id].UserEmail) 386 assert.Equal(t, user1.Username, *messageExportMap[post1.Id].Username) 387 388 // post2 was made by user1 in channel1 and team1 389 assert.Equal(t, post2.Id, *messageExportMap[post2.Id].PostId) 390 assert.Equal(t, post2.CreateAt, *messageExportMap[post2.Id].PostCreateAt) 391 assert.Equal(t, post2.Message, *messageExportMap[post2.Id].PostMessage) 392 assert.Equal(t, channel.Id, *messageExportMap[post2.Id].ChannelId) 393 assert.Equal(t, channel.DisplayName, *messageExportMap[post2.Id].ChannelDisplayName) 394 assert.Equal(t, user1.Id, *messageExportMap[post2.Id].UserId) 395 assert.Equal(t, user1.Email, *messageExportMap[post2.Id].UserEmail) 396 assert.Equal(t, user1.Username, *messageExportMap[post2.Id].Username) 397 } 398 399 func testMessageExportPrivateChannel(t *testing.T, ss store.Store) { 400 defer cleanupStoreState(t, ss) 401 402 // get the starting number of message export entries 403 startTime := model.GetMillis() 404 messages, err := ss.Compliance().MessageExport(startTime-10, 10) 405 require.NoError(t, err) 406 assert.Equal(t, 0, len(messages)) 407 408 // need a team 409 team := &model.Team{ 410 DisplayName: "DisplayName", 411 Name: "zz" + model.NewId() + "b", 412 Email: MakeEmail(), 413 Type: model.TEAM_OPEN, 414 } 415 team, err = ss.Team().Save(team) 416 require.NoError(t, err) 417 418 // and two users that are a part of that team 419 user1 := &model.User{ 420 Email: MakeEmail(), 421 Username: model.NewId(), 422 } 423 user1, err = ss.User().Save(user1) 424 require.NoError(t, err) 425 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 426 TeamId: team.Id, 427 UserId: user1.Id, 428 }, -1) 429 require.NoError(t, nErr) 430 431 user2 := &model.User{ 432 Email: MakeEmail(), 433 Username: model.NewId(), 434 } 435 user2, err = ss.User().Save(user2) 436 require.NoError(t, err) 437 _, nErr = ss.Team().SaveMember(&model.TeamMember{ 438 TeamId: team.Id, 439 UserId: user2.Id, 440 }, -1) 441 require.NoError(t, nErr) 442 443 // need a private channel 444 channel := &model.Channel{ 445 TeamId: team.Id, 446 Name: model.NewId(), 447 DisplayName: "Private Channel", 448 Type: model.CHANNEL_PRIVATE, 449 } 450 channel, nErr = ss.Channel().Save(channel, -1) 451 require.NoError(t, nErr) 452 453 // user1 posts twice in the private channel 454 post1 := &model.Post{ 455 ChannelId: channel.Id, 456 UserId: user1.Id, 457 CreateAt: startTime, 458 Message: "zz" + model.NewId() + "a", 459 } 460 post1, err = ss.Post().Save(post1) 461 require.NoError(t, err) 462 463 post2 := &model.Post{ 464 ChannelId: channel.Id, 465 UserId: user1.Id, 466 CreateAt: startTime + 10, 467 Message: "zz" + model.NewId() + "b", 468 } 469 post2, err = ss.Post().Save(post2) 470 require.NoError(t, err) 471 472 // fetch the message exports for both posts that user1 sent 473 messageExportMap := map[string]model.MessageExport{} 474 messages, err = ss.Compliance().MessageExport(startTime-10, 10) 475 require.NoError(t, err) 476 assert.Equal(t, 2, len(messages)) 477 478 for _, v := range messages { 479 messageExportMap[*v.PostId] = *v 480 } 481 482 // post1 was made by user1 in channel1 and team1 483 assert.Equal(t, post1.Id, *messageExportMap[post1.Id].PostId) 484 assert.Equal(t, post1.CreateAt, *messageExportMap[post1.Id].PostCreateAt) 485 assert.Equal(t, post1.Message, *messageExportMap[post1.Id].PostMessage) 486 assert.Equal(t, channel.Id, *messageExportMap[post1.Id].ChannelId) 487 assert.Equal(t, channel.DisplayName, *messageExportMap[post1.Id].ChannelDisplayName) 488 assert.Equal(t, channel.Type, *messageExportMap[post1.Id].ChannelType) 489 assert.Equal(t, user1.Id, *messageExportMap[post1.Id].UserId) 490 assert.Equal(t, user1.Email, *messageExportMap[post1.Id].UserEmail) 491 assert.Equal(t, user1.Username, *messageExportMap[post1.Id].Username) 492 493 // post2 was made by user1 in channel1 and team1 494 assert.Equal(t, post2.Id, *messageExportMap[post2.Id].PostId) 495 assert.Equal(t, post2.CreateAt, *messageExportMap[post2.Id].PostCreateAt) 496 assert.Equal(t, post2.Message, *messageExportMap[post2.Id].PostMessage) 497 assert.Equal(t, channel.Id, *messageExportMap[post2.Id].ChannelId) 498 assert.Equal(t, channel.DisplayName, *messageExportMap[post2.Id].ChannelDisplayName) 499 assert.Equal(t, channel.Type, *messageExportMap[post2.Id].ChannelType) 500 assert.Equal(t, user1.Id, *messageExportMap[post2.Id].UserId) 501 assert.Equal(t, user1.Email, *messageExportMap[post2.Id].UserEmail) 502 assert.Equal(t, user1.Username, *messageExportMap[post2.Id].Username) 503 } 504 505 func testMessageExportDirectMessageChannel(t *testing.T, ss store.Store) { 506 defer cleanupStoreState(t, ss) 507 508 // get the starting number of message export entries 509 startTime := model.GetMillis() 510 messages, err := ss.Compliance().MessageExport(startTime-10, 10) 511 require.NoError(t, err) 512 assert.Equal(t, 0, len(messages)) 513 514 // need a team 515 team := &model.Team{ 516 DisplayName: "DisplayName", 517 Name: "zz" + model.NewId() + "b", 518 Email: MakeEmail(), 519 Type: model.TEAM_OPEN, 520 } 521 team, err = ss.Team().Save(team) 522 require.NoError(t, err) 523 524 // and two users that are a part of that team 525 user1 := &model.User{ 526 Email: MakeEmail(), 527 Username: model.NewId(), 528 } 529 user1, err = ss.User().Save(user1) 530 require.NoError(t, err) 531 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 532 TeamId: team.Id, 533 UserId: user1.Id, 534 }, -1) 535 require.NoError(t, nErr) 536 537 user2 := &model.User{ 538 Email: MakeEmail(), 539 Username: model.NewId(), 540 } 541 user2, err = ss.User().Save(user2) 542 require.NoError(t, err) 543 _, nErr = ss.Team().SaveMember(&model.TeamMember{ 544 TeamId: team.Id, 545 UserId: user2.Id, 546 }, -1) 547 require.NoError(t, nErr) 548 549 // as well as a DM channel between those users 550 directMessageChannel, nErr := ss.Channel().CreateDirectChannel(user1, user2) 551 require.NoError(t, nErr) 552 553 // user1 also sends a DM to user2 554 post := &model.Post{ 555 ChannelId: directMessageChannel.Id, 556 UserId: user1.Id, 557 CreateAt: startTime + 20, 558 Message: "zz" + model.NewId() + "c", 559 } 560 post, err = ss.Post().Save(post) 561 require.NoError(t, err) 562 563 // fetch the message export for the post that user1 sent 564 messageExportMap := map[string]model.MessageExport{} 565 messages, err = ss.Compliance().MessageExport(startTime-10, 10) 566 require.NoError(t, err) 567 568 assert.Equal(t, 1, len(messages)) 569 570 for _, v := range messages { 571 messageExportMap[*v.PostId] = *v 572 } 573 574 // post is a DM between user1 and user2 575 // there is no channel display name for direct messages, so we sub in the string "Direct Message" instead 576 assert.Equal(t, post.Id, *messageExportMap[post.Id].PostId) 577 assert.Equal(t, post.CreateAt, *messageExportMap[post.Id].PostCreateAt) 578 assert.Equal(t, post.Message, *messageExportMap[post.Id].PostMessage) 579 assert.Equal(t, directMessageChannel.Id, *messageExportMap[post.Id].ChannelId) 580 assert.Equal(t, "Direct Message", *messageExportMap[post.Id].ChannelDisplayName) 581 assert.Equal(t, user1.Id, *messageExportMap[post.Id].UserId) 582 assert.Equal(t, user1.Email, *messageExportMap[post.Id].UserEmail) 583 assert.Equal(t, user1.Username, *messageExportMap[post.Id].Username) 584 } 585 586 func testMessageExportGroupMessageChannel(t *testing.T, ss store.Store) { 587 defer cleanupStoreState(t, ss) 588 589 // get the starting number of message export entries 590 startTime := model.GetMillis() 591 messages, err := ss.Compliance().MessageExport(startTime-10, 10) 592 require.NoError(t, err) 593 assert.Equal(t, 0, len(messages)) 594 595 // need a team 596 team := &model.Team{ 597 DisplayName: "DisplayName", 598 Name: "zz" + model.NewId() + "b", 599 Email: MakeEmail(), 600 Type: model.TEAM_OPEN, 601 } 602 team, err = ss.Team().Save(team) 603 require.NoError(t, err) 604 605 // and three users that are a part of that team 606 user1 := &model.User{ 607 Email: MakeEmail(), 608 Username: model.NewId(), 609 } 610 user1, err = ss.User().Save(user1) 611 require.NoError(t, err) 612 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 613 TeamId: team.Id, 614 UserId: user1.Id, 615 }, -1) 616 require.NoError(t, nErr) 617 618 user2 := &model.User{ 619 Email: MakeEmail(), 620 Username: model.NewId(), 621 } 622 user2, err = ss.User().Save(user2) 623 require.NoError(t, err) 624 _, nErr = ss.Team().SaveMember(&model.TeamMember{ 625 TeamId: team.Id, 626 UserId: user2.Id, 627 }, -1) 628 require.NoError(t, nErr) 629 630 user3 := &model.User{ 631 Email: MakeEmail(), 632 Username: model.NewId(), 633 } 634 user3, err = ss.User().Save(user3) 635 require.NoError(t, err) 636 _, nErr = ss.Team().SaveMember(&model.TeamMember{ 637 TeamId: team.Id, 638 UserId: user3.Id, 639 }, -1) 640 require.NoError(t, nErr) 641 642 // can't create a group channel directly, because importing app creates an import cycle, so we have to fake it 643 groupMessageChannel := &model.Channel{ 644 TeamId: team.Id, 645 Name: model.NewId(), 646 Type: model.CHANNEL_GROUP, 647 } 648 groupMessageChannel, nErr = ss.Channel().Save(groupMessageChannel, -1) 649 require.NoError(t, nErr) 650 651 // user1 posts in the GM 652 post := &model.Post{ 653 ChannelId: groupMessageChannel.Id, 654 UserId: user1.Id, 655 CreateAt: startTime + 20, 656 Message: "zz" + model.NewId() + "c", 657 } 658 post, err = ss.Post().Save(post) 659 require.NoError(t, err) 660 661 // fetch the message export for the post that user1 sent 662 messageExportMap := map[string]model.MessageExport{} 663 messages, err = ss.Compliance().MessageExport(startTime-10, 10) 664 require.NoError(t, err) 665 assert.Equal(t, 1, len(messages)) 666 667 for _, v := range messages { 668 messageExportMap[*v.PostId] = *v 669 } 670 671 // post is a DM between user1 and user2 672 // there is no channel display name for direct messages, so we sub in the string "Direct Message" instead 673 assert.Equal(t, post.Id, *messageExportMap[post.Id].PostId) 674 assert.Equal(t, post.CreateAt, *messageExportMap[post.Id].PostCreateAt) 675 assert.Equal(t, post.Message, *messageExportMap[post.Id].PostMessage) 676 assert.Equal(t, groupMessageChannel.Id, *messageExportMap[post.Id].ChannelId) 677 assert.Equal(t, "Group Message", *messageExportMap[post.Id].ChannelDisplayName) 678 assert.Equal(t, user1.Id, *messageExportMap[post.Id].UserId) 679 assert.Equal(t, user1.Email, *messageExportMap[post.Id].UserEmail) 680 assert.Equal(t, user1.Username, *messageExportMap[post.Id].Username) 681 } 682 683 //post,edit,export 684 func testEditExportMessage(t *testing.T, ss store.Store) { 685 defer cleanupStoreState(t, ss) 686 // get the starting number of message export entries 687 startTime := model.GetMillis() 688 messages, err := ss.Compliance().MessageExport(startTime-1, 10) 689 require.NoError(t, err) 690 assert.Equal(t, 0, len(messages)) 691 692 // need a team 693 team := &model.Team{ 694 DisplayName: "DisplayName", 695 Name: "zz" + model.NewId() + "b", 696 Email: MakeEmail(), 697 Type: model.TEAM_OPEN, 698 } 699 team, err = ss.Team().Save(team) 700 require.NoError(t, err) 701 702 // need a user part of that team 703 user1 := &model.User{ 704 Email: MakeEmail(), 705 Username: model.NewId(), 706 } 707 user1, err = ss.User().Save(user1) 708 require.NoError(t, err) 709 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 710 TeamId: team.Id, 711 UserId: user1.Id, 712 }, -1) 713 require.NoError(t, nErr) 714 715 // need a public channel 716 channel := &model.Channel{ 717 TeamId: team.Id, 718 Name: model.NewId(), 719 DisplayName: "Public Channel", 720 Type: model.CHANNEL_OPEN, 721 } 722 channel, nErr = ss.Channel().Save(channel, -1) 723 require.NoError(t, nErr) 724 725 // user1 posts in the public channel 726 post1 := &model.Post{ 727 ChannelId: channel.Id, 728 UserId: user1.Id, 729 CreateAt: startTime, 730 Message: "zz" + model.NewId() + "a", 731 } 732 post1, err = ss.Post().Save(post1) 733 require.NoError(t, err) 734 735 //user 1 edits the previous post 736 post1e := &model.Post{} 737 post1e = post1.Clone() 738 post1e.Message = "edit " + post1.Message 739 740 post1e, err = ss.Post().Update(post1e, post1) 741 require.NoError(t, err) 742 743 // fetch the message exports from the start 744 messages, err = ss.Compliance().MessageExport(startTime-1, 10) 745 require.NoError(t, err) 746 assert.Equal(t, 2, len(messages)) 747 748 for _, v := range messages { 749 if *v.PostDeleteAt > 0 { 750 // post1 was made by user1 in channel1 and team1 751 assert.Equal(t, post1.Id, *v.PostId) 752 assert.Equal(t, post1.OriginalId, *v.PostOriginalId) 753 assert.Equal(t, post1.CreateAt, *v.PostCreateAt) 754 assert.Equal(t, post1.UpdateAt, *v.PostUpdateAt) 755 assert.Equal(t, post1.Message, *v.PostMessage) 756 assert.Equal(t, channel.Id, *v.ChannelId) 757 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 758 assert.Equal(t, user1.Id, *v.UserId) 759 assert.Equal(t, user1.Email, *v.UserEmail) 760 assert.Equal(t, user1.Username, *v.Username) 761 } else { 762 // post1e was made by user1 in channel1 and team1 763 assert.Equal(t, post1e.Id, *v.PostId) 764 assert.Equal(t, post1e.CreateAt, *v.PostCreateAt) 765 assert.Equal(t, post1e.UpdateAt, *v.PostUpdateAt) 766 assert.Equal(t, post1e.Message, *v.PostMessage) 767 assert.Equal(t, channel.Id, *v.ChannelId) 768 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 769 assert.Equal(t, user1.Id, *v.UserId) 770 assert.Equal(t, user1.Email, *v.UserEmail) 771 assert.Equal(t, user1.Username, *v.Username) 772 } 773 } 774 } 775 776 //post, export, edit, export 777 func testEditAfterExportMessage(t *testing.T, ss store.Store) { 778 defer cleanupStoreState(t, ss) 779 // get the starting number of message export entries 780 startTime := model.GetMillis() 781 messages, err := ss.Compliance().MessageExport(startTime-1, 10) 782 require.NoError(t, err) 783 assert.Equal(t, 0, len(messages)) 784 785 // need a team 786 team := &model.Team{ 787 DisplayName: "DisplayName", 788 Name: "zz" + model.NewId() + "b", 789 Email: MakeEmail(), 790 Type: model.TEAM_OPEN, 791 } 792 team, err = ss.Team().Save(team) 793 require.NoError(t, err) 794 795 // need a user part of that team 796 user1 := &model.User{ 797 Email: MakeEmail(), 798 Username: model.NewId(), 799 } 800 user1, err = ss.User().Save(user1) 801 require.NoError(t, err) 802 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 803 TeamId: team.Id, 804 UserId: user1.Id, 805 }, -1) 806 require.NoError(t, nErr) 807 808 // need a public channel 809 channel := &model.Channel{ 810 TeamId: team.Id, 811 Name: model.NewId(), 812 DisplayName: "Public Channel", 813 Type: model.CHANNEL_OPEN, 814 } 815 channel, nErr = ss.Channel().Save(channel, -1) 816 require.NoError(t, nErr) 817 818 // user1 posts in the public channel 819 post1 := &model.Post{ 820 ChannelId: channel.Id, 821 UserId: user1.Id, 822 CreateAt: startTime, 823 Message: "zz" + model.NewId() + "a", 824 } 825 post1, err = ss.Post().Save(post1) 826 require.NoError(t, err) 827 828 // fetch the message exports from the start 829 messages, err = ss.Compliance().MessageExport(startTime-1, 10) 830 require.NoError(t, err) 831 assert.Equal(t, 1, len(messages)) 832 833 v := messages[0] 834 // post1 was made by user1 in channel1 and team1 835 assert.Equal(t, post1.Id, *v.PostId) 836 assert.Equal(t, post1.OriginalId, *v.PostOriginalId) 837 assert.Equal(t, post1.CreateAt, *v.PostCreateAt) 838 assert.Equal(t, post1.UpdateAt, *v.PostUpdateAt) 839 assert.Equal(t, post1.Message, *v.PostMessage) 840 assert.Equal(t, channel.Id, *v.ChannelId) 841 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 842 assert.Equal(t, user1.Id, *v.UserId) 843 assert.Equal(t, user1.Email, *v.UserEmail) 844 assert.Equal(t, user1.Username, *v.Username) 845 846 postEditTime := post1.UpdateAt + 1 847 //user 1 edits the previous post 848 post1e := &model.Post{} 849 post1e = post1.Clone() 850 post1e.EditAt = postEditTime 851 post1e.Message = "edit " + post1.Message 852 post1e, err = ss.Post().Update(post1e, post1) 853 require.NoError(t, err) 854 855 // fetch the message exports after edit 856 messages, err = ss.Compliance().MessageExport(postEditTime-1, 10) 857 require.NoError(t, err) 858 assert.Equal(t, 2, len(messages)) 859 860 for _, v := range messages { 861 if *v.PostDeleteAt > 0 { 862 // post1 was made by user1 in channel1 and team1 863 assert.Equal(t, post1.Id, *v.PostId) 864 assert.Equal(t, post1.OriginalId, *v.PostOriginalId) 865 assert.Equal(t, post1.CreateAt, *v.PostCreateAt) 866 assert.Equal(t, post1.UpdateAt, *v.PostUpdateAt) 867 assert.Equal(t, post1.Message, *v.PostMessage) 868 assert.Equal(t, channel.Id, *v.ChannelId) 869 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 870 assert.Equal(t, user1.Id, *v.UserId) 871 assert.Equal(t, user1.Email, *v.UserEmail) 872 assert.Equal(t, user1.Username, *v.Username) 873 } else { 874 // post1e was made by user1 in channel1 and team1 875 assert.Equal(t, post1e.Id, *v.PostId) 876 assert.Equal(t, post1e.CreateAt, *v.PostCreateAt) 877 assert.Equal(t, post1e.UpdateAt, *v.PostUpdateAt) 878 assert.Equal(t, post1e.Message, *v.PostMessage) 879 assert.Equal(t, channel.Id, *v.ChannelId) 880 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 881 assert.Equal(t, user1.Id, *v.UserId) 882 assert.Equal(t, user1.Email, *v.UserEmail) 883 assert.Equal(t, user1.Username, *v.Username) 884 } 885 } 886 } 887 888 //post, delete, export 889 func testDeleteExportMessage(t *testing.T, ss store.Store) { 890 defer cleanupStoreState(t, ss) 891 // get the starting number of message export entries 892 startTime := model.GetMillis() 893 messages, err := ss.Compliance().MessageExport(startTime-1, 10) 894 require.NoError(t, err) 895 assert.Equal(t, 0, len(messages)) 896 897 // need a team 898 team := &model.Team{ 899 DisplayName: "DisplayName", 900 Name: "zz" + model.NewId() + "b", 901 Email: MakeEmail(), 902 Type: model.TEAM_OPEN, 903 } 904 team, err = ss.Team().Save(team) 905 require.NoError(t, err) 906 907 // need a user part of that team 908 user1 := &model.User{ 909 Email: MakeEmail(), 910 Username: model.NewId(), 911 } 912 user1, err = ss.User().Save(user1) 913 require.NoError(t, err) 914 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 915 TeamId: team.Id, 916 UserId: user1.Id, 917 }, -1) 918 require.NoError(t, nErr) 919 920 // need a public channel 921 channel := &model.Channel{ 922 TeamId: team.Id, 923 Name: model.NewId(), 924 DisplayName: "Public Channel", 925 Type: model.CHANNEL_OPEN, 926 } 927 channel, nErr = ss.Channel().Save(channel, -1) 928 require.NoError(t, nErr) 929 930 // user1 posts in the public channel 931 post1 := &model.Post{ 932 ChannelId: channel.Id, 933 UserId: user1.Id, 934 CreateAt: startTime, 935 Message: "zz" + model.NewId() + "a", 936 } 937 post1, err = ss.Post().Save(post1) 938 require.NoError(t, err) 939 940 //user 1 deletes the previous post 941 postDeleteTime := post1.UpdateAt + 1 942 err = ss.Post().Delete(post1.Id, postDeleteTime, user1.Id) 943 require.NoError(t, err) 944 945 // fetch the message exports from the start 946 messages, err = ss.Compliance().MessageExport(startTime-1, 10) 947 require.NoError(t, err) 948 assert.Equal(t, 1, len(messages)) 949 950 v := messages[0] 951 // post1 was made and deleted by user1 in channel1 and team1 952 assert.Equal(t, post1.Id, *v.PostId) 953 assert.Equal(t, post1.OriginalId, *v.PostOriginalId) 954 assert.Equal(t, post1.CreateAt, *v.PostCreateAt) 955 assert.Equal(t, postDeleteTime, *v.PostUpdateAt) 956 assert.NotNil(t, v.PostProps) 957 958 props := map[string]interface{}{} 959 e := json.Unmarshal([]byte(*v.PostProps), &props) 960 require.NoError(t, e) 961 962 _, ok := props[model.POST_PROPS_DELETE_BY] 963 assert.True(t, ok) 964 965 assert.Equal(t, post1.Message, *v.PostMessage) 966 assert.Equal(t, channel.Id, *v.ChannelId) 967 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 968 assert.Equal(t, user1.Id, *v.UserId) 969 assert.Equal(t, user1.Email, *v.UserEmail) 970 assert.Equal(t, user1.Username, *v.Username) 971 } 972 973 //post,export,delete,export 974 func testDeleteAfterExportMessage(t *testing.T, ss store.Store) { 975 defer cleanupStoreState(t, ss) 976 // get the starting number of message export entries 977 startTime := model.GetMillis() 978 messages, err := ss.Compliance().MessageExport(startTime-1, 10) 979 require.NoError(t, err) 980 assert.Equal(t, 0, len(messages)) 981 982 // need a team 983 team := &model.Team{ 984 DisplayName: "DisplayName", 985 Name: "zz" + model.NewId() + "b", 986 Email: MakeEmail(), 987 Type: model.TEAM_OPEN, 988 } 989 team, err = ss.Team().Save(team) 990 require.NoError(t, err) 991 992 // need a user part of that team 993 user1 := &model.User{ 994 Email: MakeEmail(), 995 Username: model.NewId(), 996 } 997 user1, err = ss.User().Save(user1) 998 require.NoError(t, err) 999 _, nErr := ss.Team().SaveMember(&model.TeamMember{ 1000 TeamId: team.Id, 1001 UserId: user1.Id, 1002 }, -1) 1003 require.NoError(t, nErr) 1004 1005 // need a public channel 1006 channel := &model.Channel{ 1007 TeamId: team.Id, 1008 Name: model.NewId(), 1009 DisplayName: "Public Channel", 1010 Type: model.CHANNEL_OPEN, 1011 } 1012 channel, nErr = ss.Channel().Save(channel, -1) 1013 require.NoError(t, nErr) 1014 1015 // user1 posts in the public channel 1016 post1 := &model.Post{ 1017 ChannelId: channel.Id, 1018 UserId: user1.Id, 1019 CreateAt: startTime, 1020 Message: "zz" + model.NewId() + "a", 1021 } 1022 post1, err = ss.Post().Save(post1) 1023 require.NoError(t, err) 1024 1025 // fetch the message exports from the start 1026 messages, err = ss.Compliance().MessageExport(startTime-1, 10) 1027 require.NoError(t, err) 1028 assert.Equal(t, 1, len(messages)) 1029 1030 v := messages[0] 1031 // post1 was created by user1 in channel1 and team1 1032 assert.Equal(t, post1.Id, *v.PostId) 1033 assert.Equal(t, post1.OriginalId, *v.PostOriginalId) 1034 assert.Equal(t, post1.CreateAt, *v.PostCreateAt) 1035 assert.Equal(t, post1.UpdateAt, *v.PostUpdateAt) 1036 assert.Equal(t, post1.Message, *v.PostMessage) 1037 assert.Equal(t, channel.Id, *v.ChannelId) 1038 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 1039 assert.Equal(t, user1.Id, *v.UserId) 1040 assert.Equal(t, user1.Email, *v.UserEmail) 1041 assert.Equal(t, user1.Username, *v.Username) 1042 1043 //user 1 deletes the previous post 1044 postDeleteTime := post1.UpdateAt + 1 1045 err = ss.Post().Delete(post1.Id, postDeleteTime, user1.Id) 1046 require.NoError(t, err) 1047 1048 // fetch the message exports after delete 1049 messages, err = ss.Compliance().MessageExport(postDeleteTime-1, 10) 1050 require.NoError(t, err) 1051 assert.Equal(t, 1, len(messages)) 1052 1053 v = messages[0] 1054 // post1 was created and deleted by user1 in channel1 and team1 1055 assert.Equal(t, post1.Id, *v.PostId) 1056 assert.Equal(t, post1.OriginalId, *v.PostOriginalId) 1057 assert.Equal(t, post1.CreateAt, *v.PostCreateAt) 1058 assert.Equal(t, postDeleteTime, *v.PostUpdateAt) 1059 assert.NotNil(t, v.PostProps) 1060 1061 props := map[string]interface{}{} 1062 e := json.Unmarshal([]byte(*v.PostProps), &props) 1063 require.NoError(t, e) 1064 1065 _, ok := props[model.POST_PROPS_DELETE_BY] 1066 assert.True(t, ok) 1067 1068 assert.Equal(t, post1.Message, *v.PostMessage) 1069 assert.Equal(t, channel.Id, *v.ChannelId) 1070 assert.Equal(t, channel.DisplayName, *v.ChannelDisplayName) 1071 assert.Equal(t, user1.Id, *v.UserId) 1072 assert.Equal(t, user1.Email, *v.UserEmail) 1073 assert.Equal(t, user1.Username, *v.Username) 1074 }