github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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.Client4 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.Client4, 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.Client4) *model.AppError { 39 found, _ := client.TeamExists(BTEST_TEAM_NAME, "") 40 if !found { 41 newteam := &model.Team{DisplayName: BTEST_TEAM_DISPLAY_NAME, Name: BTEST_TEAM_NAME, Email: BTEST_TEAM_EMAIL, Type: BTEST_TEAM_TYPE} 42 basicteam, resp := client.CreateTeam(newteam) 43 if resp.Error != nil { 44 return resp.Error 45 } 46 newuser := &model.User{Email: BTEST_USER_EMAIL, Nickname: BTEST_USER_NAME, Password: BTEST_USER_PASSWORD} 47 ruser, resp := client.CreateUser(newuser) 48 if resp.Error != nil { 49 return resp.Error 50 } 51 store.Must(a.Srv.Store.User().VerifyEmail(ruser.Id)) 52 store.Must(a.Srv.Store.Team().SaveMember(&model.TeamMember{TeamId: basicteam.Id, UserId: ruser.Id}, *a.Config().TeamSettings.MaxUsersPerTeam)) 53 } 54 return nil 55 } 56 57 func (cfg *AutoUserCreator) createRandomUser() (*model.User, bool) { 58 var userEmail string 59 var userName string 60 if cfg.Fuzzy { 61 userEmail = "success+" + model.NewId() + "@simulator.amazonses.com" 62 userName = utils.FuzzName() 63 } else { 64 userEmail = "success+" + model.NewId() + "@simulator.amazonses.com" 65 userName = utils.RandomName(cfg.NameLength, cfg.NameCharset) 66 } 67 68 user := &model.User{ 69 Email: userEmail, 70 Nickname: userName, 71 Password: USER_PASSWORD} 72 73 ruser, resp := cfg.client.CreateUserWithInviteId(user, cfg.team.InviteId) 74 if resp.Error != nil { 75 mlog.Error(resp.Error.Error()) 76 return nil, false 77 } 78 79 status := &model.Status{UserId: ruser.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""} 80 if result := <-cfg.app.Srv.Store.Status().SaveOrUpdate(status); result.Err != nil { 81 mlog.Error(result.Err.Error()) 82 return nil, false 83 } 84 85 // We need to cheat to verify the user's email 86 store.Must(cfg.app.Srv.Store.User().VerifyEmail(ruser.Id)) 87 88 return ruser, true 89 } 90 91 func (cfg *AutoUserCreator) CreateTestUsers(num utils.Range) ([]*model.User, bool) { 92 numUsers := utils.RandIntFromRange(num) 93 users := make([]*model.User, numUsers) 94 95 for i := 0; i < numUsers; i++ { 96 var err bool 97 users[i], err = cfg.createRandomUser() 98 if !err { 99 return users, false 100 } 101 } 102 103 return users, true 104 }