1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
11 flushAndRunMultipleServers,
14 getVideosListWithToken,
16 setAccessTokensToServers,
20 } from '@shared/extra-utils'
21 import { UserNotification, UserNotificationType, Video } from '@shared/models'
23 const expect = chai.expect
25 async function checkAllVideos (server: ServerInfo, token: string) {
27 const res = await getVideosListWithToken(server.url, token)
29 expect(res.body.data).to.have.lengthOf(5)
33 const res = await getVideosList(server.url)
35 expect(res.body.data).to.have.lengthOf(5)
39 async function checkAllComments (server: ServerInfo, token: string, videoUUID: string) {
40 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token })
42 const threads = data.filter(t => t.isDeleted === false)
43 expect(threads).to.have.lengthOf(2)
45 for (const thread of threads) {
46 const tree = await server.commentsCommand.getThread({ videoId: videoUUID, threadId: thread.id, token })
47 expect(tree.children).to.have.lengthOf(1)
51 async function checkCommentNotification (
52 mainServer: ServerInfo,
53 comment: { server: ServerInfo, token: string, videoUUID: string, text: string },
54 check: 'presence' | 'absence'
56 const command = comment.server.commentsCommand
58 const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text })
60 await waitJobs([ mainServer, comment.server ])
62 const res = await getUserNotifications(mainServer.url, mainServer.accessToken, 0, 30)
63 const commentNotifications = (res.body.data as UserNotification[])
64 .filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
66 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
67 else expect(commentNotifications).to.have.lengthOf(0)
69 await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId })
71 await waitJobs([ mainServer, comment.server ])
74 describe('Test blocklist', function () {
75 let servers: ServerInfo[]
76 let videoUUID1: string
77 let videoUUID2: string
78 let videoUUID3: string
79 let userToken1: string
80 let userModeratorToken: string
81 let userToken2: string
83 let command: BlocklistCommand
84 let commentsCommand: CommentsCommand[]
86 before(async function () {
89 servers = await flushAndRunMultipleServers(3)
90 await setAccessTokensToServers(servers)
92 command = servers[0].blocklistCommand
93 commentsCommand = servers.map(s => s.commentsCommand)
96 const user = { username: 'user1', password: 'password' }
97 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
99 userToken1 = await userLogin(servers[0], user)
100 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
104 const user = { username: 'moderator', password: 'password' }
105 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
107 userModeratorToken = await userLogin(servers[0], user)
111 const user = { username: 'user2', password: 'password' }
112 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
114 userToken2 = await userLogin(servers[1], user)
115 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
119 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
120 videoUUID1 = res.body.video.uuid
124 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
125 videoUUID2 = res.body.video.uuid
129 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' })
130 videoUUID3 = res.body.video.uuid
133 await doubleFollow(servers[0], servers[1])
134 await doubleFollow(servers[0], servers[2])
137 const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' })
138 const reply = await commentsCommand[0].addReply({
141 toCommentId: created.id,
142 text: 'comment user 1'
144 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' })
148 const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' })
149 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' })
152 await waitJobs(servers)
155 describe('User blocklist', function () {
157 describe('When managing account blocklist', function () {
158 it('Should list all videos', function () {
159 return checkAllVideos(servers[0], servers[0].accessToken)
162 it('Should list the comments', function () {
163 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
166 it('Should block a remote account', async function () {
167 await command.addToMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
170 it('Should hide its videos', async function () {
171 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
173 const videos: Video[] = res.body.data
174 expect(videos).to.have.lengthOf(4)
176 const v = videos.find(v => v.name === 'video user 2')
177 expect(v).to.be.undefined
180 it('Should block a local account', async function () {
181 await command.addToMyBlocklist({ account: 'user1' })
184 it('Should hide its videos', async function () {
185 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
187 const videos: Video[] = res.body.data
188 expect(videos).to.have.lengthOf(3)
190 const v = videos.find(v => v.name === 'video user 1')
191 expect(v).to.be.undefined
194 it('Should hide its comments', async function () {
195 const { data } = await commentsCommand[0].listThreads({
196 token: servers[0].accessToken,
203 expect(data).to.have.lengthOf(1)
204 expect(data[0].totalReplies).to.equal(1)
206 const t = data.find(t => t.text === 'comment user 1')
207 expect(t).to.be.undefined
209 for (const thread of data) {
210 const tree = await commentsCommand[0].getThread({
213 token: servers[0].accessToken
215 expect(tree.children).to.have.lengthOf(0)
219 it('Should not have notifications from blocked accounts', async function () {
223 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
224 await checkCommentNotification(servers[0], comment, 'absence')
231 videoUUID: videoUUID2,
232 text: 'hello @root@localhost:' + servers[0].port
234 await checkCommentNotification(servers[0], comment, 'absence')
238 it('Should list all the videos with another user', async function () {
239 return checkAllVideos(servers[0], userToken1)
242 it('Should list blocked accounts', async function () {
244 const body = await command.listMyAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
245 expect(body.total).to.equal(2)
247 const block = body.data[0]
248 expect(block.byAccount.displayName).to.equal('root')
249 expect(block.byAccount.name).to.equal('root')
250 expect(block.blockedAccount.displayName).to.equal('user2')
251 expect(block.blockedAccount.name).to.equal('user2')
252 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
256 const body = await command.listMyAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
257 expect(body.total).to.equal(2)
259 const block = body.data[0]
260 expect(block.byAccount.displayName).to.equal('root')
261 expect(block.byAccount.name).to.equal('root')
262 expect(block.blockedAccount.displayName).to.equal('user1')
263 expect(block.blockedAccount.name).to.equal('user1')
264 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
268 it('Should not allow a remote blocked user to comment my videos', async function () {
272 await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' })
273 await waitJobs(servers)
275 await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' })
276 await waitJobs(servers)
278 const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: 'uploader' })
279 const message = 'reply by user 2'
280 const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message })
281 await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: 'another reply' })
283 await waitJobs(servers)
286 // Server 2 has all the comments
288 const { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
290 expect(data).to.have.lengthOf(2)
291 expect(data[0].text).to.equal('uploader')
292 expect(data[1].text).to.equal('comment user 2')
294 const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id })
295 expect(tree.children).to.have.lengthOf(1)
296 expect(tree.children[0].comment.text).to.equal('reply by user 2')
297 expect(tree.children[0].children).to.have.lengthOf(1)
298 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
301 // Server 1 and 3 should only have uploader comments
302 for (const server of [ servers[0], servers[2] ]) {
303 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
305 expect(data).to.have.lengthOf(1)
306 expect(data[0].text).to.equal('uploader')
308 const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id })
310 if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0)
311 else expect(tree.children).to.have.lengthOf(1)
315 it('Should unblock the remote account', async function () {
316 await command.removeFromMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
319 it('Should display its videos', async function () {
320 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
322 const videos: Video[] = res.body.data
323 expect(videos).to.have.lengthOf(4)
325 const v = videos.find(v => v.name === 'video user 2')
326 expect(v).not.to.be.undefined
329 it('Should display its comments on my video', async function () {
330 for (const server of servers) {
331 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
333 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
334 if (server.serverNumber === 3) {
335 expect(data).to.have.lengthOf(1)
339 expect(data).to.have.lengthOf(2)
340 expect(data[0].text).to.equal('uploader')
341 expect(data[1].text).to.equal('comment user 2')
343 const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id })
344 expect(tree.children).to.have.lengthOf(1)
345 expect(tree.children[0].comment.text).to.equal('reply by user 2')
346 expect(tree.children[0].children).to.have.lengthOf(1)
347 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
351 it('Should unblock the local account', async function () {
352 await command.removeFromMyBlocklist({ account: 'user1' })
355 it('Should display its comments', function () {
356 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
359 it('Should have a notification from a non blocked account', async function () {
363 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
364 await checkCommentNotification(servers[0], comment, 'presence')
371 videoUUID: videoUUID2,
372 text: 'hello @root@localhost:' + servers[0].port
374 await checkCommentNotification(servers[0], comment, 'presence')
379 describe('When managing server blocklist', function () {
381 it('Should list all videos', function () {
382 return checkAllVideos(servers[0], servers[0].accessToken)
385 it('Should list the comments', function () {
386 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
389 it('Should block a remote server', async function () {
390 await command.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
393 it('Should hide its videos', async function () {
394 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
396 const videos: Video[] = res.body.data
397 expect(videos).to.have.lengthOf(3)
399 const v1 = videos.find(v => v.name === 'video user 2')
400 const v2 = videos.find(v => v.name === 'video server 2')
402 expect(v1).to.be.undefined
403 expect(v2).to.be.undefined
406 it('Should list all the videos with another user', async function () {
407 return checkAllVideos(servers[0], userToken1)
410 it('Should hide its comments', async function () {
413 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
415 await waitJobs(servers)
417 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
419 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
422 it('Should not have notifications from blocked server', async function () {
426 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
427 await checkCommentNotification(servers[0], comment, 'absence')
434 videoUUID: videoUUID1,
435 text: 'hello @root@localhost:' + servers[0].port
437 await checkCommentNotification(servers[0], comment, 'absence')
441 it('Should list blocked servers', async function () {
442 const body = await command.listMyServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
443 expect(body.total).to.equal(1)
445 const block = body.data[0]
446 expect(block.byAccount.displayName).to.equal('root')
447 expect(block.byAccount.name).to.equal('root')
448 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
451 it('Should unblock the remote server', async function () {
452 await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port })
455 it('Should display its videos', function () {
456 return checkAllVideos(servers[0], servers[0].accessToken)
459 it('Should display its comments', function () {
460 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
463 it('Should have notification from unblocked server', async function () {
467 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
468 await checkCommentNotification(servers[0], comment, 'presence')
475 videoUUID: videoUUID1,
476 text: 'hello @root@localhost:' + servers[0].port
478 await checkCommentNotification(servers[0], comment, 'presence')
484 describe('Server blocklist', function () {
486 describe('When managing account blocklist', function () {
487 it('Should list all videos', async function () {
488 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
489 await checkAllVideos(servers[0], token)
493 it('Should list the comments', async function () {
494 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
495 await checkAllComments(servers[0], token, videoUUID1)
499 it('Should block a remote account', async function () {
500 await command.addToServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
503 it('Should hide its videos', async function () {
504 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
505 const res = await getVideosListWithToken(servers[0].url, token)
507 const videos: Video[] = res.body.data
508 expect(videos).to.have.lengthOf(4)
510 const v = videos.find(v => v.name === 'video user 2')
511 expect(v).to.be.undefined
515 it('Should block a local account', async function () {
516 await command.addToServerBlocklist({ account: 'user1' })
519 it('Should hide its videos', async function () {
520 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
521 const res = await getVideosListWithToken(servers[0].url, token)
523 const videos: Video[] = res.body.data
524 expect(videos).to.have.lengthOf(3)
526 const v = videos.find(v => v.name === 'video user 1')
527 expect(v).to.be.undefined
531 it('Should hide its comments', async function () {
532 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
533 const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token })
534 const threads = data.filter(t => t.isDeleted === false)
536 expect(threads).to.have.lengthOf(1)
537 expect(threads[0].totalReplies).to.equal(1)
539 const t = threads.find(t => t.text === 'comment user 1')
540 expect(t).to.be.undefined
542 for (const thread of threads) {
543 const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token })
544 expect(tree.children).to.have.lengthOf(0)
549 it('Should not have notification from blocked accounts by instance', async function () {
553 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
554 await checkCommentNotification(servers[0], comment, 'absence')
561 videoUUID: videoUUID1,
562 text: 'hello @root@localhost:' + servers[0].port
564 await checkCommentNotification(servers[0], comment, 'absence')
568 it('Should list blocked accounts', async function () {
570 const body = await command.listServerAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
571 expect(body.total).to.equal(2)
573 const block = body.data[0]
574 expect(block.byAccount.displayName).to.equal('peertube')
575 expect(block.byAccount.name).to.equal('peertube')
576 expect(block.blockedAccount.displayName).to.equal('user2')
577 expect(block.blockedAccount.name).to.equal('user2')
578 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
582 const body = await command.listServerAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
583 expect(body.total).to.equal(2)
585 const block = body.data[0]
586 expect(block.byAccount.displayName).to.equal('peertube')
587 expect(block.byAccount.name).to.equal('peertube')
588 expect(block.blockedAccount.displayName).to.equal('user1')
589 expect(block.blockedAccount.name).to.equal('user1')
590 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
594 it('Should unblock the remote account', async function () {
595 await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
598 it('Should display its videos', async function () {
599 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
600 const res = await getVideosListWithToken(servers[0].url, token)
602 const videos: Video[] = res.body.data
603 expect(videos).to.have.lengthOf(4)
605 const v = videos.find(v => v.name === 'video user 2')
606 expect(v).not.to.be.undefined
610 it('Should unblock the local account', async function () {
611 await command.removeFromServerBlocklist({ account: 'user1' })
614 it('Should display its comments', async function () {
615 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
616 await checkAllComments(servers[0], token, videoUUID1)
620 it('Should have notifications from unblocked accounts', async function () {
624 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'displayed comment' }
625 await checkCommentNotification(servers[0], comment, 'presence')
632 videoUUID: videoUUID1,
633 text: 'hello @root@localhost:' + servers[0].port
635 await checkCommentNotification(servers[0], comment, 'presence')
640 describe('When managing server blocklist', function () {
641 it('Should list all videos', async function () {
642 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
643 await checkAllVideos(servers[0], token)
647 it('Should list the comments', async function () {
648 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
649 await checkAllComments(servers[0], token, videoUUID1)
653 it('Should block a remote server', async function () {
654 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
657 it('Should hide its videos', async function () {
658 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
659 const res1 = await getVideosList(servers[0].url)
660 const res2 = await getVideosListWithToken(servers[0].url, token)
662 for (const res of [ res1, res2 ]) {
663 const videos: Video[] = res.body.data
664 expect(videos).to.have.lengthOf(3)
666 const v1 = videos.find(v => v.name === 'video user 2')
667 const v2 = videos.find(v => v.name === 'video server 2')
669 expect(v1).to.be.undefined
670 expect(v2).to.be.undefined
675 it('Should hide its comments', async function () {
678 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
680 await waitJobs(servers)
682 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
684 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
687 it('Should not have notification from blocked instances by instance', async function () {
691 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
692 await checkCommentNotification(servers[0], comment, 'absence')
699 videoUUID: videoUUID1,
700 text: 'hello @root@localhost:' + servers[0].port
702 await checkCommentNotification(servers[0], comment, 'absence')
706 const now = new Date()
707 await servers[1].followsCommand.unfollow({ target: servers[0] })
708 await waitJobs(servers)
709 await servers[1].followsCommand.follow({ targets: [ servers[0].host ] })
711 await waitJobs(servers)
713 const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30)
714 const commentNotifications = (res.body.data as UserNotification[])
716 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER &&
717 n.createdAt >= now.toISOString()
720 expect(commentNotifications).to.have.lengthOf(0)
724 it('Should list blocked servers', async function () {
725 const body = await command.listServerServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
726 expect(body.total).to.equal(1)
728 const block = body.data[0]
729 expect(block.byAccount.displayName).to.equal('peertube')
730 expect(block.byAccount.name).to.equal('peertube')
731 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
734 it('Should unblock the remote server', async function () {
735 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
738 it('Should list all videos', async function () {
739 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
740 await checkAllVideos(servers[0], token)
744 it('Should list the comments', async function () {
745 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
746 await checkAllComments(servers[0], token, videoUUID1)
750 it('Should have notification from unblocked instances', async function () {
754 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
755 await checkCommentNotification(servers[0], comment, 'presence')
762 videoUUID: videoUUID1,
763 text: 'hello @root@localhost:' + servers[0].port
765 await checkCommentNotification(servers[0], comment, 'presence')
769 const now = new Date()
770 await servers[1].followsCommand.unfollow({ target: servers[0] })
771 await waitJobs(servers)
772 await servers[1].followsCommand.follow({ targets: [ servers[0].host ] })
774 await waitJobs(servers)
776 const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30)
777 const commentNotifications = (res.body.data as UserNotification[])
779 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER &&
780 n.createdAt >= now.toISOString()
783 expect(commentNotifications).to.have.lengthOf(1)
789 after(async function () {
790 await cleanupTests(servers)