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