]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/moderation/blocklist.ts
Implement avatar miniatures (#4639)
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / blocklist.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { UserNotificationType } from '@shared/models'
6 import {
7 BlocklistCommand,
8 cleanupTests,
9 CommentsCommand,
10 createMultipleServers,
11 doubleFollow,
12 PeerTubeServer,
13 setAccessTokensToServers,
14 setDefaultAccountAvatar,
15 waitJobs
16 } from '@shared/server-commands'
17
18 const expect = chai.expect
19
20 async function checkAllVideos (server: PeerTubeServer, token: string) {
21 {
22 const { data } = await server.videos.listWithToken({ token })
23 expect(data).to.have.lengthOf(5)
24 }
25
26 {
27 const { data } = await server.videos.list()
28 expect(data).to.have.lengthOf(5)
29 }
30 }
31
32 async function checkAllComments (server: PeerTubeServer, token: string, videoUUID: string) {
33 const { data } = await server.comments.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token })
34
35 const threads = data.filter(t => t.isDeleted === false)
36 expect(threads).to.have.lengthOf(2)
37
38 for (const thread of threads) {
39 const tree = await server.comments.getThread({ videoId: videoUUID, threadId: thread.id, token })
40 expect(tree.children).to.have.lengthOf(1)
41 }
42 }
43
44 async function checkCommentNotification (
45 mainServer: PeerTubeServer,
46 comment: { server: PeerTubeServer, token: string, videoUUID: string, text: string },
47 check: 'presence' | 'absence'
48 ) {
49 const command = comment.server.comments
50
51 const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text })
52
53 await waitJobs([ mainServer, comment.server ])
54
55 const { data } = await mainServer.notifications.list({ start: 0, count: 30 })
56 const commentNotifications = data.filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
57
58 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
59 else expect(commentNotifications).to.have.lengthOf(0)
60
61 await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId })
62
63 await waitJobs([ mainServer, comment.server ])
64 }
65
66 describe('Test blocklist', function () {
67 let servers: PeerTubeServer[]
68 let videoUUID1: string
69 let videoUUID2: string
70 let videoUUID3: string
71 let userToken1: string
72 let userModeratorToken: string
73 let userToken2: string
74
75 let command: BlocklistCommand
76 let commentsCommand: CommentsCommand[]
77
78 before(async function () {
79 this.timeout(120000)
80
81 servers = await createMultipleServers(3)
82 await setAccessTokensToServers(servers)
83 await setDefaultAccountAvatar(servers)
84
85 command = servers[0].blocklist
86 commentsCommand = servers.map(s => s.comments)
87
88 {
89 const user = { username: 'user1', password: 'password' }
90 await servers[0].users.create({ username: user.username, password: user.password })
91
92 userToken1 = await servers[0].login.getAccessToken(user)
93 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
94 }
95
96 {
97 const user = { username: 'moderator', password: 'password' }
98 await servers[0].users.create({ username: user.username, password: user.password })
99
100 userModeratorToken = await servers[0].login.getAccessToken(user)
101 }
102
103 {
104 const user = { username: 'user2', password: 'password' }
105 await servers[1].users.create({ username: user.username, password: user.password })
106
107 userToken2 = await servers[1].login.getAccessToken(user)
108 await servers[1].videos.upload({ token: userToken2, attributes: { name: 'video user 2' } })
109 }
110
111 {
112 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
113 videoUUID1 = uuid
114 }
115
116 {
117 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
118 videoUUID2 = uuid
119 }
120
121 {
122 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 2 server 1' } })
123 videoUUID3 = uuid
124 }
125
126 await doubleFollow(servers[0], servers[1])
127 await doubleFollow(servers[0], servers[2])
128
129 {
130 const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' })
131 const reply = await commentsCommand[0].addReply({
132 token: userToken1,
133 videoId: videoUUID1,
134 toCommentId: created.id,
135 text: 'comment user 1'
136 })
137 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' })
138 }
139
140 {
141 const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' })
142 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' })
143 }
144
145 await waitJobs(servers)
146 })
147
148 describe('User blocklist', function () {
149
150 describe('When managing account blocklist', function () {
151 it('Should list all videos', function () {
152 return checkAllVideos(servers[0], servers[0].accessToken)
153 })
154
155 it('Should list the comments', function () {
156 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
157 })
158
159 it('Should block a remote account', async function () {
160 await command.addToMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
161 })
162
163 it('Should hide its videos', async function () {
164 const { data } = await servers[0].videos.listWithToken()
165
166 expect(data).to.have.lengthOf(4)
167
168 const v = data.find(v => v.name === 'video user 2')
169 expect(v).to.be.undefined
170 })
171
172 it('Should block a local account', async function () {
173 await command.addToMyBlocklist({ account: 'user1' })
174 })
175
176 it('Should hide its videos', async function () {
177 const { data } = await servers[0].videos.listWithToken()
178
179 expect(data).to.have.lengthOf(3)
180
181 const v = data.find(v => v.name === 'video user 1')
182 expect(v).to.be.undefined
183 })
184
185 it('Should hide its comments', async function () {
186 const { data } = await commentsCommand[0].listThreads({
187 token: servers[0].accessToken,
188 videoId: videoUUID1,
189 start: 0,
190 count: 25,
191 sort: '-createdAt'
192 })
193
194 expect(data).to.have.lengthOf(1)
195 expect(data[0].totalReplies).to.equal(1)
196
197 const t = data.find(t => t.text === 'comment user 1')
198 expect(t).to.be.undefined
199
200 for (const thread of data) {
201 const tree = await commentsCommand[0].getThread({
202 videoId: videoUUID1,
203 threadId: thread.id,
204 token: servers[0].accessToken
205 })
206 expect(tree.children).to.have.lengthOf(0)
207 }
208 })
209
210 it('Should not have notifications from blocked accounts', async function () {
211 this.timeout(20000)
212
213 {
214 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
215 await checkCommentNotification(servers[0], comment, 'absence')
216 }
217
218 {
219 const comment = {
220 server: servers[0],
221 token: userToken1,
222 videoUUID: videoUUID2,
223 text: 'hello @root@localhost:' + servers[0].port
224 }
225 await checkCommentNotification(servers[0], comment, 'absence')
226 }
227 })
228
229 it('Should list all the videos with another user', async function () {
230 return checkAllVideos(servers[0], userToken1)
231 })
232
233 it('Should list blocked accounts', async function () {
234 {
235 const body = await command.listMyAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
236 expect(body.total).to.equal(2)
237
238 const block = body.data[0]
239 expect(block.byAccount.displayName).to.equal('root')
240 expect(block.byAccount.name).to.equal('root')
241 expect(block.blockedAccount.displayName).to.equal('user2')
242 expect(block.blockedAccount.name).to.equal('user2')
243 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
244 }
245
246 {
247 const body = await command.listMyAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
248 expect(body.total).to.equal(2)
249
250 const block = body.data[0]
251 expect(block.byAccount.displayName).to.equal('root')
252 expect(block.byAccount.name).to.equal('root')
253 expect(block.blockedAccount.displayName).to.equal('user1')
254 expect(block.blockedAccount.name).to.equal('user1')
255 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
256 }
257 })
258
259 it('Should get blocked status', async function () {
260 const remoteHandle = 'user2@' + servers[1].host
261 const localHandle = 'user1@' + servers[0].host
262 const unknownHandle = 'user5@' + servers[0].host
263
264 {
265 const status = await command.getStatus({ accounts: [ remoteHandle ] })
266 expect(Object.keys(status.accounts)).to.have.lengthOf(1)
267 expect(status.accounts[remoteHandle].blockedByUser).to.be.false
268 expect(status.accounts[remoteHandle].blockedByServer).to.be.false
269
270 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
271 }
272
273 {
274 const status = await command.getStatus({ token: servers[0].accessToken, accounts: [ remoteHandle ] })
275 expect(Object.keys(status.accounts)).to.have.lengthOf(1)
276 expect(status.accounts[remoteHandle].blockedByUser).to.be.true
277 expect(status.accounts[remoteHandle].blockedByServer).to.be.false
278
279 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
280 }
281
282 {
283 const status = await command.getStatus({ token: servers[0].accessToken, accounts: [ localHandle, remoteHandle, unknownHandle ] })
284 expect(Object.keys(status.accounts)).to.have.lengthOf(3)
285
286 for (const handle of [ localHandle, remoteHandle ]) {
287 expect(status.accounts[handle].blockedByUser).to.be.true
288 expect(status.accounts[handle].blockedByServer).to.be.false
289 }
290
291 expect(status.accounts[unknownHandle].blockedByUser).to.be.false
292 expect(status.accounts[unknownHandle].blockedByServer).to.be.false
293
294 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
295 }
296 })
297
298 it('Should not allow a remote blocked user to comment my videos', async function () {
299 this.timeout(60000)
300
301 {
302 await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' })
303 await waitJobs(servers)
304
305 await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' })
306 await waitJobs(servers)
307
308 const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: 'uploader' })
309 const message = 'reply by user 2'
310 const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message })
311 await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: 'another reply' })
312
313 await waitJobs(servers)
314 }
315
316 // Server 2 has all the comments
317 {
318 const { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
319
320 expect(data).to.have.lengthOf(2)
321 expect(data[0].text).to.equal('uploader')
322 expect(data[1].text).to.equal('comment user 2')
323
324 const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id })
325 expect(tree.children).to.have.lengthOf(1)
326 expect(tree.children[0].comment.text).to.equal('reply by user 2')
327 expect(tree.children[0].children).to.have.lengthOf(1)
328 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
329 }
330
331 // Server 1 and 3 should only have uploader comments
332 for (const server of [ servers[0], servers[2] ]) {
333 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
334
335 expect(data).to.have.lengthOf(1)
336 expect(data[0].text).to.equal('uploader')
337
338 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
339
340 if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0)
341 else expect(tree.children).to.have.lengthOf(1)
342 }
343 })
344
345 it('Should unblock the remote account', async function () {
346 await command.removeFromMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
347 })
348
349 it('Should display its videos', async function () {
350 const { data } = await servers[0].videos.listWithToken()
351 expect(data).to.have.lengthOf(4)
352
353 const v = data.find(v => v.name === 'video user 2')
354 expect(v).not.to.be.undefined
355 })
356
357 it('Should display its comments on my video', async function () {
358 for (const server of servers) {
359 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
360
361 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
362 if (server.serverNumber === 3) {
363 expect(data).to.have.lengthOf(1)
364 continue
365 }
366
367 expect(data).to.have.lengthOf(2)
368 expect(data[0].text).to.equal('uploader')
369 expect(data[1].text).to.equal('comment user 2')
370
371 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
372 expect(tree.children).to.have.lengthOf(1)
373 expect(tree.children[0].comment.text).to.equal('reply by user 2')
374 expect(tree.children[0].children).to.have.lengthOf(1)
375 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
376 }
377 })
378
379 it('Should unblock the local account', async function () {
380 await command.removeFromMyBlocklist({ account: 'user1' })
381 })
382
383 it('Should display its comments', function () {
384 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
385 })
386
387 it('Should have a notification from a non blocked account', async function () {
388 this.timeout(20000)
389
390 {
391 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
392 await checkCommentNotification(servers[0], comment, 'presence')
393 }
394
395 {
396 const comment = {
397 server: servers[0],
398 token: userToken1,
399 videoUUID: videoUUID2,
400 text: 'hello @root@localhost:' + servers[0].port
401 }
402 await checkCommentNotification(servers[0], comment, 'presence')
403 }
404 })
405 })
406
407 describe('When managing server blocklist', function () {
408
409 it('Should list all videos', function () {
410 return checkAllVideos(servers[0], servers[0].accessToken)
411 })
412
413 it('Should list the comments', function () {
414 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
415 })
416
417 it('Should block a remote server', async function () {
418 await command.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
419 })
420
421 it('Should hide its videos', async function () {
422 const { data } = await servers[0].videos.listWithToken()
423
424 expect(data).to.have.lengthOf(3)
425
426 const v1 = data.find(v => v.name === 'video user 2')
427 const v2 = data.find(v => v.name === 'video server 2')
428
429 expect(v1).to.be.undefined
430 expect(v2).to.be.undefined
431 })
432
433 it('Should list all the videos with another user', async function () {
434 return checkAllVideos(servers[0], userToken1)
435 })
436
437 it('Should hide its comments', async function () {
438 this.timeout(10000)
439
440 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
441
442 await waitJobs(servers)
443
444 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
445
446 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
447 })
448
449 it('Should not have notifications from blocked server', async function () {
450 this.timeout(20000)
451
452 {
453 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
454 await checkCommentNotification(servers[0], comment, 'absence')
455 }
456
457 {
458 const comment = {
459 server: servers[1],
460 token: userToken2,
461 videoUUID: videoUUID1,
462 text: 'hello @root@localhost:' + servers[0].port
463 }
464 await checkCommentNotification(servers[0], comment, 'absence')
465 }
466 })
467
468 it('Should list blocked servers', async function () {
469 const body = await command.listMyServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
470 expect(body.total).to.equal(1)
471
472 const block = body.data[0]
473 expect(block.byAccount.displayName).to.equal('root')
474 expect(block.byAccount.name).to.equal('root')
475 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
476 })
477
478 it('Should get blocklist status', async function () {
479 const blockedServer = servers[1].host
480 const notBlockedServer = 'example.com'
481
482 {
483 const status = await command.getStatus({ hosts: [ blockedServer, notBlockedServer ] })
484 expect(Object.keys(status.accounts)).to.have.lengthOf(0)
485
486 expect(Object.keys(status.hosts)).to.have.lengthOf(2)
487 expect(status.hosts[blockedServer].blockedByUser).to.be.false
488 expect(status.hosts[blockedServer].blockedByServer).to.be.false
489
490 expect(status.hosts[notBlockedServer].blockedByUser).to.be.false
491 expect(status.hosts[notBlockedServer].blockedByServer).to.be.false
492 }
493
494 {
495 const status = await command.getStatus({ token: servers[0].accessToken, hosts: [ blockedServer, notBlockedServer ] })
496 expect(Object.keys(status.accounts)).to.have.lengthOf(0)
497
498 expect(Object.keys(status.hosts)).to.have.lengthOf(2)
499 expect(status.hosts[blockedServer].blockedByUser).to.be.true
500 expect(status.hosts[blockedServer].blockedByServer).to.be.false
501
502 expect(status.hosts[notBlockedServer].blockedByUser).to.be.false
503 expect(status.hosts[notBlockedServer].blockedByServer).to.be.false
504 }
505 })
506
507 it('Should unblock the remote server', async function () {
508 await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port })
509 })
510
511 it('Should display its videos', function () {
512 return checkAllVideos(servers[0], servers[0].accessToken)
513 })
514
515 it('Should display its comments', function () {
516 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
517 })
518
519 it('Should have notification from unblocked server', async function () {
520 this.timeout(20000)
521
522 {
523 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
524 await checkCommentNotification(servers[0], comment, 'presence')
525 }
526
527 {
528 const comment = {
529 server: servers[1],
530 token: userToken2,
531 videoUUID: videoUUID1,
532 text: 'hello @root@localhost:' + servers[0].port
533 }
534 await checkCommentNotification(servers[0], comment, 'presence')
535 }
536 })
537 })
538 })
539
540 describe('Server blocklist', function () {
541
542 describe('When managing account blocklist', function () {
543 it('Should list all videos', async function () {
544 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
545 await checkAllVideos(servers[0], token)
546 }
547 })
548
549 it('Should list the comments', async function () {
550 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
551 await checkAllComments(servers[0], token, videoUUID1)
552 }
553 })
554
555 it('Should block a remote account', async function () {
556 await command.addToServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
557 })
558
559 it('Should hide its videos', async function () {
560 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
561 const { data } = await servers[0].videos.listWithToken({ token })
562
563 expect(data).to.have.lengthOf(4)
564
565 const v = data.find(v => v.name === 'video user 2')
566 expect(v).to.be.undefined
567 }
568 })
569
570 it('Should block a local account', async function () {
571 await command.addToServerBlocklist({ account: 'user1' })
572 })
573
574 it('Should hide its videos', async function () {
575 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
576 const { data } = await servers[0].videos.listWithToken({ token })
577
578 expect(data).to.have.lengthOf(3)
579
580 const v = data.find(v => v.name === 'video user 1')
581 expect(v).to.be.undefined
582 }
583 })
584
585 it('Should hide its comments', async function () {
586 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
587 const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token })
588 const threads = data.filter(t => t.isDeleted === false)
589
590 expect(threads).to.have.lengthOf(1)
591 expect(threads[0].totalReplies).to.equal(1)
592
593 const t = threads.find(t => t.text === 'comment user 1')
594 expect(t).to.be.undefined
595
596 for (const thread of threads) {
597 const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token })
598 expect(tree.children).to.have.lengthOf(0)
599 }
600 }
601 })
602
603 it('Should not have notification from blocked accounts by instance', async function () {
604 this.timeout(20000)
605
606 {
607 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
608 await checkCommentNotification(servers[0], comment, 'absence')
609 }
610
611 {
612 const comment = {
613 server: servers[1],
614 token: userToken2,
615 videoUUID: videoUUID1,
616 text: 'hello @root@localhost:' + servers[0].port
617 }
618 await checkCommentNotification(servers[0], comment, 'absence')
619 }
620 })
621
622 it('Should list blocked accounts', async function () {
623 {
624 const body = await command.listServerAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
625 expect(body.total).to.equal(2)
626
627 const block = body.data[0]
628 expect(block.byAccount.displayName).to.equal('peertube')
629 expect(block.byAccount.name).to.equal('peertube')
630 expect(block.blockedAccount.displayName).to.equal('user2')
631 expect(block.blockedAccount.name).to.equal('user2')
632 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
633 }
634
635 {
636 const body = await command.listServerAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
637 expect(body.total).to.equal(2)
638
639 const block = body.data[0]
640 expect(block.byAccount.displayName).to.equal('peertube')
641 expect(block.byAccount.name).to.equal('peertube')
642 expect(block.blockedAccount.displayName).to.equal('user1')
643 expect(block.blockedAccount.name).to.equal('user1')
644 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
645 }
646 })
647
648 it('Should get blocked status', async function () {
649 const remoteHandle = 'user2@' + servers[1].host
650 const localHandle = 'user1@' + servers[0].host
651 const unknownHandle = 'user5@' + servers[0].host
652
653 for (const token of [ undefined, servers[0].accessToken ]) {
654 const status = await command.getStatus({ token, accounts: [ localHandle, remoteHandle, unknownHandle ] })
655 expect(Object.keys(status.accounts)).to.have.lengthOf(3)
656
657 for (const handle of [ localHandle, remoteHandle ]) {
658 expect(status.accounts[handle].blockedByUser).to.be.false
659 expect(status.accounts[handle].blockedByServer).to.be.true
660 }
661
662 expect(status.accounts[unknownHandle].blockedByUser).to.be.false
663 expect(status.accounts[unknownHandle].blockedByServer).to.be.false
664
665 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
666 }
667 })
668
669 it('Should unblock the remote account', async function () {
670 await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
671 })
672
673 it('Should display its videos', async function () {
674 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
675 const { data } = await servers[0].videos.listWithToken({ token })
676 expect(data).to.have.lengthOf(4)
677
678 const v = data.find(v => v.name === 'video user 2')
679 expect(v).not.to.be.undefined
680 }
681 })
682
683 it('Should unblock the local account', async function () {
684 await command.removeFromServerBlocklist({ account: 'user1' })
685 })
686
687 it('Should display its comments', async function () {
688 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
689 await checkAllComments(servers[0], token, videoUUID1)
690 }
691 })
692
693 it('Should have notifications from unblocked accounts', async function () {
694 this.timeout(20000)
695
696 {
697 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'displayed comment' }
698 await checkCommentNotification(servers[0], comment, 'presence')
699 }
700
701 {
702 const comment = {
703 server: servers[1],
704 token: userToken2,
705 videoUUID: videoUUID1,
706 text: 'hello @root@localhost:' + servers[0].port
707 }
708 await checkCommentNotification(servers[0], comment, 'presence')
709 }
710 })
711 })
712
713 describe('When managing server blocklist', function () {
714
715 it('Should list all videos', async function () {
716 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
717 await checkAllVideos(servers[0], token)
718 }
719 })
720
721 it('Should list the comments', async function () {
722 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
723 await checkAllComments(servers[0], token, videoUUID1)
724 }
725 })
726
727 it('Should block a remote server', async function () {
728 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
729 })
730
731 it('Should hide its videos', async function () {
732 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
733 const requests = [
734 servers[0].videos.list(),
735 servers[0].videos.listWithToken({ token })
736 ]
737
738 for (const req of requests) {
739 const { data } = await req
740 expect(data).to.have.lengthOf(3)
741
742 const v1 = data.find(v => v.name === 'video user 2')
743 const v2 = data.find(v => v.name === 'video server 2')
744
745 expect(v1).to.be.undefined
746 expect(v2).to.be.undefined
747 }
748 }
749 })
750
751 it('Should hide its comments', async function () {
752 this.timeout(10000)
753
754 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
755
756 await waitJobs(servers)
757
758 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
759
760 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
761 })
762
763 it('Should not have notification from blocked instances by instance', async function () {
764 this.timeout(50000)
765
766 {
767 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
768 await checkCommentNotification(servers[0], comment, 'absence')
769 }
770
771 {
772 const comment = {
773 server: servers[1],
774 token: userToken2,
775 videoUUID: videoUUID1,
776 text: 'hello @root@localhost:' + servers[0].port
777 }
778 await checkCommentNotification(servers[0], comment, 'absence')
779 }
780
781 {
782 const now = new Date()
783 await servers[1].follows.unfollow({ target: servers[0] })
784 await waitJobs(servers)
785 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
786
787 await waitJobs(servers)
788
789 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
790 const commentNotifications = data.filter(n => {
791 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
792 })
793
794 expect(commentNotifications).to.have.lengthOf(0)
795 }
796 })
797
798 it('Should list blocked servers', async function () {
799 const body = await command.listServerServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
800 expect(body.total).to.equal(1)
801
802 const block = body.data[0]
803 expect(block.byAccount.displayName).to.equal('peertube')
804 expect(block.byAccount.name).to.equal('peertube')
805 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
806 })
807
808 it('Should get blocklist status', async function () {
809 const blockedServer = servers[1].host
810 const notBlockedServer = 'example.com'
811
812 for (const token of [ undefined, servers[0].accessToken ]) {
813 const status = await command.getStatus({ token, hosts: [ blockedServer, notBlockedServer ] })
814 expect(Object.keys(status.accounts)).to.have.lengthOf(0)
815
816 expect(Object.keys(status.hosts)).to.have.lengthOf(2)
817 expect(status.hosts[blockedServer].blockedByUser).to.be.false
818 expect(status.hosts[blockedServer].blockedByServer).to.be.true
819
820 expect(status.hosts[notBlockedServer].blockedByUser).to.be.false
821 expect(status.hosts[notBlockedServer].blockedByServer).to.be.false
822 }
823 })
824
825 it('Should unblock the remote server', async function () {
826 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
827 })
828
829 it('Should list all videos', async function () {
830 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
831 await checkAllVideos(servers[0], token)
832 }
833 })
834
835 it('Should list the comments', async function () {
836 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
837 await checkAllComments(servers[0], token, videoUUID1)
838 }
839 })
840
841 it('Should have notification from unblocked instances', async function () {
842 this.timeout(50000)
843
844 {
845 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
846 await checkCommentNotification(servers[0], comment, 'presence')
847 }
848
849 {
850 const comment = {
851 server: servers[1],
852 token: userToken2,
853 videoUUID: videoUUID1,
854 text: 'hello @root@localhost:' + servers[0].port
855 }
856 await checkCommentNotification(servers[0], comment, 'presence')
857 }
858
859 {
860 const now = new Date()
861 await servers[1].follows.unfollow({ target: servers[0] })
862 await waitJobs(servers)
863 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
864
865 await waitJobs(servers)
866
867 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
868 const commentNotifications = data.filter(n => {
869 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
870 })
871
872 expect(commentNotifications).to.have.lengthOf(1)
873 }
874 })
875 })
876 })
877
878 after(async function () {
879 await cleanupTests(servers)
880 })
881 })