1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
11 flushAndRunMultipleServers,
13 getVideosListWithToken,
15 setAccessTokensToServers,
18 } from '@shared/extra-utils'
19 import { UserNotificationType, Video } from '@shared/models'
21 const expect = chai.expect
23 async function checkAllVideos (server: ServerInfo, token: string) {
25 const res = await getVideosListWithToken(server.url, token)
27 expect(res.body.data).to.have.lengthOf(5)
31 const res = await getVideosList(server.url)
33 expect(res.body.data).to.have.lengthOf(5)
37 async function checkAllComments (server: ServerInfo, token: string, videoUUID: string) {
38 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token })
40 const threads = data.filter(t => t.isDeleted === false)
41 expect(threads).to.have.lengthOf(2)
43 for (const thread of threads) {
44 const tree = await server.commentsCommand.getThread({ videoId: videoUUID, threadId: thread.id, token })
45 expect(tree.children).to.have.lengthOf(1)
49 async function checkCommentNotification (
50 mainServer: ServerInfo,
51 comment: { server: ServerInfo, token: string, videoUUID: string, text: string },
52 check: 'presence' | 'absence'
54 const command = comment.server.commentsCommand
56 const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text })
58 await waitJobs([ mainServer, comment.server ])
60 const { data } = await mainServer.notificationsCommand.list({ start: 0, count: 30 })
61 const commentNotifications = data.filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
63 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
64 else expect(commentNotifications).to.have.lengthOf(0)
66 await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId })
68 await waitJobs([ mainServer, comment.server ])
71 describe('Test blocklist', function () {
72 let servers: ServerInfo[]
73 let videoUUID1: string
74 let videoUUID2: string
75 let videoUUID3: string
76 let userToken1: string
77 let userModeratorToken: string
78 let userToken2: string
80 let command: BlocklistCommand
81 let commentsCommand: CommentsCommand[]
83 before(async function () {
86 servers = await flushAndRunMultipleServers(3)
87 await setAccessTokensToServers(servers)
89 command = servers[0].blocklistCommand
90 commentsCommand = servers.map(s => s.commentsCommand)
93 const user = { username: 'user1', password: 'password' }
94 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
96 userToken1 = await servers[0].loginCommand.getAccessToken(user)
97 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
101 const user = { username: 'moderator', password: 'password' }
102 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
104 userModeratorToken = await servers[0].loginCommand.getAccessToken(user)
108 const user = { username: 'user2', password: 'password' }
109 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
111 userToken2 = await servers[1].loginCommand.getAccessToken(user)
112 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
116 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
117 videoUUID1 = res.body.video.uuid
121 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
122 videoUUID2 = res.body.video.uuid
126 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' })
127 videoUUID3 = res.body.video.uuid
130 await doubleFollow(servers[0], servers[1])
131 await doubleFollow(servers[0], servers[2])
134 const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' })
135 const reply = await commentsCommand[0].addReply({
138 toCommentId: created.id,
139 text: 'comment user 1'
141 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' })
145 const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' })
146 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' })
149 await waitJobs(servers)
152 describe('User blocklist', function () {
154 describe('When managing account blocklist', function () {
155 it('Should list all videos', function () {
156 return checkAllVideos(servers[0], servers[0].accessToken)
159 it('Should list the comments', function () {
160 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
163 it('Should block a remote account', async function () {
164 await command.addToMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
167 it('Should hide its videos', async function () {
168 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
170 const videos: Video[] = res.body.data
171 expect(videos).to.have.lengthOf(4)
173 const v = videos.find(v => v.name === 'video user 2')
174 expect(v).to.be.undefined
177 it('Should block a local account', async function () {
178 await command.addToMyBlocklist({ account: 'user1' })
181 it('Should hide its videos', async function () {
182 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
184 const videos: Video[] = res.body.data
185 expect(videos).to.have.lengthOf(3)
187 const v = videos.find(v => v.name === 'video user 1')
188 expect(v).to.be.undefined
191 it('Should hide its comments', async function () {
192 const { data } = await commentsCommand[0].listThreads({
193 token: servers[0].accessToken,
200 expect(data).to.have.lengthOf(1)
201 expect(data[0].totalReplies).to.equal(1)
203 const t = data.find(t => t.text === 'comment user 1')
204 expect(t).to.be.undefined
206 for (const thread of data) {
207 const tree = await commentsCommand[0].getThread({
210 token: servers[0].accessToken
212 expect(tree.children).to.have.lengthOf(0)
216 it('Should not have notifications from blocked accounts', async function () {
220 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
221 await checkCommentNotification(servers[0], comment, 'absence')
228 videoUUID: videoUUID2,
229 text: 'hello @root@localhost:' + servers[0].port
231 await checkCommentNotification(servers[0], comment, 'absence')
235 it('Should list all the videos with another user', async function () {
236 return checkAllVideos(servers[0], userToken1)
239 it('Should list blocked accounts', async function () {
241 const body = await command.listMyAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
242 expect(body.total).to.equal(2)
244 const block = body.data[0]
245 expect(block.byAccount.displayName).to.equal('root')
246 expect(block.byAccount.name).to.equal('root')
247 expect(block.blockedAccount.displayName).to.equal('user2')
248 expect(block.blockedAccount.name).to.equal('user2')
249 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
253 const body = await command.listMyAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
254 expect(body.total).to.equal(2)
256 const block = body.data[0]
257 expect(block.byAccount.displayName).to.equal('root')
258 expect(block.byAccount.name).to.equal('root')
259 expect(block.blockedAccount.displayName).to.equal('user1')
260 expect(block.blockedAccount.name).to.equal('user1')
261 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
265 it('Should not allow a remote blocked user to comment my videos', async function () {
269 await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' })
270 await waitJobs(servers)
272 await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' })
273 await waitJobs(servers)
275 const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: 'uploader' })
276 const message = 'reply by user 2'
277 const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message })
278 await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: 'another reply' })
280 await waitJobs(servers)
283 // Server 2 has all the comments
285 const { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
287 expect(data).to.have.lengthOf(2)
288 expect(data[0].text).to.equal('uploader')
289 expect(data[1].text).to.equal('comment user 2')
291 const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id })
292 expect(tree.children).to.have.lengthOf(1)
293 expect(tree.children[0].comment.text).to.equal('reply by user 2')
294 expect(tree.children[0].children).to.have.lengthOf(1)
295 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
298 // Server 1 and 3 should only have uploader comments
299 for (const server of [ servers[0], servers[2] ]) {
300 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
302 expect(data).to.have.lengthOf(1)
303 expect(data[0].text).to.equal('uploader')
305 const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id })
307 if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0)
308 else expect(tree.children).to.have.lengthOf(1)
312 it('Should unblock the remote account', async function () {
313 await command.removeFromMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
316 it('Should display its videos', async function () {
317 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
319 const videos: Video[] = res.body.data
320 expect(videos).to.have.lengthOf(4)
322 const v = videos.find(v => v.name === 'video user 2')
323 expect(v).not.to.be.undefined
326 it('Should display its comments on my video', async function () {
327 for (const server of servers) {
328 const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
330 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
331 if (server.serverNumber === 3) {
332 expect(data).to.have.lengthOf(1)
336 expect(data).to.have.lengthOf(2)
337 expect(data[0].text).to.equal('uploader')
338 expect(data[1].text).to.equal('comment user 2')
340 const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id })
341 expect(tree.children).to.have.lengthOf(1)
342 expect(tree.children[0].comment.text).to.equal('reply by user 2')
343 expect(tree.children[0].children).to.have.lengthOf(1)
344 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
348 it('Should unblock the local account', async function () {
349 await command.removeFromMyBlocklist({ account: 'user1' })
352 it('Should display its comments', function () {
353 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
356 it('Should have a notification from a non blocked account', async function () {
360 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
361 await checkCommentNotification(servers[0], comment, 'presence')
368 videoUUID: videoUUID2,
369 text: 'hello @root@localhost:' + servers[0].port
371 await checkCommentNotification(servers[0], comment, 'presence')
376 describe('When managing server blocklist', function () {
378 it('Should list all videos', function () {
379 return checkAllVideos(servers[0], servers[0].accessToken)
382 it('Should list the comments', function () {
383 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
386 it('Should block a remote server', async function () {
387 await command.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
390 it('Should hide its videos', async function () {
391 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
393 const videos: Video[] = res.body.data
394 expect(videos).to.have.lengthOf(3)
396 const v1 = videos.find(v => v.name === 'video user 2')
397 const v2 = videos.find(v => v.name === 'video server 2')
399 expect(v1).to.be.undefined
400 expect(v2).to.be.undefined
403 it('Should list all the videos with another user', async function () {
404 return checkAllVideos(servers[0], userToken1)
407 it('Should hide its comments', async function () {
410 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
412 await waitJobs(servers)
414 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
416 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
419 it('Should not have notifications from blocked server', async function () {
423 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
424 await checkCommentNotification(servers[0], comment, 'absence')
431 videoUUID: videoUUID1,
432 text: 'hello @root@localhost:' + servers[0].port
434 await checkCommentNotification(servers[0], comment, 'absence')
438 it('Should list blocked servers', async function () {
439 const body = await command.listMyServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
440 expect(body.total).to.equal(1)
442 const block = body.data[0]
443 expect(block.byAccount.displayName).to.equal('root')
444 expect(block.byAccount.name).to.equal('root')
445 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
448 it('Should unblock the remote server', async function () {
449 await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port })
452 it('Should display its videos', function () {
453 return checkAllVideos(servers[0], servers[0].accessToken)
456 it('Should display its comments', function () {
457 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
460 it('Should have notification from unblocked server', async function () {
464 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
465 await checkCommentNotification(servers[0], comment, 'presence')
472 videoUUID: videoUUID1,
473 text: 'hello @root@localhost:' + servers[0].port
475 await checkCommentNotification(servers[0], comment, 'presence')
481 describe('Server blocklist', function () {
483 describe('When managing account blocklist', function () {
484 it('Should list all videos', async function () {
485 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
486 await checkAllVideos(servers[0], token)
490 it('Should list the comments', async function () {
491 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
492 await checkAllComments(servers[0], token, videoUUID1)
496 it('Should block a remote account', async function () {
497 await command.addToServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
500 it('Should hide its videos', async function () {
501 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
502 const res = await getVideosListWithToken(servers[0].url, token)
504 const videos: Video[] = res.body.data
505 expect(videos).to.have.lengthOf(4)
507 const v = videos.find(v => v.name === 'video user 2')
508 expect(v).to.be.undefined
512 it('Should block a local account', async function () {
513 await command.addToServerBlocklist({ account: 'user1' })
516 it('Should hide its videos', async function () {
517 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
518 const res = await getVideosListWithToken(servers[0].url, token)
520 const videos: Video[] = res.body.data
521 expect(videos).to.have.lengthOf(3)
523 const v = videos.find(v => v.name === 'video user 1')
524 expect(v).to.be.undefined
528 it('Should hide its comments', async function () {
529 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
530 const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token })
531 const threads = data.filter(t => t.isDeleted === false)
533 expect(threads).to.have.lengthOf(1)
534 expect(threads[0].totalReplies).to.equal(1)
536 const t = threads.find(t => t.text === 'comment user 1')
537 expect(t).to.be.undefined
539 for (const thread of threads) {
540 const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token })
541 expect(tree.children).to.have.lengthOf(0)
546 it('Should not have notification from blocked accounts by instance', async function () {
550 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
551 await checkCommentNotification(servers[0], comment, 'absence')
558 videoUUID: videoUUID1,
559 text: 'hello @root@localhost:' + servers[0].port
561 await checkCommentNotification(servers[0], comment, 'absence')
565 it('Should list blocked accounts', async function () {
567 const body = await command.listServerAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
568 expect(body.total).to.equal(2)
570 const block = body.data[0]
571 expect(block.byAccount.displayName).to.equal('peertube')
572 expect(block.byAccount.name).to.equal('peertube')
573 expect(block.blockedAccount.displayName).to.equal('user2')
574 expect(block.blockedAccount.name).to.equal('user2')
575 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
579 const body = await command.listServerAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
580 expect(body.total).to.equal(2)
582 const block = body.data[0]
583 expect(block.byAccount.displayName).to.equal('peertube')
584 expect(block.byAccount.name).to.equal('peertube')
585 expect(block.blockedAccount.displayName).to.equal('user1')
586 expect(block.blockedAccount.name).to.equal('user1')
587 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
591 it('Should unblock the remote account', async function () {
592 await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
595 it('Should display its videos', async function () {
596 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
597 const res = await getVideosListWithToken(servers[0].url, token)
599 const videos: Video[] = res.body.data
600 expect(videos).to.have.lengthOf(4)
602 const v = videos.find(v => v.name === 'video user 2')
603 expect(v).not.to.be.undefined
607 it('Should unblock the local account', async function () {
608 await command.removeFromServerBlocklist({ account: 'user1' })
611 it('Should display its comments', async function () {
612 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
613 await checkAllComments(servers[0], token, videoUUID1)
617 it('Should have notifications from unblocked accounts', async function () {
621 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'displayed comment' }
622 await checkCommentNotification(servers[0], comment, 'presence')
629 videoUUID: videoUUID1,
630 text: 'hello @root@localhost:' + servers[0].port
632 await checkCommentNotification(servers[0], comment, 'presence')
637 describe('When managing server blocklist', function () {
638 it('Should list all videos', async function () {
639 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
640 await checkAllVideos(servers[0], token)
644 it('Should list the comments', async function () {
645 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
646 await checkAllComments(servers[0], token, videoUUID1)
650 it('Should block a remote server', async function () {
651 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
654 it('Should hide its videos', async function () {
655 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
656 const res1 = await getVideosList(servers[0].url)
657 const res2 = await getVideosListWithToken(servers[0].url, token)
659 for (const res of [ res1, res2 ]) {
660 const videos: Video[] = res.body.data
661 expect(videos).to.have.lengthOf(3)
663 const v1 = videos.find(v => v.name === 'video user 2')
664 const v2 = videos.find(v => v.name === 'video server 2')
666 expect(v1).to.be.undefined
667 expect(v2).to.be.undefined
672 it('Should hide its comments', async function () {
675 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
677 await waitJobs(servers)
679 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
681 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
684 it('Should not have notification from blocked instances by instance', async function () {
688 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
689 await checkCommentNotification(servers[0], comment, 'absence')
696 videoUUID: videoUUID1,
697 text: 'hello @root@localhost:' + servers[0].port
699 await checkCommentNotification(servers[0], comment, 'absence')
703 const now = new Date()
704 await servers[1].followsCommand.unfollow({ target: servers[0] })
705 await waitJobs(servers)
706 await servers[1].followsCommand.follow({ targets: [ servers[0].host ] })
708 await waitJobs(servers)
710 const { data } = await servers[0].notificationsCommand.list({ start: 0, count: 30 })
711 const commentNotifications = data.filter(n => {
712 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
715 expect(commentNotifications).to.have.lengthOf(0)
719 it('Should list blocked servers', async function () {
720 const body = await command.listServerServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
721 expect(body.total).to.equal(1)
723 const block = body.data[0]
724 expect(block.byAccount.displayName).to.equal('peertube')
725 expect(block.byAccount.name).to.equal('peertube')
726 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
729 it('Should unblock the remote server', async function () {
730 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
733 it('Should list all videos', async function () {
734 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
735 await checkAllVideos(servers[0], token)
739 it('Should list the comments', async function () {
740 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
741 await checkAllComments(servers[0], token, videoUUID1)
745 it('Should have notification from unblocked instances', async function () {
749 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
750 await checkCommentNotification(servers[0], comment, 'presence')
757 videoUUID: videoUUID1,
758 text: 'hello @root@localhost:' + servers[0].port
760 await checkCommentNotification(servers[0], comment, 'presence')
764 const now = new Date()
765 await servers[1].followsCommand.unfollow({ target: servers[0] })
766 await waitJobs(servers)
767 await servers[1].followsCommand.follow({ targets: [ servers[0].host ] })
769 await waitJobs(servers)
771 const { data } = await servers[0].notificationsCommand.list({ start: 0, count: 30 })
772 const commentNotifications = data.filter(n => {
773 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
776 expect(commentNotifications).to.have.lengthOf(1)
782 after(async function () {
783 await cleanupTests(servers)