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