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