]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/users/users.ts
Translated using Weblate (Hungarian)
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / users.ts
1 import * as request from 'supertest'
2 import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
3 import { UserAdminFlag } from '../../models/users/user-flag.model'
4 import { UserRegister } from '../../models/users/user-register.model'
5 import { UserRole } from '../../models/users/user-role'
6 import { ServerInfo } from '../server/servers'
7 import { userLogin } from './login'
8 import { UserUpdateMe } from '../../models/users'
9 import { omit } from 'lodash'
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 deleteMe (url: string, accessToken: string, specialStatus = 204) {
113 const path = '/api/v1/users/me'
114
115 return request(url)
116 .delete(path)
117 .set('Accept', 'application/json')
118 .set('Authorization', 'Bearer ' + accessToken)
119 .expect(specialStatus)
120 }
121
122 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
123 const path = '/api/v1/users/me/video-quota-used'
124
125 return request(url)
126 .get(path)
127 .set('Accept', 'application/json')
128 .set('Authorization', 'Bearer ' + accessToken)
129 .expect(specialStatus)
130 .expect('Content-Type', /json/)
131 }
132
133 function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
134 const path = '/api/v1/users/' + userId
135
136 return request(url)
137 .get(path)
138 .query({ withStats })
139 .set('Accept', 'application/json')
140 .set('Authorization', 'Bearer ' + accessToken)
141 .expect(200)
142 .expect('Content-Type', /json/)
143 }
144
145 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
146 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
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 getUsersList (url: string, accessToken: string) {
157 const path = '/api/v1/users'
158
159 return request(url)
160 .get(path)
161 .set('Accept', 'application/json')
162 .set('Authorization', 'Bearer ' + accessToken)
163 .expect(200)
164 .expect('Content-Type', /json/)
165 }
166
167 function getUsersListPaginationAndSort (
168 url: string,
169 accessToken: string,
170 start: number,
171 count: number,
172 sort: string,
173 search?: string,
174 blocked?: boolean
175 ) {
176 const path = '/api/v1/users'
177
178 const query = {
179 start,
180 count,
181 sort,
182 search,
183 blocked
184 }
185
186 return request(url)
187 .get(path)
188 .query(query)
189 .set('Accept', 'application/json')
190 .set('Authorization', 'Bearer ' + accessToken)
191 .expect(200)
192 .expect('Content-Type', /json/)
193 }
194
195 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
196 const path = '/api/v1/users'
197
198 return request(url)
199 .delete(path + '/' + userId)
200 .set('Accept', 'application/json')
201 .set('Authorization', 'Bearer ' + accessToken)
202 .expect(expectedStatus)
203 }
204
205 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
206 const path = '/api/v1/users'
207 let body: any
208 if (reason) body = { reason }
209
210 return request(url)
211 .post(path + '/' + userId + '/block')
212 .send(body)
213 .set('Accept', 'application/json')
214 .set('Authorization', 'Bearer ' + accessToken)
215 .expect(expectedStatus)
216 }
217
218 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
219 const path = '/api/v1/users'
220
221 return request(url)
222 .post(path + '/' + userId + '/unblock')
223 .set('Accept', 'application/json')
224 .set('Authorization', 'Bearer ' + accessToken)
225 .expect(expectedStatus)
226 }
227
228 function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) {
229 const path = '/api/v1/users/me'
230
231 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
232
233 return makePutBodyRequest({
234 url: options.url,
235 path,
236 token: options.accessToken,
237 fields: toSend,
238 statusCodeExpected: options.statusCodeExpected || 204
239 })
240 }
241
242 function updateMyAvatar (options: {
243 url: string
244 accessToken: string
245 fixture: string
246 }) {
247 const path = '/api/v1/users/me/avatar/pick'
248
249 return updateAvatarRequest(Object.assign(options, { path }))
250 }
251
252 function updateUser (options: {
253 url: string
254 userId: number
255 accessToken: string
256 email?: string
257 emailVerified?: boolean
258 videoQuota?: number
259 videoQuotaDaily?: number
260 password?: string
261 adminFlags?: UserAdminFlag
262 role?: UserRole
263 }) {
264 const path = '/api/v1/users/' + options.userId
265
266 const toSend = {}
267 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
268 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
269 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
270 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
271 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
272 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
273 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
274
275 return makePutBodyRequest({
276 url: options.url,
277 path,
278 token: options.accessToken,
279 fields: toSend,
280 statusCodeExpected: 204
281 })
282 }
283
284 function askResetPassword (url: string, email: string) {
285 const path = '/api/v1/users/ask-reset-password'
286
287 return makePostBodyRequest({
288 url,
289 path,
290 fields: { email },
291 statusCodeExpected: 204
292 })
293 }
294
295 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
296 const path = '/api/v1/users/' + userId + '/reset-password'
297
298 return makePostBodyRequest({
299 url,
300 path,
301 fields: { password, verificationString },
302 statusCodeExpected
303 })
304 }
305
306 function askSendVerifyEmail (url: string, email: string) {
307 const path = '/api/v1/users/ask-send-verify-email'
308
309 return makePostBodyRequest({
310 url,
311 path,
312 fields: { email },
313 statusCodeExpected: 204
314 })
315 }
316
317 function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
318 const path = '/api/v1/users/' + userId + '/verify-email'
319
320 return makePostBodyRequest({
321 url,
322 path,
323 fields: {
324 verificationString,
325 isPendingEmail
326 },
327 statusCodeExpected
328 })
329 }
330
331 // ---------------------------------------------------------------------------
332
333 export {
334 createUser,
335 registerUser,
336 getMyUserInformation,
337 getMyUserVideoRating,
338 deleteMe,
339 registerUserWithChannel,
340 getMyUserVideoQuotaUsed,
341 getUsersList,
342 getUsersListPaginationAndSort,
343 removeUser,
344 updateUser,
345 updateMyUser,
346 getUserInformation,
347 blockUser,
348 unblockUser,
349 askResetPassword,
350 resetPassword,
351 updateMyAvatar,
352 askSendVerifyEmail,
353 generateUserAccessToken,
354 verifyEmail
355 }