github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/app/auto_users.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "github.com/mattermost/mattermost-server/mlog" 8 "github.com/mattermost/mattermost-server/model" 9 "github.com/mattermost/mattermost-server/store" 10 "github.com/mattermost/mattermost-server/utils" 11 ) 12 13 type AutoUserCreator struct { 14 app *App 15 client *model.Client 16 team *model.Team 17 EmailLength utils.Range 18 EmailCharset string 19 NameLength utils.Range 20 NameCharset string 21 Fuzzy bool 22 } 23 24 func NewAutoUserCreator(a *App, client *model.Client, team *model.Team) *AutoUserCreator { 25 return &AutoUserCreator{ 26 app: a, 27 client: client, 28 team: team, 29 EmailLength: USER_EMAIL_LEN, 30 EmailCharset: utils.LOWERCASE, 31 NameLength: USER_NAME_LEN, 32 NameCharset: utils.LOWERCASE, 33 Fuzzy: false, 34 } 35 } 36 37 // Basic test team and user so you always know one 38 func (a *App) CreateBasicUser(client *model.Client) *model.AppError { 39 result, _ := client.FindTeamByName(BTEST_TEAM_NAME) 40 if !result.Data.(bool) { 41 newteam := &model.Team{DisplayName: BTEST_TEAM_DISPLAY_NAME, Name: BTEST_TEAM_NAME, Email: BTEST_TEAM_EMAIL, Type: BTEST_TEAM_TYPE} 42 result, err := client.CreateTeam(newteam) 43 if err != nil { 44 return err 45 } 46 basicteam := result.Data.(*model.Team) 47 newuser := &model.User{Email: BTEST_USER_EMAIL, Nickname: BTEST_USER_NAME, Password: BTEST_USER_PASSWORD} 48 result, err = client.CreateUser(newuser, "") 49 if err != nil { 50 return err 51 } 52 ruser := result.Data.(*model.User) 53 store.Must(a.Srv.Store.User().VerifyEmail(ruser.Id)) 54 store.Must(a.Srv.Store.Team().SaveMember(&model.TeamMember{TeamId: basicteam.Id, UserId: ruser.Id}, *a.Config().TeamSettings.MaxUsersPerTeam)) 55 } 56 return nil 57 } 58 59 func (cfg *AutoUserCreator) createRandomUser() (*model.User, bool) { 60 var userEmail string 61 var userName string 62 if cfg.Fuzzy { 63 userEmail = "success+" + model.NewId() + "@simulator.amazonses.com" 64 userName = utils.FuzzName() 65 } else { 66 userEmail = "success+" + model.NewId() + "@simulator.amazonses.com" 67 userName = utils.RandomName(cfg.NameLength, cfg.NameCharset) 68 } 69 70 user := &model.User{ 71 Email: userEmail, 72 Nickname: userName, 73 Password: USER_PASSWORD} 74 75 result, err := cfg.client.CreateUserWithInvite(user, "", "", cfg.team.InviteId) 76 if err != nil { 77 mlog.Error(err.Error()) 78 return nil, false 79 } 80 81 ruser := result.Data.(*model.User) 82 83 status := &model.Status{UserId: ruser.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""} 84 if result := <-cfg.app.Srv.Store.Status().SaveOrUpdate(status); result.Err != nil { 85 mlog.Error(result.Err.Error()) 86 return nil, false 87 } 88 89 // We need to cheat to verify the user's email 90 store.Must(cfg.app.Srv.Store.User().VerifyEmail(ruser.Id)) 91 92 return result.Data.(*model.User), true 93 } 94 95 func (cfg *AutoUserCreator) CreateTestUsers(num utils.Range) ([]*model.User, bool) { 96 numUsers := utils.RandIntFromRange(num) 97 users := make([]*model.User, numUsers) 98 99 for i := 0; i < numUsers; i++ { 100 var err bool 101 users[i], err = cfg.createRandomUser() 102 if !err { 103 return users, false 104 } 105 } 106 107 return users, true 108 }