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