]>
Commit | Line | Data |
---|---|---|
1 | import * as request from 'supertest' | |
2 | import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../' | |
3 | ||
4 | import { UserRole } from '../../../../shared/index' | |
5 | import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type' | |
6 | ||
7 | function createUser ( | |
8 | url: string, | |
9 | accessToken: string, | |
10 | username: string, | |
11 | password: string, | |
12 | videoQuota = 1000000, | |
13 | videoQuotaDaily = -1, | |
14 | role: UserRole = UserRole.USER, | |
15 | specialStatus = 200 | |
16 | ) { | |
17 | const path = '/api/v1/users' | |
18 | const body = { | |
19 | username, | |
20 | password, | |
21 | role, | |
22 | email: username + '@example.com', | |
23 | videoQuota, | |
24 | videoQuotaDaily | |
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 | ||
50 | function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { | |
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) | |
57 | .expect(specialStatus) | |
58 | .expect('Content-Type', /json/) | |
59 | } | |
60 | ||
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 | ||
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 | ||
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 | ||
93 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { | |
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) | |
100 | .expect(specialStatus) | |
101 | .expect('Content-Type', /json/) | |
102 | } | |
103 | ||
104 | function getUsersList (url: string, accessToken: string) { | |
105 | const path = '/api/v1/users' | |
106 | ||
107 | return request(url) | |
108 | .get(path) | |
109 | .set('Accept', 'application/json') | |
110 | .set('Authorization', 'Bearer ' + accessToken) | |
111 | .expect(200) | |
112 | .expect('Content-Type', /json/) | |
113 | } | |
114 | ||
115 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) { | |
116 | const path = '/api/v1/users' | |
117 | ||
118 | return request(url) | |
119 | .get(path) | |
120 | .query({ start }) | |
121 | .query({ count }) | |
122 | .query({ sort }) | |
123 | .set('Accept', 'application/json') | |
124 | .set('Authorization', 'Bearer ' + accessToken) | |
125 | .expect(200) | |
126 | .expect('Content-Type', /json/) | |
127 | } | |
128 | ||
129 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { | |
130 | const path = '/api/v1/users' | |
131 | ||
132 | return request(url) | |
133 | .delete(path + '/' + userId) | |
134 | .set('Accept', 'application/json') | |
135 | .set('Authorization', 'Bearer ' + accessToken) | |
136 | .expect(expectedStatus) | |
137 | } | |
138 | ||
139 | function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { | |
140 | const path = '/api/v1/users' | |
141 | let body: any | |
142 | if (reason) body = { reason } | |
143 | ||
144 | return request(url) | |
145 | .post(path + '/' + userId + '/block') | |
146 | .send(body) | |
147 | .set('Accept', 'application/json') | |
148 | .set('Authorization', 'Bearer ' + accessToken) | |
149 | .expect(expectedStatus) | |
150 | } | |
151 | ||
152 | function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { | |
153 | const path = '/api/v1/users' | |
154 | ||
155 | return request(url) | |
156 | .post(path + '/' + userId + '/unblock') | |
157 | .set('Accept', 'application/json') | |
158 | .set('Authorization', 'Bearer ' + accessToken) | |
159 | .expect(expectedStatus) | |
160 | } | |
161 | ||
162 | function updateMyUser (options: { | |
163 | url: string | |
164 | accessToken: string, | |
165 | newPassword?: string, | |
166 | nsfwPolicy?: NSFWPolicyType, | |
167 | email?: string, | |
168 | autoPlayVideo?: boolean | |
169 | displayName?: string, | |
170 | description?: string | |
171 | }) { | |
172 | const path = '/api/v1/users/me' | |
173 | ||
174 | const toSend = {} | |
175 | if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword | |
176 | if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy | |
177 | if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo | |
178 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
179 | if (options.description !== undefined && options.description !== null) toSend['description'] = options.description | |
180 | if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName | |
181 | ||
182 | return makePutBodyRequest({ | |
183 | url: options.url, | |
184 | path, | |
185 | token: options.accessToken, | |
186 | fields: toSend, | |
187 | statusCodeExpected: 204 | |
188 | }) | |
189 | } | |
190 | ||
191 | function updateMyAvatar (options: { | |
192 | url: string, | |
193 | accessToken: string, | |
194 | fixture: string | |
195 | }) { | |
196 | const path = '/api/v1/users/me/avatar/pick' | |
197 | ||
198 | return updateAvatarRequest(Object.assign(options, { path })) | |
199 | } | |
200 | ||
201 | function updateUser (options: { | |
202 | url: string | |
203 | userId: number, | |
204 | accessToken: string, | |
205 | email?: string, | |
206 | videoQuota?: number, | |
207 | videoQuotaDaily?: number, | |
208 | role?: UserRole | |
209 | }) { | |
210 | const path = '/api/v1/users/' + options.userId | |
211 | ||
212 | const toSend = {} | |
213 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email | |
214 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota | |
215 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily | |
216 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role | |
217 | ||
218 | return makePutBodyRequest({ | |
219 | url: options.url, | |
220 | path, | |
221 | token: options.accessToken, | |
222 | fields: toSend, | |
223 | statusCodeExpected: 204 | |
224 | }) | |
225 | } | |
226 | ||
227 | function askResetPassword (url: string, email: string) { | |
228 | const path = '/api/v1/users/ask-reset-password' | |
229 | ||
230 | return makePostBodyRequest({ | |
231 | url, | |
232 | path, | |
233 | fields: { email }, | |
234 | statusCodeExpected: 204 | |
235 | }) | |
236 | } | |
237 | ||
238 | function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { | |
239 | const path = '/api/v1/users/' + userId + '/reset-password' | |
240 | ||
241 | return makePostBodyRequest({ | |
242 | url, | |
243 | path, | |
244 | fields: { password, verificationString }, | |
245 | statusCodeExpected | |
246 | }) | |
247 | } | |
248 | ||
249 | function askSendVerifyEmail (url: string, email: string) { | |
250 | const path = '/api/v1/users/ask-send-verify-email' | |
251 | ||
252 | return makePostBodyRequest({ | |
253 | url, | |
254 | path, | |
255 | fields: { email }, | |
256 | statusCodeExpected: 204 | |
257 | }) | |
258 | } | |
259 | ||
260 | function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) { | |
261 | const path = '/api/v1/users/' + userId + '/verify-email' | |
262 | ||
263 | return makePostBodyRequest({ | |
264 | url, | |
265 | path, | |
266 | fields: { verificationString }, | |
267 | statusCodeExpected | |
268 | }) | |
269 | } | |
270 | ||
271 | // --------------------------------------------------------------------------- | |
272 | ||
273 | export { | |
274 | createUser, | |
275 | registerUser, | |
276 | getMyUserInformation, | |
277 | getMyUserVideoRating, | |
278 | deleteMe, | |
279 | getMyUserVideoQuotaUsed, | |
280 | getUsersList, | |
281 | getUsersListPaginationAndSort, | |
282 | removeUser, | |
283 | updateUser, | |
284 | updateMyUser, | |
285 | getUserInformation, | |
286 | blockUser, | |
287 | unblockUser, | |
288 | askResetPassword, | |
289 | resetPassword, | |
290 | updateMyAvatar, | |
291 | askSendVerifyEmail, | |
292 | verifyEmail | |
293 | } |