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