1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
10 flushAndRunMultipleServers,
12 getVideosListWithToken,
14 setAccessTokensToServers,
17 } from '@shared/extra-utils'
18 import { UserNotificationType, Video } from '@shared/models'
20 const expect = chai.expect
22 async function checkAllVideos (server: ServerInfo, token: string) {
24 const res = await getVideosListWithToken(server.url, token)
26 expect(res.body.data).to.have.lengthOf(5)
30 const res = await getVideosList(server.url)
32 expect(res.body.data).to.have.lengthOf(5)
36 async function checkAllComments (server: ServerInfo, token: string, videoUUID: string) {
37 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token })
39 const threads = data.filter(t => t.isDeleted === false)
40 expect(threads).to.have.lengthOf(2)
42 for (const thread of threads) {
43 const tree = await server.commentsCommand.getThread({ videoId: videoUUID, threadId: thread.id, token })
44 expect(tree.children).to.have.lengthOf(1)
48 async function checkCommentNotification (
49 mainServer: ServerInfo,
50 comment: { server: ServerInfo, token: string, videoUUID: string, text: string },
51 check: 'presence' | 'absence'
53 const command = comment.server.commentsCommand
55 const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text })
57 await waitJobs([ mainServer, comment.server ])
59 const { data } = await mainServer.notificationsCommand.list({ start: 0, count: 30 })
60 const commentNotifications = data.filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
62 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
63 else expect(commentNotifications).to.have.lengthOf(0)
65 await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId })
67 await waitJobs([ mainServer, comment.server ])
70 describe('Test blocklist', function () {
71 let servers: ServerInfo[]
72 let videoUUID1: string
73 let videoUUID2: string
74 let videoUUID3: string
75 let userToken1: string
76 let userModeratorToken: string
77 let userToken2: string
79 let command: BlocklistCommand
80 let commentsCommand: CommentsCommand[]
82 before(async function () {
85 servers = await flushAndRunMultipleServers(3)
86 await setAccessTokensToServers(servers)
88 command = servers[0].blocklistCommand
89 commentsCommand = servers.map(s => s.commentsCommand)
92 const user = { username: 'user1', password: 'password' }
93 await servers[0].usersCommand.create({ username: user.username, password: user.password })
95 userToken1 = await servers[0].loginCommand.getAccessToken(user)
96 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
100 const user = { username: 'moderator', password: 'password' }
101 await servers[0].usersCommand.create({ username: user.username, password: user.password })
103 userModeratorToken = await servers[0].loginCommand.getAccessToken(user)
107 const user = { username: 'user2', password: 'password' }
108 await servers[1].usersCommand.create({ username: user.username, password: user.password })
110 userToken2 = await servers[1].loginCommand.getAccessToken(user)
111 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
115 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
116 videoUUID1 = res.body.video.uuid
120 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
121 videoUUID2 = res.body.video.uuid
125 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' })
126 videoUUID3 = res.body.video.uuid
129 await doubleFollow(servers[0], servers[1])
130 await doubleFollow(servers[0], servers[2])
133 const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' })
134 const reply = await commentsCommand[0].addReply({
137 toCommentId: created.id,
138 text: 'comment user 1'
140 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' })
144 const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' })
145 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' })
148 await waitJobs(servers)
151 describe('User blocklist', function () {
153 describe('When managing account blocklist', function () {
154 it('Should list all videos', function () {
155 return checkAllVideos(servers[0], servers[0].accessToken)
158 it('Should list the comments', function () {
159 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
162 it('Should block a remote account', async function () {
163 await command.addToMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
166 it('Should hide its videos', async function () {
167 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
169 const videos: Video[] = res.body.data
170 expect(videos).to.have.lengthOf(4)
172 const v = videos.find(v => v.name === 'video user 2')
173 expect(v).to.be.undefined
176 it('Should block a local account', async function () {
177 await command.addToMyBlocklist({ account: 'user1' })
180 it('Should hide its videos', async function () {
181 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
183 const videos: Video[] = res.body.data
184 expect(videos).to.have.lengthOf(3)
186 const v = videos.find(v => v.name === 'video user 1')
187 expect(v).to.be.undefined
190 it('Should hide its comments', async function () {
191 const { data } = await commentsCommand[0].listThreads({
192 token: servers[0].accessToken,
199 expect(data).to.have.lengthOf(1)
200 expect(data[0].totalReplies).to.equal(1)
202 const t = data.find(t => t.text === 'comment user 1')
203 expect(t).to.be.undefined
205 for (const thread of data) {
206 const tree = await commentsCommand[0].getThread({
209 token: servers[0].accessToken
211 expect(tree.children).to.have.lengthOf(0)
215 it('Should not have notifications from blocked accounts', async function () {
219 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
220 await checkCommentNotification(servers[0], comment, 'absence')
227 videoUUID: videoUUID2,
228 text: 'hello @root@localhost:' + servers[0].port
230 await checkCommentNotification(servers[0], comment, 'absence')
234 it('Should list all the videos with another user', async function () {
235 return checkAllVideos(servers[0], userToken1)
238 it('Should list blocked accounts', async function () {
240 const body = await command.listMyAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
241 expect(body.total).to.equal(2)
243 const block = body.data[0]
244 expect(block.byAccount.displayName).to.equal('root')
245 expect(block.byAccount.name).to.equal('root')
246 expect(block.blockedAccount.displayName).to.equal('user2')
247 expect(block.blockedAccount.name).to.equal('user2')
248 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
252 const body = await command.listMyAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
253 expect(body.total).to.equal(2)
255 const block = body.data[0]
256 expect(block.byAccount.displayName).to.equal('root')
257 expect(block.byAccount.name).to.equal('root')
258 expect(block.blockedAccount.displayName).to.equal('user1')
259 expect(block.blockedAccount.name).to.equal('user1')
260 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
264 it('Should not allow a remote blocked user to comment my videos', async function () {
268 await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' })
269 await waitJobs(servers)
271 await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' })
272 await waitJobs(servers)
274 const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: 'uploader' })
275 const message = 'reply by user 2'
276 const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message })
277 await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: 'another reply' })
279 await waitJobs(servers)
282 // Server 2 has all the comments
284 const { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
286 expect(data).to.have.lengthOf(2)
287 expect(data[0].text).to.equal('uploader')
288 expect(data[1].text).to.equal('comment user 2')
290 const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id })
291 expect(tree.children).to.have.lengthOf(1)
292 expect(tree.children[0].comment.text).to.equal('reply by user 2')
293 expect(tree.children[0].children).to.have.lengthOf(1)
294 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
297 // Server 1 and 3 should only have uploader comments
298 for (const server of [ servers[0], servers[2] ]) {
299 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
301 expect(data).to.have.lengthOf(1)
302 expect(data[0].text).to.equal('uploader')
304 const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id })
306 if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0)
307 else expect(tree.children).to.have.lengthOf(1)
311 it('Should unblock the remote account', async function () {
312 await command.removeFromMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
315 it('Should display its videos', async function () {
316 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
318 const videos: Video[] = res.body.data
319 expect(videos).to.have.lengthOf(4)
321 const v = videos.find(v => v.name === 'video user 2')
322 expect(v).not.to.be.undefined
325 it('Should display its comments on my video', async function () {
326 for (const server of servers) {
327 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
329 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
330 if (server.serverNumber === 3) {
331 expect(data).to.have.lengthOf(1)
335 expect(data).to.have.lengthOf(2)
336 expect(data[0].text).to.equal('uploader')
337 expect(data[1].text).to.equal('comment user 2')
339 const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id })
340 expect(tree.children).to.have.lengthOf(1)
341 expect(tree.children[0].comment.text).to.equal('reply by user 2')
342 expect(tree.children[0].children).to.have.lengthOf(1)
343 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
347 it('Should unblock the local account', async function () {
348 await command.removeFromMyBlocklist({ account: 'user1' })
351 it('Should display its comments', function () {
352 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
355 it('Should have a notification from a non blocked account', async function () {
359 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
360 await checkCommentNotification(servers[0], comment, 'presence')
367 videoUUID: videoUUID2,
368 text: 'hello @root@localhost:' + servers[0].port
370 await checkCommentNotification(servers[0], comment, 'presence')
375 describe('When managing server blocklist', function () {
377 it('Should list all videos', function () {
378 return checkAllVideos(servers[0], servers[0].accessToken)
381 it('Should list the comments', function () {
382 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
385 it('Should block a remote server', async function () {
386 await command.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
389 it('Should hide its videos', async function () {
390 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
392 const videos: Video[] = res.body.data
393 expect(videos).to.have.lengthOf(3)
395 const v1 = videos.find(v => v.name === 'video user 2')
396 const v2 = videos.find(v => v.name === 'video server 2')
398 expect(v1).to.be.undefined
399 expect(v2).to.be.undefined
402 it('Should list all the videos with another user', async function () {
403 return checkAllVideos(servers[0], userToken1)
406 it('Should hide its comments', async function () {
409 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
411 await waitJobs(servers)
413 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
415 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
418 it('Should not have notifications from blocked server', async function () {
422 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
423 await checkCommentNotification(servers[0], comment, 'absence')
430 videoUUID: videoUUID1,
431 text: 'hello @root@localhost:' + servers[0].port
433 await checkCommentNotification(servers[0], comment, 'absence')
437 it('Should list blocked servers', async function () {
438 const body = await command.listMyServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
439 expect(body.total).to.equal(1)
441 const block = body.data[0]
442 expect(block.byAccount.displayName).to.equal('root')
443 expect(block.byAccount.name).to.equal('root')
444 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
447 it('Should unblock the remote server', async function () {
448 await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port })
451 it('Should display its videos', function () {
452 return checkAllVideos(servers[0], servers[0].accessToken)
455 it('Should display its comments', function () {
456 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
459 it('Should have notification from unblocked server', async function () {
463 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
464 await checkCommentNotification(servers[0], comment, 'presence')
471 videoUUID: videoUUID1,
472 text: 'hello @root@localhost:' + servers[0].port
474 await checkCommentNotification(servers[0], comment, 'presence')
480 describe('Server blocklist', function () {
482 describe('When managing account blocklist', function () {
483 it('Should list all videos', async function () {
484 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
485 await checkAllVideos(servers[0], token)
489 it('Should list the comments', async function () {
490 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
491 await checkAllComments(servers[0], token, videoUUID1)
495 it('Should block a remote account', async function () {
496 await command.addToServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
499 it('Should hide its videos', async function () {
500 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
501 const res = await getVideosListWithToken(servers[0].url, token)
503 const videos: Video[] = res.body.data
504 expect(videos).to.have.lengthOf(4)
506 const v = videos.find(v => v.name === 'video user 2')
507 expect(v).to.be.undefined
511 it('Should block a local account', async function () {
512 await command.addToServerBlocklist({ account: 'user1' })
515 it('Should hide its videos', async function () {
516 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
517 const res = await getVideosListWithToken(servers[0].url, token)
519 const videos: Video[] = res.body.data
520 expect(videos).to.have.lengthOf(3)
522 const v = videos.find(v => v.name === 'video user 1')
523 expect(v).to.be.undefined
527 it('Should hide its comments', async function () {
528 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
529 const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token })
530 const threads = data.filter(t => t.isDeleted === false)
532 expect(threads).to.have.lengthOf(1)
533 expect(threads[0].totalReplies).to.equal(1)
535 const t = threads.find(t => t.text === 'comment user 1')
536 expect(t).to.be.undefined
538 for (const thread of threads) {
539 const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token })
540 expect(tree.children).to.have.lengthOf(0)
545 it('Should not have notification from blocked accounts by instance', async function () {
549 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
550 await checkCommentNotification(servers[0], comment, 'absence')
557 videoUUID: videoUUID1,
558 text: 'hello @root@localhost:' + servers[0].port
560 await checkCommentNotification(servers[0], comment, 'absence')
564 it('Should list blocked accounts', async function () {
566 const body = await command.listServerAccountBlocklist({ start: 0, count: 1, 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('user2')
573 expect(block.blockedAccount.name).to.equal('user2')
574 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
578 const body = await command.listServerAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
579 expect(body.total).to.equal(2)
581 const block = body.data[0]
582 expect(block.byAccount.displayName).to.equal('peertube')
583 expect(block.byAccount.name).to.equal('peertube')
584 expect(block.blockedAccount.displayName).to.equal('user1')
585 expect(block.blockedAccount.name).to.equal('user1')
586 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
590 it('Should unblock the remote account', async function () {
591 await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
594 it('Should display its videos', async function () {
595 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
596 const res = await getVideosListWithToken(servers[0].url, token)
598 const videos: Video[] = res.body.data
599 expect(videos).to.have.lengthOf(4)
601 const v = videos.find(v => v.name === 'video user 2')
602 expect(v).not.to.be.undefined
606 it('Should unblock the local account', async function () {
607 await command.removeFromServerBlocklist({ account: 'user1' })
610 it('Should display its comments', async function () {
611 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
612 await checkAllComments(servers[0], token, videoUUID1)
616 it('Should have notifications from unblocked accounts', async function () {
620 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'displayed comment' }
621 await checkCommentNotification(servers[0], comment, 'presence')
628 videoUUID: videoUUID1,
629 text: 'hello @root@localhost:' + servers[0].port
631 await checkCommentNotification(servers[0], comment, 'presence')
636 describe('When managing server blocklist', function () {
637 it('Should list all videos', async function () {
638 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
639 await checkAllVideos(servers[0], token)
643 it('Should list the comments', async function () {
644 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
645 await checkAllComments(servers[0], token, videoUUID1)
649 it('Should block a remote server', async function () {
650 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
653 it('Should hide its videos', async function () {
654 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
655 const res1 = await getVideosList(servers[0].url)
656 const res2 = await getVideosListWithToken(servers[0].url, token)
658 for (const res of [ res1, res2 ]) {
659 const videos: Video[] = res.body.data
660 expect(videos).to.have.lengthOf(3)
662 const v1 = videos.find(v => v.name === 'video user 2')
663 const v2 = videos.find(v => v.name === 'video server 2')
665 expect(v1).to.be.undefined
666 expect(v2).to.be.undefined
671 it('Should hide its comments', async function () {
674 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
676 await waitJobs(servers)
678 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
680 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
683 it('Should not have notification from blocked instances by instance', async function () {
687 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
688 await checkCommentNotification(servers[0], comment, 'absence')
695 videoUUID: videoUUID1,
696 text: 'hello @root@localhost:' + servers[0].port
698 await checkCommentNotification(servers[0], comment, 'absence')
702 const now = new Date()
703 await servers[1].followsCommand.unfollow({ target: servers[0] })
704 await waitJobs(servers)
705 await servers[1].followsCommand.follow({ targets: [ servers[0].host ] })
707 await waitJobs(servers)
709 const { data } = await servers[0].notificationsCommand.list({ start: 0, count: 30 })
710 const commentNotifications = data.filter(n => {
711 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
714 expect(commentNotifications).to.have.lengthOf(0)
718 it('Should list blocked servers', async function () {
719 const body = await command.listServerServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
720 expect(body.total).to.equal(1)
722 const block = body.data[0]
723 expect(block.byAccount.displayName).to.equal('peertube')
724 expect(block.byAccount.name).to.equal('peertube')
725 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
728 it('Should unblock the remote server', async function () {
729 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
732 it('Should list all videos', async function () {
733 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
734 await checkAllVideos(servers[0], token)
738 it('Should list the comments', async function () {
739 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
740 await checkAllComments(servers[0], token, videoUUID1)
744 it('Should have notification from unblocked instances', async function () {
748 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
749 await checkCommentNotification(servers[0], comment, 'presence')
756 videoUUID: videoUUID1,
757 text: 'hello @root@localhost:' + servers[0].port
759 await checkCommentNotification(servers[0], comment, 'presence')
763 const now = new Date()
764 await servers[1].followsCommand.unfollow({ target: servers[0] })
765 await waitJobs(servers)
766 await servers[1].followsCommand.follow({ targets: [ servers[0].host ] })
768 await waitJobs(servers)
770 const { data } = await servers[0].notificationsCommand.list({ start: 0, count: 30 })
771 const commentNotifications = data.filter(n => {
772 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
775 expect(commentNotifications).to.have.lengthOf(1)
781 after(async function () {
782 await cleanupTests(servers)