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