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