diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/tests/api/check-params/services.ts | 1 | ||||
-rw-r--r-- | server/tests/api/check-params/users.ts | 423 | ||||
-rw-r--r-- | server/tests/api/users/users.ts | 4 | ||||
-rw-r--r-- | server/tests/utils/miscs/miscs.ts | 7 | ||||
-rw-r--r-- | server/tests/utils/users/users.ts | 78 |
5 files changed, 172 insertions, 341 deletions
diff --git a/server/tests/api/check-params/services.ts b/server/tests/api/check-params/services.ts index 62a14f51f..fcde7e179 100644 --- a/server/tests/api/check-params/services.ts +++ b/server/tests/api/check-params/services.ts | |||
@@ -102,7 +102,6 @@ function checkParamEmbed (server: ServerInfo, embedUrl: string, statusCodeExpect | |||
102 | url: server.url, | 102 | url: server.url, |
103 | path, | 103 | path, |
104 | query: Object.assign(query, { url: embedUrl }), | 104 | query: Object.assign(query, { url: embedUrl }), |
105 | token: server.accessToken, | ||
106 | statusCodeExpected | 105 | statusCodeExpected |
107 | }) | 106 | }) |
108 | } | 107 | } |
diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index b566a2f1e..0c126dbff 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts | |||
@@ -1,25 +1,15 @@ | |||
1 | /* tslint:disable:no-unused-expression */ | 1 | /* tslint:disable:no-unused-expression */ |
2 | 2 | ||
3 | import * as request from 'supertest' | 3 | import { omit } from 'lodash' |
4 | import 'mocha' | 4 | import 'mocha' |
5 | import { UserRole } from '../../../../shared' | ||
5 | 6 | ||
6 | import { | 7 | import { |
7 | ServerInfo, | 8 | createUser, flushTests, getMyUserInformation, getMyUserVideoRating, getUsersList, immutableAssign, killallServers, makeGetRequest, |
8 | flushTests, | 9 | makePostBodyRequest, makePutBodyRequest, registerUser, removeUser, runServer, ServerInfo, setAccessTokensToServers, updateUser, |
9 | runServer, | 10 | uploadVideo, userLogin |
10 | uploadVideo, | ||
11 | getVideosList, | ||
12 | makePutBodyRequest, | ||
13 | createUser, | ||
14 | serverLogin, | ||
15 | getUsersList, | ||
16 | registerUser, | ||
17 | setAccessTokensToServers, | ||
18 | killallServers, | ||
19 | makePostBodyRequest, | ||
20 | userLogin | ||
21 | } from '../../utils' | 11 | } from '../../utils' |
22 | import { UserRole } from '../../../../shared' | 12 | import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params' |
23 | 13 | ||
24 | describe('Test users API validators', function () { | 14 | describe('Test users API validators', function () { |
25 | const path = '/api/v1/users/' | 15 | const path = '/api/v1/users/' |
@@ -42,269 +32,169 @@ describe('Test users API validators', function () { | |||
42 | 32 | ||
43 | await setAccessTokensToServers([ server ]) | 33 | await setAccessTokensToServers([ server ]) |
44 | 34 | ||
45 | const username = 'user1' | ||
46 | const password = 'my super password' | ||
47 | const videoQuota = 42000000 | ||
48 | await createUser(server.url, server.accessToken, username, password, videoQuota) | ||
49 | |||
50 | const videoAttributes = {} | ||
51 | await uploadVideo(server.url, server.accessToken, videoAttributes) | ||
52 | |||
53 | const res = await getVideosList(server.url) | ||
54 | const videos = res.body.data | ||
55 | videoId = videos[0].id | ||
56 | |||
57 | const user = { | 35 | const user = { |
58 | username: 'user1', | 36 | username: 'user1', |
59 | password: 'my super password' | 37 | password: 'my super password' |
60 | } | 38 | } |
39 | const videoQuota = 42000000 | ||
40 | await createUser(server.url, server.accessToken, user.username, user.password, videoQuota) | ||
61 | userAccessToken = await userLogin(server, user) | 41 | userAccessToken = await userLogin(server, user) |
42 | |||
43 | const res = await uploadVideo(server.url, server.accessToken, {}) | ||
44 | videoId = res.body.video.id | ||
62 | }) | 45 | }) |
63 | 46 | ||
64 | describe('When listing users', function () { | 47 | describe('When listing users', function () { |
65 | it('Should fail with a bad start pagination', async function () { | 48 | it('Should fail with a bad start pagination', async function () { |
66 | await request(server.url) | 49 | await checkBadStartPagination(server.url, path, server.accessToken) |
67 | .get(path) | ||
68 | .query({ start: 'hello' }) | ||
69 | .set('Accept', 'application/json') | ||
70 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
71 | .expect(400) | ||
72 | }) | 50 | }) |
73 | 51 | ||
74 | it('Should fail with a bad count pagination', async function () { | 52 | it('Should fail with a bad count pagination', async function () { |
75 | await request(server.url) | 53 | await checkBadCountPagination(server.url, path, server.accessToken) |
76 | .get(path) | ||
77 | .query({ count: 'hello' }) | ||
78 | .set('Accept', 'application/json') | ||
79 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
80 | .expect(400) | ||
81 | }) | 54 | }) |
82 | 55 | ||
83 | it('Should fail with an incorrect sort', async function () { | 56 | it('Should fail with an incorrect sort', async function () { |
84 | await request(server.url) | 57 | await checkBadSortPagination(server.url, path, server.accessToken) |
85 | .get(path) | ||
86 | .query({ sort: 'hello' }) | ||
87 | .set('Accept', 'application/json') | ||
88 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
89 | .expect(400) | ||
90 | }) | 58 | }) |
91 | 59 | ||
92 | it('Should fail with a non authenticated user', async function () { | 60 | it('Should fail with a non authenticated user', async function () { |
93 | await request(server.url) | 61 | await makeGetRequest({ |
94 | .get(path) | 62 | url: server.url, |
95 | .set('Accept', 'application/json') | 63 | path, |
96 | .expect(401) | 64 | statusCodeExpected: 401 |
65 | }) | ||
97 | }) | 66 | }) |
98 | 67 | ||
99 | it('Should fail with a non admin user', async function () { | 68 | it('Should fail with a non admin user', async function () { |
100 | await request(server.url) | 69 | await makeGetRequest({ |
101 | .get(path) | 70 | url: server.url, |
102 | .set('Accept', 'application/json') | 71 | path, |
103 | .set('Authorization', 'Bearer ' + userAccessToken) | 72 | token: userAccessToken, |
104 | .expect(403) | 73 | statusCodeExpected: 403 |
74 | }) | ||
105 | }) | 75 | }) |
106 | }) | 76 | }) |
107 | 77 | ||
108 | describe('When adding a new user', function () { | 78 | describe('When adding a new user', function () { |
79 | const baseCorrectParams = { | ||
80 | username: 'user2', | ||
81 | email: 'test@example.com', | ||
82 | password: 'my super password', | ||
83 | videoQuota: -1, | ||
84 | role: UserRole.USER | ||
85 | } | ||
86 | |||
109 | it('Should fail with a too small username', async function () { | 87 | it('Should fail with a too small username', async function () { |
110 | const fields = { | 88 | const fields = immutableAssign(baseCorrectParams, { username: 'fi' }) |
111 | username: 'ji', | ||
112 | email: 'test@example.com', | ||
113 | password: 'my_super_password', | ||
114 | role: UserRole.USER, | ||
115 | videoQuota: 42000000 | ||
116 | } | ||
117 | 89 | ||
118 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 90 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
119 | }) | 91 | }) |
120 | 92 | ||
121 | it('Should fail with a too long username', async function () { | 93 | it('Should fail with a too long username', async function () { |
122 | const fields = { | 94 | const fields = immutableAssign(baseCorrectParams, { username: 'my_super_username_which_is_very_long' }) |
123 | username: 'my_super_username_which_is_very_long', | ||
124 | email: 'test@example.com', | ||
125 | password: 'my_super_password', | ||
126 | videoQuota: 42000000, | ||
127 | role: UserRole.USER | ||
128 | } | ||
129 | 95 | ||
130 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 96 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
131 | }) | 97 | }) |
132 | 98 | ||
133 | it('Should fail with a not lowercase username', async function () { | 99 | it('Should fail with a not lowercase username', async function () { |
134 | const fields = { | 100 | const fields = immutableAssign(baseCorrectParams, { username: 'Toto' }) |
135 | username: 'Toto', | ||
136 | email: 'test@example.com', | ||
137 | password: 'my_super_password', | ||
138 | videoQuota: 42000000, | ||
139 | role: UserRole.USER | ||
140 | } | ||
141 | 101 | ||
142 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 102 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
143 | }) | 103 | }) |
144 | 104 | ||
145 | it('Should fail with an incorrect username', async function () { | 105 | it('Should fail with an incorrect username', async function () { |
146 | const fields = { | 106 | const fields = immutableAssign(baseCorrectParams, { username: 'my username' }) |
147 | username: 'my username', | ||
148 | email: 'test@example.com', | ||
149 | password: 'my_super_password', | ||
150 | videoQuota: 42000000, | ||
151 | role: UserRole.USER | ||
152 | } | ||
153 | 107 | ||
154 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 108 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
155 | }) | 109 | }) |
156 | 110 | ||
157 | it('Should fail with a missing email', async function () { | 111 | it('Should fail with a missing email', async function () { |
158 | const fields = { | 112 | const fields = omit(baseCorrectParams, 'email') |
159 | username: 'ji', | ||
160 | password: 'my_super_password', | ||
161 | videoQuota: 42000000, | ||
162 | role: UserRole.USER | ||
163 | } | ||
164 | 113 | ||
165 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 114 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
166 | }) | 115 | }) |
167 | 116 | ||
168 | it('Should fail with an invalid email', async function () { | 117 | it('Should fail with an invalid email', async function () { |
169 | const fields = { | 118 | const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' }) |
170 | username: 'my_super_username_which_is_very_long', | ||
171 | email: 'test_example.com', | ||
172 | password: 'my_super_password', | ||
173 | videoQuota: 42000000, | ||
174 | role: UserRole.USER | ||
175 | } | ||
176 | 119 | ||
177 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 120 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
178 | }) | 121 | }) |
179 | 122 | ||
180 | it('Should fail with a too small password', async function () { | 123 | it('Should fail with a too small password', async function () { |
181 | const fields = { | 124 | const fields = immutableAssign(baseCorrectParams, { password: 'bla' }) |
182 | username: 'my_username', | ||
183 | email: 'test@example.com', | ||
184 | password: 'bla', | ||
185 | videoQuota: 42000000, | ||
186 | role: UserRole.USER | ||
187 | } | ||
188 | 125 | ||
189 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 126 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
190 | }) | 127 | }) |
191 | 128 | ||
192 | it('Should fail with a too long password', async function () { | 129 | it('Should fail with a too long password', async function () { |
193 | const fields = { | 130 | const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) }) |
194 | username: 'my_username', | ||
195 | email: 'test@example.com', | ||
196 | password: 'my super long password which is very very very very very very very very very very very very very very' + | ||
197 | 'very very very very very very very very very very very very very very very veryv very very very very' + | ||
198 | 'very very very very very very very very very very very very very very very very very very very very long', | ||
199 | videoQuota: 42000000, | ||
200 | role: UserRole.USER | ||
201 | } | ||
202 | 131 | ||
203 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 132 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
204 | }) | 133 | }) |
205 | 134 | ||
206 | it('Should fail with an non authenticated user', async function () { | 135 | it('Should fail with an non authenticated user', async function () { |
207 | const fields = { | 136 | await makePostBodyRequest({ |
208 | username: 'my_username', | 137 | url: server.url, |
209 | email: 'test@example.com', | 138 | path, |
210 | password: 'my super password', | 139 | token: 'super token', |
211 | videoQuota: 42000000, | 140 | fields: baseCorrectParams, |
212 | role: UserRole.USER | 141 | statusCodeExpected: 401 |
213 | } | 142 | }) |
214 | |||
215 | await makePostBodyRequest({ url: server.url, path, token: 'super token', fields, statusCodeExpected: 401 }) | ||
216 | }) | 143 | }) |
217 | 144 | ||
218 | it('Should fail if we add a user with the same username', async function () { | 145 | it('Should fail if we add a user with the same username', async function () { |
219 | const fields = { | 146 | const fields = immutableAssign(baseCorrectParams, { username: 'user1' }) |
220 | username: 'user1', | ||
221 | email: 'test@example.com', | ||
222 | password: 'my super password', | ||
223 | videoQuota: 42000000, | ||
224 | role: UserRole.USER | ||
225 | } | ||
226 | 147 | ||
227 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 }) | 148 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 }) |
228 | }) | 149 | }) |
229 | 150 | ||
230 | it('Should fail if we add a user with the same email', async function () { | 151 | it('Should fail if we add a user with the same email', async function () { |
231 | const fields = { | 152 | const fields = immutableAssign(baseCorrectParams, { email: 'user1@example.com' }) |
232 | username: 'my_username', | ||
233 | email: 'user1@example.com', | ||
234 | password: 'my super password', | ||
235 | videoQuota: 42000000, | ||
236 | role: UserRole.USER | ||
237 | } | ||
238 | 153 | ||
239 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 }) | 154 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 }) |
240 | }) | 155 | }) |
241 | 156 | ||
242 | it('Should fail without a videoQuota', async function () { | 157 | it('Should fail without a videoQuota', async function () { |
243 | const fields = { | 158 | const fields = omit(baseCorrectParams, 'videoQuota') |
244 | username: 'my_username', | ||
245 | email: 'user1@example.com', | ||
246 | password: 'my super password', | ||
247 | role: UserRole.USER | ||
248 | } | ||
249 | 159 | ||
250 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 160 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
251 | }) | 161 | }) |
252 | 162 | ||
253 | it('Should fail with an invalid videoQuota', async function () { | 163 | it('Should fail with an invalid videoQuota', async function () { |
254 | const fields = { | 164 | const fields = immutableAssign(baseCorrectParams, { videoQuota: -5 }) |
255 | username: 'my_username', | ||
256 | email: 'user1@example.com', | ||
257 | password: 'my super password', | ||
258 | videoQuota: -5, | ||
259 | role: UserRole.USER | ||
260 | } | ||
261 | 165 | ||
262 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 166 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
263 | }) | 167 | }) |
264 | 168 | ||
265 | it('Should fail without a user role', async function () { | 169 | it('Should fail without a user role', async function () { |
266 | const fields = { | 170 | const fields = omit(baseCorrectParams, 'role') |
267 | username: 'my_username', | ||
268 | email: 'user1@example.com', | ||
269 | password: 'my super password', | ||
270 | videoQuota: 0 | ||
271 | } | ||
272 | 171 | ||
273 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 172 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
274 | }) | 173 | }) |
275 | 174 | ||
276 | it('Should fail with an invalid user role', async function () { | 175 | it('Should fail with an invalid user role', async function () { |
277 | const fields = { | 176 | const fields = immutableAssign(baseCorrectParams, { role: 88989 }) |
278 | username: 'my_username', | ||
279 | email: 'user1@example.com', | ||
280 | password: 'my super password', | ||
281 | videoQuota: 0, | ||
282 | role: 88989 | ||
283 | } | ||
284 | 177 | ||
285 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | 178 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) |
286 | }) | 179 | }) |
287 | 180 | ||
288 | it('Should succeed with the correct params', async function () { | 181 | it('Should succeed with the correct params', async function () { |
289 | const fields = { | 182 | await makePostBodyRequest({ |
290 | username: 'user2', | 183 | url: server.url, |
291 | email: 'test@example.com', | 184 | path, |
292 | password: 'my super password', | 185 | token: server.accessToken, |
293 | videoQuota: -1, | 186 | fields: baseCorrectParams, |
294 | role: UserRole.USER | 187 | statusCodeExpected: 204 |
295 | } | 188 | }) |
296 | |||
297 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 }) | ||
298 | }) | 189 | }) |
299 | 190 | ||
300 | it('Should fail with a non admin user', async function () { | 191 | it('Should fail with a non admin user', async function () { |
301 | server.user = { | 192 | const user = { |
302 | username: 'user1', | 193 | username: 'user1', |
303 | email: 'test@example.com', | ||
304 | password: 'my super password' | 194 | password: 'my super password' |
305 | } | 195 | } |
196 | userAccessToken = await userLogin(server, user) | ||
306 | 197 | ||
307 | userAccessToken = await serverLogin(server) | ||
308 | const fields = { | 198 | const fields = { |
309 | username: 'user3', | 199 | username: 'user3', |
310 | email: 'test@example.com', | 200 | email: 'test@example.com', |
@@ -334,9 +224,7 @@ describe('Test users API validators', function () { | |||
334 | 224 | ||
335 | it('Should fail with a too long password', async function () { | 225 | it('Should fail with a too long password', async function () { |
336 | const fields = { | 226 | const fields = { |
337 | password: 'my super long password which is very very very very very very very very very very very very very very' + | 227 | password: 'super'.repeat(61) |
338 | 'very very very very very very very very very very very very very very very veryv very very very very' + | ||
339 | 'very very very very very very very very very very very very very very very very very very very very long' | ||
340 | } | 228 | } |
341 | 229 | ||
342 | await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) | 230 | await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) |
@@ -432,204 +320,128 @@ describe('Test users API validators', function () { | |||
432 | 320 | ||
433 | describe('When getting my information', function () { | 321 | describe('When getting my information', function () { |
434 | it('Should fail with a non authenticated user', async function () { | 322 | it('Should fail with a non authenticated user', async function () { |
435 | await request(server.url) | 323 | await getMyUserInformation(server.url, 'fake_token', 401) |
436 | .get(path + 'me') | ||
437 | .set('Authorization', 'Bearer fake_token') | ||
438 | .set('Accept', 'application/json') | ||
439 | .expect(401) | ||
440 | }) | 324 | }) |
441 | 325 | ||
442 | it('Should success with the correct parameters', async function () { | 326 | it('Should success with the correct parameters', async function () { |
443 | await request(server.url) | 327 | await getMyUserInformation(server.url, userAccessToken) |
444 | .get(path + 'me') | ||
445 | .set('Authorization', 'Bearer ' + userAccessToken) | ||
446 | .set('Accept', 'application/json') | ||
447 | .expect(200) | ||
448 | }) | 328 | }) |
449 | }) | 329 | }) |
450 | 330 | ||
451 | describe('When getting my video rating', function () { | 331 | describe('When getting my video rating', function () { |
452 | it('Should fail with a non authenticated user', async function () { | 332 | it('Should fail with a non authenticated user', async function () { |
453 | await request(server.url) | 333 | await getMyUserVideoRating(server.url, 'fake_token', videoId, 401) |
454 | .get(path + 'me/videos/' + videoId + '/rating') | ||
455 | .set('Authorization', 'Bearer fake_token') | ||
456 | .set('Accept', 'application/json') | ||
457 | .expect(401) | ||
458 | }) | 334 | }) |
459 | 335 | ||
460 | it('Should fail with an incorrect video uuid', async function () { | 336 | it('Should fail with an incorrect video uuid', async function () { |
461 | await request(server.url) | 337 | await getMyUserVideoRating(server.url, server.accessToken, 'blabla', 400) |
462 | .get(path + 'me/videos/blabla/rating') | ||
463 | .set('Authorization', 'Bearer ' + userAccessToken) | ||
464 | .set('Accept', 'application/json') | ||
465 | .expect(400) | ||
466 | }) | 338 | }) |
467 | 339 | ||
468 | it('Should fail with an unknown video', async function () { | 340 | it('Should fail with an unknown video', async function () { |
469 | await request(server.url) | 341 | await getMyUserVideoRating(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', 404) |
470 | .get(path + 'me/videos/4da6fde3-88f7-4d16-b119-108df5630b06/rating') | ||
471 | .set('Authorization', 'Bearer ' + userAccessToken) | ||
472 | .set('Accept', 'application/json') | ||
473 | .expect(404) | ||
474 | }) | 342 | }) |
475 | 343 | ||
476 | it('Should success with the correct parameters', async function () { | 344 | it('Should succeed with the correct parameters', async function () { |
477 | await request(server.url) | 345 | await getMyUserVideoRating(server.url, server.accessToken, videoId) |
478 | .get(path + 'me/videos/' + videoId + '/rating') | ||
479 | .set('Authorization', 'Bearer ' + userAccessToken) | ||
480 | .set('Accept', 'application/json') | ||
481 | .expect(200) | ||
482 | }) | ||
483 | }) | ||
484 | |||
485 | describe('When removing an user', function () { | ||
486 | it('Should fail with an incorrect id', async function () { | ||
487 | await request(server.url) | ||
488 | .delete(path + 'bla-bla') | ||
489 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
490 | .expect(400) | ||
491 | }) | ||
492 | |||
493 | it('Should fail with the root user', async function () { | ||
494 | await request(server.url) | ||
495 | .delete(path + rootId) | ||
496 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
497 | .expect(400) | ||
498 | }) | ||
499 | |||
500 | it('Should return 404 with a non existing id', async function () { | ||
501 | await request(server.url) | ||
502 | .delete(path + '45') | ||
503 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
504 | .expect(404) | ||
505 | }) | 346 | }) |
506 | }) | 347 | }) |
507 | 348 | ||
508 | describe('When removing an user', function () { | 349 | describe('When removing an user', function () { |
509 | it('Should fail with an incorrect id', async function () { | 350 | it('Should fail with an incorrect id', async function () { |
510 | await request(server.url) | 351 | await removeUser(server.url, 'blabla', server.accessToken, 400) |
511 | .delete(path + 'bla-bla') | ||
512 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
513 | .expect(400) | ||
514 | }) | 352 | }) |
515 | 353 | ||
516 | it('Should fail with the root user', async function () { | 354 | it('Should fail with the root user', async function () { |
517 | await request(server.url) | 355 | await removeUser(server.url, rootId, server.accessToken, 400) |
518 | .delete(path + rootId) | ||
519 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
520 | .expect(400) | ||
521 | }) | 356 | }) |
522 | 357 | ||
523 | it('Should return 404 with a non existing id', async function () { | 358 | it('Should return 404 with a non existing id', async function () { |
524 | await request(server.url) | 359 | await removeUser(server.url, 4545454, server.accessToken, 404) |
525 | .delete(path + '45') | ||
526 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
527 | .expect(404) | ||
528 | }) | 360 | }) |
529 | }) | 361 | }) |
530 | 362 | ||
531 | describe('When register a new user', function () { | 363 | describe('When register a new user', function () { |
532 | const registrationPath = path + '/register' | 364 | const registrationPath = path + '/register' |
365 | const baseCorrectParams = { | ||
366 | username: 'user3', | ||
367 | email: 'test3@example.com', | ||
368 | password: 'my super password' | ||
369 | } | ||
533 | 370 | ||
534 | it('Should fail with a too small username', async function () { | 371 | it('Should fail with a too small username', async function () { |
535 | const fields = { | 372 | const fields = immutableAssign(baseCorrectParams, { username: 'ji' }) |
536 | username: 'ji', | ||
537 | email: 'test@example.com', | ||
538 | password: 'my_super_password' | ||
539 | } | ||
540 | 373 | ||
541 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) | 374 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) |
542 | }) | 375 | }) |
543 | 376 | ||
544 | it('Should fail with a too long username', async function () { | 377 | it('Should fail with a too long username', async function () { |
545 | const fields = { | 378 | const fields = immutableAssign(baseCorrectParams, { username: 'my_super_username_which_is_very_long' }) |
546 | username: 'my_super_username_which_is_very_long', | ||
547 | email: 'test@example.com', | ||
548 | password: 'my_super_password' | ||
549 | } | ||
550 | 379 | ||
551 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) | 380 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) |
552 | }) | 381 | }) |
553 | 382 | ||
554 | it('Should fail with an incorrect username', async function () { | 383 | it('Should fail with an incorrect username', async function () { |
555 | const fields = { | 384 | const fields = immutableAssign(baseCorrectParams, { username: 'my username' }) |
556 | username: 'my username', | ||
557 | email: 'test@example.com', | ||
558 | password: 'my_super_password' | ||
559 | } | ||
560 | 385 | ||
561 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) | 386 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) |
562 | }) | 387 | }) |
563 | 388 | ||
564 | it('Should fail with a missing email', async function () { | 389 | it('Should fail with a missing email', async function () { |
565 | const fields = { | 390 | const fields = omit(baseCorrectParams, 'email') |
566 | username: 'ji', | ||
567 | password: 'my_super_password' | ||
568 | } | ||
569 | 391 | ||
570 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) | 392 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) |
571 | }) | 393 | }) |
572 | 394 | ||
573 | it('Should fail with an invalid email', async function () { | 395 | it('Should fail with an invalid email', async function () { |
574 | const fields = { | 396 | const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' }) |
575 | username: 'my_super_username_which_is_very_long', | ||
576 | email: 'test_example.com', | ||
577 | password: 'my_super_password' | ||
578 | } | ||
579 | 397 | ||
580 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) | 398 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) |
581 | }) | 399 | }) |
582 | 400 | ||
583 | it('Should fail with a too small password', async function () { | 401 | it('Should fail with a too small password', async function () { |
584 | const fields = { | 402 | const fields = immutableAssign(baseCorrectParams, { password: 'bla' }) |
585 | username: 'my_username', | ||
586 | email: 'test@example.com', | ||
587 | password: 'bla' | ||
588 | } | ||
589 | 403 | ||
590 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) | 404 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) |
591 | }) | 405 | }) |
592 | 406 | ||
593 | it('Should fail with a too long password', async function () { | 407 | it('Should fail with a too long password', async function () { |
594 | const fields = { | 408 | const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) }) |
595 | username: 'my_username', | ||
596 | email: 'test@example.com', | ||
597 | password: 'my super long password which is very very very very very very very very very very very very very very' + | ||
598 | 'very very very very very very very very very very very very very very very veryv very very very very' + | ||
599 | 'very very very very very very very very very very very very very very very very very very very very long' | ||
600 | } | ||
601 | 409 | ||
602 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) | 410 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields }) |
603 | }) | 411 | }) |
604 | 412 | ||
605 | it('Should fail if we register a user with the same username', async function () { | 413 | it('Should fail if we register a user with the same username', async function () { |
606 | const fields = { | 414 | const fields = immutableAssign(baseCorrectParams, { username: 'root' }) |
607 | username: 'root', | ||
608 | email: 'test@example.com', | ||
609 | password: 'my super password' | ||
610 | } | ||
611 | 415 | ||
612 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 }) | 416 | await makePostBodyRequest({ |
417 | url: server.url, | ||
418 | path: registrationPath, | ||
419 | token: server.accessToken, | ||
420 | fields, | ||
421 | statusCodeExpected: 409 | ||
422 | }) | ||
613 | }) | 423 | }) |
614 | 424 | ||
615 | it('Should fail if we register a user with the same email', async function () { | 425 | it('Should fail if we register a user with the same email', async function () { |
616 | const fields = { | 426 | const fields = immutableAssign(baseCorrectParams, { email: 'admin1@example.com' }) |
617 | username: 'my_username', | ||
618 | email: 'admin1@example.com', | ||
619 | password: 'my super password' | ||
620 | } | ||
621 | 427 | ||
622 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 }) | 428 | await makePostBodyRequest({ |
429 | url: server.url, | ||
430 | path: registrationPath, | ||
431 | token: server.accessToken, | ||
432 | fields, | ||
433 | statusCodeExpected: 409 | ||
434 | }) | ||
623 | }) | 435 | }) |
624 | 436 | ||
625 | it('Should succeed with the correct params', async function () { | 437 | it('Should succeed with the correct params', async function () { |
626 | const fields = { | 438 | await makePostBodyRequest({ |
627 | username: 'user3', | 439 | url: server.url, |
628 | email: 'test3@example.com', | 440 | path: registrationPath, |
629 | password: 'my super password' | 441 | token: server.accessToken, |
630 | } | 442 | fields: baseCorrectParams, |
631 | 443 | statusCodeExpected: 204 | |
632 | await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 204 }) | 444 | }) |
633 | }) | 445 | }) |
634 | 446 | ||
635 | it('Should fail on a server with registration disabled', async function () { | 447 | it('Should fail on a server with registration disabled', async function () { |
@@ -657,25 +469,24 @@ describe('Test users API validators', function () { | |||
657 | 469 | ||
658 | describe('When having a video quota', function () { | 470 | describe('When having a video quota', function () { |
659 | it('Should fail with a user having too many video', async function () { | 471 | it('Should fail with a user having too many video', async function () { |
660 | const fields = { | 472 | await updateUser({ |
473 | url: server.url, | ||
474 | userId: rootId, | ||
475 | accessToken: server.accessToken, | ||
661 | videoQuota: 42 | 476 | videoQuota: 42 |
662 | } | 477 | }) |
663 | |||
664 | await makePutBodyRequest({ url: server.url, path: path + rootId, token: server.accessToken, fields, statusCodeExpected: 204 }) | ||
665 | 478 | ||
666 | const videoAttributes = {} | 479 | await uploadVideo(server.url, server.accessToken, {}, 403) |
667 | await uploadVideo(server.url, server.accessToken, videoAttributes, 403) | ||
668 | }) | 480 | }) |
669 | 481 | ||
670 | it('Should fail with a registered user having too many video', async function () { | 482 | it('Should fail with a registered user having too many video', async function () { |
671 | this.timeout(10000) | 483 | this.timeout(10000) |
672 | 484 | ||
673 | server.user = { | 485 | const user = { |
674 | username: 'user3', | 486 | username: 'user3', |
675 | email: 'test3@example.com', | ||
676 | password: 'my super password' | 487 | password: 'my super password' |
677 | } | 488 | } |
678 | userAccessToken = await serverLogin(server) | 489 | userAccessToken = await userLogin(server, user) |
679 | 490 | ||
680 | const videoAttributes = { fixture: 'video_short2.webm' } | 491 | const videoAttributes = { fixture: 'video_short2.webm' } |
681 | await uploadVideo(server.url, userAccessToken, videoAttributes) | 492 | await uploadVideo(server.url, userAccessToken, videoAttributes) |
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 298dbce2c..ec164cadc 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts | |||
@@ -11,7 +11,7 @@ import { | |||
11 | getUserInformation, | 11 | getUserInformation, |
12 | getUsersList, | 12 | getUsersList, |
13 | getUsersListPaginationAndSort, | 13 | getUsersListPaginationAndSort, |
14 | getUserVideoRating, | 14 | getMyUserVideoRating, |
15 | getVideosList, | 15 | getVideosList, |
16 | killallServers, | 16 | killallServers, |
17 | login, | 17 | login, |
@@ -130,7 +130,7 @@ describe('Test users', function () { | |||
130 | 130 | ||
131 | it('Should retrieve a video rating', async function () { | 131 | it('Should retrieve a video rating', async function () { |
132 | await rateVideo(server.url, accessToken, videoId, 'like') | 132 | await rateVideo(server.url, accessToken, videoId, 'like') |
133 | const res = await getUserVideoRating(server.url, accessToken, videoId) | 133 | const res = await getMyUserVideoRating(server.url, accessToken, videoId) |
134 | const rating = res.body | 134 | const rating = res.body |
135 | 135 | ||
136 | expect(rating.videoId) | 136 | expect(rating.videoId) |
diff --git a/server/tests/utils/miscs/miscs.ts b/server/tests/utils/miscs/miscs.ts index 424b0db98..2147a07ff 100644 --- a/server/tests/utils/miscs/miscs.ts +++ b/server/tests/utils/miscs/miscs.ts | |||
@@ -3,6 +3,10 @@ import { readFile, readdir } from 'fs' | |||
3 | 3 | ||
4 | let webtorrent = new WebTorrent() | 4 | let webtorrent = new WebTorrent() |
5 | 5 | ||
6 | function immutableAssign <T, U> (target: T, source: U) { | ||
7 | return Object.assign<{}, T, U>({}, target, source) | ||
8 | } | ||
9 | |||
6 | function readFilePromise (path: string) { | 10 | function readFilePromise (path: string) { |
7 | return new Promise<Buffer>((res, rej) => { | 11 | return new Promise<Buffer>((res, rej) => { |
8 | readFile(path, (err, data) => { | 12 | readFile(path, (err, data) => { |
@@ -48,5 +52,6 @@ export { | |||
48 | readdirPromise, | 52 | readdirPromise, |
49 | dateIsValid, | 53 | dateIsValid, |
50 | wait, | 54 | wait, |
51 | webtorrentAdd | 55 | webtorrentAdd, |
56 | immutableAssign | ||
52 | } | 57 | } |
diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts index bd8d7ab04..e0cca3f51 100644 --- a/server/tests/utils/users/users.ts +++ b/server/tests/utils/users/users.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | import { makePutBodyRequest } from '../' | ||
2 | 3 | ||
3 | import { UserRole } from '../../../../shared/index' | 4 | import { UserRole } from '../../../../shared/index' |
4 | 5 | ||
@@ -43,14 +44,14 @@ function registerUser (url: string, username: string, password: string, specialS | |||
43 | .expect(specialStatus) | 44 | .expect(specialStatus) |
44 | } | 45 | } |
45 | 46 | ||
46 | function getMyUserInformation (url: string, accessToken: string) { | 47 | function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { |
47 | const path = '/api/v1/users/me' | 48 | const path = '/api/v1/users/me' |
48 | 49 | ||
49 | return request(url) | 50 | return request(url) |
50 | .get(path) | 51 | .get(path) |
51 | .set('Accept', 'application/json') | 52 | .set('Accept', 'application/json') |
52 | .set('Authorization', 'Bearer ' + accessToken) | 53 | .set('Authorization', 'Bearer ' + accessToken) |
53 | .expect(200) | 54 | .expect(specialStatus) |
54 | .expect('Content-Type', /json/) | 55 | .expect('Content-Type', /json/) |
55 | } | 56 | } |
56 | 57 | ||
@@ -65,14 +66,14 @@ function getUserInformation (url: string, accessToken: string, userId: number) { | |||
65 | .expect('Content-Type', /json/) | 66 | .expect('Content-Type', /json/) |
66 | } | 67 | } |
67 | 68 | ||
68 | function getUserVideoRating (url: string, accessToken: string, videoId: number) { | 69 | function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { |
69 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | 70 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' |
70 | 71 | ||
71 | return request(url) | 72 | return request(url) |
72 | .get(path) | 73 | .get(path) |
73 | .set('Accept', 'application/json') | 74 | .set('Accept', 'application/json') |
74 | .set('Authorization', 'Bearer ' + accessToken) | 75 | .set('Authorization', 'Bearer ' + accessToken) |
75 | .expect(200) | 76 | .expect(specialStatus) |
76 | .expect('Content-Type', /json/) | 77 | .expect('Content-Type', /json/) |
77 | } | 78 | } |
78 | 79 | ||
@@ -101,7 +102,7 @@ function getUsersListPaginationAndSort (url: string, accessToken: string, start: | |||
101 | .expect('Content-Type', /json/) | 102 | .expect('Content-Type', /json/) |
102 | } | 103 | } |
103 | 104 | ||
104 | function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) { | 105 | function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { |
105 | const path = '/api/v1/users' | 106 | const path = '/api/v1/users' |
106 | 107 | ||
107 | return request(url) | 108 | return request(url) |
@@ -111,38 +112,53 @@ function removeUser (url: string, userId: number, accessToken: string, expectedS | |||
111 | .expect(expectedStatus) | 112 | .expect(expectedStatus) |
112 | } | 113 | } |
113 | 114 | ||
114 | function updateMyUser (url: string, accessToken: string, newPassword: string, displayNSFW?: boolean, | 115 | function updateMyUser (options: { |
115 | email?: string, autoPlayVideo?: boolean) { | 116 | url: string |
117 | accessToken: string, | ||
118 | newPassword?: string, | ||
119 | displayNSFW?: boolean, | ||
120 | email?: string, | ||
121 | autoPlayVideo?: boolean | ||
122 | }) { | ||
116 | const path = '/api/v1/users/me' | 123 | const path = '/api/v1/users/me' |
117 | 124 | ||
118 | const toSend = {} | 125 | const toSend = {} |
119 | if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword | 126 | if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword |
120 | if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW | 127 | if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW |
121 | if (autoPlayVideo !== undefined && autoPlayVideo !== null) toSend['autoPlayVideo'] = autoPlayVideo | 128 | if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo |
122 | if (email !== undefined && email !== null) toSend['email'] = email | 129 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
123 | 130 | ||
124 | return request(url) | 131 | return makePutBodyRequest({ |
125 | .put(path) | 132 | url: options.url, |
126 | .set('Accept', 'application/json') | 133 | path, |
127 | .set('Authorization', 'Bearer ' + accessToken) | 134 | token: options.accessToken, |
128 | .send(toSend) | 135 | fields: toSend, |
129 | .expect(204) | 136 | statusCodeExpected: 204 |
137 | }) | ||
130 | } | 138 | } |
131 | 139 | ||
132 | function updateUser (url: string, userId: number, accessToken: string, email: string, videoQuota: number, role: UserRole) { | 140 | function updateUser (options: { |
133 | const path = '/api/v1/users/' + userId | 141 | url: string |
142 | userId: number, | ||
143 | accessToken: string, | ||
144 | email?: string, | ||
145 | videoQuota?: number, | ||
146 | role?: UserRole | ||
147 | }) { | ||
148 | const path = '/api/v1/users/' + options.userId | ||
134 | 149 | ||
135 | const toSend = {} | 150 | const toSend = {} |
136 | if (email !== undefined && email !== null) toSend['email'] = email | 151 | if (options.email !== undefined && options.email !== null) toSend['email'] = options.email |
137 | if (videoQuota !== undefined && videoQuota !== null) toSend['videoQuota'] = videoQuota | 152 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota |
138 | if (role !== undefined && role !== null) toSend['role'] = role | 153 | if (options.role !== undefined && options.role !== null) toSend['role'] = options.role |
139 | 154 | ||
140 | return request(url) | 155 | return makePutBodyRequest({ |
141 | .put(path) | 156 | url: options.url, |
142 | .set('Accept', 'application/json') | 157 | path, |
143 | .set('Authorization', 'Bearer ' + accessToken) | 158 | token: options.accessToken, |
144 | .send(toSend) | 159 | fields: toSend, |
145 | .expect(204) | 160 | statusCodeExpected: 204 |
161 | }) | ||
146 | } | 162 | } |
147 | 163 | ||
148 | // --------------------------------------------------------------------------- | 164 | // --------------------------------------------------------------------------- |
@@ -151,7 +167,7 @@ export { | |||
151 | createUser, | 167 | createUser, |
152 | registerUser, | 168 | registerUser, |
153 | getMyUserInformation, | 169 | getMyUserInformation, |
154 | getUserVideoRating, | 170 | getMyUserVideoRating, |
155 | getUsersList, | 171 | getUsersList, |
156 | getUsersListPaginationAndSort, | 172 | getUsersListPaginationAndSort, |
157 | removeUser, | 173 | removeUser, |