1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
12 setAccessTokensToServers,
14 } from '@shared/extra-utils'
15 import { UserNotificationType } from '@shared/models'
17 const expect = chai.expect
19 async function checkAllVideos (server: PeerTubeServer, token: string) {
21 const { data } = await server.videos.listWithToken({ token })
22 expect(data).to.have.lengthOf(5)
26 const { data } = await server.videos.list()
27 expect(data).to.have.lengthOf(5)
31 async function checkAllComments (server: PeerTubeServer, token: string, videoUUID: string) {
32 const { data } = await server.comments.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token })
34 const threads = data.filter(t => t.isDeleted === false)
35 expect(threads).to.have.lengthOf(2)
37 for (const thread of threads) {
38 const tree = await server.comments.getThread({ videoId: videoUUID, threadId: thread.id, token })
39 expect(tree.children).to.have.lengthOf(1)
43 async function checkCommentNotification (
44 mainServer: PeerTubeServer,
45 comment: { server: PeerTubeServer, token: string, videoUUID: string, text: string },
46 check: 'presence' | 'absence'
48 const command = comment.server.comments
50 const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text })
52 await waitJobs([ mainServer, comment.server ])
54 const { data } = await mainServer.notifications.list({ start: 0, count: 30 })
55 const commentNotifications = data.filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
57 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
58 else expect(commentNotifications).to.have.lengthOf(0)
60 await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId })
62 await waitJobs([ mainServer, comment.server ])
65 describe('Test blocklist', function () {
66 let servers: PeerTubeServer[]
67 let videoUUID1: string
68 let videoUUID2: string
69 let videoUUID3: string
70 let userToken1: string
71 let userModeratorToken: string
72 let userToken2: string
74 let command: BlocklistCommand
75 let commentsCommand: CommentsCommand[]
77 before(async function () {
80 servers = await createMultipleServers(3)
81 await setAccessTokensToServers(servers)
83 command = servers[0].blocklist
84 commentsCommand = servers.map(s => s.comments)
87 const user = { username: 'user1', password: 'password' }
88 await servers[0].users.create({ username: user.username, password: user.password })
90 userToken1 = await servers[0].login.getAccessToken(user)
91 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
95 const user = { username: 'moderator', password: 'password' }
96 await servers[0].users.create({ username: user.username, password: user.password })
98 userModeratorToken = await servers[0].login.getAccessToken(user)
102 const user = { username: 'user2', password: 'password' }
103 await servers[1].users.create({ username: user.username, password: user.password })
105 userToken2 = await servers[1].login.getAccessToken(user)
106 await servers[1].videos.upload({ token: userToken2, attributes: { name: 'video user 2' } })
110 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
115 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
120 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 2 server 1' } })
124 await doubleFollow(servers[0], servers[1])
125 await doubleFollow(servers[0], servers[2])
128 const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' })
129 const reply = await commentsCommand[0].addReply({
132 toCommentId: created.id,
133 text: 'comment user 1'
135 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' })
139 const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' })
140 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' })
143 await waitJobs(servers)
146 describe('User blocklist', function () {
148 describe('When managing account blocklist', function () {
149 it('Should list all videos', function () {
150 return checkAllVideos(servers[0], servers[0].accessToken)
153 it('Should list the comments', function () {
154 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
157 it('Should block a remote account', async function () {
158 await command.addToMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
161 it('Should hide its videos', async function () {
162 const { data } = await servers[0].videos.listWithToken()
164 expect(data).to.have.lengthOf(4)
166 const v = data.find(v => v.name === 'video user 2')
167 expect(v).to.be.undefined
170 it('Should block a local account', async function () {
171 await command.addToMyBlocklist({ account: 'user1' })
174 it('Should hide its videos', async function () {
175 const { data } = await servers[0].videos.listWithToken()
177 expect(data).to.have.lengthOf(3)
179 const v = data.find(v => v.name === 'video user 1')
180 expect(v).to.be.undefined
183 it('Should hide its comments', async function () {
184 const { data } = await commentsCommand[0].listThreads({
185 token: servers[0].accessToken,
192 expect(data).to.have.lengthOf(1)
193 expect(data[0].totalReplies).to.equal(1)
195 const t = data.find(t => t.text === 'comment user 1')
196 expect(t).to.be.undefined
198 for (const thread of data) {
199 const tree = await commentsCommand[0].getThread({
202 token: servers[0].accessToken
204 expect(tree.children).to.have.lengthOf(0)
208 it('Should not have notifications from blocked accounts', async function () {
212 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
213 await checkCommentNotification(servers[0], comment, 'absence')
220 videoUUID: videoUUID2,
221 text: 'hello @root@localhost:' + servers[0].port
223 await checkCommentNotification(servers[0], comment, 'absence')
227 it('Should list all the videos with another user', async function () {
228 return checkAllVideos(servers[0], userToken1)
231 it('Should list blocked accounts', async function () {
233 const body = await command.listMyAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
234 expect(body.total).to.equal(2)
236 const block = body.data[0]
237 expect(block.byAccount.displayName).to.equal('root')
238 expect(block.byAccount.name).to.equal('root')
239 expect(block.blockedAccount.displayName).to.equal('user2')
240 expect(block.blockedAccount.name).to.equal('user2')
241 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
245 const body = await command.listMyAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
246 expect(body.total).to.equal(2)
248 const block = body.data[0]
249 expect(block.byAccount.displayName).to.equal('root')
250 expect(block.byAccount.name).to.equal('root')
251 expect(block.blockedAccount.displayName).to.equal('user1')
252 expect(block.blockedAccount.name).to.equal('user1')
253 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
257 it('Should not allow a remote blocked user to comment my videos', async function () {
261 await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' })
262 await waitJobs(servers)
264 await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' })
265 await waitJobs(servers)
267 const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: 'uploader' })
268 const message = 'reply by user 2'
269 const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message })
270 await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: 'another reply' })
272 await waitJobs(servers)
275 // Server 2 has all the comments
277 const { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
279 expect(data).to.have.lengthOf(2)
280 expect(data[0].text).to.equal('uploader')
281 expect(data[1].text).to.equal('comment user 2')
283 const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id })
284 expect(tree.children).to.have.lengthOf(1)
285 expect(tree.children[0].comment.text).to.equal('reply by user 2')
286 expect(tree.children[0].children).to.have.lengthOf(1)
287 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
290 // Server 1 and 3 should only have uploader comments
291 for (const server of [ servers[0], servers[2] ]) {
292 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
294 expect(data).to.have.lengthOf(1)
295 expect(data[0].text).to.equal('uploader')
297 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
299 if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0)
300 else expect(tree.children).to.have.lengthOf(1)
304 it('Should unblock the remote account', async function () {
305 await command.removeFromMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
308 it('Should display its videos', async function () {
309 const { data } = await servers[0].videos.listWithToken()
310 expect(data).to.have.lengthOf(4)
312 const v = data.find(v => v.name === 'video user 2')
313 expect(v).not.to.be.undefined
316 it('Should display its comments on my video', async function () {
317 for (const server of servers) {
318 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
320 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
321 if (server.serverNumber === 3) {
322 expect(data).to.have.lengthOf(1)
326 expect(data).to.have.lengthOf(2)
327 expect(data[0].text).to.equal('uploader')
328 expect(data[1].text).to.equal('comment user 2')
330 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
331 expect(tree.children).to.have.lengthOf(1)
332 expect(tree.children[0].comment.text).to.equal('reply by user 2')
333 expect(tree.children[0].children).to.have.lengthOf(1)
334 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
338 it('Should unblock the local account', async function () {
339 await command.removeFromMyBlocklist({ account: 'user1' })
342 it('Should display its comments', function () {
343 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
346 it('Should have a notification from a non blocked account', async function () {
350 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
351 await checkCommentNotification(servers[0], comment, 'presence')
358 videoUUID: videoUUID2,
359 text: 'hello @root@localhost:' + servers[0].port
361 await checkCommentNotification(servers[0], comment, 'presence')
366 describe('When managing server blocklist', function () {
368 it('Should list all videos', function () {
369 return checkAllVideos(servers[0], servers[0].accessToken)
372 it('Should list the comments', function () {
373 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
376 it('Should block a remote server', async function () {
377 await command.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
380 it('Should hide its videos', async function () {
381 const { data } = await servers[0].videos.listWithToken()
383 expect(data).to.have.lengthOf(3)
385 const v1 = data.find(v => v.name === 'video user 2')
386 const v2 = data.find(v => v.name === 'video server 2')
388 expect(v1).to.be.undefined
389 expect(v2).to.be.undefined
392 it('Should list all the videos with another user', async function () {
393 return checkAllVideos(servers[0], userToken1)
396 it('Should hide its comments', async function () {
399 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
401 await waitJobs(servers)
403 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
405 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
408 it('Should not have notifications from blocked server', async function () {
412 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
413 await checkCommentNotification(servers[0], comment, 'absence')
420 videoUUID: videoUUID1,
421 text: 'hello @root@localhost:' + servers[0].port
423 await checkCommentNotification(servers[0], comment, 'absence')
427 it('Should list blocked servers', async function () {
428 const body = await command.listMyServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
429 expect(body.total).to.equal(1)
431 const block = body.data[0]
432 expect(block.byAccount.displayName).to.equal('root')
433 expect(block.byAccount.name).to.equal('root')
434 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
437 it('Should unblock the remote server', async function () {
438 await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port })
441 it('Should display its videos', function () {
442 return checkAllVideos(servers[0], servers[0].accessToken)
445 it('Should display its comments', function () {
446 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
449 it('Should have notification from unblocked server', async function () {
453 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
454 await checkCommentNotification(servers[0], comment, 'presence')
461 videoUUID: videoUUID1,
462 text: 'hello @root@localhost:' + servers[0].port
464 await checkCommentNotification(servers[0], comment, 'presence')
470 describe('Server blocklist', function () {
472 describe('When managing account blocklist', function () {
473 it('Should list all videos', async function () {
474 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
475 await checkAllVideos(servers[0], token)
479 it('Should list the comments', async function () {
480 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
481 await checkAllComments(servers[0], token, videoUUID1)
485 it('Should block a remote account', async function () {
486 await command.addToServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
489 it('Should hide its videos', async function () {
490 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
491 const { data } = await servers[0].videos.listWithToken({ token })
493 expect(data).to.have.lengthOf(4)
495 const v = data.find(v => v.name === 'video user 2')
496 expect(v).to.be.undefined
500 it('Should block a local account', async function () {
501 await command.addToServerBlocklist({ account: 'user1' })
504 it('Should hide its videos', async function () {
505 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
506 const { data } = await servers[0].videos.listWithToken({ token })
508 expect(data).to.have.lengthOf(3)
510 const v = data.find(v => v.name === 'video user 1')
511 expect(v).to.be.undefined
515 it('Should hide its comments', async function () {
516 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
517 const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token })
518 const threads = data.filter(t => t.isDeleted === false)
520 expect(threads).to.have.lengthOf(1)
521 expect(threads[0].totalReplies).to.equal(1)
523 const t = threads.find(t => t.text === 'comment user 1')
524 expect(t).to.be.undefined
526 for (const thread of threads) {
527 const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token })
528 expect(tree.children).to.have.lengthOf(0)
533 it('Should not have notification from blocked accounts by instance', async function () {
537 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
538 await checkCommentNotification(servers[0], comment, 'absence')
545 videoUUID: videoUUID1,
546 text: 'hello @root@localhost:' + servers[0].port
548 await checkCommentNotification(servers[0], comment, 'absence')
552 it('Should list blocked accounts', async function () {
554 const body = await command.listServerAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
555 expect(body.total).to.equal(2)
557 const block = body.data[0]
558 expect(block.byAccount.displayName).to.equal('peertube')
559 expect(block.byAccount.name).to.equal('peertube')
560 expect(block.blockedAccount.displayName).to.equal('user2')
561 expect(block.blockedAccount.name).to.equal('user2')
562 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
566 const body = await command.listServerAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
567 expect(body.total).to.equal(2)
569 const block = body.data[0]
570 expect(block.byAccount.displayName).to.equal('peertube')
571 expect(block.byAccount.name).to.equal('peertube')
572 expect(block.blockedAccount.displayName).to.equal('user1')
573 expect(block.blockedAccount.name).to.equal('user1')
574 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
578 it('Should unblock the remote account', async function () {
579 await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
582 it('Should display its videos', async function () {
583 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
584 const { data } = await servers[0].videos.listWithToken({ token })
585 expect(data).to.have.lengthOf(4)
587 const v = data.find(v => v.name === 'video user 2')
588 expect(v).not.to.be.undefined
592 it('Should unblock the local account', async function () {
593 await command.removeFromServerBlocklist({ account: 'user1' })
596 it('Should display its comments', async function () {
597 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
598 await checkAllComments(servers[0], token, videoUUID1)
602 it('Should have notifications from unblocked accounts', async function () {
606 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'displayed comment' }
607 await checkCommentNotification(servers[0], comment, 'presence')
614 videoUUID: videoUUID1,
615 text: 'hello @root@localhost:' + servers[0].port
617 await checkCommentNotification(servers[0], comment, 'presence')
622 describe('When managing server blocklist', function () {
623 it('Should list all videos', async function () {
624 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
625 await checkAllVideos(servers[0], token)
629 it('Should list the comments', async function () {
630 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
631 await checkAllComments(servers[0], token, videoUUID1)
635 it('Should block a remote server', async function () {
636 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
639 it('Should hide its videos', async function () {
640 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
642 servers[0].videos.list(),
643 servers[0].videos.listWithToken({ token })
646 for (const req of requests) {
647 const { data } = await req
648 expect(data).to.have.lengthOf(3)
650 const v1 = data.find(v => v.name === 'video user 2')
651 const v2 = data.find(v => v.name === 'video server 2')
653 expect(v1).to.be.undefined
654 expect(v2).to.be.undefined
659 it('Should hide its comments', async function () {
662 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
664 await waitJobs(servers)
666 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
668 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
671 it('Should not have notification from blocked instances by instance', async function () {
675 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
676 await checkCommentNotification(servers[0], comment, 'absence')
683 videoUUID: videoUUID1,
684 text: 'hello @root@localhost:' + servers[0].port
686 await checkCommentNotification(servers[0], comment, 'absence')
690 const now = new Date()
691 await servers[1].follows.unfollow({ target: servers[0] })
692 await waitJobs(servers)
693 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
695 await waitJobs(servers)
697 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
698 const commentNotifications = data.filter(n => {
699 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
702 expect(commentNotifications).to.have.lengthOf(0)
706 it('Should list blocked servers', async function () {
707 const body = await command.listServerServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
708 expect(body.total).to.equal(1)
710 const block = body.data[0]
711 expect(block.byAccount.displayName).to.equal('peertube')
712 expect(block.byAccount.name).to.equal('peertube')
713 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
716 it('Should unblock the remote server', async function () {
717 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
720 it('Should list all videos', async function () {
721 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
722 await checkAllVideos(servers[0], token)
726 it('Should list the comments', async function () {
727 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
728 await checkAllComments(servers[0], token, videoUUID1)
732 it('Should have notification from unblocked instances', async function () {
736 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
737 await checkCommentNotification(servers[0], comment, 'presence')
744 videoUUID: videoUUID1,
745 text: 'hello @root@localhost:' + servers[0].port
747 await checkCommentNotification(servers[0], comment, 'presence')
751 const now = new Date()
752 await servers[1].follows.unfollow({ target: servers[0] })
753 await waitJobs(servers)
754 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
756 await waitJobs(servers)
758 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
759 const commentNotifications = data.filter(n => {
760 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
763 expect(commentNotifications).to.have.lengthOf(1)
769 after(async function () {
770 await cleanupTests(servers)