/
user.go
239 lines (207 loc) · 5.91 KB
/
user.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package userentity
import (
"errors"
"net/http"
"os"
"time"
"github.com/mattermost/mattermost-load-test-ng/loadtest/store"
"github.com/mattermost/mattermost-load-test-ng/performance"
"github.com/gocolly/colly/v2"
"github.com/mattermost/mattermost/server/public/model"
)
// UserEntity is an implementation of the User interface
// which provides methods to interact with the Mattermost server.
type UserEntity struct {
store store.MutableUserStore
client *model.Client4
wsClosing chan struct{}
wsClosed chan struct{}
wsErrorChan chan error
wsEventChan chan *model.WebSocketEvent
dataChan chan any
connected bool
config Config
metrics *performance.UserEntityMetrics
wsConnID string
wsServerSeq int64
}
// Config holds necessary information required by a UserEntity.
type Config struct {
// The URL of the Mattermost web server.
ServerURL string
// The URL of the mattermost WebSocket server.
WebSocketURL string
// The username to be used by the entity.
Username string
// The email to be used by the entity.
Email string
// The password to be used by the entity.
Password string
}
// Setup contains data used to create a new instance of UserEntity.
type Setup struct {
// The store to be used to save user's data.
Store store.MutableUserStore
// The transport to be used to execute API calls.
Transport http.RoundTripper
// An optional object used to collect metrics.
Metrics *performance.UserEntityMetrics
// The HTTP client timeout to use.
ClientTimeout time.Duration
}
type userTypingMsg struct {
channelId string
parentId string
}
type channelPresenceMsg struct {
channelId string
}
type threadPresenceMsg struct {
channelId string
threadView bool
}
type teamPresenceMsg struct {
teamId string
}
type postedAckMsg struct {
postId string
result string
reason string
postedData string
}
type ueTransport struct {
transport http.RoundTripper
ue *UserEntity
}
// RoundTrip implements the RoundTripper interface for ueTransport.
// This is used to collect metrics regarding the timing of HTTP calls.
func (t *ueTransport) RoundTrip(req *http.Request) (*http.Response, error) {
startTime := time.Now()
resp, err := t.transport.RoundTrip(req)
t.ue.observeHTTPRequestTimes(time.Since(startTime).Seconds())
if os.IsTimeout(err) {
t.ue.incHTTPTimeouts(req.URL.Path, req.Method)
}
if resp != nil && resp.StatusCode >= 400 {
t.ue.incHTTPErrors(req.URL.Path, req.Method, resp.StatusCode)
}
return resp, err
}
// Store returns the underlying store of the user.
func (ue *UserEntity) Store() store.UserStore {
return ue.store
}
// New returns a new instance of a UserEntity.
func New(setup Setup, config Config) *UserEntity {
var ue UserEntity
ue.config = config
ue.store = setup.Store
ue.metrics = setup.Metrics
ue.client = model.NewAPIv4Client(config.ServerURL)
if setup.Transport == nil {
setup.Transport = http.DefaultTransport
}
if setup.Metrics != nil {
setup.Transport = &ueTransport{
transport: setup.Transport,
ue: &ue,
}
}
ue.client.HTTPClient = &http.Client{
Transport: setup.Transport,
Timeout: setup.ClientTimeout,
}
err := ue.store.SetUser(&model.User{
Username: config.Username,
Email: config.Email,
Password: config.Password,
Id: ue.store.Id(),
})
if err != nil {
return nil
}
return &ue
}
// Connect creates a WebSocket connection to the server and starts listening for messages.
func (ue *UserEntity) Connect() (<-chan error, error) {
if ue.connected {
return nil, errors.New("user is already connected")
}
ue.wsClosing = make(chan struct{})
ue.wsClosed = make(chan struct{})
ue.wsErrorChan = make(chan error, 1)
if ue.client.AuthToken == "" {
return nil, errors.New("user is not authenticated")
}
if ue.connected {
return nil, errors.New("user is already connected")
}
ue.wsEventChan = make(chan *model.WebSocketEvent)
ue.dataChan = make(chan any, 10)
go ue.listen(ue.wsErrorChan)
ue.connected = true
return ue.wsErrorChan, nil
}
// FetchStaticAssets parses index.html and fetches static assets mentioned in link/script tags.
func (ue *UserEntity) FetchStaticAssets() error {
c := colly.NewCollector(colly.MaxDepth(1))
c.OnHTML("link[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
c.Visit(e.Request.AbsoluteURL(link))
})
c.OnHTML("script[src]", func(e *colly.HTMLElement) {
link := e.Attr("src")
c.Visit(e.Request.AbsoluteURL(link))
})
return c.Visit(ue.client.URL)
}
// Disconnect closes the WebSocket connection.
func (ue *UserEntity) Disconnect() error {
ue.client.HTTPClient.CloseIdleConnections()
if !ue.connected {
return errors.New("user is not connected")
}
// We exit the listener loop first, and then close the connection.
// Otherwise, it tries to reconnect first, and then
// exits, which causes unnecessary delay.
close(ue.wsClosing)
<-ue.wsClosed
close(ue.wsEventChan)
close(ue.dataChan)
close(ue.wsErrorChan)
ue.connected = false
return nil
}
// Events returns the WebSocket event chan for the controller
// to listen and react to events.
func (ue *UserEntity) Events() <-chan *model.WebSocketEvent {
return ue.wsEventChan
}
// IsSysAdmin returns whether the user is a system admin or not.
func (ue *UserEntity) IsSysAdmin() (bool, error) {
user, err := ue.getUserFromStore()
if err != nil {
return false, err
}
return user.IsInRole(model.SystemAdminRoleId), nil
}
// IsTeamAdmin returns whether the user is a team admin or not.
func (ue *UserEntity) IsTeamAdmin() (bool, error) {
user, err := ue.getUserFromStore()
if err != nil {
return false, err
}
return user.IsInRole(model.TeamAdminRoleId), nil
}
func (ue *UserEntity) getUserFromStore() (*model.User, error) {
user, err := ue.store.User()
if err != nil {
return nil, err
}
if user == nil {
return nil, errors.New("user was not initialized")
}
return user, nil
}