]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/users/users.ts
Fix migration and test
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / users.ts
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
11 type CreateUserArgs = {
12 url: string
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 function createUser (parameters: CreateUserArgs) {
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 = 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 generateUserAccessToken (server: ServerInfo, username: string) {
55 const password = 'my super password'
56 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
57
58 return userLogin(server, { username, password })
59 }
60
61 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
62 const path = '/api/v1/users/register'
63 const body = {
64 username,
65 password,
66 email: username + '@example.com'
67 }
68
69 return request(url)
70 .post(path)
71 .set('Accept', 'application/json')
72 .send(body)
73 .expect(specialStatus)
74 }
75
76 function registerUserWithChannel (options: {
77 url: string
78 user: { username: string, password: string, displayName?: string }
79 channel: { name: string, displayName: string }
80 }) {
81 const path = '/api/v1/users/register'
82 const body: UserRegister = {
83 username: options.user.username,
84 password: options.user.password,
85 email: options.user.username + '@example.com',
86 channel: options.channel
87 }
88
89 if (options.user.displayName) {
90 Object.assign(body, { displayName: options.user.displayName })
91 }
92
93 return makePostBodyRequest({
94 url: options.url,
95 path,
96 fields: body,
97 statusCodeExpected: 204
98 })
99 }
100
101 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
102 const path = '/api/v1/users/me'
103
104 return request(url)
105 .get(path)
106 .set('Accept', 'application/json')
107 .set('Authorization', 'Bearer ' + accessToken)
108 .expect(specialStatus)
109 .expect('Content-Type', /json/)
110 }
111
112 function getUserScopedTokens (url: string, token: string, statusCodeExpected = 200) {
113 const path = '/api/v1/users/scoped-tokens'
114
115 return makeGetRequest({
116 url,
117 path,
118 token,
119 statusCodeExpected
120 })
121 }
122
123 function renewUserScopedTokens (url: string, token: string, statusCodeExpected = 200) {
124 const path = '/api/v1/users/scoped-tokens'
125
126 return makePostBodyRequest({
127 url,
128 path,
129 token,
130 statusCodeExpected
131 })
132 }
133
134 function deleteMe (url: string, accessToken: string, specialStatus = 204) {
135 const path = '/api/v1/users/me'
136
137 return request(url)
138 .delete(path)
139 .set('Accept', 'application/json')
140 .set('Authorization', 'Bearer ' + accessToken)
141 .expect(specialStatus)
142 }
143
144 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
145 const path = '/api/v1/users/me/video-quota-used'
146
147 return request(url)
148 .get(path)
149 .set('Accept', 'application/json')
150 .set('Authorization', 'Bearer ' + accessToken)
151 .expect(specialStatus)
152 .expect('Content-Type', /json/)
153 }
154
155 function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
156 const path = '/api/v1/users/' + userId
157
158 return request(url)
159 .get(path)
160 .query({ withStats })
161 .set('Accept', 'application/json')
162 .set('Authorization', 'Bearer ' + accessToken)
163 .expect(200)
164 .expect('Content-Type', /json/)
165 }
166
167 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
168 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
169
170 return request(url)
171 .get(path)
172 .set('Accept', 'application/json')
173 .set('Authorization', 'Bearer ' + accessToken)
174 .expect(specialStatus)
175 .expect('Content-Type', /json/)
176 }
177
178 function getUsersList (url: string, accessToken: string) {
179 const path = '/api/v1/users'
180
181 return request(url)
182 .get(path)
183 .set('Accept', 'application/json')
184 .set('Authorization', 'Bearer ' + accessToken)
185 .expect(200)
186 .expect('Content-Type', /json/)
187 }
188
189 function getUsersListPaginationAndSort (
190 url: string,
191 accessToken: string,
192 start: number,
193 count: number,
194 sort: string,
195 search?: string,
196 blocked?: boolean
197 ) {
198 const path = '/api/v1/users'
199
200 const query = {
201 start,
202 count,
203 sort,
204 search,
205 blocked
206 }
207
208 return request(url)
209 .get(path)
210 .query(query)
211 .set('Accept', 'application/json')
212 .set('Authorization', 'Bearer ' + accessToken)
213 .expect(200)
214 .expect('Content-Type', /json/)
215 }
216
217 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
218 const path = '/api/v1/users'
219
220 return request(url)
221 .delete(path + '/' + userId)
222 .set('Accept', 'application/json')
223 .set('Authorization', 'Bearer ' + accessToken)
224 .expect(expectedStatus)
225 }
226
227 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
228 const path = '/api/v1/users'
229 let body: any
230 if (reason) body = { reason }
231
232 return request(url)
233 .post(path + '/' + userId + '/block')
234 .send(body)
235 .set('Accept', 'application/json')
236 .set('Authorization', 'Bearer ' + accessToken)
237 .expect(expectedStatus)
238 }
239
240 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
241 const path = '/api/v1/users'
242
243 return request(url)
244 .post(path + '/' + userId + '/unblock')
245 .set('Accept', 'application/json')
246 .set('Authorization', 'Bearer ' + accessToken)
247 .expect(expectedStatus)
248 }
249
250 function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) {
251 const path = '/api/v1/users/me'
252
253 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
254
255 return makePutBodyRequest({
256 url: options.url,
257 path,
258 token: options.accessToken,
259 fields: toSend,
260 statusCodeExpected: options.statusCodeExpected || 204
261 })
262 }
263
264 function updateMyAvatar (options: {
265 url: string
266 accessToken: string
267 fixture: string
268 }) {
269 const path = '/api/v1/users/me/avatar/pick'
270
271 return updateAvatarRequest(Object.assign(options, { path }))
272 }
273
274 function updateUser (options: {
275 url: string
276 userId: number
277 accessToken: string
278 email?: string
279 emailVerified?: boolean
280 videoQuota?: number
281 videoQuotaDaily?: number
282 password?: string
283 adminFlags?: UserAdminFlag
284 role?: UserRole
285 }) {
286 const path = '/api/v1/users/' + options.userId
287
288 const toSend = {}
289 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
290 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
291 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
292 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
293 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
294 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
295 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
296
297 return makePutBodyRequest({
298 url: options.url,
299 path,
300 token: options.accessToken,
301 fields: toSend,
302 statusCodeExpected: 204
303 })
304 }
305
306 function askResetPassword (url: string, email: string) {
307 const path = '/api/v1/users/ask-reset-password'
308
309 return makePostBodyRequest({
310 url,
311 path,
312 fields: { email },
313 statusCodeExpected: 204
314 })
315 }
316
317 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
318 const path = '/api/v1/users/' + userId + '/reset-password'
319
320 return makePostBodyRequest({
321 url,
322 path,
323 fields: { password, verificationString },
324 statusCodeExpected
325 })
326 }
327
328 function askSendVerifyEmail (url: string, email: string) {
329 const path = '/api/v1/users/ask-send-verify-email'
330
331 return makePostBodyRequest({
332 url,
333 path,
334 fields: { email },
335 statusCodeExpected: 204
336 })
337 }
338
339 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
340 const path = '/api/v1/users/' + userId + '/verify-email'
341
342 return makePostBodyRequest({
343 url,
344 path,
345 fields: {
346 verificationString,
347 isPendingEmail
348 },
349 statusCodeExpected
350 })
351 }
352
353 // ---------------------------------------------------------------------------
354
355 export {
356 createUser,
357 registerUser,
358 getMyUserInformation,
359 getMyUserVideoRating,
360 deleteMe,
361 registerUserWithChannel,
362 getMyUserVideoQuotaUsed,
363 getUsersList,
364 getUsersListPaginationAndSort,
365 removeUser,
366 updateUser,
367 updateMyUser,
368 getUserInformation,
369 blockUser,
370 unblockUser,
371 askResetPassword,
372 resetPassword,
373 renewUserScopedTokens,
374 updateMyAvatar,
375 askSendVerifyEmail,
376 generateUserAccessToken,
377 verifyEmail,
378 getUserScopedTokens
379 }