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