github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/storetest/user_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 "strings" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/mattermost/mattermost-server/model" 14 "github.com/mattermost/mattermost-server/store" 15 ) 16 17 func TestUserStore(t *testing.T, ss store.Store) { 18 t.Run("Save", func(t *testing.T) { testUserStoreSave(t, ss) }) 19 t.Run("Update", func(t *testing.T) { testUserStoreUpdate(t, ss) }) 20 t.Run("UpdateUpdateAt", func(t *testing.T) { testUserStoreUpdateUpdateAt(t, ss) }) 21 t.Run("UpdateFailedPasswordAttempts", func(t *testing.T) { testUserStoreUpdateFailedPasswordAttempts(t, ss) }) 22 t.Run("Get", func(t *testing.T) { testUserStoreGet(t, ss) }) 23 t.Run("UserCount", func(t *testing.T) { testUserCount(t, ss) }) 24 t.Run("GetAllUsingAuthService", func(t *testing.T) { testGetAllUsingAuthService(t, ss) }) 25 t.Run("GetAllProfiles", func(t *testing.T) { testUserStoreGetAllProfiles(t, ss) }) 26 t.Run("GetProfiles", func(t *testing.T) { testUserStoreGetProfiles(t, ss) }) 27 t.Run("GetProfilesInChannel", func(t *testing.T) { testUserStoreGetProfilesInChannel(t, ss) }) 28 t.Run("GetProfilesInChannelByStatus", func(t *testing.T) { testUserStoreGetProfilesInChannelByStatus(t, ss) }) 29 t.Run("GetProfilesWithoutTeam", func(t *testing.T) { testUserStoreGetProfilesWithoutTeam(t, ss) }) 30 t.Run("GetAllProfilesInChannel", func(t *testing.T) { testUserStoreGetAllProfilesInChannel(t, ss) }) 31 t.Run("GetProfilesNotInChannel", func(t *testing.T) { testUserStoreGetProfilesNotInChannel(t, ss) }) 32 t.Run("GetProfilesByIds", func(t *testing.T) { testUserStoreGetProfilesByIds(t, ss) }) 33 t.Run("GetProfilesByUsernames", func(t *testing.T) { testUserStoreGetProfilesByUsernames(t, ss) }) 34 t.Run("GetSystemAdminProfiles", func(t *testing.T) { testUserStoreGetSystemAdminProfiles(t, ss) }) 35 t.Run("GetByEmail", func(t *testing.T) { testUserStoreGetByEmail(t, ss) }) 36 t.Run("GetByAuthData", func(t *testing.T) { testUserStoreGetByAuthData(t, ss) }) 37 t.Run("GetByUsername", func(t *testing.T) { testUserStoreGetByUsername(t, ss) }) 38 t.Run("GetForLogin", func(t *testing.T) { testUserStoreGetForLogin(t, ss) }) 39 t.Run("UpdatePassword", func(t *testing.T) { testUserStoreUpdatePassword(t, ss) }) 40 t.Run("Delete", func(t *testing.T) { testUserStoreDelete(t, ss) }) 41 t.Run("UpdateAuthData", func(t *testing.T) { testUserStoreUpdateAuthData(t, ss) }) 42 t.Run("UserUnreadCount", func(t *testing.T) { testUserUnreadCount(t, ss) }) 43 t.Run("UpdateMfaSecret", func(t *testing.T) { testUserStoreUpdateMfaSecret(t, ss) }) 44 t.Run("UpdateMfaActive", func(t *testing.T) { testUserStoreUpdateMfaActive(t, ss) }) 45 t.Run("GetRecentlyActiveUsersForTeam", func(t *testing.T) { testUserStoreGetRecentlyActiveUsersForTeam(t, ss) }) 46 t.Run("GetNewUsersForTeam", func(t *testing.T) { testUserStoreGetNewUsersForTeam(t, ss) }) 47 t.Run("Search", func(t *testing.T) { testUserStoreSearch(t, ss) }) 48 t.Run("SearchWithoutTeam", func(t *testing.T) { testUserStoreSearchWithoutTeam(t, ss) }) 49 t.Run("AnalyticsGetInactiveUsersCount", func(t *testing.T) { testUserStoreAnalyticsGetInactiveUsersCount(t, ss) }) 50 t.Run("AnalyticsGetSystemAdminCount", func(t *testing.T) { testUserStoreAnalyticsGetSystemAdminCount(t, ss) }) 51 t.Run("GetProfilesNotInTeam", func(t *testing.T) { testUserStoreGetProfilesNotInTeam(t, ss) }) 52 } 53 54 func testUserStoreSave(t *testing.T, ss store.Store) { 55 teamId := model.NewId() 56 maxUsersPerTeam := 50 57 58 u1 := model.User{} 59 u1.Email = model.NewId() 60 u1.Username = model.NewId() 61 62 if err := (<-ss.User().Save(&u1)).Err; err != nil { 63 t.Fatal("couldn't save user", err) 64 } 65 66 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, maxUsersPerTeam)) 67 68 if err := (<-ss.User().Save(&u1)).Err; err == nil { 69 t.Fatal("shouldn't be able to update user from save") 70 } 71 72 u1.Id = "" 73 if err := (<-ss.User().Save(&u1)).Err; err == nil { 74 t.Fatal("should be unique email") 75 } 76 77 u1.Email = "" 78 if err := (<-ss.User().Save(&u1)).Err; err == nil { 79 t.Fatal("should be unique username") 80 } 81 82 u1.Email = strings.Repeat("0123456789", 20) 83 u1.Username = "" 84 if err := (<-ss.User().Save(&u1)).Err; err == nil { 85 t.Fatal("should be unique username") 86 } 87 88 for i := 0; i < 49; i++ { 89 u1.Id = "" 90 u1.Email = model.NewId() 91 u1.Username = model.NewId() 92 if err := (<-ss.User().Save(&u1)).Err; err != nil { 93 t.Fatal("couldn't save item", err) 94 } 95 96 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, maxUsersPerTeam)) 97 } 98 99 u1.Id = "" 100 u1.Email = model.NewId() 101 u1.Username = model.NewId() 102 if err := (<-ss.User().Save(&u1)).Err; err != nil { 103 t.Fatal("couldn't save item", err) 104 } 105 106 if err := (<-ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, maxUsersPerTeam)).Err; err == nil { 107 t.Fatal("should be the limit") 108 } 109 } 110 111 func testUserStoreUpdate(t *testing.T, ss store.Store) { 112 u1 := &model.User{} 113 u1.Email = model.NewId() 114 store.Must(ss.User().Save(u1)) 115 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 116 117 u2 := &model.User{} 118 u2.Email = model.NewId() 119 u2.AuthService = "ldap" 120 store.Must(ss.User().Save(u2)) 121 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 122 123 time.Sleep(100 * time.Millisecond) 124 125 if err := (<-ss.User().Update(u1, false)).Err; err != nil { 126 t.Fatal(err) 127 } 128 129 u1.Id = "missing" 130 if err := (<-ss.User().Update(u1, false)).Err; err == nil { 131 t.Fatal("Update should have failed because of missing key") 132 } 133 134 u1.Id = model.NewId() 135 if err := (<-ss.User().Update(u1, false)).Err; err == nil { 136 t.Fatal("Update should have faile because id change") 137 } 138 139 u2.Email = model.NewId() 140 if err := (<-ss.User().Update(u2, false)).Err; err == nil { 141 t.Fatal("Update should have failed because you can't modify AD/LDAP fields") 142 } 143 144 u3 := &model.User{} 145 u3.Email = model.NewId() 146 oldEmail := u3.Email 147 u3.AuthService = "gitlab" 148 store.Must(ss.User().Save(u3)) 149 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u3.Id}, -1)) 150 151 u3.Email = model.NewId() 152 if result := <-ss.User().Update(u3, false); result.Err != nil { 153 t.Fatal("Update should not have failed") 154 } else { 155 newUser := result.Data.([2]*model.User)[0] 156 if newUser.Email != oldEmail { 157 t.Fatal("Email should not have been updated as the update is not trusted") 158 } 159 } 160 161 u3.Email = model.NewId() 162 if result := <-ss.User().Update(u3, true); result.Err != nil { 163 t.Fatal("Update should not have failed") 164 } else { 165 newUser := result.Data.([2]*model.User)[0] 166 if newUser.Email == oldEmail { 167 t.Fatal("Email should have been updated as the update is trusted") 168 } 169 } 170 171 if result := <-ss.User().UpdateLastPictureUpdate(u1.Id); result.Err != nil { 172 t.Fatal("Update should not have failed") 173 } 174 } 175 176 func testUserStoreUpdateUpdateAt(t *testing.T, ss store.Store) { 177 u1 := &model.User{} 178 u1.Email = model.NewId() 179 store.Must(ss.User().Save(u1)) 180 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 181 182 time.Sleep(10 * time.Millisecond) 183 184 if err := (<-ss.User().UpdateUpdateAt(u1.Id)).Err; err != nil { 185 t.Fatal(err) 186 } 187 188 if r1 := <-ss.User().Get(u1.Id); r1.Err != nil { 189 t.Fatal(r1.Err) 190 } else { 191 if r1.Data.(*model.User).UpdateAt <= u1.UpdateAt { 192 t.Fatal("UpdateAt not updated correctly") 193 } 194 } 195 196 } 197 198 func testUserStoreUpdateFailedPasswordAttempts(t *testing.T, ss store.Store) { 199 u1 := &model.User{} 200 u1.Email = model.NewId() 201 store.Must(ss.User().Save(u1)) 202 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 203 204 if err := (<-ss.User().UpdateFailedPasswordAttempts(u1.Id, 3)).Err; err != nil { 205 t.Fatal(err) 206 } 207 208 if r1 := <-ss.User().Get(u1.Id); r1.Err != nil { 209 t.Fatal(r1.Err) 210 } else { 211 if r1.Data.(*model.User).FailedAttempts != 3 { 212 t.Fatal("FailedAttempts not updated correctly") 213 } 214 } 215 216 } 217 218 func testUserStoreGet(t *testing.T, ss store.Store) { 219 u1 := &model.User{} 220 u1.Email = model.NewId() 221 store.Must(ss.User().Save(u1)) 222 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 223 224 if r1 := <-ss.User().Get(u1.Id); r1.Err != nil { 225 t.Fatal(r1.Err) 226 } else { 227 if r1.Data.(*model.User).ToJson() != u1.ToJson() { 228 t.Fatal("invalid returned user") 229 } 230 } 231 232 if err := (<-ss.User().Get("")).Err; err == nil { 233 t.Fatal("Missing id should have failed") 234 } 235 } 236 237 func testUserCount(t *testing.T, ss store.Store) { 238 u1 := &model.User{} 239 u1.Email = model.NewId() 240 store.Must(ss.User().Save(u1)) 241 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 242 243 if result := <-ss.User().GetTotalUsersCount(); result.Err != nil { 244 t.Fatal(result.Err) 245 } else { 246 count := result.Data.(int64) 247 if count <= 0 { 248 t.Fatal() 249 } 250 } 251 } 252 253 func testGetAllUsingAuthService(t *testing.T, ss store.Store) { 254 u1 := &model.User{} 255 u1.Email = model.NewId() 256 u1.AuthService = "someservice" 257 store.Must(ss.User().Save(u1)) 258 259 u2 := &model.User{} 260 u2.Email = model.NewId() 261 u2.AuthService = "someservice" 262 store.Must(ss.User().Save(u2)) 263 264 if r1 := <-ss.User().GetAllUsingAuthService(u1.AuthService); r1.Err != nil { 265 t.Fatal(r1.Err) 266 } else { 267 users := r1.Data.([]*model.User) 268 if len(users) < 2 { 269 t.Fatal("invalid returned users") 270 } 271 } 272 } 273 274 func testUserStoreGetAllProfiles(t *testing.T, ss store.Store) { 275 u1 := &model.User{} 276 u1.Email = model.NewId() 277 store.Must(ss.User().Save(u1)) 278 279 u2 := &model.User{} 280 u2.Email = model.NewId() 281 store.Must(ss.User().Save(u2)) 282 283 if r1 := <-ss.User().GetAllProfiles(0, 100); r1.Err != nil { 284 t.Fatal(r1.Err) 285 } else { 286 users := r1.Data.([]*model.User) 287 if len(users) < 2 { 288 t.Fatal("invalid returned users") 289 } 290 } 291 292 if r2 := <-ss.User().GetAllProfiles(0, 1); r2.Err != nil { 293 t.Fatal(r2.Err) 294 } else { 295 users := r2.Data.([]*model.User) 296 if len(users) != 1 { 297 t.Fatal("invalid returned users, limit did not work") 298 } 299 } 300 301 if r2 := <-ss.User().GetAll(); r2.Err != nil { 302 t.Fatal(r2.Err) 303 } else { 304 users := r2.Data.([]*model.User) 305 if len(users) < 2 { 306 t.Fatal("invalid returned users") 307 } 308 } 309 310 etag := "" 311 if r2 := <-ss.User().GetEtagForAllProfiles(); r2.Err != nil { 312 t.Fatal(r2.Err) 313 } else { 314 etag = r2.Data.(string) 315 } 316 317 u3 := &model.User{} 318 u3.Email = model.NewId() 319 store.Must(ss.User().Save(u3)) 320 321 if r2 := <-ss.User().GetEtagForAllProfiles(); r2.Err != nil { 322 t.Fatal(r2.Err) 323 } else { 324 if etag == r2.Data.(string) { 325 t.Fatal("etags should not match") 326 } 327 } 328 } 329 330 func testUserStoreGetProfiles(t *testing.T, ss store.Store) { 331 teamId := model.NewId() 332 333 u1 := &model.User{} 334 u1.Email = model.NewId() 335 store.Must(ss.User().Save(u1)) 336 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 337 338 u2 := &model.User{} 339 u2.Email = model.NewId() 340 store.Must(ss.User().Save(u2)) 341 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 342 343 if r1 := <-ss.User().GetProfiles(teamId, 0, 100); r1.Err != nil { 344 t.Fatal(r1.Err) 345 } else { 346 users := r1.Data.([]*model.User) 347 if len(users) != 2 { 348 t.Fatal("invalid returned users") 349 } 350 351 found := false 352 for _, u := range users { 353 if u.Id == u1.Id { 354 found = true 355 } 356 } 357 358 if !found { 359 t.Fatal("missing user") 360 } 361 } 362 363 if r2 := <-ss.User().GetProfiles("123", 0, 100); r2.Err != nil { 364 t.Fatal(r2.Err) 365 } else { 366 if len(r2.Data.([]*model.User)) != 0 { 367 t.Fatal("should have returned empty map") 368 } 369 } 370 371 etag := "" 372 if r2 := <-ss.User().GetEtagForProfiles(teamId); r2.Err != nil { 373 t.Fatal(r2.Err) 374 } else { 375 etag = r2.Data.(string) 376 } 377 378 u3 := &model.User{} 379 u3.Email = model.NewId() 380 store.Must(ss.User().Save(u3)) 381 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u3.Id}, -1)) 382 383 if r2 := <-ss.User().GetEtagForProfiles(teamId); r2.Err != nil { 384 t.Fatal(r2.Err) 385 } else { 386 if etag == r2.Data.(string) { 387 t.Fatal("etags should not match") 388 } 389 } 390 } 391 392 func testUserStoreGetProfilesInChannel(t *testing.T, ss store.Store) { 393 teamId := model.NewId() 394 395 u1 := &model.User{} 396 u1.Email = model.NewId() 397 store.Must(ss.User().Save(u1)) 398 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 399 400 u2 := &model.User{} 401 u2.Email = model.NewId() 402 store.Must(ss.User().Save(u2)) 403 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 404 405 c1 := model.Channel{} 406 c1.TeamId = teamId 407 c1.DisplayName = "Profiles in channel" 408 c1.Name = "profiles-" + model.NewId() 409 c1.Type = model.CHANNEL_OPEN 410 411 c2 := model.Channel{} 412 c2.TeamId = teamId 413 c2.DisplayName = "Profiles in private" 414 c2.Name = "profiles-" + model.NewId() 415 c2.Type = model.CHANNEL_PRIVATE 416 417 store.Must(ss.Channel().Save(&c1, -1)) 418 store.Must(ss.Channel().Save(&c2, -1)) 419 420 m1 := model.ChannelMember{} 421 m1.ChannelId = c1.Id 422 m1.UserId = u1.Id 423 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 424 425 m2 := model.ChannelMember{} 426 m2.ChannelId = c1.Id 427 m2.UserId = u2.Id 428 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 429 430 m3 := model.ChannelMember{} 431 m3.ChannelId = c2.Id 432 m3.UserId = u1.Id 433 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 434 435 store.Must(ss.Channel().SaveMember(&m1)) 436 store.Must(ss.Channel().SaveMember(&m2)) 437 store.Must(ss.Channel().SaveMember(&m3)) 438 439 if r1 := <-ss.User().GetProfilesInChannel(c1.Id, 0, 100); r1.Err != nil { 440 t.Fatal(r1.Err) 441 } else { 442 users := r1.Data.([]*model.User) 443 if len(users) != 2 { 444 t.Fatal("invalid returned users") 445 } 446 447 found := false 448 for _, u := range users { 449 if u.Id == u1.Id { 450 found = true 451 } 452 } 453 454 if !found { 455 t.Fatal("missing user") 456 } 457 } 458 459 if r2 := <-ss.User().GetProfilesInChannel(c2.Id, 0, 1); r2.Err != nil { 460 t.Fatal(r2.Err) 461 } else { 462 if len(r2.Data.([]*model.User)) != 1 { 463 t.Fatal("should have returned only 1 user") 464 } 465 } 466 } 467 468 func testUserStoreGetProfilesInChannelByStatus(t *testing.T, ss store.Store) { 469 teamId := model.NewId() 470 471 u1 := &model.User{} 472 u1.Email = model.NewId() 473 store.Must(ss.User().Save(u1)) 474 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 475 476 u2 := &model.User{} 477 u2.Email = model.NewId() 478 store.Must(ss.User().Save(u2)) 479 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 480 481 c1 := model.Channel{} 482 c1.TeamId = teamId 483 c1.DisplayName = "Profiles in channel" 484 c1.Name = "profiles-" + model.NewId() 485 c1.Type = model.CHANNEL_OPEN 486 487 c2 := model.Channel{} 488 c2.TeamId = teamId 489 c2.DisplayName = "Profiles in private" 490 c2.Name = "profiles-" + model.NewId() 491 c2.Type = model.CHANNEL_PRIVATE 492 493 store.Must(ss.Channel().Save(&c1, -1)) 494 store.Must(ss.Channel().Save(&c2, -1)) 495 496 m1 := model.ChannelMember{} 497 m1.ChannelId = c1.Id 498 m1.UserId = u1.Id 499 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 500 501 m2 := model.ChannelMember{} 502 m2.ChannelId = c1.Id 503 m2.UserId = u2.Id 504 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 505 506 m3 := model.ChannelMember{} 507 m3.ChannelId = c2.Id 508 m3.UserId = u1.Id 509 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 510 511 store.Must(ss.Channel().SaveMember(&m1)) 512 store.Must(ss.Channel().SaveMember(&m2)) 513 store.Must(ss.Channel().SaveMember(&m3)) 514 515 if r1 := <-ss.User().GetProfilesInChannelByStatus(c1.Id, 0, 100); r1.Err != nil { 516 t.Fatal(r1.Err) 517 } else { 518 users := r1.Data.([]*model.User) 519 if len(users) != 2 { 520 t.Fatal("invalid returned users") 521 } 522 523 found := false 524 for _, u := range users { 525 if u.Id == u1.Id { 526 found = true 527 } 528 } 529 530 if !found { 531 t.Fatal("missing user") 532 } 533 } 534 535 if r2 := <-ss.User().GetProfilesInChannelByStatus(c2.Id, 0, 1); r2.Err != nil { 536 t.Fatal(r2.Err) 537 } else { 538 if len(r2.Data.([]*model.User)) != 1 { 539 t.Fatal("should have returned only 1 user") 540 } 541 } 542 } 543 544 func testUserStoreGetProfilesWithoutTeam(t *testing.T, ss store.Store) { 545 teamId := model.NewId() 546 547 // These usernames need to appear in the first 100 users for this to work 548 549 u1 := &model.User{} 550 u1.Username = "a000000000" + model.NewId() 551 u1.Email = model.NewId() 552 store.Must(ss.User().Save(u1)) 553 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 554 defer ss.User().PermanentDelete(u1.Id) 555 556 u2 := &model.User{} 557 u2.Username = "a000000001" + model.NewId() 558 u2.Email = model.NewId() 559 store.Must(ss.User().Save(u2)) 560 defer ss.User().PermanentDelete(u2.Id) 561 562 if r1 := <-ss.User().GetProfilesWithoutTeam(0, 100); r1.Err != nil { 563 t.Fatal(r1.Err) 564 } else { 565 users := r1.Data.([]*model.User) 566 567 found1 := false 568 found2 := false 569 for _, u := range users { 570 if u.Id == u1.Id { 571 found1 = true 572 } else if u.Id == u2.Id { 573 found2 = true 574 } 575 } 576 577 if found1 { 578 t.Fatal("shouldn't have returned user on team") 579 } else if !found2 { 580 t.Fatal("should've returned user without any teams") 581 } 582 } 583 } 584 585 func testUserStoreGetAllProfilesInChannel(t *testing.T, ss store.Store) { 586 teamId := model.NewId() 587 588 u1 := &model.User{} 589 u1.Email = model.NewId() 590 store.Must(ss.User().Save(u1)) 591 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 592 593 u2 := &model.User{} 594 u2.Email = model.NewId() 595 store.Must(ss.User().Save(u2)) 596 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 597 598 c1 := model.Channel{} 599 c1.TeamId = teamId 600 c1.DisplayName = "Profiles in channel" 601 c1.Name = "profiles-" + model.NewId() 602 c1.Type = model.CHANNEL_OPEN 603 604 c2 := model.Channel{} 605 c2.TeamId = teamId 606 c2.DisplayName = "Profiles in private" 607 c2.Name = "profiles-" + model.NewId() 608 c2.Type = model.CHANNEL_PRIVATE 609 610 store.Must(ss.Channel().Save(&c1, -1)) 611 store.Must(ss.Channel().Save(&c2, -1)) 612 613 m1 := model.ChannelMember{} 614 m1.ChannelId = c1.Id 615 m1.UserId = u1.Id 616 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 617 618 m2 := model.ChannelMember{} 619 m2.ChannelId = c1.Id 620 m2.UserId = u2.Id 621 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 622 623 m3 := model.ChannelMember{} 624 m3.ChannelId = c2.Id 625 m3.UserId = u1.Id 626 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 627 628 store.Must(ss.Channel().SaveMember(&m1)) 629 store.Must(ss.Channel().SaveMember(&m2)) 630 store.Must(ss.Channel().SaveMember(&m3)) 631 632 if r1 := <-ss.User().GetAllProfilesInChannel(c1.Id, false); r1.Err != nil { 633 t.Fatal(r1.Err) 634 } else { 635 users := r1.Data.(map[string]*model.User) 636 if len(users) != 2 { 637 t.Fatal("invalid returned users") 638 } 639 640 if users[u1.Id].Id != u1.Id { 641 t.Fatal("invalid returned user") 642 } 643 } 644 645 if r2 := <-ss.User().GetAllProfilesInChannel(c2.Id, false); r2.Err != nil { 646 t.Fatal(r2.Err) 647 } else { 648 if len(r2.Data.(map[string]*model.User)) != 1 { 649 t.Fatal("should have returned empty map") 650 } 651 } 652 653 if r2 := <-ss.User().GetAllProfilesInChannel(c2.Id, true); r2.Err != nil { 654 t.Fatal(r2.Err) 655 } else { 656 if len(r2.Data.(map[string]*model.User)) != 1 { 657 t.Fatal("should have returned empty map") 658 } 659 } 660 661 if r2 := <-ss.User().GetAllProfilesInChannel(c2.Id, true); r2.Err != nil { 662 t.Fatal(r2.Err) 663 } else { 664 if len(r2.Data.(map[string]*model.User)) != 1 { 665 t.Fatal("should have returned empty map") 666 } 667 } 668 669 ss.User().InvalidateProfilesInChannelCacheByUser(u1.Id) 670 ss.User().InvalidateProfilesInChannelCache(c2.Id) 671 } 672 673 func testUserStoreGetProfilesNotInChannel(t *testing.T, ss store.Store) { 674 teamId := model.NewId() 675 676 u1 := &model.User{} 677 u1.Email = model.NewId() 678 store.Must(ss.User().Save(u1)) 679 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 680 681 u2 := &model.User{} 682 u2.Email = model.NewId() 683 store.Must(ss.User().Save(u2)) 684 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 685 686 c1 := model.Channel{} 687 c1.TeamId = teamId 688 c1.DisplayName = "Profiles in channel" 689 c1.Name = "profiles-" + model.NewId() 690 c1.Type = model.CHANNEL_OPEN 691 692 c2 := model.Channel{} 693 c2.TeamId = teamId 694 c2.DisplayName = "Profiles in private" 695 c2.Name = "profiles-" + model.NewId() 696 c2.Type = model.CHANNEL_PRIVATE 697 698 store.Must(ss.Channel().Save(&c1, -1)) 699 store.Must(ss.Channel().Save(&c2, -1)) 700 701 if r1 := <-ss.User().GetProfilesNotInChannel(teamId, c1.Id, 0, 100); r1.Err != nil { 702 t.Fatal(r1.Err) 703 } else { 704 users := r1.Data.([]*model.User) 705 if len(users) != 2 { 706 t.Fatal("invalid returned users") 707 } 708 709 found := false 710 for _, u := range users { 711 if u.Id == u1.Id { 712 found = true 713 } 714 } 715 716 if !found { 717 t.Fatal("missing user") 718 } 719 } 720 721 if r2 := <-ss.User().GetProfilesNotInChannel(teamId, c2.Id, 0, 100); r2.Err != nil { 722 t.Fatal(r2.Err) 723 } else { 724 if len(r2.Data.([]*model.User)) != 2 { 725 t.Fatal("invalid returned users") 726 } 727 } 728 729 m1 := model.ChannelMember{} 730 m1.ChannelId = c1.Id 731 m1.UserId = u1.Id 732 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 733 734 m2 := model.ChannelMember{} 735 m2.ChannelId = c1.Id 736 m2.UserId = u2.Id 737 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 738 739 m3 := model.ChannelMember{} 740 m3.ChannelId = c2.Id 741 m3.UserId = u1.Id 742 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 743 744 store.Must(ss.Channel().SaveMember(&m1)) 745 store.Must(ss.Channel().SaveMember(&m2)) 746 store.Must(ss.Channel().SaveMember(&m3)) 747 748 if r1 := <-ss.User().GetProfilesNotInChannel(teamId, c1.Id, 0, 100); r1.Err != nil { 749 t.Fatal(r1.Err) 750 } else { 751 users := r1.Data.([]*model.User) 752 if len(users) != 0 { 753 t.Fatal("invalid returned users") 754 } 755 } 756 757 if r2 := <-ss.User().GetProfilesNotInChannel(teamId, c2.Id, 0, 100); r2.Err != nil { 758 t.Fatal(r2.Err) 759 } else { 760 if len(r2.Data.([]*model.User)) != 1 { 761 t.Fatal("should have had 1 user not in channel") 762 } 763 } 764 } 765 766 func testUserStoreGetProfilesByIds(t *testing.T, ss store.Store) { 767 teamId := model.NewId() 768 769 u1 := &model.User{} 770 u1.Email = model.NewId() 771 store.Must(ss.User().Save(u1)) 772 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 773 774 u2 := &model.User{} 775 u2.Email = model.NewId() 776 store.Must(ss.User().Save(u2)) 777 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 778 779 if r1 := <-ss.User().GetProfileByIds([]string{u1.Id}, false); r1.Err != nil { 780 t.Fatal(r1.Err) 781 } else { 782 users := r1.Data.([]*model.User) 783 if len(users) != 1 { 784 t.Fatal("invalid returned users") 785 } 786 787 found := false 788 for _, u := range users { 789 if u.Id == u1.Id { 790 found = true 791 } 792 } 793 794 if !found { 795 t.Fatal("missing user") 796 } 797 } 798 799 if r1 := <-ss.User().GetProfileByIds([]string{u1.Id}, true); r1.Err != nil { 800 t.Fatal(r1.Err) 801 } else { 802 users := r1.Data.([]*model.User) 803 if len(users) != 1 { 804 t.Fatal("invalid returned users") 805 } 806 807 found := false 808 for _, u := range users { 809 if u.Id == u1.Id { 810 found = true 811 } 812 } 813 814 if !found { 815 t.Fatal("missing user") 816 } 817 } 818 819 if r1 := <-ss.User().GetProfileByIds([]string{u1.Id, u2.Id}, true); r1.Err != nil { 820 t.Fatal(r1.Err) 821 } else { 822 users := r1.Data.([]*model.User) 823 if len(users) != 2 { 824 t.Fatal("invalid returned users") 825 } 826 827 found := false 828 for _, u := range users { 829 if u.Id == u1.Id { 830 found = true 831 } 832 } 833 834 if !found { 835 t.Fatal("missing user") 836 } 837 } 838 839 if r1 := <-ss.User().GetProfileByIds([]string{u1.Id, u2.Id}, true); r1.Err != nil { 840 t.Fatal(r1.Err) 841 } else { 842 users := r1.Data.([]*model.User) 843 if len(users) != 2 { 844 t.Fatal("invalid returned users") 845 } 846 847 found := false 848 for _, u := range users { 849 if u.Id == u1.Id { 850 found = true 851 } 852 } 853 854 if !found { 855 t.Fatal("missing user") 856 } 857 } 858 859 if r1 := <-ss.User().GetProfileByIds([]string{u1.Id, u2.Id}, false); r1.Err != nil { 860 t.Fatal(r1.Err) 861 } else { 862 users := r1.Data.([]*model.User) 863 if len(users) != 2 { 864 t.Fatal("invalid returned users") 865 } 866 867 found := false 868 for _, u := range users { 869 if u.Id == u1.Id { 870 found = true 871 } 872 } 873 874 if !found { 875 t.Fatal("missing user") 876 } 877 } 878 879 if r1 := <-ss.User().GetProfileByIds([]string{u1.Id}, false); r1.Err != nil { 880 t.Fatal(r1.Err) 881 } else { 882 users := r1.Data.([]*model.User) 883 if len(users) != 1 { 884 t.Fatal("invalid returned users") 885 } 886 887 found := false 888 for _, u := range users { 889 if u.Id == u1.Id { 890 found = true 891 } 892 } 893 894 if !found { 895 t.Fatal("missing user") 896 } 897 } 898 899 if r2 := <-ss.User().GetProfiles("123", 0, 100); r2.Err != nil { 900 t.Fatal(r2.Err) 901 } else { 902 if len(r2.Data.([]*model.User)) != 0 { 903 t.Fatal("should have returned empty array") 904 } 905 } 906 } 907 908 func testUserStoreGetProfilesByUsernames(t *testing.T, ss store.Store) { 909 teamId := model.NewId() 910 911 u1 := &model.User{} 912 u1.Email = model.NewId() 913 u1.Username = "username1" + model.NewId() 914 store.Must(ss.User().Save(u1)) 915 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 916 917 u2 := &model.User{} 918 u2.Email = model.NewId() 919 u2.Username = "username2" + model.NewId() 920 store.Must(ss.User().Save(u2)) 921 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 922 923 if r1 := <-ss.User().GetProfilesByUsernames([]string{u1.Username, u2.Username}, teamId); r1.Err != nil { 924 t.Fatal(r1.Err) 925 } else { 926 users := r1.Data.([]*model.User) 927 if len(users) != 2 { 928 t.Fatal("invalid returned users") 929 } 930 931 if users[0].Id != u1.Id && users[1].Id != u1.Id { 932 t.Fatal("invalid returned user 1") 933 } 934 935 if users[0].Id != u2.Id && users[1].Id != u2.Id { 936 t.Fatal("invalid returned user 2") 937 } 938 } 939 940 if r1 := <-ss.User().GetProfilesByUsernames([]string{u1.Username}, teamId); r1.Err != nil { 941 t.Fatal(r1.Err) 942 } else { 943 users := r1.Data.([]*model.User) 944 if len(users) != 1 { 945 t.Fatal("invalid returned users") 946 } 947 948 if users[0].Id != u1.Id { 949 t.Fatal("invalid returned user") 950 } 951 } 952 953 team2Id := model.NewId() 954 955 u3 := &model.User{} 956 u3.Email = model.NewId() 957 u3.Username = "username3" + model.NewId() 958 store.Must(ss.User().Save(u3)) 959 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: team2Id, UserId: u3.Id}, -1)) 960 961 if r1 := <-ss.User().GetProfilesByUsernames([]string{u1.Username, u3.Username}, ""); r1.Err != nil { 962 t.Fatal(r1.Err) 963 } else { 964 users := r1.Data.([]*model.User) 965 if len(users) != 2 { 966 t.Fatal("invalid returned users") 967 } 968 969 if users[0].Id != u1.Id && users[1].Id != u1.Id { 970 t.Fatal("invalid returned user 1") 971 } 972 973 if users[0].Id != u3.Id && users[1].Id != u3.Id { 974 t.Fatal("invalid returned user 3") 975 } 976 } 977 978 if r1 := <-ss.User().GetProfilesByUsernames([]string{u1.Username, u3.Username}, teamId); r1.Err != nil { 979 t.Fatal(r1.Err) 980 } else { 981 users := r1.Data.([]*model.User) 982 if len(users) != 1 { 983 t.Fatal("invalid returned users") 984 } 985 986 if users[0].Id != u1.Id { 987 t.Fatal("invalid returned user") 988 } 989 } 990 } 991 992 func testUserStoreGetSystemAdminProfiles(t *testing.T, ss store.Store) { 993 teamId := model.NewId() 994 995 u1 := &model.User{} 996 u1.Email = model.NewId() 997 u1.Roles = model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID 998 store.Must(ss.User().Save(u1)) 999 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1000 1001 u2 := &model.User{} 1002 u2.Email = model.NewId() 1003 store.Must(ss.User().Save(u2)) 1004 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 1005 1006 if r1 := <-ss.User().GetSystemAdminProfiles(); r1.Err != nil { 1007 t.Fatal(r1.Err) 1008 } else { 1009 users := r1.Data.(map[string]*model.User) 1010 if len(users) <= 0 { 1011 t.Fatal("invalid returned system admin users") 1012 } 1013 } 1014 } 1015 1016 func testUserStoreGetByEmail(t *testing.T, ss store.Store) { 1017 teamid := model.NewId() 1018 1019 u1 := &model.User{} 1020 u1.Email = model.NewId() 1021 store.Must(ss.User().Save(u1)) 1022 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamid, UserId: u1.Id}, -1)) 1023 1024 if err := (<-ss.User().GetByEmail(u1.Email)).Err; err != nil { 1025 t.Fatal(err) 1026 } 1027 1028 if err := (<-ss.User().GetByEmail("")).Err; err == nil { 1029 t.Fatal("Should have failed because of missing email") 1030 } 1031 } 1032 1033 func testUserStoreGetByAuthData(t *testing.T, ss store.Store) { 1034 teamId := model.NewId() 1035 1036 auth := "123" + model.NewId() 1037 1038 u1 := &model.User{} 1039 u1.Email = model.NewId() 1040 u1.AuthData = &auth 1041 u1.AuthService = "service" 1042 store.Must(ss.User().Save(u1)) 1043 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1044 1045 if err := (<-ss.User().GetByAuth(u1.AuthData, u1.AuthService)).Err; err != nil { 1046 t.Fatal(err) 1047 } 1048 1049 rauth := "" 1050 if err := (<-ss.User().GetByAuth(&rauth, "")).Err; err == nil { 1051 t.Fatal("Should have failed because of missing auth data") 1052 } 1053 } 1054 1055 func testUserStoreGetByUsername(t *testing.T, ss store.Store) { 1056 teamId := model.NewId() 1057 1058 u1 := &model.User{} 1059 u1.Email = model.NewId() 1060 u1.Username = model.NewId() 1061 store.Must(ss.User().Save(u1)) 1062 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1063 1064 if err := (<-ss.User().GetByUsername(u1.Username)).Err; err != nil { 1065 t.Fatal(err) 1066 } 1067 1068 if err := (<-ss.User().GetByUsername("")).Err; err == nil { 1069 t.Fatal("Should have failed because of missing username") 1070 } 1071 } 1072 1073 func testUserStoreGetForLogin(t *testing.T, ss store.Store) { 1074 auth := model.NewId() 1075 1076 u1 := &model.User{ 1077 Email: model.NewId(), 1078 Username: model.NewId(), 1079 AuthService: model.USER_AUTH_SERVICE_GITLAB, 1080 AuthData: &auth, 1081 } 1082 store.Must(ss.User().Save(u1)) 1083 1084 auth2 := model.NewId() 1085 1086 u2 := &model.User{ 1087 Email: model.NewId(), 1088 Username: model.NewId(), 1089 AuthService: model.USER_AUTH_SERVICE_LDAP, 1090 AuthData: &auth2, 1091 } 1092 store.Must(ss.User().Save(u2)) 1093 1094 if result := <-ss.User().GetForLogin(u1.Username, true, true); result.Err != nil { 1095 t.Fatal("Should have gotten user by username", result.Err) 1096 } else if result.Data.(*model.User).Id != u1.Id { 1097 t.Fatal("Should have gotten user1 by username") 1098 } 1099 1100 if result := <-ss.User().GetForLogin(u1.Email, true, true); result.Err != nil { 1101 t.Fatal("Should have gotten user by email", result.Err) 1102 } else if result.Data.(*model.User).Id != u1.Id { 1103 t.Fatal("Should have gotten user1 by email") 1104 } 1105 1106 // prevent getting user when different login methods are disabled 1107 if result := <-ss.User().GetForLogin(u1.Username, false, true); result.Err == nil { 1108 t.Fatal("Should have failed to get user1 by username") 1109 } 1110 1111 if result := <-ss.User().GetForLogin(u1.Email, true, false); result.Err == nil { 1112 t.Fatal("Should have failed to get user1 by email") 1113 } 1114 } 1115 1116 func testUserStoreUpdatePassword(t *testing.T, ss store.Store) { 1117 teamId := model.NewId() 1118 1119 u1 := &model.User{} 1120 u1.Email = model.NewId() 1121 store.Must(ss.User().Save(u1)) 1122 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1123 1124 hashedPassword := model.HashPassword("newpwd") 1125 1126 if err := (<-ss.User().UpdatePassword(u1.Id, hashedPassword)).Err; err != nil { 1127 t.Fatal(err) 1128 } 1129 1130 if r1 := <-ss.User().GetByEmail(u1.Email); r1.Err != nil { 1131 t.Fatal(r1.Err) 1132 } else { 1133 user := r1.Data.(*model.User) 1134 if user.Password != hashedPassword { 1135 t.Fatal("Password was not updated correctly") 1136 } 1137 } 1138 } 1139 1140 func testUserStoreDelete(t *testing.T, ss store.Store) { 1141 u1 := &model.User{} 1142 u1.Email = model.NewId() 1143 store.Must(ss.User().Save(u1)) 1144 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 1145 1146 if err := (<-ss.User().PermanentDelete(u1.Id)).Err; err != nil { 1147 t.Fatal(err) 1148 } 1149 } 1150 1151 func testUserStoreUpdateAuthData(t *testing.T, ss store.Store) { 1152 teamId := model.NewId() 1153 1154 u1 := &model.User{} 1155 u1.Email = model.NewId() 1156 store.Must(ss.User().Save(u1)) 1157 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1158 1159 service := "someservice" 1160 authData := model.NewId() 1161 1162 if err := (<-ss.User().UpdateAuthData(u1.Id, service, &authData, "", true)).Err; err != nil { 1163 t.Fatal(err) 1164 } 1165 1166 if r1 := <-ss.User().GetByEmail(u1.Email); r1.Err != nil { 1167 t.Fatal(r1.Err) 1168 } else { 1169 user := r1.Data.(*model.User) 1170 if user.AuthService != service { 1171 t.Fatal("AuthService was not updated correctly") 1172 } 1173 if *user.AuthData != authData { 1174 t.Fatal("AuthData was not updated correctly") 1175 } 1176 if user.Password != "" { 1177 t.Fatal("Password was not cleared properly") 1178 } 1179 } 1180 } 1181 1182 func testUserUnreadCount(t *testing.T, ss store.Store) { 1183 teamId := model.NewId() 1184 1185 c1 := model.Channel{} 1186 c1.TeamId = teamId 1187 c1.DisplayName = "Unread Messages" 1188 c1.Name = "unread-messages-" + model.NewId() 1189 c1.Type = model.CHANNEL_OPEN 1190 1191 c2 := model.Channel{} 1192 c2.TeamId = teamId 1193 c2.DisplayName = "Unread Direct" 1194 c2.Name = "unread-direct-" + model.NewId() 1195 c2.Type = model.CHANNEL_DIRECT 1196 1197 u1 := &model.User{} 1198 u1.Username = "user1" + model.NewId() 1199 u1.Email = model.NewId() 1200 store.Must(ss.User().Save(u1)) 1201 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1202 1203 u2 := &model.User{} 1204 u2.Email = model.NewId() 1205 u2.Username = "user2" + model.NewId() 1206 store.Must(ss.User().Save(u2)) 1207 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 1208 1209 if err := (<-ss.Channel().Save(&c1, -1)).Err; err != nil { 1210 t.Fatal("couldn't save item", err) 1211 } 1212 1213 m1 := model.ChannelMember{} 1214 m1.ChannelId = c1.Id 1215 m1.UserId = u1.Id 1216 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1217 1218 m2 := model.ChannelMember{} 1219 m2.ChannelId = c1.Id 1220 m2.UserId = u2.Id 1221 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1222 1223 store.Must(ss.Channel().SaveMember(&m1)) 1224 store.Must(ss.Channel().SaveMember(&m2)) 1225 1226 m1.ChannelId = c2.Id 1227 m2.ChannelId = c2.Id 1228 1229 if err := (<-ss.Channel().SaveDirectChannel(&c2, &m1, &m2)).Err; err != nil { 1230 t.Fatal("couldn't save direct channel", err) 1231 } 1232 1233 p1 := model.Post{} 1234 p1.ChannelId = c1.Id 1235 p1.UserId = u1.Id 1236 p1.Message = "this is a message for @" + u2.Username 1237 1238 // Post one message with mention to open channel 1239 store.Must(ss.Post().Save(&p1)) 1240 store.Must(ss.Channel().IncrementMentionCount(c1.Id, u2.Id)) 1241 1242 // Post 2 messages without mention to direct channel 1243 p2 := model.Post{} 1244 p2.ChannelId = c2.Id 1245 p2.UserId = u1.Id 1246 p2.Message = "first message" 1247 store.Must(ss.Post().Save(&p2)) 1248 store.Must(ss.Channel().IncrementMentionCount(c2.Id, u2.Id)) 1249 1250 p3 := model.Post{} 1251 p3.ChannelId = c2.Id 1252 p3.UserId = u1.Id 1253 p3.Message = "second message" 1254 store.Must(ss.Post().Save(&p3)) 1255 store.Must(ss.Channel().IncrementMentionCount(c2.Id, u2.Id)) 1256 1257 badge := (<-ss.User().GetUnreadCount(u2.Id)).Data.(int64) 1258 if badge != 3 { 1259 t.Fatal("should have 3 unread messages") 1260 } 1261 1262 badge = (<-ss.User().GetUnreadCountForChannel(u2.Id, c1.Id)).Data.(int64) 1263 if badge != 1 { 1264 t.Fatal("should have 1 unread messages for that channel") 1265 } 1266 1267 badge = (<-ss.User().GetUnreadCountForChannel(u2.Id, c2.Id)).Data.(int64) 1268 if badge != 2 { 1269 t.Fatal("should have 2 unread messages for that channel") 1270 } 1271 } 1272 1273 func testUserStoreUpdateMfaSecret(t *testing.T, ss store.Store) { 1274 u1 := model.User{} 1275 u1.Email = model.NewId() 1276 store.Must(ss.User().Save(&u1)) 1277 1278 time.Sleep(100 * time.Millisecond) 1279 1280 if err := (<-ss.User().UpdateMfaSecret(u1.Id, "12345")).Err; err != nil { 1281 t.Fatal(err) 1282 } 1283 1284 // should pass, no update will occur though 1285 if err := (<-ss.User().UpdateMfaSecret("junk", "12345")).Err; err != nil { 1286 t.Fatal(err) 1287 } 1288 } 1289 1290 func testUserStoreUpdateMfaActive(t *testing.T, ss store.Store) { 1291 u1 := model.User{} 1292 u1.Email = model.NewId() 1293 store.Must(ss.User().Save(&u1)) 1294 1295 time.Sleep(100 * time.Millisecond) 1296 1297 if err := (<-ss.User().UpdateMfaActive(u1.Id, true)).Err; err != nil { 1298 t.Fatal(err) 1299 } 1300 1301 if err := (<-ss.User().UpdateMfaActive(u1.Id, false)).Err; err != nil { 1302 t.Fatal(err) 1303 } 1304 1305 // should pass, no update will occur though 1306 if err := (<-ss.User().UpdateMfaActive("junk", true)).Err; err != nil { 1307 t.Fatal(err) 1308 } 1309 } 1310 1311 func testUserStoreGetRecentlyActiveUsersForTeam(t *testing.T, ss store.Store) { 1312 u1 := &model.User{} 1313 u1.Email = model.NewId() 1314 store.Must(ss.User().Save(u1)) 1315 store.Must(ss.Status().SaveOrUpdate(&model.Status{UserId: u1.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""})) 1316 tid := model.NewId() 1317 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u1.Id}, -1)) 1318 1319 if r1 := <-ss.User().GetRecentlyActiveUsersForTeam(tid, 0, 100); r1.Err != nil { 1320 t.Fatal(r1.Err) 1321 } 1322 } 1323 1324 func testUserStoreGetNewUsersForTeam(t *testing.T, ss store.Store) { 1325 u1 := &model.User{} 1326 u1.Email = model.NewId() 1327 store.Must(ss.User().Save(u1)) 1328 store.Must(ss.Status().SaveOrUpdate(&model.Status{UserId: u1.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""})) 1329 tid := model.NewId() 1330 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u1.Id}, -1)) 1331 1332 if r1 := <-ss.User().GetNewUsersForTeam(tid, 0, 100); r1.Err != nil { 1333 t.Fatal(r1.Err) 1334 } 1335 } 1336 1337 func testUserStoreSearch(t *testing.T, ss store.Store) { 1338 u1 := &model.User{} 1339 u1.Username = "jimbo" + model.NewId() 1340 u1.FirstName = "Tim" 1341 u1.LastName = "Bill" 1342 u1.Nickname = "Rob" 1343 u1.Email = "harold" + model.NewId() + "@simulator.amazonses.com" 1344 store.Must(ss.User().Save(u1)) 1345 1346 u2 := &model.User{} 1347 u2.Username = "jim-bobby" + model.NewId() 1348 u2.Email = model.NewId() 1349 store.Must(ss.User().Save(u2)) 1350 1351 u3 := &model.User{} 1352 u3.Username = "jimbo" + model.NewId() 1353 u3.Email = model.NewId() 1354 u3.DeleteAt = 1 1355 store.Must(ss.User().Save(u3)) 1356 1357 u5 := &model.User{} 1358 u5.Username = "yu" + model.NewId() 1359 u5.FirstName = "En" 1360 u5.LastName = "Yu" 1361 u5.Nickname = "enyu" 1362 u5.Email = model.NewId() + "@simulator.amazonses.com" 1363 store.Must(ss.User().Save(u5)) 1364 1365 u6 := &model.User{} 1366 u6.Username = "underscore" + model.NewId() 1367 u6.FirstName = "Du_" 1368 u6.LastName = "_DE" 1369 u6.Nickname = "lodash" 1370 u6.Email = model.NewId() + "@simulator.amazonses.com" 1371 store.Must(ss.User().Save(u6)) 1372 1373 tid := model.NewId() 1374 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u1.Id}, -1)) 1375 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u2.Id}, -1)) 1376 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u3.Id}, -1)) 1377 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u5.Id}, -1)) 1378 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u6.Id}, -1)) 1379 1380 searchOptions := map[string]bool{} 1381 searchOptions[store.USER_SEARCH_OPTION_NAMES_ONLY] = true 1382 1383 if r1 := <-ss.User().Search(tid, "jimb", searchOptions); r1.Err != nil { 1384 t.Fatal(r1.Err) 1385 } else { 1386 profiles := r1.Data.([]*model.User) 1387 found1 := false 1388 found2 := false 1389 for _, profile := range profiles { 1390 if profile.Id == u1.Id { 1391 found1 = true 1392 } 1393 1394 if profile.Id == u3.Id { 1395 found2 = true 1396 } 1397 } 1398 1399 if !found1 { 1400 t.Fatal("should have found user") 1401 } 1402 1403 if found2 { 1404 t.Fatal("should not have found inactive user") 1405 } 1406 } 1407 1408 if r1 := <-ss.User().Search(tid, "en", searchOptions); r1.Err != nil { 1409 t.Fatal(r1.Err) 1410 } else { 1411 profiles := r1.Data.([]*model.User) 1412 found1 := false 1413 for _, profile := range profiles { 1414 if profile.Id == u5.Id { 1415 found1 = true 1416 } 1417 } 1418 1419 if !found1 { 1420 t.Fatal("should have found user") 1421 } 1422 } 1423 1424 searchOptions[store.USER_SEARCH_OPTION_NAMES_ONLY] = false 1425 1426 if r1 := <-ss.User().Search(tid, u1.Email, searchOptions); r1.Err != nil { 1427 t.Fatal(r1.Err) 1428 } else { 1429 profiles := r1.Data.([]*model.User) 1430 found1 := false 1431 for _, profile := range profiles { 1432 if profile.Id == u1.Id { 1433 found1 = true 1434 } 1435 } 1436 1437 if !found1 { 1438 t.Fatal("should have found user") 1439 } 1440 } 1441 1442 searchOptions[store.USER_SEARCH_OPTION_NAMES_ONLY] = true 1443 1444 // * should be treated as a space 1445 if r1 := <-ss.User().Search(tid, "jimb*", searchOptions); r1.Err != nil { 1446 t.Fatal(r1.Err) 1447 } else { 1448 profiles := r1.Data.([]*model.User) 1449 found1 := false 1450 found2 := false 1451 for _, profile := range profiles { 1452 if profile.Id == u1.Id { 1453 found1 = true 1454 } 1455 1456 if profile.Id == u3.Id { 1457 found2 = true 1458 } 1459 } 1460 1461 if !found1 { 1462 t.Fatal("should have found user") 1463 } 1464 1465 if found2 { 1466 t.Fatal("should not have found inactive user") 1467 } 1468 } 1469 1470 if r1 := <-ss.User().Search(tid, "harol", searchOptions); r1.Err != nil { 1471 t.Fatal(r1.Err) 1472 } else { 1473 profiles := r1.Data.([]*model.User) 1474 found1 := false 1475 for _, profile := range profiles { 1476 if profile.Id == u1.Id { 1477 found1 = true 1478 } 1479 } 1480 1481 if found1 { 1482 t.Fatal("should not have found user") 1483 } 1484 } 1485 1486 // % should be escaped and searched for. 1487 if r1 := <-ss.User().Search(tid, "h%", searchOptions); r1.Err != nil { 1488 t.Fatal(r1.Err) 1489 } else { 1490 profiles := r1.Data.([]*model.User) 1491 if len(profiles) != 0 { 1492 t.Fatal("shouldn't have found anything") 1493 } 1494 } 1495 1496 // "_" should be properly escaped and searched for. 1497 if r1 := <-ss.User().Search(tid, "h_", searchOptions); r1.Err != nil { 1498 t.Fatal(r1.Err) 1499 } else { 1500 profiles := r1.Data.([]*model.User) 1501 if len(profiles) != 0 { 1502 t.Fatal("shouldn't have found anything") 1503 } 1504 } 1505 if r1 := <-ss.User().Search(tid, "Du_", searchOptions); r1.Err != nil { 1506 t.Fatal(r1.Err) 1507 } else { 1508 profiles := r1.Data.([]*model.User) 1509 found6 := false 1510 for _, profile := range profiles { 1511 if profile.Id == u6.Id { 1512 found6 = true 1513 } 1514 } 1515 1516 if !found6 { 1517 t.Fatal("should have found user") 1518 } 1519 } 1520 if r1 := <-ss.User().Search(tid, "_dE", searchOptions); r1.Err != nil { 1521 t.Fatal(r1.Err) 1522 } else { 1523 profiles := r1.Data.([]*model.User) 1524 found6 := false 1525 for _, profile := range profiles { 1526 if profile.Id == u6.Id { 1527 found6 = true 1528 } 1529 } 1530 1531 if !found6 { 1532 t.Fatal("should have found user") 1533 } 1534 } 1535 1536 searchOptions[store.USER_SEARCH_OPTION_ALLOW_INACTIVE] = true 1537 1538 if r1 := <-ss.User().Search(tid, "jimb", searchOptions); r1.Err != nil { 1539 t.Fatal(r1.Err) 1540 } else { 1541 profiles := r1.Data.([]*model.User) 1542 found1 := false 1543 found2 := false 1544 for _, profile := range profiles { 1545 if profile.Id == u1.Id { 1546 found1 = true 1547 } 1548 1549 if profile.Id == u3.Id { 1550 found2 = true 1551 } 1552 } 1553 1554 if !found1 { 1555 t.Fatal("should have found user") 1556 } 1557 1558 if !found2 { 1559 t.Fatal("should have found inactive user") 1560 } 1561 } 1562 1563 searchOptions[store.USER_SEARCH_OPTION_ALLOW_INACTIVE] = false 1564 1565 if r1 := <-ss.User().Search(tid, "jimb", searchOptions); r1.Err != nil { 1566 t.Fatal(r1.Err) 1567 } else { 1568 profiles := r1.Data.([]*model.User) 1569 found := false 1570 for _, profile := range profiles { 1571 if profile.Id == u1.Id { 1572 found = true 1573 break 1574 } 1575 } 1576 1577 if !found { 1578 t.Fatal("should have found user") 1579 } 1580 } 1581 1582 if r1 := <-ss.User().Search("", "jimb", searchOptions); r1.Err != nil { 1583 t.Fatal(r1.Err) 1584 } else { 1585 profiles := r1.Data.([]*model.User) 1586 found := false 1587 for _, profile := range profiles { 1588 if profile.Id == u1.Id { 1589 found = true 1590 break 1591 } 1592 } 1593 1594 if !found { 1595 t.Fatal("should have found user") 1596 } 1597 } 1598 1599 if r1 := <-ss.User().Search("", "jim-bobb", searchOptions); r1.Err != nil { 1600 t.Fatal(r1.Err) 1601 } else { 1602 profiles := r1.Data.([]*model.User) 1603 found := false 1604 for _, profile := range profiles { 1605 t.Log(profile.Username) 1606 if profile.Id == u2.Id { 1607 found = true 1608 break 1609 } 1610 } 1611 1612 if !found { 1613 t.Fatal("should have found user") 1614 } 1615 } 1616 1617 if r1 := <-ss.User().Search(tid, "", searchOptions); r1.Err != nil { 1618 t.Fatal(r1.Err) 1619 } 1620 1621 c1 := model.Channel{} 1622 c1.TeamId = tid 1623 c1.DisplayName = "NameName" 1624 c1.Name = "zz" + model.NewId() + "b" 1625 c1.Type = model.CHANNEL_OPEN 1626 c1 = *store.Must(ss.Channel().Save(&c1, -1)).(*model.Channel) 1627 1628 if r1 := <-ss.User().SearchNotInChannel(tid, c1.Id, "jimb", searchOptions); r1.Err != nil { 1629 t.Fatal(r1.Err) 1630 } else { 1631 profiles := r1.Data.([]*model.User) 1632 found := false 1633 for _, profile := range profiles { 1634 if profile.Id == u1.Id { 1635 found = true 1636 break 1637 } 1638 } 1639 1640 if !found { 1641 t.Fatal("should have found user") 1642 } 1643 } 1644 1645 if r1 := <-ss.User().SearchNotInChannel("", c1.Id, "jimb", searchOptions); r1.Err != nil { 1646 t.Fatal(r1.Err) 1647 } else { 1648 profiles := r1.Data.([]*model.User) 1649 found := false 1650 for _, profile := range profiles { 1651 if profile.Id == u1.Id { 1652 found = true 1653 break 1654 } 1655 } 1656 1657 if !found { 1658 t.Fatal("should have found user") 1659 } 1660 } 1661 1662 if r1 := <-ss.User().SearchNotInChannel("junk", c1.Id, "jimb", searchOptions); r1.Err != nil { 1663 t.Fatal(r1.Err) 1664 } else { 1665 profiles := r1.Data.([]*model.User) 1666 found := false 1667 for _, profile := range profiles { 1668 if profile.Id == u1.Id { 1669 found = true 1670 break 1671 } 1672 } 1673 1674 if found { 1675 t.Fatal("should not have found user") 1676 } 1677 } 1678 1679 if r1 := <-ss.User().SearchInChannel(c1.Id, "jimb", searchOptions); r1.Err != nil { 1680 t.Fatal(r1.Err) 1681 } else { 1682 profiles := r1.Data.([]*model.User) 1683 found := false 1684 for _, profile := range profiles { 1685 if profile.Id == u1.Id { 1686 found = true 1687 break 1688 } 1689 } 1690 1691 if found { 1692 t.Fatal("should not have found user") 1693 } 1694 } 1695 1696 store.Must(ss.Channel().SaveMember(&model.ChannelMember{ChannelId: c1.Id, UserId: u1.Id, NotifyProps: model.GetDefaultChannelNotifyProps()})) 1697 1698 if r1 := <-ss.User().SearchInChannel(c1.Id, "jimb", searchOptions); r1.Err != nil { 1699 t.Fatal(r1.Err) 1700 } else { 1701 profiles := r1.Data.([]*model.User) 1702 found := false 1703 for _, profile := range profiles { 1704 if profile.Id == u1.Id { 1705 found = true 1706 break 1707 } 1708 } 1709 1710 if !found { 1711 t.Fatal("should have found user") 1712 } 1713 } 1714 1715 searchOptions = map[string]bool{} 1716 1717 if r1 := <-ss.User().Search(tid, "harol", searchOptions); r1.Err != nil { 1718 t.Fatal(r1.Err) 1719 } else { 1720 profiles := r1.Data.([]*model.User) 1721 found1 := false 1722 for _, profile := range profiles { 1723 if profile.Id == u1.Id { 1724 found1 = true 1725 } 1726 } 1727 1728 if !found1 { 1729 t.Fatal("should have found user") 1730 } 1731 } 1732 1733 if r1 := <-ss.User().Search(tid, "Tim", searchOptions); r1.Err != nil { 1734 t.Fatal(r1.Err) 1735 } else { 1736 profiles := r1.Data.([]*model.User) 1737 found := false 1738 for _, profile := range profiles { 1739 if profile.Id == u1.Id { 1740 found = true 1741 break 1742 } 1743 } 1744 1745 if !found { 1746 t.Fatal("should have found user") 1747 } 1748 } 1749 1750 if r1 := <-ss.User().Search(tid, "Bill", searchOptions); r1.Err != nil { 1751 t.Fatal(r1.Err) 1752 } else { 1753 profiles := r1.Data.([]*model.User) 1754 found := false 1755 for _, profile := range profiles { 1756 if profile.Id == u1.Id { 1757 found = true 1758 break 1759 } 1760 } 1761 1762 if !found { 1763 t.Fatal("should have found user") 1764 } 1765 } 1766 1767 if r1 := <-ss.User().Search(tid, "Rob", searchOptions); r1.Err != nil { 1768 t.Fatal(r1.Err) 1769 } else { 1770 profiles := r1.Data.([]*model.User) 1771 found := false 1772 for _, profile := range profiles { 1773 if profile.Id == u1.Id { 1774 found = true 1775 break 1776 } 1777 } 1778 1779 if !found { 1780 t.Fatal("should have found user") 1781 } 1782 } 1783 1784 // Search Users not in Team. 1785 u4 := &model.User{} 1786 u4.Username = "simon" + model.NewId() 1787 u4.Email = model.NewId() 1788 u4.DeleteAt = 0 1789 store.Must(ss.User().Save(u4)) 1790 1791 if r1 := <-ss.User().SearchNotInTeam(tid, "simo", searchOptions); r1.Err != nil { 1792 t.Fatal(r1.Err) 1793 } else { 1794 profiles := r1.Data.([]*model.User) 1795 found := false 1796 for _, profile := range profiles { 1797 if profile.Id == u4.Id { 1798 found = true 1799 break 1800 } 1801 } 1802 1803 if !found { 1804 t.Fatal("should have found user") 1805 } 1806 } 1807 1808 if r1 := <-ss.User().SearchNotInTeam(tid, "jimb", searchOptions); r1.Err != nil { 1809 t.Fatal(r1.Err) 1810 } else { 1811 profiles := r1.Data.([]*model.User) 1812 found := false 1813 for _, profile := range profiles { 1814 if profile.Id == u1.Id { 1815 found = true 1816 break 1817 } 1818 } 1819 1820 if found { 1821 t.Fatal("should not have found user") 1822 } 1823 } 1824 1825 // Check SearchNotInTeam finds previously deleted team members. 1826 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u4.Id}, -1)) 1827 1828 if r1 := <-ss.User().SearchNotInTeam(tid, "simo", searchOptions); r1.Err != nil { 1829 t.Fatal(r1.Err) 1830 } else { 1831 profiles := r1.Data.([]*model.User) 1832 found := false 1833 for _, profile := range profiles { 1834 if profile.Id == u4.Id { 1835 found = true 1836 break 1837 } 1838 } 1839 1840 if found { 1841 t.Fatal("should not have found user") 1842 } 1843 } 1844 1845 store.Must(ss.Team().UpdateMember(&model.TeamMember{TeamId: tid, UserId: u4.Id, DeleteAt: model.GetMillis() - 1000})) 1846 if r1 := <-ss.User().SearchNotInTeam(tid, "simo", searchOptions); r1.Err != nil { 1847 t.Fatal(r1.Err) 1848 } else { 1849 profiles := r1.Data.([]*model.User) 1850 found := false 1851 for _, profile := range profiles { 1852 if profile.Id == u4.Id { 1853 found = true 1854 break 1855 } 1856 } 1857 1858 if !found { 1859 t.Fatal("should have found user") 1860 } 1861 } 1862 1863 // Check PLT-8354 - search that ends up with just space for terms doesn't error. 1864 r1 := <-ss.User().SearchWithoutTeam("* ", searchOptions) 1865 assert.Nil(t, r1.Err) 1866 } 1867 1868 func testUserStoreSearchWithoutTeam(t *testing.T, ss store.Store) { 1869 u1 := &model.User{} 1870 u1.Username = "jimbo" + model.NewId() 1871 u1.FirstName = "Tim" 1872 u1.LastName = "Bill" 1873 u1.Nickname = "Rob" 1874 u1.Email = "harold" + model.NewId() + "@simulator.amazonses.com" 1875 store.Must(ss.User().Save(u1)) 1876 1877 u2 := &model.User{} 1878 u2.Username = "jim-bobby" + model.NewId() 1879 u2.Email = model.NewId() 1880 store.Must(ss.User().Save(u2)) 1881 1882 u3 := &model.User{} 1883 u3.Username = "jimbo" + model.NewId() 1884 u3.Email = model.NewId() 1885 u3.DeleteAt = 1 1886 store.Must(ss.User().Save(u3)) 1887 1888 tid := model.NewId() 1889 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u3.Id}, -1)) 1890 1891 searchOptions := map[string]bool{} 1892 searchOptions[store.USER_SEARCH_OPTION_NAMES_ONLY] = true 1893 1894 if r1 := <-ss.User().SearchWithoutTeam("", searchOptions); r1.Err != nil { 1895 t.Fatal(r1.Err) 1896 } 1897 1898 if r1 := <-ss.User().SearchWithoutTeam("jim", searchOptions); r1.Err != nil { 1899 t.Fatal(r1.Err) 1900 } else { 1901 profiles := r1.Data.([]*model.User) 1902 1903 found1 := false 1904 found2 := false 1905 found3 := false 1906 1907 for _, profile := range profiles { 1908 if profile.Id == u1.Id { 1909 found1 = true 1910 } else if profile.Id == u2.Id { 1911 found2 = true 1912 } else if profile.Id == u3.Id { 1913 found3 = true 1914 } 1915 } 1916 1917 if !found1 { 1918 t.Fatal("should have found user1") 1919 } else if !found2 { 1920 t.Fatal("should have found user2") 1921 } else if found3 { 1922 t.Fatal("should not have found user3") 1923 } 1924 } 1925 } 1926 1927 func testUserStoreAnalyticsGetInactiveUsersCount(t *testing.T, ss store.Store) { 1928 u1 := &model.User{} 1929 u1.Email = model.NewId() 1930 store.Must(ss.User().Save(u1)) 1931 1932 var count int64 1933 1934 if result := <-ss.User().AnalyticsGetInactiveUsersCount(); result.Err != nil { 1935 t.Fatal(result.Err) 1936 } else { 1937 count = result.Data.(int64) 1938 } 1939 1940 u2 := &model.User{} 1941 u2.Email = model.NewId() 1942 u2.DeleteAt = model.GetMillis() 1943 store.Must(ss.User().Save(u2)) 1944 1945 if result := <-ss.User().AnalyticsGetInactiveUsersCount(); result.Err != nil { 1946 t.Fatal(result.Err) 1947 } else { 1948 newCount := result.Data.(int64) 1949 if count != newCount-1 { 1950 t.Fatal("Expected 1 more inactive users but found otherwise.", count, newCount) 1951 } 1952 } 1953 } 1954 1955 func testUserStoreAnalyticsGetSystemAdminCount(t *testing.T, ss store.Store) { 1956 var countBefore int64 1957 if result := <-ss.User().AnalyticsGetSystemAdminCount(); result.Err != nil { 1958 t.Fatal(result.Err) 1959 } else { 1960 countBefore = result.Data.(int64) 1961 } 1962 1963 u1 := model.User{} 1964 u1.Email = model.NewId() 1965 u1.Username = model.NewId() 1966 u1.Roles = "system_user system_admin" 1967 1968 u2 := model.User{} 1969 u2.Email = model.NewId() 1970 u2.Username = model.NewId() 1971 1972 if err := (<-ss.User().Save(&u1)).Err; err != nil { 1973 t.Fatal("couldn't save user", err) 1974 } 1975 1976 if err := (<-ss.User().Save(&u2)).Err; err != nil { 1977 t.Fatal("couldn't save user", err) 1978 } 1979 1980 if result := <-ss.User().AnalyticsGetSystemAdminCount(); result.Err != nil { 1981 t.Fatal(result.Err) 1982 } else { 1983 // We expect to find 1 more system admin than there was at the start of this test function. 1984 if count := result.Data.(int64); count != countBefore+1 { 1985 t.Fatal("Did not get the expected number of system admins. Expected, got: ", countBefore+1, count) 1986 } 1987 } 1988 } 1989 1990 func testUserStoreGetProfilesNotInTeam(t *testing.T, ss store.Store) { 1991 teamId := model.NewId() 1992 1993 u1 := &model.User{} 1994 u1.Email = model.NewId() 1995 store.Must(ss.User().Save(u1)) 1996 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1997 store.Must(ss.User().UpdateUpdateAt(u1.Id)) 1998 1999 u2 := &model.User{} 2000 u2.Email = model.NewId() 2001 store.Must(ss.User().Save(u2)) 2002 store.Must(ss.User().UpdateUpdateAt(u2.Id)) 2003 2004 var initialUsersNotInTeam int 2005 var etag1, etag2, etag3 string 2006 2007 if er1 := <-ss.User().GetEtagForProfilesNotInTeam(teamId); er1.Err != nil { 2008 t.Fatal(er1.Err) 2009 } else { 2010 etag1 = er1.Data.(string) 2011 } 2012 2013 if r1 := <-ss.User().GetProfilesNotInTeam(teamId, 0, 100000); r1.Err != nil { 2014 t.Fatal(r1.Err) 2015 } else { 2016 users := r1.Data.([]*model.User) 2017 initialUsersNotInTeam = len(users) 2018 if initialUsersNotInTeam < 1 { 2019 t.Fatalf("Should be at least 1 user not in the team") 2020 } 2021 2022 found := false 2023 for _, u := range users { 2024 if u.Id == u2.Id { 2025 found = true 2026 } 2027 if u.Id == u1.Id { 2028 t.Fatalf("Should not have found user1") 2029 } 2030 } 2031 2032 if !found { 2033 t.Fatal("missing user2") 2034 } 2035 } 2036 2037 time.Sleep(time.Millisecond * 10) 2038 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 2039 store.Must(ss.User().UpdateUpdateAt(u2.Id)) 2040 2041 if er2 := <-ss.User().GetEtagForProfilesNotInTeam(teamId); er2.Err != nil { 2042 t.Fatal(er2.Err) 2043 } else { 2044 etag2 = er2.Data.(string) 2045 if etag1 == etag2 { 2046 t.Fatalf("etag should have changed") 2047 } 2048 } 2049 2050 if r2 := <-ss.User().GetProfilesNotInTeam(teamId, 0, 100000); r2.Err != nil { 2051 t.Fatal(r2.Err) 2052 } else { 2053 users := r2.Data.([]*model.User) 2054 2055 if len(users) != initialUsersNotInTeam-1 { 2056 t.Fatalf("Should be one less user not in team") 2057 } 2058 2059 for _, u := range users { 2060 if u.Id == u2.Id { 2061 t.Fatalf("Should not have found user2") 2062 } 2063 if u.Id == u1.Id { 2064 t.Fatalf("Should not have found user1") 2065 } 2066 } 2067 } 2068 2069 time.Sleep(time.Millisecond * 10) 2070 store.Must(ss.Team().RemoveMember(teamId, u1.Id)) 2071 store.Must(ss.Team().RemoveMember(teamId, u2.Id)) 2072 store.Must(ss.User().UpdateUpdateAt(u1.Id)) 2073 store.Must(ss.User().UpdateUpdateAt(u2.Id)) 2074 2075 if er3 := <-ss.User().GetEtagForProfilesNotInTeam(teamId); er3.Err != nil { 2076 t.Fatal(er3.Err) 2077 } else { 2078 etag3 = er3.Data.(string) 2079 t.Log(etag3) 2080 if etag1 == etag3 || etag3 == etag2 { 2081 t.Fatalf("etag should have changed") 2082 } 2083 } 2084 2085 if r3 := <-ss.User().GetProfilesNotInTeam(teamId, 0, 100000); r3.Err != nil { 2086 t.Fatal(r3.Err) 2087 } else { 2088 users := r3.Data.([]*model.User) 2089 found1, found2 := false, false 2090 for _, u := range users { 2091 if u.Id == u2.Id { 2092 found2 = true 2093 } 2094 if u.Id == u1.Id { 2095 found1 = true 2096 } 2097 } 2098 2099 if !found1 || !found2 { 2100 t.Fatal("missing user1 or user2") 2101 } 2102 } 2103 2104 time.Sleep(time.Millisecond * 10) 2105 u3 := &model.User{} 2106 u3.Email = model.NewId() 2107 store.Must(ss.User().Save(u3)) 2108 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u3.Id}, -1)) 2109 store.Must(ss.User().UpdateUpdateAt(u3.Id)) 2110 2111 if er4 := <-ss.User().GetEtagForProfilesNotInTeam(teamId); er4.Err != nil { 2112 t.Fatal(er4.Err) 2113 } else { 2114 etag4 := er4.Data.(string) 2115 t.Log(etag4) 2116 if etag4 != etag3 { 2117 t.Fatalf("etag should be the same") 2118 } 2119 } 2120 }