]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/users/users.ts
Add ability to specify channel on registration
[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 return request(url)
164 .get(path)
165 .query({ start })
166 .query({ count })
167 .query({ sort })
168 .query({ search })
169 .set('Accept', 'application/json')
170 .set('Authorization', 'Bearer ' + accessToken)
171 .expect(200)
172 .expect('Content-Type', /json/)
173 }
174
175 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
176 const path = '/api/v1/users'
177
178 return request(url)
179 .delete(path + '/' + userId)
180 .set('Accept', 'application/json')
181 .set('Authorization', 'Bearer ' + accessToken)
182 .expect(expectedStatus)
183 }
184
185 function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
186 const path = '/api/v1/users'
187 let body: any
188 if (reason) body = { reason }
189
190 return request(url)
191 .post(path + '/' + userId + '/block')
192 .send(body)
193 .set('Accept', 'application/json')
194 .set('Authorization', 'Bearer ' + accessToken)
195 .expect(expectedStatus)
196 }
197
198 function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
199 const path = '/api/v1/users'
200
201 return request(url)
202 .post(path + '/' + userId + '/unblock')
203 .set('Accept', 'application/json')
204 .set('Authorization', 'Bearer ' + accessToken)
205 .expect(expectedStatus)
206 }
207
208 function updateMyUser (options: {
209 url: string
210 accessToken: string
211 currentPassword?: string
212 newPassword?: string
213 nsfwPolicy?: NSFWPolicyType
214 email?: string
215 autoPlayVideo?: boolean
216 displayName?: string
217 description?: string
218 videosHistoryEnabled?: boolean
219 }) {
220 const path = '/api/v1/users/me'
221
222 const toSend = {}
223 if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
224 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
225 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
226 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
227 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
228 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
229 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
230 if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
231 toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
232 }
233
234 return makePutBodyRequest({
235 url: options.url,
236 path,
237 token: options.accessToken,
238 fields: toSend,
239 statusCodeExpected: 204
240 })
241 }
242
243 function updateMyAvatar (options: {
244 url: string,
245 accessToken: string,
246 fixture: string
247 }) {
248 const path = '/api/v1/users/me/avatar/pick'
249
250 return updateAvatarRequest(Object.assign(options, { path }))
251 }
252
253 function updateUser (options: {
254 url: string
255 userId: number,
256 accessToken: string,
257 email?: string,
258 emailVerified?: boolean,
259 videoQuota?: number,
260 videoQuotaDaily?: number,
261 password?: string,
262 adminFlags?: UserAdminFlag,
263 role?: UserRole
264 }) {
265 const path = '/api/v1/users/' + options.userId
266
267 const toSend = {}
268 if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
269 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
270 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
271 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
272 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
273 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
274 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
275
276 return makePutBodyRequest({
277 url: options.url,
278 path,
279 token: options.accessToken,
280 fields: toSend,
281 statusCodeExpected: 204
282 })
283 }
284
285 function askResetPassword (url: string, email: string) {
286 const path = '/api/v1/users/ask-reset-password'
287
288 return makePostBodyRequest({
289 url,
290 path,
291 fields: { email },
292 statusCodeExpected: 204
293 })
294 }
295
296 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
297 const path = '/api/v1/users/' + userId + '/reset-password'
298
299 return makePostBodyRequest({
300 url,
301 path,
302 fields: { password, verificationString },
303 statusCodeExpected
304 })
305 }
306
307 function askSendVerifyEmail (url: string, email: string) {
308 const path = '/api/v1/users/ask-send-verify-email'
309
310 return makePostBodyRequest({
311 url,
312 path,
313 fields: { email },
314 statusCodeExpected: 204
315 })
316 }
317
318 function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
319 const path = '/api/v1/users/' + userId + '/verify-email'
320
321 return makePostBodyRequest({
322 url,
323 path,
324 fields: { verificationString },
325 statusCodeExpected
326 })
327 }
328
329 // ---------------------------------------------------------------------------
330
331 export {
332 createUser,
333 registerUser,
334 getMyUserInformation,
335 getMyUserVideoRating,
336 deleteMe,
337 registerUserWithChannel,
338 getMyUserVideoQuotaUsed,
339 getUsersList,
340 getUsersListPaginationAndSort,
341 removeUser,
342 updateUser,
343 updateMyUser,
344 getUserInformation,
345 blockUser,
346 unblockUser,
347 askResetPassword,
348 resetPassword,
349 updateMyAvatar,
350 askSendVerifyEmail,
351 generateUserAccessToken,
352 verifyEmail
353 }