]>
Commit | Line | Data |
---|---|---|
18490b07 | 1 | import { omit } from 'lodash' |
0e1dc3e7 | 2 | import * as request from 'supertest' |
a1bb73f9 | 3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
18490b07 | 4 | import { UserUpdateMe } from '../../models/users' |
1eddc9a7 | 5 | import { UserAdminFlag } from '../../models/users/user-flag.model' |
e590b4a5 | 6 | import { UserRegister } from '../../models/users/user-register.model' |
8d2be0ed | 7 | import { UserRole } from '../../models/users/user-role' |
213e30ef | 8 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateImageRequest } from '../requests/requests' |
8d2be0ed C |
9 | import { ServerInfo } from '../server/servers' |
10 | import { userLogin } from './login' | |
757f0da3 | 11 | |
a1bb73f9 | 12 | function createUser (parameters: { |
a1587156 C |
13 | url: string |
14 | accessToken: string | |
15 | username: string | |
16 | password: string | |
17 | videoQuota?: number | |
18 | videoQuotaDaily?: number | |
19 | role?: UserRole | |
20 | adminFlags?: UserAdminFlag | |
1eddc9a7 | 21 | specialStatus?: number |
a1bb73f9 | 22 | }) { |
1eddc9a7 C |
23 | const { |
24 | url, | |
25 | accessToken, | |
26 | username, | |
27 | adminFlags, | |
28 | password = 'password', | |
29 | videoQuota = 1000000, | |
30 | videoQuotaDaily = -1, | |
31 | role = UserRole.USER, | |
2d53be02 | 32 | specialStatus = HttpStatusCode.OK_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 | ||
a1bb73f9 C |
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 | ||
df0b219d C |
69 | async function generateUserAccessToken (server: ServerInfo, username: string) { |
70 | const password = 'my super password' | |
1eddc9a7 | 71 | await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) |
df0b219d C |
72 | |
73 | return userLogin(server, { username, password }) | |
74 | } | |
75 | ||
2d53be02 | 76 | function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { |
0e1dc3e7 C |
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 | ||
e590b4a5 | 91 | function registerUserWithChannel (options: { |
a1587156 C |
92 | url: string |
93 | user: { username: string, password: string, displayName?: string } | |
e590b4a5 C |
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 | ||
1f20622f C |
104 | if (options.user.displayName) { |
105 | Object.assign(body, { displayName: options.user.displayName }) | |
106 | } | |
107 | ||
e590b4a5 C |
108 | return makePostBodyRequest({ |
109 | url: options.url, | |
110 | path, | |
111 | fields: body, | |
2d53be02 | 112 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
e590b4a5 C |
113 | }) |
114 | } | |
115 | ||
2d53be02 | 116 | function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) { |
0e1dc3e7 C |
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) | |
26d21b78 | 123 | .expect(specialStatus) |
0e1dc3e7 C |
124 | .expect('Content-Type', /json/) |
125 | } | |
126 | ||
2d53be02 | 127 | function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { |
afff310e RK |
128 | const path = '/api/v1/users/scoped-tokens' |
129 | ||
18490b07 C |
130 | return makeGetRequest({ |
131 | url, | |
132 | path, | |
133 | token, | |
134 | statusCodeExpected | |
135 | }) | |
136 | } | |
137 | ||
2d53be02 | 138 | function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { |
18490b07 C |
139 | const path = '/api/v1/users/scoped-tokens' |
140 | ||
141 | return makePostBodyRequest({ | |
142 | url, | |
143 | path, | |
144 | token, | |
145 | statusCodeExpected | |
146 | }) | |
afff310e RK |
147 | } |
148 | ||
2d53be02 | 149 | function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { |
92b9d60c C |
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 | ||
2d53be02 | 159 | function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) { |
ce5496d6 C |
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 | ||
76314386 | 170 | function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) { |
5c98d3bf C |
171 | const path = '/api/v1/users/' + userId |
172 | ||
173 | return request(url) | |
174 | .get(path) | |
76314386 | 175 | .query({ withStats }) |
5c98d3bf C |
176 | .set('Accept', 'application/json') |
177 | .set('Authorization', 'Bearer ' + accessToken) | |
2d53be02 | 178 | .expect(HttpStatusCode.OK_200) |
5c98d3bf C |
179 | .expect('Content-Type', /json/) |
180 | } | |
181 | ||
2d53be02 | 182 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) { |
0e1dc3e7 C |
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) | |
26d21b78 | 189 | .expect(specialStatus) |
0e1dc3e7 C |
190 | .expect('Content-Type', /json/) |
191 | } | |
192 | ||
86d13ec2 | 193 | function getUsersList (url: string, accessToken: string) { |
0e1dc3e7 C |
194 | const path = '/api/v1/users' |
195 | ||
196 | return request(url) | |
197 | .get(path) | |
198 | .set('Accept', 'application/json') | |
86d13ec2 | 199 | .set('Authorization', 'Bearer ' + accessToken) |
2d53be02 | 200 | .expect(HttpStatusCode.OK_200) |
0e1dc3e7 C |
201 | .expect('Content-Type', /json/) |
202 | } | |
203 | ||
8491293b RK |
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 | ) { | |
0e1dc3e7 C |
213 | const path = '/api/v1/users' |
214 | ||
60919831 C |
215 | const query = { |
216 | start, | |
217 | count, | |
218 | sort, | |
8491293b RK |
219 | search, |
220 | blocked | |
60919831 C |
221 | } |
222 | ||
0e1dc3e7 C |
223 | return request(url) |
224 | .get(path) | |
60919831 | 225 | .query(query) |
0e1dc3e7 | 226 | .set('Accept', 'application/json') |
86d13ec2 | 227 | .set('Authorization', 'Bearer ' + accessToken) |
2d53be02 | 228 | .expect(HttpStatusCode.OK_200) |
0e1dc3e7 C |
229 | .expect('Content-Type', /json/) |
230 | } | |
231 | ||
2d53be02 | 232 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { |
0e1dc3e7 C |
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 | ||
2d53be02 RK |
242 | function blockUser ( |
243 | url: string, | |
244 | userId: number | string, | |
245 | accessToken: string, | |
246 | expectedStatus = HttpStatusCode.NO_CONTENT_204, | |
247 | reason?: string | |
248 | ) { | |
e6921918 | 249 | const path = '/api/v1/users' |
eacb25c4 C |
250 | let body: any |
251 | if (reason) body = { reason } | |
e6921918 C |
252 | |
253 | return request(url) | |
254 | .post(path + '/' + userId + '/block') | |
eacb25c4 | 255 | .send(body) |
e6921918 C |
256 | .set('Accept', 'application/json') |
257 | .set('Authorization', 'Bearer ' + accessToken) | |
258 | .expect(expectedStatus) | |
259 | } | |
260 | ||
2d53be02 | 261 | function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { |
e6921918 C |
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 | ||
2d53be02 | 271 | function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) { |
5c98d3bf | 272 | const path = '/api/v1/users/me' |
0e1dc3e7 | 273 | |
43d0ea7f | 274 | const toSend: UserUpdateMe = omit(options, 'url', 'accessToken') |
26d21b78 C |
275 | |
276 | return makePutBodyRequest({ | |
277 | url: options.url, | |
278 | path, | |
279 | token: options.accessToken, | |
280 | fields: toSend, | |
2d53be02 | 281 | statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204 |
26d21b78 | 282 | }) |
5c98d3bf C |
283 | } |
284 | ||
c5911fd3 | 285 | function updateMyAvatar (options: { |
a1587156 C |
286 | url: string |
287 | accessToken: string | |
c5911fd3 C |
288 | fixture: string |
289 | }) { | |
290 | const path = '/api/v1/users/me/avatar/pick' | |
c5911fd3 | 291 | |
213e30ef | 292 | return updateImageRequest({ ...options, path, fieldname: 'avatarfile' }) |
c5911fd3 C |
293 | } |
294 | ||
26d21b78 C |
295 | function updateUser (options: { |
296 | url: string | |
a1587156 C |
297 | userId: number |
298 | accessToken: string | |
299 | email?: string | |
300 | emailVerified?: boolean | |
301 | videoQuota?: number | |
302 | videoQuotaDaily?: number | |
303 | password?: string | |
304 | adminFlags?: UserAdminFlag | |
6d989edc | 305 | pluginAuth?: string |
26d21b78 C |
306 | role?: UserRole |
307 | }) { | |
308 | const path = '/api/v1/users/' + options.userId | |
5c98d3bf C |
309 | |
310 | const toSend = {} | |
b426edd4 | 311 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password |
26d21b78 | 312 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
fc2ec87a | 313 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified |
26d21b78 | 314 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota |
bee0abff | 315 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily |
26d21b78 | 316 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role |
1eddc9a7 | 317 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags |
6d989edc | 318 | if (options.pluginAuth !== undefined) toSend['pluginAuth'] = options.pluginAuth |
26d21b78 C |
319 | |
320 | return makePutBodyRequest({ | |
321 | url: options.url, | |
322 | path, | |
323 | token: options.accessToken, | |
324 | fields: toSend, | |
2d53be02 | 325 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
26d21b78 | 326 | }) |
0e1dc3e7 C |
327 | } |
328 | ||
f076daa7 C |
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 }, | |
2d53be02 | 336 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
f076daa7 C |
337 | }) |
338 | } | |
339 | ||
2d53be02 RK |
340 | function resetPassword ( |
341 | url: string, | |
342 | userId: number, | |
343 | verificationString: string, | |
344 | password: string, | |
345 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | |
346 | ) { | |
f076daa7 C |
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 | ||
d9eaee39 JM |
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 }, | |
2d53be02 | 364 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
d9eaee39 JM |
365 | }) |
366 | } | |
367 | ||
2d53be02 RK |
368 | function verifyEmail ( |
369 | url: string, | |
370 | userId: number, | |
371 | verificationString: string, | |
372 | isPendingEmail = false, | |
373 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | |
374 | ) { | |
d9eaee39 JM |
375 | const path = '/api/v1/users/' + userId + '/verify-email' |
376 | ||
377 | return makePostBodyRequest({ | |
378 | url, | |
379 | path, | |
d1ab89de C |
380 | fields: { |
381 | verificationString, | |
382 | isPendingEmail | |
383 | }, | |
d9eaee39 JM |
384 | statusCodeExpected |
385 | }) | |
386 | } | |
387 | ||
0e1dc3e7 C |
388 | // --------------------------------------------------------------------------- |
389 | ||
390 | export { | |
391 | createUser, | |
392 | registerUser, | |
5c98d3bf | 393 | getMyUserInformation, |
26d21b78 | 394 | getMyUserVideoRating, |
92b9d60c | 395 | deleteMe, |
e590b4a5 | 396 | registerUserWithChannel, |
ce5496d6 | 397 | getMyUserVideoQuotaUsed, |
0e1dc3e7 C |
398 | getUsersList, |
399 | getUsersListPaginationAndSort, | |
400 | removeUser, | |
5c98d3bf C |
401 | updateUser, |
402 | updateMyUser, | |
c5911fd3 | 403 | getUserInformation, |
e6921918 C |
404 | blockUser, |
405 | unblockUser, | |
f076daa7 C |
406 | askResetPassword, |
407 | resetPassword, | |
18490b07 | 408 | renewUserScopedTokens, |
d9eaee39 | 409 | updateMyAvatar, |
a1bb73f9 | 410 | generateUser, |
d9eaee39 | 411 | askSendVerifyEmail, |
df0b219d | 412 | generateUserAccessToken, |
afff310e RK |
413 | verifyEmail, |
414 | getUserScopedTokens | |
0e1dc3e7 | 415 | } |