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