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