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