]>
Commit | Line | Data |
---|---|---|
0e1dc3e7 | 1 | import * as request from 'supertest' |
d175a6f7 | 2 | import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' |
0e1dc3e7 | 3 | |
9639bd17 | 4 | import { UserRole } from '../../index' |
5 | import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' | |
df0b219d | 6 | import { ServerInfo, userLogin } from '..' |
757f0da3 C |
7 | |
8 | function createUser ( | |
9 | url: string, | |
10 | accessToken: string, | |
11 | username: string, | |
12 | password: string, | |
13 | videoQuota = 1000000, | |
bee0abff | 14 | videoQuotaDaily = -1, |
757f0da3 | 15 | role: UserRole = UserRole.USER, |
f05a1c30 | 16 | specialStatus = 200 |
757f0da3 | 17 | ) { |
0e1dc3e7 C |
18 | const path = '/api/v1/users' |
19 | const body = { | |
20 | username, | |
21 | password, | |
757f0da3 | 22 | role, |
5c98d3bf | 23 | email: username + '@example.com', |
bee0abff FA |
24 | videoQuota, |
25 | videoQuotaDaily | |
0e1dc3e7 C |
26 | } |
27 | ||
28 | return request(url) | |
29 | .post(path) | |
30 | .set('Accept', 'application/json') | |
31 | .set('Authorization', 'Bearer ' + accessToken) | |
32 | .send(body) | |
33 | .expect(specialStatus) | |
34 | } | |
35 | ||
df0b219d C |
36 | async function generateUserAccessToken (server: ServerInfo, username: string) { |
37 | const password = 'my super password' | |
38 | await createUser(server.url, server.accessToken, username, password) | |
39 | ||
40 | return userLogin(server, { username, password }) | |
41 | } | |
42 | ||
0e1dc3e7 C |
43 | function registerUser (url: string, username: string, password: string, specialStatus = 204) { |
44 | const path = '/api/v1/users/register' | |
45 | const body = { | |
46 | username, | |
47 | password, | |
48 | email: username + '@example.com' | |
49 | } | |
50 | ||
51 | return request(url) | |
52 | .post(path) | |
53 | .set('Accept', 'application/json') | |
54 | .send(body) | |
55 | .expect(specialStatus) | |
56 | } | |
57 | ||
26d21b78 | 58 | function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { |
0e1dc3e7 C |
59 | const path = '/api/v1/users/me' |
60 | ||
61 | return request(url) | |
62 | .get(path) | |
63 | .set('Accept', 'application/json') | |
64 | .set('Authorization', 'Bearer ' + accessToken) | |
26d21b78 | 65 | .expect(specialStatus) |
0e1dc3e7 C |
66 | .expect('Content-Type', /json/) |
67 | } | |
68 | ||
92b9d60c C |
69 | function deleteMe (url: string, accessToken: string, specialStatus = 204) { |
70 | const path = '/api/v1/users/me' | |
71 | ||
72 | return request(url) | |
73 | .delete(path) | |
74 | .set('Accept', 'application/json') | |
75 | .set('Authorization', 'Bearer ' + accessToken) | |
76 | .expect(specialStatus) | |
77 | } | |
78 | ||
ce5496d6 C |
79 | function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { |
80 | const path = '/api/v1/users/me/video-quota-used' | |
81 | ||
82 | return request(url) | |
83 | .get(path) | |
84 | .set('Accept', 'application/json') | |
85 | .set('Authorization', 'Bearer ' + accessToken) | |
86 | .expect(specialStatus) | |
87 | .expect('Content-Type', /json/) | |
88 | } | |
89 | ||
5c98d3bf C |
90 | function getUserInformation (url: string, accessToken: string, userId: number) { |
91 | const path = '/api/v1/users/' + userId | |
92 | ||
93 | return request(url) | |
94 | .get(path) | |
95 | .set('Accept', 'application/json') | |
96 | .set('Authorization', 'Bearer ' + accessToken) | |
97 | .expect(200) | |
98 | .expect('Content-Type', /json/) | |
99 | } | |
100 | ||
26d21b78 | 101 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { |
0e1dc3e7 C |
102 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' |
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 | ||
86d13ec2 | 112 | function getUsersList (url: string, accessToken: string) { |
0e1dc3e7 C |
113 | const path = '/api/v1/users' |
114 | ||
115 | return request(url) | |
116 | .get(path) | |
117 | .set('Accept', 'application/json') | |
86d13ec2 | 118 | .set('Authorization', 'Bearer ' + accessToken) |
0e1dc3e7 C |
119 | .expect(200) |
120 | .expect('Content-Type', /json/) | |
121 | } | |
122 | ||
24b9417c | 123 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) { |
0e1dc3e7 C |
124 | const path = '/api/v1/users' |
125 | ||
126 | return request(url) | |
127 | .get(path) | |
128 | .query({ start }) | |
129 | .query({ count }) | |
130 | .query({ sort }) | |
24b9417c | 131 | .query({ search }) |
0e1dc3e7 | 132 | .set('Accept', 'application/json') |
86d13ec2 | 133 | .set('Authorization', 'Bearer ' + accessToken) |
0e1dc3e7 C |
134 | .expect(200) |
135 | .expect('Content-Type', /json/) | |
136 | } | |
137 | ||
26d21b78 | 138 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { |
0e1dc3e7 C |
139 | const path = '/api/v1/users' |
140 | ||
141 | return request(url) | |
142 | .delete(path + '/' + userId) | |
143 | .set('Accept', 'application/json') | |
144 | .set('Authorization', 'Bearer ' + accessToken) | |
145 | .expect(expectedStatus) | |
146 | } | |
147 | ||
eacb25c4 | 148 | function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { |
e6921918 | 149 | const path = '/api/v1/users' |
eacb25c4 C |
150 | let body: any |
151 | if (reason) body = { reason } | |
e6921918 C |
152 | |
153 | return request(url) | |
154 | .post(path + '/' + userId + '/block') | |
eacb25c4 | 155 | .send(body) |
e6921918 C |
156 | .set('Accept', 'application/json') |
157 | .set('Authorization', 'Bearer ' + accessToken) | |
158 | .expect(expectedStatus) | |
159 | } | |
160 | ||
161 | function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { | |
162 | const path = '/api/v1/users' | |
163 | ||
164 | return request(url) | |
165 | .post(path + '/' + userId + '/unblock') | |
166 | .set('Accept', 'application/json') | |
167 | .set('Authorization', 'Bearer ' + accessToken) | |
168 | .expect(expectedStatus) | |
169 | } | |
170 | ||
26d21b78 C |
171 | function updateMyUser (options: { |
172 | url: string | |
8b9a525a C |
173 | accessToken: string |
174 | currentPassword?: string | |
175 | newPassword?: string | |
176 | nsfwPolicy?: NSFWPolicyType | |
177 | email?: string | |
26d21b78 | 178 | autoPlayVideo?: boolean |
8b9a525a | 179 | displayName?: string |
2422c46b | 180 | description?: string |
8b9a525a | 181 | videosHistoryEnabled?: boolean |
26d21b78 | 182 | }) { |
5c98d3bf | 183 | const path = '/api/v1/users/me' |
0e1dc3e7 C |
184 | |
185 | const toSend = {} | |
a890d1e0 | 186 | if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword |
26d21b78 | 187 | if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword |
0883b324 | 188 | if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy |
26d21b78 C |
189 | if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo |
190 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
2422c46b | 191 | if (options.description !== undefined && options.description !== null) toSend['description'] = options.description |
ed56ad11 | 192 | if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName |
8b9a525a C |
193 | if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) { |
194 | toSend['videosHistoryEnabled'] = options.videosHistoryEnabled | |
195 | } | |
26d21b78 C |
196 | |
197 | return makePutBodyRequest({ | |
198 | url: options.url, | |
199 | path, | |
200 | token: options.accessToken, | |
201 | fields: toSend, | |
202 | statusCodeExpected: 204 | |
203 | }) | |
5c98d3bf C |
204 | } |
205 | ||
c5911fd3 C |
206 | function updateMyAvatar (options: { |
207 | url: string, | |
208 | accessToken: string, | |
209 | fixture: string | |
210 | }) { | |
211 | const path = '/api/v1/users/me/avatar/pick' | |
c5911fd3 | 212 | |
4bbfc6c6 | 213 | return updateAvatarRequest(Object.assign(options, { path })) |
c5911fd3 C |
214 | } |
215 | ||
26d21b78 C |
216 | function updateUser (options: { |
217 | url: string | |
218 | userId: number, | |
219 | accessToken: string, | |
220 | email?: string, | |
fc2ec87a | 221 | emailVerified?: boolean, |
26d21b78 | 222 | videoQuota?: number, |
bee0abff | 223 | videoQuotaDaily?: number, |
b426edd4 | 224 | password?: string, |
26d21b78 C |
225 | role?: UserRole |
226 | }) { | |
227 | const path = '/api/v1/users/' + options.userId | |
5c98d3bf C |
228 | |
229 | const toSend = {} | |
b426edd4 | 230 | if (options.password !== undefined && options.password !== null) toSend['password'] = options.password |
26d21b78 | 231 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
fc2ec87a | 232 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified |
26d21b78 | 233 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota |
bee0abff | 234 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily |
26d21b78 C |
235 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role |
236 | ||
237 | return makePutBodyRequest({ | |
238 | url: options.url, | |
239 | path, | |
240 | token: options.accessToken, | |
241 | fields: toSend, | |
242 | statusCodeExpected: 204 | |
243 | }) | |
0e1dc3e7 C |
244 | } |
245 | ||
f076daa7 C |
246 | function askResetPassword (url: string, email: string) { |
247 | const path = '/api/v1/users/ask-reset-password' | |
248 | ||
249 | return makePostBodyRequest({ | |
250 | url, | |
251 | path, | |
252 | fields: { email }, | |
253 | statusCodeExpected: 204 | |
254 | }) | |
255 | } | |
256 | ||
257 | function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { | |
258 | const path = '/api/v1/users/' + userId + '/reset-password' | |
259 | ||
260 | return makePostBodyRequest({ | |
261 | url, | |
262 | path, | |
263 | fields: { password, verificationString }, | |
264 | statusCodeExpected | |
265 | }) | |
266 | } | |
267 | ||
d9eaee39 JM |
268 | function askSendVerifyEmail (url: string, email: string) { |
269 | const path = '/api/v1/users/ask-send-verify-email' | |
270 | ||
271 | return makePostBodyRequest({ | |
272 | url, | |
273 | path, | |
274 | fields: { email }, | |
275 | statusCodeExpected: 204 | |
276 | }) | |
277 | } | |
278 | ||
279 | function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) { | |
280 | const path = '/api/v1/users/' + userId + '/verify-email' | |
281 | ||
282 | return makePostBodyRequest({ | |
283 | url, | |
284 | path, | |
285 | fields: { verificationString }, | |
286 | statusCodeExpected | |
287 | }) | |
288 | } | |
289 | ||
0e1dc3e7 C |
290 | // --------------------------------------------------------------------------- |
291 | ||
292 | export { | |
293 | createUser, | |
294 | registerUser, | |
5c98d3bf | 295 | getMyUserInformation, |
26d21b78 | 296 | getMyUserVideoRating, |
92b9d60c | 297 | deleteMe, |
ce5496d6 | 298 | getMyUserVideoQuotaUsed, |
0e1dc3e7 C |
299 | getUsersList, |
300 | getUsersListPaginationAndSort, | |
301 | removeUser, | |
5c98d3bf C |
302 | updateUser, |
303 | updateMyUser, | |
c5911fd3 | 304 | getUserInformation, |
e6921918 C |
305 | blockUser, |
306 | unblockUser, | |
f076daa7 C |
307 | askResetPassword, |
308 | resetPassword, | |
d9eaee39 JM |
309 | updateMyAvatar, |
310 | askSendVerifyEmail, | |
df0b219d | 311 | generateUserAccessToken, |
d9eaee39 | 312 | verifyEmail |
0e1dc3e7 | 313 | } |