]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/blocklist.ts
99fe04b8c0de1376f61c2d05e5158622777390b3
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / blocklist.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
6 import {
7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 flushTests,
11 killallServers,
12 ServerInfo,
13 uploadVideo,
14 userLogin
15 } from '../../utils/index'
16 import { setAccessTokensToServers } from '../../utils/users/login'
17 import { getVideosListWithToken } from '../../utils/videos/videos'
18 import {
19 addVideoCommentReply,
20 addVideoCommentThread,
21 getVideoCommentThreads,
22 getVideoThreadComments
23 } from '../../utils/videos/video-comments'
24 import { waitJobs } from '../../utils/server/jobs'
25 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
26 import {
27 addAccountToAccountBlocklist,
28 addAccountToServerBlocklist,
29 addServerToAccountBlocklist,
30 addServerToServerBlocklist,
31 getAccountBlocklistByAccount,
32 getAccountBlocklistByServer,
33 getServerBlocklistByAccount,
34 getServerBlocklistByServer,
35 removeAccountFromAccountBlocklist,
36 removeAccountFromServerBlocklist,
37 removeServerFromAccountBlocklist,
38 removeServerFromServerBlocklist
39 } from '../../utils/users/blocklist'
40
41 const expect = chai.expect
42
43 async function checkAllVideos (url: string, token: string) {
44 const res = await getVideosListWithToken(url, token)
45
46 expect(res.body.data).to.have.lengthOf(4)
47 }
48
49 async function checkAllComments (url: string, token: string, videoUUID: string) {
50 const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
51
52 const threads: VideoComment[] = resThreads.body.data
53 expect(threads).to.have.lengthOf(2)
54
55 for (const thread of threads) {
56 const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
57
58 const tree: VideoCommentThreadTree = res.body
59 expect(tree.children).to.have.lengthOf(1)
60 }
61 }
62
63 describe('Test blocklist', function () {
64 let servers: ServerInfo[]
65 let videoUUID1: string
66 let videoUUID2: string
67 let userToken1: string
68 let userModeratorToken: string
69 let userToken2: string
70
71 before(async function () {
72 this.timeout(60000)
73
74 await flushTests()
75
76 servers = await flushAndRunMultipleServers(2)
77 await setAccessTokensToServers(servers)
78
79 {
80 const user = { username: 'user1', password: 'password' }
81 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
82
83 userToken1 = await userLogin(servers[0], user)
84 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
85 }
86
87 {
88 const user = { username: 'moderator', password: 'password' }
89 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
90
91 userModeratorToken = await userLogin(servers[0], user)
92 }
93
94 {
95 const user = { username: 'user2', password: 'password' }
96 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
97
98 userToken2 = await userLogin(servers[1], user)
99 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
100 }
101
102 {
103 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
104 videoUUID1 = res.body.video.uuid
105 }
106
107 {
108 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
109 videoUUID2 = res.body.video.uuid
110 }
111
112 await doubleFollow(servers[0], servers[1])
113
114 {
115 const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
116 const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
117 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
118 }
119
120 {
121 const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
122 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
123 }
124
125 await waitJobs(servers)
126 })
127
128 describe('User blocklist', function () {
129
130 describe('When managing account blocklist', function () {
131 it('Should list all videos', function () {
132 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
133 })
134
135 it('Should list the comments', function () {
136 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
137 })
138
139 it('Should block a remote account', async function () {
140 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
141 })
142
143 it('Should hide its videos', async function () {
144 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
145
146 const videos: Video[] = res.body.data
147 expect(videos).to.have.lengthOf(3)
148
149 const v = videos.find(v => v.name === 'video user 2')
150 expect(v).to.be.undefined
151 })
152
153 it('Should block a local account', async function () {
154 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
155 })
156
157 it('Should hide its videos', async function () {
158 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
159
160 const videos: Video[] = res.body.data
161 expect(videos).to.have.lengthOf(2)
162
163 const v = videos.find(v => v.name === 'video user 1')
164 expect(v).to.be.undefined
165 })
166
167 it('Should hide its comments', async function () {
168 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', servers[ 0 ].accessToken)
169
170 const threads: VideoComment[] = resThreads.body.data
171 expect(threads).to.have.lengthOf(1)
172 expect(threads[ 0 ].totalReplies).to.equal(0)
173
174 const t = threads.find(t => t.text === 'comment user 1')
175 expect(t).to.be.undefined
176
177 for (const thread of threads) {
178 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, servers[ 0 ].accessToken)
179
180 const tree: VideoCommentThreadTree = res.body
181 expect(tree.children).to.have.lengthOf(0)
182 }
183 })
184
185 it('Should list all the videos with another user', async function () {
186 return checkAllVideos(servers[ 0 ].url, userToken1)
187 })
188
189 it('Should list all the comments with another user', async function () {
190 return checkAllComments(servers[ 0 ].url, userToken1, videoUUID1)
191 })
192
193 it('Should list blocked accounts', async function () {
194 {
195 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
196 const blocks: AccountBlock[] = res.body.data
197
198 expect(res.body.total).to.equal(2)
199
200 const block = blocks[ 0 ]
201 expect(block.byAccount.displayName).to.equal('root')
202 expect(block.byAccount.name).to.equal('root')
203 expect(block.blockedAccount.displayName).to.equal('user2')
204 expect(block.blockedAccount.name).to.equal('user2')
205 expect(block.blockedAccount.host).to.equal('localhost:9002')
206 }
207
208 {
209 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
210 const blocks: AccountBlock[] = res.body.data
211
212 expect(res.body.total).to.equal(2)
213
214 const block = blocks[ 0 ]
215 expect(block.byAccount.displayName).to.equal('root')
216 expect(block.byAccount.name).to.equal('root')
217 expect(block.blockedAccount.displayName).to.equal('user1')
218 expect(block.blockedAccount.name).to.equal('user1')
219 expect(block.blockedAccount.host).to.equal('localhost:9001')
220 }
221 })
222
223 it('Should unblock the remote account', async function () {
224 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
225 })
226
227 it('Should display its videos', async function () {
228 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
229
230 const videos: Video[] = res.body.data
231 expect(videos).to.have.lengthOf(3)
232
233 const v = videos.find(v => v.name === 'video user 2')
234 expect(v).not.to.be.undefined
235 })
236
237 it('Should unblock the local account', async function () {
238 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
239 })
240
241 it('Should display its comments', function () {
242 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
243 })
244 })
245
246 describe('When managing server blocklist', function () {
247 it('Should list all videos', function () {
248 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
249 })
250
251 it('Should list the comments', function () {
252 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
253 })
254
255 it('Should block a remote server', async function () {
256 await addServerToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
257 })
258
259 it('Should hide its videos', async function () {
260 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
261
262 const videos: Video[] = res.body.data
263 expect(videos).to.have.lengthOf(2)
264
265 const v1 = videos.find(v => v.name === 'video user 2')
266 const v2 = videos.find(v => v.name === 'video server 2')
267
268 expect(v1).to.be.undefined
269 expect(v2).to.be.undefined
270 })
271
272 it('Should list all the videos with another user', async function () {
273 return checkAllVideos(servers[ 0 ].url, userToken1)
274 })
275
276 it('Should hide its comments')
277
278 it('Should list blocked servers', async function () {
279 const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
280 const blocks: ServerBlock[] = res.body.data
281
282 expect(res.body.total).to.equal(1)
283
284 const block = blocks[ 0 ]
285 expect(block.byAccount.displayName).to.equal('root')
286 expect(block.byAccount.name).to.equal('root')
287 expect(block.blockedServer.host).to.equal('localhost:9002')
288 })
289
290 it('Should unblock the remote server', async function () {
291 await removeServerFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
292 })
293
294 it('Should display its videos', function () {
295 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
296 })
297
298 it('Should display its comments', function () {
299 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
300 })
301 })
302 })
303
304 describe('Server blocklist', function () {
305
306 describe('When managing account blocklist', function () {
307 it('Should list all videos', async function () {
308 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
309 await checkAllVideos(servers[ 0 ].url, token)
310 }
311 })
312
313 it('Should list the comments', async function () {
314 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
315 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
316 }
317 })
318
319 it('Should block a remote account', async function () {
320 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
321 })
322
323 it('Should hide its videos', async function () {
324 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
325 const res = await getVideosListWithToken(servers[ 0 ].url, token)
326
327 const videos: Video[] = res.body.data
328 expect(videos).to.have.lengthOf(3)
329
330 const v = videos.find(v => v.name === 'video user 2')
331 expect(v).to.be.undefined
332 }
333 })
334
335 it('Should block a local account', async function () {
336 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
337 })
338
339 it('Should hide its videos', async function () {
340 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
341 const res = await getVideosListWithToken(servers[ 0 ].url, token)
342
343 const videos: Video[] = res.body.data
344 expect(videos).to.have.lengthOf(2)
345
346 const v = videos.find(v => v.name === 'video user 1')
347 expect(v).to.be.undefined
348 }
349 })
350
351 it('Should hide its comments', async function () {
352 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
353 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', token)
354
355 const threads: VideoComment[] = resThreads.body.data
356 expect(threads).to.have.lengthOf(1)
357 expect(threads[ 0 ].totalReplies).to.equal(0)
358
359 const t = threads.find(t => t.text === 'comment user 1')
360 expect(t).to.be.undefined
361
362 for (const thread of threads) {
363 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, token)
364
365 const tree: VideoCommentThreadTree = res.body
366 expect(tree.children).to.have.lengthOf(0)
367 }
368 }
369 })
370
371 it('Should list blocked accounts', async function () {
372 {
373 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
374 const blocks: AccountBlock[] = res.body.data
375
376 expect(res.body.total).to.equal(2)
377
378 const block = blocks[ 0 ]
379 expect(block.byAccount.displayName).to.equal('peertube')
380 expect(block.byAccount.name).to.equal('peertube')
381 expect(block.blockedAccount.displayName).to.equal('user2')
382 expect(block.blockedAccount.name).to.equal('user2')
383 expect(block.blockedAccount.host).to.equal('localhost:9002')
384 }
385
386 {
387 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
388 const blocks: AccountBlock[] = res.body.data
389
390 expect(res.body.total).to.equal(2)
391
392 const block = blocks[ 0 ]
393 expect(block.byAccount.displayName).to.equal('peertube')
394 expect(block.byAccount.name).to.equal('peertube')
395 expect(block.blockedAccount.displayName).to.equal('user1')
396 expect(block.blockedAccount.name).to.equal('user1')
397 expect(block.blockedAccount.host).to.equal('localhost:9001')
398 }
399 })
400
401 it('Should unblock the remote account', async function () {
402 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
403 })
404
405 it('Should display its videos', async function () {
406 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
407 const res = await getVideosListWithToken(servers[ 0 ].url, token)
408
409 const videos: Video[] = res.body.data
410 expect(videos).to.have.lengthOf(3)
411
412 const v = videos.find(v => v.name === 'video user 2')
413 expect(v).not.to.be.undefined
414 }
415 })
416
417 it('Should unblock the local account', async function () {
418 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
419 })
420
421 it('Should display its comments', async function () {
422 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
423 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
424 }
425 })
426 })
427
428 describe('When managing server blocklist', function () {
429 it('Should list all videos', async function () {
430 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
431 await checkAllVideos(servers[ 0 ].url, token)
432 }
433 })
434
435 it('Should list the comments', async function () {
436 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
437 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
438 }
439 })
440
441 it('Should block a remote server', async function () {
442 await addServerToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
443 })
444
445 it('Should hide its videos', async function () {
446 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
447 const res = await getVideosListWithToken(servers[ 0 ].url, token)
448
449 const videos: Video[] = res.body.data
450 expect(videos).to.have.lengthOf(2)
451
452 const v1 = videos.find(v => v.name === 'video user 2')
453 const v2 = videos.find(v => v.name === 'video server 2')
454
455 expect(v1).to.be.undefined
456 expect(v2).to.be.undefined
457 }
458 })
459
460 it('Should hide its comments')
461
462 it('Should list blocked servers', async function () {
463 const res = await getServerBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
464 const blocks: ServerBlock[] = res.body.data
465
466 expect(res.body.total).to.equal(1)
467
468 const block = blocks[ 0 ]
469 expect(block.byAccount.displayName).to.equal('peertube')
470 expect(block.byAccount.name).to.equal('peertube')
471 expect(block.blockedServer.host).to.equal('localhost:9002')
472 })
473
474 it('Should unblock the remote server', async function () {
475 await removeServerFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
476 })
477
478 it('Should list all videos', async function () {
479 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
480 await checkAllVideos(servers[ 0 ].url, token)
481 }
482 })
483
484 it('Should list the comments', async function () {
485 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
486 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
487 }
488 })
489 })
490 })
491
492 after(async function () {
493 killallServers(servers)
494
495 // Keep the logs if the test failed
496 if (this[ 'ok' ]) {
497 await flushTests()
498 }
499 })
500 })