]>
Commit | Line | Data |
---|---|---|
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 | pluginAuth?: string | |
292 | role?: UserRole | |
293 | }) { | |
294 | const path = '/api/v1/users/' + options.userId | |
295 | ||
296 | const toSend = {} | |
297 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password | |
298 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
299 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified | |
300 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota | |
301 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily | |
302 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role | |
303 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags | |
304 | if (options.pluginAuth !== undefined) toSend['pluginAuth'] = options.pluginAuth | |
305 | ||
306 | return makePutBodyRequest({ | |
307 | url: options.url, | |
308 | path, | |
309 | token: options.accessToken, | |
310 | fields: toSend, | |
311 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 | |
312 | }) | |
313 | } | |
314 | ||
315 | function askResetPassword (url: string, email: string) { | |
316 | const path = '/api/v1/users/ask-reset-password' | |
317 | ||
318 | return makePostBodyRequest({ | |
319 | url, | |
320 | path, | |
321 | fields: { email }, | |
322 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 | |
323 | }) | |
324 | } | |
325 | ||
326 | function resetPassword ( | |
327 | url: string, | |
328 | userId: number, | |
329 | verificationString: string, | |
330 | password: string, | |
331 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | |
332 | ) { | |
333 | const path = '/api/v1/users/' + userId + '/reset-password' | |
334 | ||
335 | return makePostBodyRequest({ | |
336 | url, | |
337 | path, | |
338 | fields: { password, verificationString }, | |
339 | statusCodeExpected | |
340 | }) | |
341 | } | |
342 | ||
343 | function askSendVerifyEmail (url: string, email: string) { | |
344 | const path = '/api/v1/users/ask-send-verify-email' | |
345 | ||
346 | return makePostBodyRequest({ | |
347 | url, | |
348 | path, | |
349 | fields: { email }, | |
350 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 | |
351 | }) | |
352 | } | |
353 | ||
354 | function verifyEmail ( | |
355 | url: string, | |
356 | userId: number, | |
357 | verificationString: string, | |
358 | isPendingEmail = false, | |
359 | statusCodeExpected = HttpStatusCode.NO_CONTENT_204 | |
360 | ) { | |
361 | const path = '/api/v1/users/' + userId + '/verify-email' | |
362 | ||
363 | return makePostBodyRequest({ | |
364 | url, | |
365 | path, | |
366 | fields: { | |
367 | verificationString, | |
368 | isPendingEmail | |
369 | }, | |
370 | statusCodeExpected | |
371 | }) | |
372 | } | |
373 | ||
374 | // --------------------------------------------------------------------------- | |
375 | ||
376 | export { | |
377 | createUser, | |
378 | registerUser, | |
379 | getMyUserInformation, | |
380 | getMyUserVideoRating, | |
381 | deleteMe, | |
382 | registerUserWithChannel, | |
383 | getMyUserVideoQuotaUsed, | |
384 | getUsersList, | |
385 | getUsersListPaginationAndSort, | |
386 | removeUser, | |
387 | updateUser, | |
388 | updateMyUser, | |
389 | getUserInformation, | |
390 | blockUser, | |
391 | unblockUser, | |
392 | askResetPassword, | |
393 | resetPassword, | |
394 | renewUserScopedTokens, | |
395 | updateMyAvatar, | |
396 | askSendVerifyEmail, | |
397 | generateUserAccessToken, | |
398 | verifyEmail, | |
399 | getUserScopedTokens | |
400 | } |