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