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