]>
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, | |
00494d6e | 32 | specialStatus = 201 |
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 | ||
24b9417c | 167 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) { |
0e1dc3e7 C |
168 | const path = '/api/v1/users' |
169 | ||
60919831 C |
170 | const query = { |
171 | start, | |
172 | count, | |
173 | sort, | |
174 | search | |
175 | } | |
176 | ||
0e1dc3e7 C |
177 | return request(url) |
178 | .get(path) | |
60919831 | 179 | .query(query) |
0e1dc3e7 | 180 | .set('Accept', 'application/json') |
86d13ec2 | 181 | .set('Authorization', 'Bearer ' + accessToken) |
0e1dc3e7 C |
182 | .expect(200) |
183 | .expect('Content-Type', /json/) | |
184 | } | |
185 | ||
26d21b78 | 186 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { |
0e1dc3e7 C |
187 | const path = '/api/v1/users' |
188 | ||
189 | return request(url) | |
190 | .delete(path + '/' + userId) | |
191 | .set('Accept', 'application/json') | |
192 | .set('Authorization', 'Bearer ' + accessToken) | |
193 | .expect(expectedStatus) | |
194 | } | |
195 | ||
eacb25c4 | 196 | function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { |
e6921918 | 197 | const path = '/api/v1/users' |
eacb25c4 C |
198 | let body: any |
199 | if (reason) body = { reason } | |
e6921918 C |
200 | |
201 | return request(url) | |
202 | .post(path + '/' + userId + '/block') | |
eacb25c4 | 203 | .send(body) |
e6921918 C |
204 | .set('Accept', 'application/json') |
205 | .set('Authorization', 'Bearer ' + accessToken) | |
206 | .expect(expectedStatus) | |
207 | } | |
208 | ||
209 | function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { | |
210 | const path = '/api/v1/users' | |
211 | ||
212 | return request(url) | |
213 | .post(path + '/' + userId + '/unblock') | |
214 | .set('Accept', 'application/json') | |
215 | .set('Authorization', 'Bearer ' + accessToken) | |
216 | .expect(expectedStatus) | |
217 | } | |
218 | ||
9a7fd960 | 219 | function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) { |
5c98d3bf | 220 | const path = '/api/v1/users/me' |
0e1dc3e7 | 221 | |
43d0ea7f | 222 | const toSend: UserUpdateMe = omit(options, 'url', 'accessToken') |
26d21b78 C |
223 | |
224 | return makePutBodyRequest({ | |
225 | url: options.url, | |
226 | path, | |
227 | token: options.accessToken, | |
228 | fields: toSend, | |
9a7fd960 | 229 | statusCodeExpected: options.statusCodeExpected || 204 |
26d21b78 | 230 | }) |
5c98d3bf C |
231 | } |
232 | ||
c5911fd3 | 233 | function updateMyAvatar (options: { |
a1587156 C |
234 | url: string |
235 | accessToken: string | |
c5911fd3 C |
236 | fixture: string |
237 | }) { | |
238 | const path = '/api/v1/users/me/avatar/pick' | |
c5911fd3 | 239 | |
4bbfc6c6 | 240 | return updateAvatarRequest(Object.assign(options, { path })) |
c5911fd3 C |
241 | } |
242 | ||
26d21b78 C |
243 | function updateUser (options: { |
244 | url: string | |
a1587156 C |
245 | userId: number |
246 | accessToken: string | |
247 | email?: string | |
248 | emailVerified?: boolean | |
249 | videoQuota?: number | |
250 | videoQuotaDaily?: number | |
251 | password?: string | |
252 | adminFlags?: UserAdminFlag | |
26d21b78 C |
253 | role?: UserRole |
254 | }) { | |
255 | const path = '/api/v1/users/' + options.userId | |
5c98d3bf C |
256 | |
257 | const toSend = {} | |
b426edd4 | 258 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password |
26d21b78 | 259 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
fc2ec87a | 260 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified |
26d21b78 | 261 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota |
bee0abff | 262 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily |
26d21b78 | 263 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role |
1eddc9a7 | 264 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags |
26d21b78 C |
265 | |
266 | return makePutBodyRequest({ | |
267 | url: options.url, | |
268 | path, | |
269 | token: options.accessToken, | |
270 | fields: toSend, | |
271 | statusCodeExpected: 204 | |
272 | }) | |
0e1dc3e7 C |
273 | } |
274 | ||
f076daa7 C |
275 | function askResetPassword (url: string, email: string) { |
276 | const path = '/api/v1/users/ask-reset-password' | |
277 | ||
278 | return makePostBodyRequest({ | |
279 | url, | |
280 | path, | |
281 | fields: { email }, | |
282 | statusCodeExpected: 204 | |
283 | }) | |
284 | } | |
285 | ||
286 | function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { | |
287 | const path = '/api/v1/users/' + userId + '/reset-password' | |
288 | ||
289 | return makePostBodyRequest({ | |
290 | url, | |
291 | path, | |
292 | fields: { password, verificationString }, | |
293 | statusCodeExpected | |
294 | }) | |
295 | } | |
296 | ||
d9eaee39 JM |
297 | function askSendVerifyEmail (url: string, email: string) { |
298 | const path = '/api/v1/users/ask-send-verify-email' | |
299 | ||
300 | return makePostBodyRequest({ | |
301 | url, | |
302 | path, | |
303 | fields: { email }, | |
304 | statusCodeExpected: 204 | |
305 | }) | |
306 | } | |
307 | ||
d1ab89de | 308 | function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) { |
d9eaee39 JM |
309 | const path = '/api/v1/users/' + userId + '/verify-email' |
310 | ||
311 | return makePostBodyRequest({ | |
312 | url, | |
313 | path, | |
d1ab89de C |
314 | fields: { |
315 | verificationString, | |
316 | isPendingEmail | |
317 | }, | |
d9eaee39 JM |
318 | statusCodeExpected |
319 | }) | |
320 | } | |
321 | ||
0e1dc3e7 C |
322 | // --------------------------------------------------------------------------- |
323 | ||
324 | export { | |
325 | createUser, | |
326 | registerUser, | |
5c98d3bf | 327 | getMyUserInformation, |
26d21b78 | 328 | getMyUserVideoRating, |
92b9d60c | 329 | deleteMe, |
e590b4a5 | 330 | registerUserWithChannel, |
ce5496d6 | 331 | getMyUserVideoQuotaUsed, |
0e1dc3e7 C |
332 | getUsersList, |
333 | getUsersListPaginationAndSort, | |
334 | removeUser, | |
5c98d3bf C |
335 | updateUser, |
336 | updateMyUser, | |
c5911fd3 | 337 | getUserInformation, |
e6921918 C |
338 | blockUser, |
339 | unblockUser, | |
f076daa7 C |
340 | askResetPassword, |
341 | resetPassword, | |
d9eaee39 JM |
342 | updateMyAvatar, |
343 | askSendVerifyEmail, | |
df0b219d | 344 | generateUserAccessToken, |
d9eaee39 | 345 | verifyEmail |
0e1dc3e7 | 346 | } |