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