]>
Commit | Line | Data |
---|---|---|
18490b07 | 1 | import { omit } from 'lodash' |
0e1dc3e7 | 2 | import * as request from 'supertest' |
18490b07 | 3 | import { UserUpdateMe } from '../../models/users' |
1eddc9a7 | 4 | import { UserAdminFlag } from '../../models/users/user-flag.model' |
e590b4a5 | 5 | import { UserRegister } from '../../models/users/user-register.model' |
8d2be0ed | 6 | import { UserRole } from '../../models/users/user-role' |
18490b07 | 7 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' |
8d2be0ed C |
8 | import { ServerInfo } from '../server/servers' |
9 | import { userLogin } from './login' | |
2d53be02 | 10 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
757f0da3 | 11 | |
6f3fe96f | 12 | type CreateUserArgs = { |
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 C |
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, | |
2d53be02 | 33 | specialStatus = HttpStatusCode.OK_200 |
1eddc9a7 C |
34 | } = parameters |
35 | ||
0e1dc3e7 C |
36 | const path = '/api/v1/users' |
37 | const body = { | |
38 | username, | |
39 | password, | |
757f0da3 | 40 | role, |
1eddc9a7 | 41 | adminFlags, |
5c98d3bf | 42 | email: username + '@example.com', |
bee0abff FA |
43 | videoQuota, |
44 | videoQuotaDaily | |
0e1dc3e7 C |
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 | ||
df0b219d C |
55 | async function generateUserAccessToken (server: ServerInfo, username: string) { |
56 | const password = 'my super password' | |
1eddc9a7 | 57 | await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) |
df0b219d C |
58 | |
59 | return userLogin(server, { username, password }) | |
60 | } | |
61 | ||
2d53be02 | 62 | function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { |
0e1dc3e7 C |
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 | ||
e590b4a5 | 77 | function registerUserWithChannel (options: { |
a1587156 C |
78 | url: string |
79 | user: { username: string, password: string, displayName?: string } | |
e590b4a5 C |
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 | ||
1f20622f C |
90 | if (options.user.displayName) { |
91 | Object.assign(body, { displayName: options.user.displayName }) | |
92 | } | |
93 | ||
e590b4a5 C |
94 | return makePostBodyRequest({ |
95 | url: options.url, | |
96 | path, | |
97 | fields: body, | |
2d53be02 | 98 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
e590b4a5 C |
99 | }) |
100 | } | |
101 | ||
2d53be02 | 102 | function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) { |
0e1dc3e7 C |
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) | |
26d21b78 | 109 | .expect(specialStatus) |
0e1dc3e7 C |
110 | .expect('Content-Type', /json/) |
111 | } | |
112 | ||
2d53be02 | 113 | function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { |
afff310e RK |
114 | const path = '/api/v1/users/scoped-tokens' |
115 | ||
18490b07 C |
116 | return makeGetRequest({ |
117 | url, | |
118 | path, | |
119 | token, | |
120 | statusCodeExpected | |
121 | }) | |
122 | } | |
123 | ||
2d53be02 | 124 | function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { |
18490b07 C |
125 | const path = '/api/v1/users/scoped-tokens' |
126 | ||
127 | return makePostBodyRequest({ | |
128 | url, | |
129 | path, | |
130 | token, | |
131 | statusCodeExpected | |
132 | }) | |
afff310e RK |
133 | } |
134 | ||
2d53be02 | 135 | function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { |
92b9d60c C |
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 | ||
2d53be02 | 145 | function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) { |
ce5496d6 C |
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 | ||
76314386 | 156 | function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) { |
5c98d3bf C |
157 | const path = '/api/v1/users/' + userId |
158 | ||
159 | return request(url) | |
160 | .get(path) | |
76314386 | 161 | .query({ withStats }) |
5c98d3bf C |
162 | .set('Accept', 'application/json') |
163 | .set('Authorization', 'Bearer ' + accessToken) | |
2d53be02 | 164 | .expect(HttpStatusCode.OK_200) |
5c98d3bf C |
165 | .expect('Content-Type', /json/) |
166 | } | |
167 | ||
2d53be02 | 168 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) { |
0e1dc3e7 C |
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) | |
26d21b78 | 175 | .expect(specialStatus) |
0e1dc3e7 C |
176 | .expect('Content-Type', /json/) |
177 | } | |
178 | ||
86d13ec2 | 179 | function getUsersList (url: string, accessToken: string) { |
0e1dc3e7 C |
180 | const path = '/api/v1/users' |
181 | ||
182 | return request(url) | |
183 | .get(path) | |
184 | .set('Accept', 'application/json') | |
86d13ec2 | 185 | .set('Authorization', 'Bearer ' + accessToken) |
2d53be02 | 186 | .expect(HttpStatusCode.OK_200) |
0e1dc3e7 C |
187 | .expect('Content-Type', /json/) |
188 | } | |
189 | ||
8491293b RK |
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 | ) { | |
0e1dc3e7 C |
199 | const path = '/api/v1/users' |
200 | ||
60919831 C |
201 | const query = { |
202 | start, | |
203 | count, | |
204 | sort, | |
8491293b RK |
205 | search, |
206 | blocked | |
60919831 C |
207 | } |
208 | ||
0e1dc3e7 C |
209 | return request(url) |
210 | .get(path) | |
60919831 | 211 | .query(query) |
0e1dc3e7 | 212 | .set('Accept', 'application/json') |
86d13ec2 | 213 | .set('Authorization', 'Bearer ' + accessToken) |
2d53be02 | 214 | .expect(HttpStatusCode.OK_200) |
0e1dc3e7 C |
215 | .expect('Content-Type', /json/) |
216 | } | |
217 | ||
2d53be02 | 218 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { |
0e1dc3e7 C |
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 | ||
2d53be02 RK |
228 | function blockUser ( |
229 | url: string, | |
230 | userId: number | string, | |
231 | accessToken: string, | |
232 | expectedStatus = HttpStatusCode.NO_CONTENT_204, | |
233 | reason?: string | |
234 | ) { | |
e6921918 | 235 | const path = '/api/v1/users' |
eacb25c4 C |
236 | let body: any |
237 | if (reason) body = { reason } | |
e6921918 C |
238 | |
239 | return request(url) | |
240 | .post(path + '/' + userId + '/block') | |
eacb25c4 | 241 | .send(body) |
e6921918 C |
242 | .set('Accept', 'application/json') |
243 | .set('Authorization', 'Bearer ' + accessToken) | |
244 | .expect(expectedStatus) | |
245 | } | |
246 | ||
2d53be02 | 247 | function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { |
e6921918 C |
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 | ||
2d53be02 | 257 | function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) { |
5c98d3bf | 258 | const path = '/api/v1/users/me' |
0e1dc3e7 | 259 | |
43d0ea7f | 260 | const toSend: UserUpdateMe = omit(options, 'url', 'accessToken') |
26d21b78 C |
261 | |
262 | return makePutBodyRequest({ | |
263 | url: options.url, | |
264 | path, | |
265 | token: options.accessToken, | |
266 | fields: toSend, | |
2d53be02 | 267 | statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204 |
26d21b78 | 268 | }) |
5c98d3bf C |
269 | } |
270 | ||
c5911fd3 | 271 | function updateMyAvatar (options: { |
a1587156 C |
272 | url: string |
273 | accessToken: string | |
c5911fd3 C |
274 | fixture: string |
275 | }) { | |
276 | const path = '/api/v1/users/me/avatar/pick' | |
c5911fd3 | 277 | |
4bbfc6c6 | 278 | return updateAvatarRequest(Object.assign(options, { path })) |
c5911fd3 C |
279 | } |
280 | ||
26d21b78 C |
281 | function updateUser (options: { |
282 | url: string | |
a1587156 C |
283 | userId: number |
284 | accessToken: string | |
285 | email?: string | |
286 | emailVerified?: boolean | |
287 | videoQuota?: number | |
288 | videoQuotaDaily?: number | |
289 | password?: string | |
290 | adminFlags?: UserAdminFlag | |
26d21b78 C |
291 | role?: UserRole |
292 | }) { | |
293 | const path = '/api/v1/users/' + options.userId | |
5c98d3bf C |
294 | |
295 | const toSend = {} | |
b426edd4 | 296 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password |
26d21b78 | 297 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
fc2ec87a | 298 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified |
26d21b78 | 299 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota |
bee0abff | 300 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily |
26d21b78 | 301 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role |
1eddc9a7 | 302 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags |
26d21b78 C |
303 | |
304 | return makePutBodyRequest({ | |
305 | url: options.url, | |
306 | path, | |
307 | token: options.accessToken, | |
308 | fields: toSend, | |
2d53be02 | 309 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
26d21b78 | 310 | }) |
0e1dc3e7 C |
311 | } |
312 | ||
f076daa7 C |
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 }, | |
2d53be02 | 320 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
f076daa7 C |
321 | }) |
322 | } | |
323 | ||
2d53be02 RK |
324 | function resetPassword ( |
325 | url: string, | |
326 | userId: number, | |
327 | verificationString: string, | |
328 | password: string, | |
329 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | |
330 | ) { | |
f076daa7 C |
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 | ||
d9eaee39 JM |
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 }, | |
2d53be02 | 348 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 |
d9eaee39 JM |
349 | }) |
350 | } | |
351 | ||
2d53be02 RK |
352 | function verifyEmail ( |
353 | url: string, | |
354 | userId: number, | |
355 | verificationString: string, | |
356 | isPendingEmail = false, | |
357 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | |
358 | ) { | |
d9eaee39 JM |
359 | const path = '/api/v1/users/' + userId + '/verify-email' |
360 | ||
361 | return makePostBodyRequest({ | |
362 | url, | |
363 | path, | |
d1ab89de C |
364 | fields: { |
365 | verificationString, | |
366 | isPendingEmail | |
367 | }, | |
d9eaee39 JM |
368 | statusCodeExpected |
369 | }) | |
370 | } | |
371 | ||
0e1dc3e7 C |
372 | // --------------------------------------------------------------------------- |
373 | ||
374 | export { | |
375 | createUser, | |
376 | registerUser, | |
5c98d3bf | 377 | getMyUserInformation, |
26d21b78 | 378 | getMyUserVideoRating, |
92b9d60c | 379 | deleteMe, |
e590b4a5 | 380 | registerUserWithChannel, |
ce5496d6 | 381 | getMyUserVideoQuotaUsed, |
0e1dc3e7 C |
382 | getUsersList, |
383 | getUsersListPaginationAndSort, | |
384 | removeUser, | |
5c98d3bf C |
385 | updateUser, |
386 | updateMyUser, | |
c5911fd3 | 387 | getUserInformation, |
e6921918 C |
388 | blockUser, |
389 | unblockUser, | |
f076daa7 C |
390 | askResetPassword, |
391 | resetPassword, | |
18490b07 | 392 | renewUserScopedTokens, |
d9eaee39 JM |
393 | updateMyAvatar, |
394 | askSendVerifyEmail, | |
df0b219d | 395 | generateUserAccessToken, |
afff310e RK |
396 | verifyEmail, |
397 | getUserScopedTokens | |
0e1dc3e7 | 398 | } |