]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/users/users.ts
Fix "Too many packets buffered for output stream"
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / users.ts
CommitLineData
18490b07 1import { omit } from 'lodash'
0e1dc3e7 2import * as request from 'supertest'
18490b07 3import { UserUpdateMe } from '../../models/users'
1eddc9a7 4import { UserAdminFlag } from '../../models/users/user-flag.model'
e590b4a5 5import { UserRegister } from '../../models/users/user-register.model'
8d2be0ed 6import { UserRole } from '../../models/users/user-role'
18490b07 7import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
8d2be0ed
C
8import { ServerInfo } from '../server/servers'
9import { userLogin } from './login'
757f0da3 10
6f3fe96f 11type CreateUserArgs = {
a1587156
C
12 url: string
13 accessToken: string
14 username: string
15 password: string
16 videoQuota?: number
17 videoQuotaDaily?: number
18 role?: UserRole
19 adminFlags?: UserAdminFlag
1eddc9a7
C
20 specialStatus?: number
21}
22function createUser (parameters: CreateUserArgs) {
23 const {
24 url,
25 accessToken,
26 username,
27 adminFlags,
28 password = 'password',
29 videoQuota = 1000000,
30 videoQuotaDaily = -1,
31 role = UserRole.USER,
1e904cde 32 specialStatus = 200
1eddc9a7
C
33 } = parameters
34
0e1dc3e7
C
35 const path = '/api/v1/users'
36 const body = {
37 username,
38 password,
757f0da3 39 role,
1eddc9a7 40 adminFlags,
5c98d3bf 41 email: username + '@example.com',
bee0abff
FA
42 videoQuota,
43 videoQuotaDaily
0e1dc3e7
C
44 }
45
46 return request(url)
47 .post(path)
48 .set('Accept', 'application/json')
49 .set('Authorization', 'Bearer ' + accessToken)
50 .send(body)
51 .expect(specialStatus)
52}
53
df0b219d
C
54async function generateUserAccessToken (server: ServerInfo, username: string) {
55 const password = 'my super password'
1eddc9a7 56 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
df0b219d
C
57
58 return userLogin(server, { username, password })
59}
60
0e1dc3e7
C
61function registerUser (url: string, username: string, password: string, specialStatus = 204) {
62 const path = '/api/v1/users/register'
63 const body = {
64 username,
65 password,
66 email: username + '@example.com'
67 }
68
69 return request(url)
70 .post(path)
71 .set('Accept', 'application/json')
72 .send(body)
73 .expect(specialStatus)
74}
75
e590b4a5 76function registerUserWithChannel (options: {
a1587156
C
77 url: string
78 user: { username: string, password: string, displayName?: string }
e590b4a5
C
79 channel: { name: string, displayName: string }
80}) {
81 const path = '/api/v1/users/register'
82 const body: UserRegister = {
83 username: options.user.username,
84 password: options.user.password,
85 email: options.user.username + '@example.com',
86 channel: options.channel
87 }
88
1f20622f
C
89 if (options.user.displayName) {
90 Object.assign(body, { displayName: options.user.displayName })
91 }
92
e590b4a5
C
93 return makePostBodyRequest({
94 url: options.url,
95 path,
96 fields: body,
97 statusCodeExpected: 204
98 })
99}
100
26d21b78 101function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
0e1dc3e7
C
102 const path = '/api/v1/users/me'
103
104 return request(url)
105 .get(path)
106 .set('Accept', 'application/json')
107 .set('Authorization', 'Bearer ' + accessToken)
26d21b78 108 .expect(specialStatus)
0e1dc3e7
C
109 .expect('Content-Type', /json/)
110}
111
18490b07 112function getUserScopedTokens (url: string, token: string, statusCodeExpected = 200) {
afff310e
RK
113 const path = '/api/v1/users/scoped-tokens'
114
18490b07
C
115 return makeGetRequest({
116 url,
117 path,
118 token,
119 statusCodeExpected
120 })
121}
122
123function renewUserScopedTokens (url: string, token: string, statusCodeExpected = 200) {
124 const path = '/api/v1/users/scoped-tokens'
125
126 return makePostBodyRequest({
127 url,
128 path,
129 token,
130 statusCodeExpected
131 })
afff310e
RK
132}
133
92b9d60c
C
134function deleteMe (url: string, accessToken: string, specialStatus = 204) {
135 const path = '/api/v1/users/me'
136
137 return request(url)
138 .delete(path)
139 .set('Accept', 'application/json')
140 .set('Authorization', 'Bearer ' + accessToken)
141 .expect(specialStatus)
142}
143
ce5496d6
C
144function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
145 const path = '/api/v1/users/me/video-quota-used'
146
147 return request(url)
148 .get(path)
149 .set('Accept', 'application/json')
150 .set('Authorization', 'Bearer ' + accessToken)
151 .expect(specialStatus)
152 .expect('Content-Type', /json/)
153}
154
76314386 155function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
5c98d3bf
C
156 const path = '/api/v1/users/' + userId
157
158 return request(url)
159 .get(path)
76314386 160 .query({ withStats })
5c98d3bf
C
161 .set('Accept', 'application/json')
162 .set('Authorization', 'Bearer ' + accessToken)
163 .expect(200)
164 .expect('Content-Type', /json/)
165}
166
26d21b78 167function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
0e1dc3e7
C
168 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
169
170 return request(url)
171 .get(path)
172 .set('Accept', 'application/json')
173 .set('Authorization', 'Bearer ' + accessToken)
26d21b78 174 .expect(specialStatus)
0e1dc3e7
C
175 .expect('Content-Type', /json/)
176}
177
86d13ec2 178function getUsersList (url: string, accessToken: string) {
0e1dc3e7
C
179 const path = '/api/v1/users'
180
181 return request(url)
182 .get(path)
183 .set('Accept', 'application/json')
86d13ec2 184 .set('Authorization', 'Bearer ' + accessToken)
0e1dc3e7
C
185 .expect(200)
186 .expect('Content-Type', /json/)
187}
188
8491293b
RK
189function getUsersListPaginationAndSort (
190 url: string,
191 accessToken: string,
192 start: number,
193 count: number,
194 sort: string,
195 search?: string,
196 blocked?: boolean
197) {
0e1dc3e7
C
198 const path = '/api/v1/users'
199
60919831
C
200 const query = {
201 start,
202 count,
203 sort,
8491293b
RK
204 search,
205 blocked
60919831
C
206 }
207
0e1dc3e7
C
208 return request(url)
209 .get(path)
60919831 210 .query(query)
0e1dc3e7 211 .set('Accept', 'application/json')
86d13ec2 212 .set('Authorization', 'Bearer ' + accessToken)
0e1dc3e7
C
213 .expect(200)
214 .expect('Content-Type', /json/)
215}
216
26d21b78 217function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
0e1dc3e7
C
218 const path = '/api/v1/users'
219
220 return request(url)
221 .delete(path + '/' + userId)
222 .set('Accept', 'application/json')
223 .set('Authorization', 'Bearer ' + accessToken)
224 .expect(expectedStatus)
225}
226
eacb25c4 227function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
e6921918 228 const path = '/api/v1/users'
eacb25c4
C
229 let body: any
230 if (reason) body = { reason }
e6921918
C
231
232 return request(url)
233 .post(path + '/' + userId + '/block')
eacb25c4 234 .send(body)
e6921918
C
235 .set('Accept', 'application/json')
236 .set('Authorization', 'Bearer ' + accessToken)
237 .expect(expectedStatus)
238}
239
240function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
241 const path = '/api/v1/users'
242
243 return request(url)
244 .post(path + '/' + userId + '/unblock')
245 .set('Accept', 'application/json')
246 .set('Authorization', 'Bearer ' + accessToken)
247 .expect(expectedStatus)
248}
249
9a7fd960 250function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) {
5c98d3bf 251 const path = '/api/v1/users/me'
0e1dc3e7 252
43d0ea7f 253 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
26d21b78
C
254
255 return makePutBodyRequest({
256 url: options.url,
257 path,
258 token: options.accessToken,
259 fields: toSend,
9a7fd960 260 statusCodeExpected: options.statusCodeExpected || 204
26d21b78 261 })
5c98d3bf
C
262}
263
c5911fd3 264function updateMyAvatar (options: {
a1587156
C
265 url: string
266 accessToken: string
c5911fd3
C
267 fixture: string
268}) {
269 const path = '/api/v1/users/me/avatar/pick'
c5911fd3 270
4bbfc6c6 271 return updateAvatarRequest(Object.assign(options, { path }))
c5911fd3
C
272}
273
26d21b78
C
274function updateUser (options: {
275 url: string
a1587156
C
276 userId: number
277 accessToken: string
278 email?: string
279 emailVerified?: boolean
280 videoQuota?: number
281 videoQuotaDaily?: number
282 password?: string
283 adminFlags?: UserAdminFlag
26d21b78
C
284 role?: UserRole
285}) {
286 const path = '/api/v1/users/' + options.userId
5c98d3bf
C
287
288 const toSend = {}
b426edd4 289 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
26d21b78 290 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
fc2ec87a 291 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
26d21b78 292 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
bee0abff 293 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
26d21b78 294 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
1eddc9a7 295 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
26d21b78
C
296
297 return makePutBodyRequest({
298 url: options.url,
299 path,
300 token: options.accessToken,
301 fields: toSend,
302 statusCodeExpected: 204
303 })
0e1dc3e7
C
304}
305
f076daa7
C
306function askResetPassword (url: string, email: string) {
307 const path = '/api/v1/users/ask-reset-password'
308
309 return makePostBodyRequest({
310 url,
311 path,
312 fields: { email },
313 statusCodeExpected: 204
314 })
315}
316
317function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
318 const path = '/api/v1/users/' + userId + '/reset-password'
319
320 return makePostBodyRequest({
321 url,
322 path,
323 fields: { password, verificationString },
324 statusCodeExpected
325 })
326}
327
d9eaee39
JM
328function askSendVerifyEmail (url: string, email: string) {
329 const path = '/api/v1/users/ask-send-verify-email'
330
331 return makePostBodyRequest({
332 url,
333 path,
334 fields: { email },
335 statusCodeExpected: 204
336 })
337}
338
d1ab89de 339function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
d9eaee39
JM
340 const path = '/api/v1/users/' + userId + '/verify-email'
341
342 return makePostBodyRequest({
343 url,
344 path,
d1ab89de
C
345 fields: {
346 verificationString,
347 isPendingEmail
348 },
d9eaee39
JM
349 statusCodeExpected
350 })
351}
352
0e1dc3e7
C
353// ---------------------------------------------------------------------------
354
355export {
356 createUser,
357 registerUser,
5c98d3bf 358 getMyUserInformation,
26d21b78 359 getMyUserVideoRating,
92b9d60c 360 deleteMe,
e590b4a5 361 registerUserWithChannel,
ce5496d6 362 getMyUserVideoQuotaUsed,
0e1dc3e7
C
363 getUsersList,
364 getUsersListPaginationAndSort,
365 removeUser,
5c98d3bf
C
366 updateUser,
367 updateMyUser,
c5911fd3 368 getUserInformation,
e6921918
C
369 blockUser,
370 unblockUser,
f076daa7
C
371 askResetPassword,
372 resetPassword,
18490b07 373 renewUserScopedTokens,
d9eaee39
JM
374 updateMyAvatar,
375 askSendVerifyEmail,
df0b219d 376 generateUserAccessToken,
afff310e
RK
377 verifyEmail,
378 getUserScopedTokens
0e1dc3e7 379}