]>
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, | |
32 | specialStatus = 200 | |
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 | ||
5c98d3bf C |
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 | ||
26d21b78 | 144 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { |
0e1dc3e7 C |
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) | |
26d21b78 | 151 | .expect(specialStatus) |
0e1dc3e7 C |
152 | .expect('Content-Type', /json/) |
153 | } | |
154 | ||
86d13ec2 | 155 | function getUsersList (url: string, accessToken: string) { |
0e1dc3e7 C |
156 | const path = '/api/v1/users' |
157 | ||
158 | return request(url) | |
159 | .get(path) | |
160 | .set('Accept', 'application/json') | |
86d13ec2 | 161 | .set('Authorization', 'Bearer ' + accessToken) |
0e1dc3e7 C |
162 | .expect(200) |
163 | .expect('Content-Type', /json/) | |
164 | } | |
165 | ||
24b9417c | 166 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) { |
0e1dc3e7 C |
167 | const path = '/api/v1/users' |
168 | ||
60919831 C |
169 | const query = { |
170 | start, | |
171 | count, | |
172 | sort, | |
173 | search | |
174 | } | |
175 | ||
0e1dc3e7 C |
176 | return request(url) |
177 | .get(path) | |
60919831 | 178 | .query(query) |
0e1dc3e7 | 179 | .set('Accept', 'application/json') |
86d13ec2 | 180 | .set('Authorization', 'Bearer ' + accessToken) |
0e1dc3e7 C |
181 | .expect(200) |
182 | .expect('Content-Type', /json/) | |
183 | } | |
184 | ||
26d21b78 | 185 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { |
0e1dc3e7 C |
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 | ||
eacb25c4 | 195 | function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { |
e6921918 | 196 | const path = '/api/v1/users' |
eacb25c4 C |
197 | let body: any |
198 | if (reason) body = { reason } | |
e6921918 C |
199 | |
200 | return request(url) | |
201 | .post(path + '/' + userId + '/block') | |
eacb25c4 | 202 | .send(body) |
e6921918 C |
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 | ||
43d0ea7f | 218 | function updateMyUser (options: { url: string, accessToken: string } & UserUpdateMe) { |
5c98d3bf | 219 | const path = '/api/v1/users/me' |
0e1dc3e7 | 220 | |
43d0ea7f | 221 | const toSend: UserUpdateMe = omit(options, 'url', 'accessToken') |
26d21b78 C |
222 | |
223 | return makePutBodyRequest({ | |
224 | url: options.url, | |
225 | path, | |
226 | token: options.accessToken, | |
227 | fields: toSend, | |
228 | statusCodeExpected: 204 | |
229 | }) | |
5c98d3bf C |
230 | } |
231 | ||
c5911fd3 | 232 | function updateMyAvatar (options: { |
a1587156 C |
233 | url: string |
234 | accessToken: string | |
c5911fd3 C |
235 | fixture: string |
236 | }) { | |
237 | const path = '/api/v1/users/me/avatar/pick' | |
c5911fd3 | 238 | |
4bbfc6c6 | 239 | return updateAvatarRequest(Object.assign(options, { path })) |
c5911fd3 C |
240 | } |
241 | ||
26d21b78 C |
242 | function updateUser (options: { |
243 | url: string | |
a1587156 C |
244 | userId: number |
245 | accessToken: string | |
246 | email?: string | |
247 | emailVerified?: boolean | |
248 | videoQuota?: number | |
249 | videoQuotaDaily?: number | |
250 | password?: string | |
251 | adminFlags?: UserAdminFlag | |
26d21b78 C |
252 | role?: UserRole |
253 | }) { | |
254 | const path = '/api/v1/users/' + options.userId | |
5c98d3bf C |
255 | |
256 | const toSend = {} | |
b426edd4 | 257 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password |
26d21b78 | 258 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
fc2ec87a | 259 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified |
26d21b78 | 260 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota |
bee0abff | 261 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily |
26d21b78 | 262 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role |
1eddc9a7 | 263 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags |
26d21b78 C |
264 | |
265 | return makePutBodyRequest({ | |
266 | url: options.url, | |
267 | path, | |
268 | token: options.accessToken, | |
269 | fields: toSend, | |
270 | statusCodeExpected: 204 | |
271 | }) | |
0e1dc3e7 C |
272 | } |
273 | ||
f076daa7 C |
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 | ||
d9eaee39 JM |
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 | ||
d1ab89de | 307 | function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) { |
d9eaee39 JM |
308 | const path = '/api/v1/users/' + userId + '/verify-email' |
309 | ||
310 | return makePostBodyRequest({ | |
311 | url, | |
312 | path, | |
d1ab89de C |
313 | fields: { |
314 | verificationString, | |
315 | isPendingEmail | |
316 | }, | |
d9eaee39 JM |
317 | statusCodeExpected |
318 | }) | |
319 | } | |
320 | ||
0e1dc3e7 C |
321 | // --------------------------------------------------------------------------- |
322 | ||
323 | export { | |
324 | createUser, | |
325 | registerUser, | |
5c98d3bf | 326 | getMyUserInformation, |
26d21b78 | 327 | getMyUserVideoRating, |
92b9d60c | 328 | deleteMe, |
e590b4a5 | 329 | registerUserWithChannel, |
ce5496d6 | 330 | getMyUserVideoQuotaUsed, |
0e1dc3e7 C |
331 | getUsersList, |
332 | getUsersListPaginationAndSort, | |
333 | removeUser, | |
5c98d3bf C |
334 | updateUser, |
335 | updateMyUser, | |
c5911fd3 | 336 | getUserInformation, |
e6921918 C |
337 | blockUser, |
338 | unblockUser, | |
f076daa7 C |
339 | askResetPassword, |
340 | resetPassword, | |
d9eaee39 JM |
341 | updateMyAvatar, |
342 | askSendVerifyEmail, | |
df0b219d | 343 | generateUserAccessToken, |
d9eaee39 | 344 | verifyEmail |
0e1dc3e7 | 345 | } |