github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/searchtest/helper.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package searchtest 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/pkg/errors" 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 type SearchTestHelper struct { 19 Store store.Store 20 Team *model.Team 21 AnotherTeam *model.Team 22 User *model.User 23 User2 *model.User 24 UserAnotherTeam *model.User 25 ChannelBasic *model.Channel 26 ChannelPrivate *model.Channel 27 ChannelAnotherTeam *model.Channel 28 ChannelDeleted *model.Channel 29 } 30 31 func (th *SearchTestHelper) SetupBasicFixtures() error { 32 // Remove users from previous tests 33 err := th.cleanAllUsers() 34 if err != nil { 35 return err 36 } 37 38 // Create teams 39 team, err := th.createTeam("searchtest-team", "Searchtest team", model.TEAM_OPEN) 40 if err != nil { 41 return err 42 } 43 anotherTeam, err := th.createTeam("another-searchtest-team", "Another Searchtest team", model.TEAM_OPEN) 44 if err != nil { 45 return err 46 } 47 48 // Create users 49 user, err := th.createUser("basicusername1", "basicnickname1", "basicfirstname1", "basiclastname1") 50 if err != nil { 51 return err 52 } 53 user2, err := th.createUser("basicusername2", "basicnickname2", "basicfirstname2", "basiclastname2") 54 if err != nil { 55 return err 56 } 57 useranother, err := th.createUser("basicusername3", "basicnickname3", "basicfirstname3", "basiclastname3") 58 if err != nil { 59 return err 60 } 61 62 // Create channels 63 channelBasic, err := th.createChannel(team.Id, "channel-a", "ChannelA", "", model.CHANNEL_OPEN, false) 64 if err != nil { 65 return err 66 } 67 channelPrivate, err := th.createChannel(team.Id, "channel-private", "ChannelPrivate", "", model.CHANNEL_PRIVATE, false) 68 if err != nil { 69 return err 70 } 71 channelDeleted, err := th.createChannel(team.Id, "channel-deleted", "ChannelA (deleted)", "", model.CHANNEL_OPEN, true) 72 if err != nil { 73 return err 74 } 75 channelAnotherTeam, err := th.createChannel(anotherTeam.Id, "channel-a", "ChannelA", "", model.CHANNEL_OPEN, false) 76 if err != nil { 77 return err 78 } 79 80 err = th.addUserToTeams(user, []string{team.Id, anotherTeam.Id}) 81 if err != nil { 82 return err 83 } 84 85 err = th.addUserToTeams(user2, []string{team.Id, anotherTeam.Id}) 86 if err != nil { 87 return err 88 } 89 90 err = th.addUserToTeams(useranother, []string{anotherTeam.Id}) 91 if err != nil { 92 return err 93 } 94 95 err = th.addUserToChannels(user, []string{channelBasic.Id, channelPrivate.Id, channelDeleted.Id}) 96 if err != nil { 97 return err 98 } 99 100 err = th.addUserToChannels(user2, []string{channelPrivate.Id, channelDeleted.Id}) 101 if err != nil { 102 return err 103 } 104 105 err = th.addUserToChannels(useranother, []string{channelAnotherTeam.Id}) 106 if err != nil { 107 return err 108 } 109 110 th.Team = team 111 th.AnotherTeam = anotherTeam 112 th.User = user 113 th.User2 = user2 114 th.UserAnotherTeam = useranother 115 th.ChannelBasic = channelBasic 116 th.ChannelPrivate = channelPrivate 117 th.ChannelAnotherTeam = channelAnotherTeam 118 th.ChannelDeleted = channelDeleted 119 120 return nil 121 } 122 123 func (th *SearchTestHelper) CleanFixtures() error { 124 err := th.deleteChannels([]*model.Channel{ 125 th.ChannelBasic, th.ChannelPrivate, th.ChannelAnotherTeam, th.ChannelDeleted, 126 }) 127 if err != nil { 128 return err 129 } 130 131 err = th.deleteTeam(th.Team) 132 if err != nil { 133 return err 134 } 135 136 err = th.deleteTeam(th.AnotherTeam) 137 if err != nil { 138 return err 139 } 140 141 err = th.cleanAllUsers() 142 if err != nil { 143 return err 144 } 145 146 return nil 147 } 148 149 func (th *SearchTestHelper) createTeam(name, displayName, teamType string) (*model.Team, error) { 150 return th.Store.Team().Save(&model.Team{ 151 Name: name, 152 DisplayName: displayName, 153 Type: teamType, 154 }) 155 } 156 157 func (th *SearchTestHelper) deleteTeam(team *model.Team) error { 158 err := th.Store.Team().RemoveAllMembersByTeam(team.Id) 159 if err != nil { 160 return err 161 } 162 return th.Store.Team().PermanentDelete(team.Id) 163 } 164 165 func (th *SearchTestHelper) makeEmail() string { 166 return "success_" + model.NewId() + "@simulator.amazon.com" 167 } 168 169 func (th *SearchTestHelper) createUser(username, nickname, firstName, lastName string) (*model.User, error) { 170 return th.Store.User().Save(&model.User{ 171 Username: username, 172 Password: username, 173 Nickname: nickname, 174 FirstName: firstName, 175 LastName: lastName, 176 Email: th.makeEmail(), 177 }) 178 } 179 180 func (th *SearchTestHelper) createGuest(username, nickname, firstName, lastName string) (*model.User, error) { 181 return th.Store.User().Save(&model.User{ 182 Username: username, 183 Password: username, 184 Nickname: nickname, 185 FirstName: firstName, 186 LastName: lastName, 187 Email: th.makeEmail(), 188 Roles: model.SYSTEM_GUEST_ROLE_ID, 189 }) 190 } 191 192 func (th *SearchTestHelper) deleteUser(user *model.User) error { 193 return th.Store.User().PermanentDelete(user.Id) 194 } 195 196 func (th *SearchTestHelper) cleanAllUsers() error { 197 users, err := th.Store.User().GetAll() 198 if err != nil { 199 return err 200 } 201 202 for _, u := range users { 203 err := th.deleteUser(u) 204 if err != nil { 205 return err 206 } 207 } 208 209 return nil 210 } 211 212 func (th *SearchTestHelper) createBot(username, displayName, ownerID string) (*model.Bot, error) { 213 botModel := &model.Bot{ 214 Username: username, 215 DisplayName: displayName, 216 OwnerId: ownerID, 217 } 218 219 user, err := th.Store.User().Save(model.UserFromBot(botModel)) 220 if err != nil { 221 return nil, errors.New(err.Error()) 222 } 223 224 botModel.UserId = user.Id 225 bot, err := th.Store.Bot().Save(botModel) 226 if err != nil { 227 th.Store.User().PermanentDelete(bot.UserId) 228 return nil, errors.New(err.Error()) 229 } 230 231 return bot, nil 232 } 233 234 func (th *SearchTestHelper) deleteBot(botID string) error { 235 err := th.Store.Bot().PermanentDelete(botID) 236 if err != nil { 237 return errors.New(err.Error()) 238 } 239 return nil 240 } 241 242 func (th *SearchTestHelper) createChannel(teamID, name, displayName, purpose, channelType string, deleted bool) (*model.Channel, error) { 243 channel, err := th.Store.Channel().Save(&model.Channel{ 244 TeamId: teamID, 245 DisplayName: displayName, 246 Name: name, 247 Type: channelType, 248 Purpose: purpose, 249 }, 999) 250 if err != nil { 251 return nil, err 252 } 253 254 if deleted { 255 err := th.Store.Channel().Delete(channel.Id, model.GetMillis()) 256 if err != nil { 257 return nil, err 258 } 259 } 260 261 return channel, nil 262 } 263 264 func (th *SearchTestHelper) createDirectChannel(teamID, name, displayName string, users []*model.User) (*model.Channel, error) { 265 channel := &model.Channel{ 266 TeamId: teamID, 267 Name: name, 268 DisplayName: displayName, 269 Type: model.CHANNEL_DIRECT, 270 } 271 272 m1 := &model.ChannelMember{} 273 m1.ChannelId = channel.Id 274 m1.UserId = users[0].Id 275 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 276 277 m2 := &model.ChannelMember{} 278 m2.ChannelId = channel.Id 279 m2.UserId = users[0].Id 280 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 281 282 channel, err := th.Store.Channel().SaveDirectChannel(channel, m1, m2) 283 if err != nil { 284 return nil, err 285 } 286 return channel, nil 287 } 288 289 func (th *SearchTestHelper) createGroupChannel(teamID, displayName string, users []*model.User) (*model.Channel, error) { 290 userIDS := make([]string, len(users)) 291 for _, user := range users { 292 userIDS = append(userIDS, user.Id) 293 } 294 295 group := &model.Channel{ 296 TeamId: teamID, 297 Name: model.GetGroupNameFromUserIds(userIDS), 298 DisplayName: displayName, 299 Type: model.CHANNEL_GROUP, 300 } 301 302 channel, err := th.Store.Channel().Save(group, 10000) 303 if err != nil { 304 return nil, errors.New(err.Error()) 305 } 306 307 for _, user := range users { 308 err := th.addUserToChannels(user, []string{channel.Id}) 309 if err != nil { 310 return nil, err 311 } 312 } 313 314 return channel, nil 315 316 } 317 318 func (th *SearchTestHelper) deleteChannel(channel *model.Channel) error { 319 err := th.Store.Channel().PermanentDeleteMembersByChannel(channel.Id) 320 if err != nil { 321 return err 322 } 323 324 return th.Store.Channel().PermanentDelete(channel.Id) 325 } 326 327 func (th *SearchTestHelper) deleteChannels(channels []*model.Channel) error { 328 for _, channel := range channels { 329 err := th.deleteChannel(channel) 330 if err != nil { 331 return err 332 } 333 } 334 335 return nil 336 } 337 338 func (th *SearchTestHelper) createPostModel(userID, channelID, message, hashtags, postType string, createAt int64, pinned bool) *model.Post { 339 return &model.Post{ 340 Message: message, 341 ChannelId: channelID, 342 PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), 343 UserId: userID, 344 Hashtags: hashtags, 345 IsPinned: pinned, 346 CreateAt: createAt, 347 Type: postType, 348 } 349 } 350 351 func (th *SearchTestHelper) createPost(userID, channelID, message, hashtags, postType string, createAt int64, pinned bool) (*model.Post, error) { 352 var creationTime int64 = 1000000 353 if createAt > 0 { 354 creationTime = createAt 355 } 356 postModel := th.createPostModel(userID, channelID, message, hashtags, postType, creationTime, pinned) 357 return th.Store.Post().Save(postModel) 358 } 359 360 func (th *SearchTestHelper) createFileInfoModel(creatorID, postID, name, content, extension, mimeType string, createAt, size int64) *model.FileInfo { 361 return &model.FileInfo{ 362 CreatorId: creatorID, 363 PostId: postID, 364 CreateAt: createAt, 365 UpdateAt: createAt, 366 DeleteAt: 0, 367 Name: name, 368 Content: content, 369 Path: name, 370 Extension: extension, 371 Size: size, 372 MimeType: mimeType, 373 } 374 } 375 376 func (th *SearchTestHelper) createFileInfo(creatorID, postID, name, content, extension, mimeType string, createAt, size int64) (*model.FileInfo, error) { 377 var creationTime int64 = 1000000 378 if createAt > 0 { 379 creationTime = createAt 380 } 381 fileInfoModel := th.createFileInfoModel(creatorID, postID, name, content, extension, mimeType, creationTime, size) 382 return th.Store.FileInfo().Save(fileInfoModel) 383 } 384 385 func (th *SearchTestHelper) createReply(userID, message, hashtags string, parent *model.Post, createAt int64, pinned bool) (*model.Post, error) { 386 replyModel := th.createPostModel(userID, parent.ChannelId, message, hashtags, parent.Type, createAt, pinned) 387 replyModel.ParentId = parent.Id 388 replyModel.RootId = parent.Id 389 return th.Store.Post().Save(replyModel) 390 } 391 392 func (th *SearchTestHelper) deleteUserPosts(userID string) error { 393 err := th.Store.Post().PermanentDeleteByUser(userID) 394 if err != nil { 395 return errors.New(err.Error()) 396 } 397 return nil 398 } 399 400 func (th *SearchTestHelper) deleteUserFileInfos(userID string) error { 401 if _, err := th.Store.FileInfo().PermanentDeleteByUser(userID); err != nil { 402 return errors.New(err.Error()) 403 } 404 return nil 405 } 406 407 func (th *SearchTestHelper) addUserToTeams(user *model.User, teamIDS []string) error { 408 for _, teamID := range teamIDS { 409 _, err := th.Store.Team().SaveMember(&model.TeamMember{TeamId: teamID, UserId: user.Id}, -1) 410 if err != nil { 411 return errors.New(err.Error()) 412 } 413 } 414 415 return nil 416 } 417 418 func (th *SearchTestHelper) addUserToChannels(user *model.User, channelIDS []string) error { 419 for _, channelID := range channelIDS { 420 _, err := th.Store.Channel().SaveMember(&model.ChannelMember{ 421 ChannelId: channelID, 422 UserId: user.Id, 423 NotifyProps: model.GetDefaultChannelNotifyProps(), 424 }) 425 if err != nil { 426 return errors.New(err.Error()) 427 } 428 } 429 430 return nil 431 } 432 433 func (th *SearchTestHelper) assertUsersMatchInAnyOrder(t *testing.T, expected, actual []*model.User) { 434 expectedUsernames := make([]string, 0, len(expected)) 435 for _, user := range expected { 436 user.Sanitize(map[string]bool{}) 437 expectedUsernames = append(expectedUsernames, user.Username) 438 } 439 440 actualUsernames := make([]string, 0, len(actual)) 441 for _, user := range actual { 442 user.Sanitize(map[string]bool{}) 443 actualUsernames = append(actualUsernames, user.Username) 444 } 445 446 if assert.ElementsMatch(t, expectedUsernames, actualUsernames) { 447 assert.ElementsMatch(t, expected, actual) 448 } 449 } 450 451 func (th *SearchTestHelper) checkPostInSearchResults(t *testing.T, postID string, searchResults map[string]*model.Post) { 452 t.Helper() 453 postIDS := make([]string, len(searchResults)) 454 for ID := range searchResults { 455 postIDS = append(postIDS, ID) 456 } 457 assert.Contains(t, postIDS, postID, "Did not find expected post in search results.") 458 } 459 460 func (th *SearchTestHelper) checkFileInfoInSearchResults(t *testing.T, fileID string, searchResults map[string]*model.FileInfo) { 461 t.Helper() 462 fileIDS := make([]string, len(searchResults)) 463 for ID := range searchResults { 464 fileIDS = append(fileIDS, ID) 465 } 466 assert.Contains(t, fileIDS, fileID, "Did not find expected file in search results.") 467 } 468 469 func (th *SearchTestHelper) checkChannelIdsMatch(t *testing.T, expected []string, results *model.ChannelList) { 470 t.Helper() 471 channelIds := make([]string, len(*results)) 472 for i, channel := range *results { 473 channelIds[i] = channel.Id 474 } 475 require.ElementsMatch(t, expected, channelIds) 476 } 477 478 type ByChannelDisplayName model.ChannelList 479 480 func (s ByChannelDisplayName) Len() int { return len(s) } 481 func (s ByChannelDisplayName) Swap(i, j int) { 482 s[i], s[j] = s[j], s[i] 483 } 484 func (s ByChannelDisplayName) Less(i, j int) bool { 485 if s[i].DisplayName != s[j].DisplayName { 486 return s[i].DisplayName < s[j].DisplayName 487 } 488 489 return s[i].Id < s[j].Id 490 }