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