]>
Commit | Line | Data |
---|---|---|
1 | import * as request from 'supertest' | |
2 | import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' | |
3 | import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' | |
4 | import { UserAdminFlag } from '../../models/users/user-flag.model' | |
5 | import { UserRegister } from '../../models/users/user-register.model' | |
6 | import { UserRole } from '../../models/users/user-role' | |
7 | import { ServerInfo } from '../server/servers' | |
8 | import { userLogin } from './login' | |
9 | ||
10 | type CreateUserArgs = { url: string, | |
11 | accessToken: string, | |
12 | username: string, | |
13 | password: string, | |
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 | ||
33 | const path = '/api/v1/users' | |
34 | const body = { | |
35 | username, | |
36 | password, | |
37 | role, | |
38 | adminFlags, | |
39 | email: username + '@example.com', | |
40 | videoQuota, | |
41 | videoQuotaDaily | |
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 | ||
52 | async function generateUserAccessToken (server: ServerInfo, username: string) { | |
53 | const password = 'my super password' | |
54 | await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) | |
55 | ||
56 | return userLogin(server, { username, password }) | |
57 | } | |
58 | ||
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 | ||
74 | function registerUserWithChannel (options: { | |
75 | url: string, | |
76 | user: { username: string, password: string, displayName?: string }, | |
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 | ||
87 | if (options.user.displayName) { | |
88 | Object.assign(body, { displayName: options.user.displayName }) | |
89 | } | |
90 | ||
91 | return makePostBodyRequest({ | |
92 | url: options.url, | |
93 | path, | |
94 | fields: body, | |
95 | statusCodeExpected: 204 | |
96 | }) | |
97 | } | |
98 | ||
99 | function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { | |
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) | |
106 | .expect(specialStatus) | |
107 | .expect('Content-Type', /json/) | |
108 | } | |
109 | ||
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 | ||
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 | ||
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 | ||
142 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { | |
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) | |
149 | .expect(specialStatus) | |
150 | .expect('Content-Type', /json/) | |
151 | } | |
152 | ||
153 | function getUsersList (url: string, accessToken: string) { | |
154 | const path = '/api/v1/users' | |
155 | ||
156 | return request(url) | |
157 | .get(path) | |
158 | .set('Accept', 'application/json') | |
159 | .set('Authorization', 'Bearer ' + accessToken) | |
160 | .expect(200) | |
161 | .expect('Content-Type', /json/) | |
162 | } | |
163 | ||
164 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) { | |
165 | const path = '/api/v1/users' | |
166 | ||
167 | const query = { | |
168 | start, | |
169 | count, | |
170 | sort, | |
171 | search | |
172 | } | |
173 | ||
174 | return request(url) | |
175 | .get(path) | |
176 | .query(query) | |
177 | .set('Accept', 'application/json') | |
178 | .set('Authorization', 'Bearer ' + accessToken) | |
179 | .expect(200) | |
180 | .expect('Content-Type', /json/) | |
181 | } | |
182 | ||
183 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { | |
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 | ||
193 | function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { | |
194 | const path = '/api/v1/users' | |
195 | let body: any | |
196 | if (reason) body = { reason } | |
197 | ||
198 | return request(url) | |
199 | .post(path + '/' + userId + '/block') | |
200 | .send(body) | |
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 | ||
216 | function updateMyUser (options: { | |
217 | url: string | |
218 | accessToken: string | |
219 | currentPassword?: string | |
220 | newPassword?: string | |
221 | nsfwPolicy?: NSFWPolicyType | |
222 | email?: string | |
223 | autoPlayVideo?: boolean | |
224 | displayName?: string | |
225 | description?: string | |
226 | videosHistoryEnabled?: boolean | |
227 | }) { | |
228 | const path = '/api/v1/users/me' | |
229 | ||
230 | const toSend = {} | |
231 | if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword | |
232 | if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword | |
233 | if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy | |
234 | if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo | |
235 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
236 | if (options.description !== undefined && options.description !== null) toSend['description'] = options.description | |
237 | if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName | |
238 | if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) { | |
239 | toSend['videosHistoryEnabled'] = options.videosHistoryEnabled | |
240 | } | |
241 | ||
242 | return makePutBodyRequest({ | |
243 | url: options.url, | |
244 | path, | |
245 | token: options.accessToken, | |
246 | fields: toSend, | |
247 | statusCodeExpected: 204 | |
248 | }) | |
249 | } | |
250 | ||
251 | function updateMyAvatar (options: { | |
252 | url: string, | |
253 | accessToken: string, | |
254 | fixture: string | |
255 | }) { | |
256 | const path = '/api/v1/users/me/avatar/pick' | |
257 | ||
258 | return updateAvatarRequest(Object.assign(options, { path })) | |
259 | } | |
260 | ||
261 | function updateUser (options: { | |
262 | url: string | |
263 | userId: number, | |
264 | accessToken: string, | |
265 | email?: string, | |
266 | emailVerified?: boolean, | |
267 | videoQuota?: number, | |
268 | videoQuotaDaily?: number, | |
269 | password?: string, | |
270 | adminFlags?: UserAdminFlag, | |
271 | role?: UserRole | |
272 | }) { | |
273 | const path = '/api/v1/users/' + options.userId | |
274 | ||
275 | const toSend = {} | |
276 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password | |
277 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
278 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified | |
279 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota | |
280 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily | |
281 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role | |
282 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags | |
283 | ||
284 | return makePutBodyRequest({ | |
285 | url: options.url, | |
286 | path, | |
287 | token: options.accessToken, | |
288 | fields: toSend, | |
289 | statusCodeExpected: 204 | |
290 | }) | |
291 | } | |
292 | ||
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 | ||
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 | ||
326 | function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) { | |
327 | const path = '/api/v1/users/' + userId + '/verify-email' | |
328 | ||
329 | return makePostBodyRequest({ | |
330 | url, | |
331 | path, | |
332 | fields: { | |
333 | verificationString, | |
334 | isPendingEmail | |
335 | }, | |
336 | statusCodeExpected | |
337 | }) | |
338 | } | |
339 | ||
340 | // --------------------------------------------------------------------------- | |
341 | ||
342 | export { | |
343 | createUser, | |
344 | registerUser, | |
345 | getMyUserInformation, | |
346 | getMyUserVideoRating, | |
347 | deleteMe, | |
348 | registerUserWithChannel, | |
349 | getMyUserVideoQuotaUsed, | |
350 | getUsersList, | |
351 | getUsersListPaginationAndSort, | |
352 | removeUser, | |
353 | updateUser, | |
354 | updateMyUser, | |
355 | getUserInformation, | |
356 | blockUser, | |
357 | unblockUser, | |
358 | askResetPassword, | |
359 | resetPassword, | |
360 | updateMyAvatar, | |
361 | askSendVerifyEmail, | |
362 | generateUserAccessToken, | |
363 | verifyEmail | |
364 | } |