]>
Commit | Line | Data |
---|---|---|
0e1dc3e7 | 1 | import * as request from 'supertest' |
8d2be0ed | 2 | import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' |
9639bd17 | 3 | import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' |
1eddc9a7 | 4 | import { UserAdminFlag } from '../../models/users/user-flag.model' |
e590b4a5 | 5 | import { UserRegister } from '../../models/users/user-register.model' |
8d2be0ed C |
6 | import { UserRole } from '../../models/users/user-role' |
7 | import { ServerInfo } from '../server/servers' | |
8 | import { userLogin } from './login' | |
757f0da3 | 9 | |
1eddc9a7 | 10 | type CreateUserArgs = { url: string, |
757f0da3 C |
11 | accessToken: string, |
12 | username: string, | |
13 | password: string, | |
1eddc9a7 C |
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 | ||
0e1dc3e7 C |
33 | const path = '/api/v1/users' |
34 | const body = { | |
35 | username, | |
36 | password, | |
757f0da3 | 37 | role, |
1eddc9a7 | 38 | adminFlags, |
5c98d3bf | 39 | email: username + '@example.com', |
bee0abff FA |
40 | videoQuota, |
41 | videoQuotaDaily | |
0e1dc3e7 C |
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 | ||
df0b219d C |
52 | async function generateUserAccessToken (server: ServerInfo, username: string) { |
53 | const password = 'my super password' | |
1eddc9a7 | 54 | await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) |
df0b219d C |
55 | |
56 | return userLogin(server, { username, password }) | |
57 | } | |
58 | ||
0e1dc3e7 C |
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 | ||
e590b4a5 C |
74 | function registerUserWithChannel (options: { |
75 | url: string, | |
1f20622f | 76 | user: { username: string, password: string, displayName?: string }, |
e590b4a5 C |
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 | ||
1f20622f C |
87 | if (options.user.displayName) { |
88 | Object.assign(body, { displayName: options.user.displayName }) | |
89 | } | |
90 | ||
e590b4a5 C |
91 | return makePostBodyRequest({ |
92 | url: options.url, | |
93 | path, | |
94 | fields: body, | |
95 | statusCodeExpected: 204 | |
96 | }) | |
97 | } | |
98 | ||
26d21b78 | 99 | function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { |
0e1dc3e7 C |
100 | const path = '/api/v1/users/me' |
101 | ||
102 | return request(url) | |
103 | .get(path) | |
104 | .set('Accept', 'application/json') | |
105 | .set('Authorization', 'Bearer ' + accessToken) | |
26d21b78 | 106 | .expect(specialStatus) |
0e1dc3e7 C |
107 | .expect('Content-Type', /json/) |
108 | } | |
109 | ||
92b9d60c C |
110 | function deleteMe (url: string, accessToken: string, specialStatus = 204) { |
111 | const path = '/api/v1/users/me' | |
112 | ||
113 | return request(url) | |
114 | .delete(path) | |
115 | .set('Accept', 'application/json') | |
116 | .set('Authorization', 'Bearer ' + accessToken) | |
117 | .expect(specialStatus) | |
118 | } | |
119 | ||
ce5496d6 C |
120 | function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { |
121 | const path = '/api/v1/users/me/video-quota-used' | |
122 | ||
123 | return request(url) | |
124 | .get(path) | |
125 | .set('Accept', 'application/json') | |
126 | .set('Authorization', 'Bearer ' + accessToken) | |
127 | .expect(specialStatus) | |
128 | .expect('Content-Type', /json/) | |
129 | } | |
130 | ||
5c98d3bf C |
131 | function getUserInformation (url: string, accessToken: string, userId: number) { |
132 | const path = '/api/v1/users/' + userId | |
133 | ||
134 | return request(url) | |
135 | .get(path) | |
136 | .set('Accept', 'application/json') | |
137 | .set('Authorization', 'Bearer ' + accessToken) | |
138 | .expect(200) | |
139 | .expect('Content-Type', /json/) | |
140 | } | |
141 | ||
26d21b78 | 142 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { |
0e1dc3e7 C |
143 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' |
144 | ||
145 | return request(url) | |
146 | .get(path) | |
147 | .set('Accept', 'application/json') | |
148 | .set('Authorization', 'Bearer ' + accessToken) | |
26d21b78 | 149 | .expect(specialStatus) |
0e1dc3e7 C |
150 | .expect('Content-Type', /json/) |
151 | } | |
152 | ||
86d13ec2 | 153 | function getUsersList (url: string, accessToken: string) { |
0e1dc3e7 C |
154 | const path = '/api/v1/users' |
155 | ||
156 | return request(url) | |
157 | .get(path) | |
158 | .set('Accept', 'application/json') | |
86d13ec2 | 159 | .set('Authorization', 'Bearer ' + accessToken) |
0e1dc3e7 C |
160 | .expect(200) |
161 | .expect('Content-Type', /json/) | |
162 | } | |
163 | ||
24b9417c | 164 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) { |
0e1dc3e7 C |
165 | const path = '/api/v1/users' |
166 | ||
60919831 C |
167 | const query = { |
168 | start, | |
169 | count, | |
170 | sort, | |
171 | search | |
172 | } | |
173 | ||
0e1dc3e7 C |
174 | return request(url) |
175 | .get(path) | |
60919831 | 176 | .query(query) |
0e1dc3e7 | 177 | .set('Accept', 'application/json') |
86d13ec2 | 178 | .set('Authorization', 'Bearer ' + accessToken) |
0e1dc3e7 C |
179 | .expect(200) |
180 | .expect('Content-Type', /json/) | |
181 | } | |
182 | ||
26d21b78 | 183 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { |
0e1dc3e7 C |
184 | const path = '/api/v1/users' |
185 | ||
186 | return request(url) | |
187 | .delete(path + '/' + userId) | |
188 | .set('Accept', 'application/json') | |
189 | .set('Authorization', 'Bearer ' + accessToken) | |
190 | .expect(expectedStatus) | |
191 | } | |
192 | ||
eacb25c4 | 193 | function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { |
e6921918 | 194 | const path = '/api/v1/users' |
eacb25c4 C |
195 | let body: any |
196 | if (reason) body = { reason } | |
e6921918 C |
197 | |
198 | return request(url) | |
199 | .post(path + '/' + userId + '/block') | |
eacb25c4 | 200 | .send(body) |
e6921918 C |
201 | .set('Accept', 'application/json') |
202 | .set('Authorization', 'Bearer ' + accessToken) | |
203 | .expect(expectedStatus) | |
204 | } | |
205 | ||
206 | function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { | |
207 | const path = '/api/v1/users' | |
208 | ||
209 | return request(url) | |
210 | .post(path + '/' + userId + '/unblock') | |
211 | .set('Accept', 'application/json') | |
212 | .set('Authorization', 'Bearer ' + accessToken) | |
213 | .expect(expectedStatus) | |
214 | } | |
215 | ||
26d21b78 C |
216 | function updateMyUser (options: { |
217 | url: string | |
8b9a525a C |
218 | accessToken: string |
219 | currentPassword?: string | |
220 | newPassword?: string | |
221 | nsfwPolicy?: NSFWPolicyType | |
222 | email?: string | |
26d21b78 | 223 | autoPlayVideo?: boolean |
8b9a525a | 224 | displayName?: string |
2422c46b | 225 | description?: string |
8b9a525a | 226 | videosHistoryEnabled?: boolean |
26d21b78 | 227 | }) { |
5c98d3bf | 228 | const path = '/api/v1/users/me' |
0e1dc3e7 C |
229 | |
230 | const toSend = {} | |
a890d1e0 | 231 | if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword |
26d21b78 | 232 | if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword |
0883b324 | 233 | if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy |
26d21b78 C |
234 | if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo |
235 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
2422c46b | 236 | if (options.description !== undefined && options.description !== null) toSend['description'] = options.description |
ed56ad11 | 237 | if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName |
8b9a525a C |
238 | if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) { |
239 | toSend['videosHistoryEnabled'] = options.videosHistoryEnabled | |
240 | } | |
26d21b78 C |
241 | |
242 | return makePutBodyRequest({ | |
243 | url: options.url, | |
244 | path, | |
245 | token: options.accessToken, | |
246 | fields: toSend, | |
247 | statusCodeExpected: 204 | |
248 | }) | |
5c98d3bf C |
249 | } |
250 | ||
c5911fd3 C |
251 | function updateMyAvatar (options: { |
252 | url: string, | |
253 | accessToken: string, | |
254 | fixture: string | |
255 | }) { | |
256 | const path = '/api/v1/users/me/avatar/pick' | |
c5911fd3 | 257 | |
4bbfc6c6 | 258 | return updateAvatarRequest(Object.assign(options, { path })) |
c5911fd3 C |
259 | } |
260 | ||
26d21b78 C |
261 | function updateUser (options: { |
262 | url: string | |
263 | userId: number, | |
264 | accessToken: string, | |
265 | email?: string, | |
fc2ec87a | 266 | emailVerified?: boolean, |
26d21b78 | 267 | videoQuota?: number, |
bee0abff | 268 | videoQuotaDaily?: number, |
b426edd4 | 269 | password?: string, |
1eddc9a7 | 270 | adminFlags?: UserAdminFlag, |
26d21b78 C |
271 | role?: UserRole |
272 | }) { | |
273 | const path = '/api/v1/users/' + options.userId | |
5c98d3bf C |
274 | |
275 | const toSend = {} | |
b426edd4 | 276 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password |
26d21b78 | 277 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
fc2ec87a | 278 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified |
26d21b78 | 279 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota |
bee0abff | 280 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily |
26d21b78 | 281 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role |
1eddc9a7 | 282 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags |
26d21b78 C |
283 | |
284 | return makePutBodyRequest({ | |
285 | url: options.url, | |
286 | path, | |
287 | token: options.accessToken, | |
288 | fields: toSend, | |
289 | statusCodeExpected: 204 | |
290 | }) | |
0e1dc3e7 C |
291 | } |
292 | ||
f076daa7 C |
293 | function askResetPassword (url: string, email: string) { |
294 | const path = '/api/v1/users/ask-reset-password' | |
295 | ||
296 | return makePostBodyRequest({ | |
297 | url, | |
298 | path, | |
299 | fields: { email }, | |
300 | statusCodeExpected: 204 | |
301 | }) | |
302 | } | |
303 | ||
304 | function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { | |
305 | const path = '/api/v1/users/' + userId + '/reset-password' | |
306 | ||
307 | return makePostBodyRequest({ | |
308 | url, | |
309 | path, | |
310 | fields: { password, verificationString }, | |
311 | statusCodeExpected | |
312 | }) | |
313 | } | |
314 | ||
d9eaee39 JM |
315 | function askSendVerifyEmail (url: string, email: string) { |
316 | const path = '/api/v1/users/ask-send-verify-email' | |
317 | ||
318 | return makePostBodyRequest({ | |
319 | url, | |
320 | path, | |
321 | fields: { email }, | |
322 | statusCodeExpected: 204 | |
323 | }) | |
324 | } | |
325 | ||
d1ab89de | 326 | function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) { |
d9eaee39 JM |
327 | const path = '/api/v1/users/' + userId + '/verify-email' |
328 | ||
329 | return makePostBodyRequest({ | |
330 | url, | |
331 | path, | |
d1ab89de C |
332 | fields: { |
333 | verificationString, | |
334 | isPendingEmail | |
335 | }, | |
d9eaee39 JM |
336 | statusCodeExpected |
337 | }) | |
338 | } | |
339 | ||
0e1dc3e7 C |
340 | // --------------------------------------------------------------------------- |
341 | ||
342 | export { | |
343 | createUser, | |
344 | registerUser, | |
5c98d3bf | 345 | getMyUserInformation, |
26d21b78 | 346 | getMyUserVideoRating, |
92b9d60c | 347 | deleteMe, |
e590b4a5 | 348 | registerUserWithChannel, |
ce5496d6 | 349 | getMyUserVideoQuotaUsed, |
0e1dc3e7 C |
350 | getUsersList, |
351 | getUsersListPaginationAndSort, | |
352 | removeUser, | |
5c98d3bf C |
353 | updateUser, |
354 | updateMyUser, | |
c5911fd3 | 355 | getUserInformation, |
e6921918 C |
356 | blockUser, |
357 | unblockUser, | |
f076daa7 C |
358 | askResetPassword, |
359 | resetPassword, | |
d9eaee39 JM |
360 | updateMyAvatar, |
361 | askSendVerifyEmail, | |
df0b219d | 362 | generateUserAccessToken, |
d9eaee39 | 363 | verifyEmail |
0e1dc3e7 | 364 | } |