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