1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
9 flushAndRunMultipleServers,
15 } from '../../../../shared/extra-utils/index'
16 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
17 import { getVideosListWithToken, getVideosList } from '../../../../shared/extra-utils/videos/videos'
20 addVideoCommentThread,
21 getVideoCommentThreads,
22 getVideoThreadComments
23 } from '../../../../shared/extra-utils/videos/video-comments'
24 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
25 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
27 addAccountToAccountBlocklist,
28 addAccountToServerBlocklist,
29 addServerToAccountBlocklist,
30 addServerToServerBlocklist,
31 getAccountBlocklistByAccount,
32 getAccountBlocklistByServer,
33 getServerBlocklistByAccount,
34 getServerBlocklistByServer,
35 removeAccountFromAccountBlocklist,
36 removeAccountFromServerBlocklist,
37 removeServerFromAccountBlocklist,
38 removeServerFromServerBlocklist
39 } from '../../../../shared/extra-utils/users/blocklist'
41 const expect = chai.expect
43 async function checkAllVideos (url: string, token: string) {
45 const res = await getVideosListWithToken(url, token)
47 expect(res.body.data).to.have.lengthOf(4)
51 const res = await getVideosList(url)
53 expect(res.body.data).to.have.lengthOf(4)
57 async function checkAllComments (url: string, token: string, videoUUID: string) {
58 const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
60 const threads: VideoComment[] = resThreads.body.data
61 expect(threads).to.have.lengthOf(2)
63 for (const thread of threads) {
64 const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
66 const tree: VideoCommentThreadTree = res.body
67 expect(tree.children).to.have.lengthOf(1)
71 describe('Test blocklist', function () {
72 let servers: ServerInfo[]
73 let videoUUID1: string
74 let videoUUID2: string
75 let userToken1: string
76 let userModeratorToken: string
77 let userToken2: string
79 before(async function () {
82 servers = await flushAndRunMultipleServers(2)
83 await setAccessTokensToServers(servers)
86 const user = { username: 'user1', password: 'password' }
87 await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
89 userToken1 = await userLogin(servers[0], user)
90 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
94 const user = { username: 'moderator', password: 'password' }
95 await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
97 userModeratorToken = await userLogin(servers[0], user)
101 const user = { username: 'user2', password: 'password' }
102 await createUser({ url: servers[ 1 ].url, accessToken: servers[ 1 ].accessToken, username: user.username, password: user.password })
104 userToken2 = await userLogin(servers[1], user)
105 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
109 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
110 videoUUID1 = res.body.video.uuid
114 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
115 videoUUID2 = res.body.video.uuid
118 await doubleFollow(servers[0], servers[1])
121 const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
122 const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
123 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
127 const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
128 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
131 await waitJobs(servers)
134 describe('User blocklist', function () {
136 describe('When managing account blocklist', function () {
137 it('Should list all videos', function () {
138 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
141 it('Should list the comments', function () {
142 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
145 it('Should block a remote account', async function () {
146 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
149 it('Should hide its videos', async function () {
150 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
152 const videos: Video[] = res.body.data
153 expect(videos).to.have.lengthOf(3)
155 const v = videos.find(v => v.name === 'video user 2')
156 expect(v).to.be.undefined
159 it('Should block a local account', async function () {
160 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
163 it('Should hide its videos', async function () {
164 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
166 const videos: Video[] = res.body.data
167 expect(videos).to.have.lengthOf(2)
169 const v = videos.find(v => v.name === 'video user 1')
170 expect(v).to.be.undefined
173 it('Should hide its comments', async function () {
174 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', servers[ 0 ].accessToken)
176 const threads: VideoComment[] = resThreads.body.data
177 expect(threads).to.have.lengthOf(1)
178 expect(threads[ 0 ].totalReplies).to.equal(0)
180 const t = threads.find(t => t.text === 'comment user 1')
181 expect(t).to.be.undefined
183 for (const thread of threads) {
184 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, servers[ 0 ].accessToken)
186 const tree: VideoCommentThreadTree = res.body
187 expect(tree.children).to.have.lengthOf(0)
191 it('Should list all the videos with another user', async function () {
192 return checkAllVideos(servers[ 0 ].url, userToken1)
195 it('Should list all the comments with another user', async function () {
196 return checkAllComments(servers[ 0 ].url, userToken1, videoUUID1)
199 it('Should list blocked accounts', async function () {
201 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
202 const blocks: AccountBlock[] = res.body.data
204 expect(res.body.total).to.equal(2)
206 const block = blocks[ 0 ]
207 expect(block.byAccount.displayName).to.equal('root')
208 expect(block.byAccount.name).to.equal('root')
209 expect(block.blockedAccount.displayName).to.equal('user2')
210 expect(block.blockedAccount.name).to.equal('user2')
211 expect(block.blockedAccount.host).to.equal('localhost:9002')
215 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
216 const blocks: AccountBlock[] = res.body.data
218 expect(res.body.total).to.equal(2)
220 const block = blocks[ 0 ]
221 expect(block.byAccount.displayName).to.equal('root')
222 expect(block.byAccount.name).to.equal('root')
223 expect(block.blockedAccount.displayName).to.equal('user1')
224 expect(block.blockedAccount.name).to.equal('user1')
225 expect(block.blockedAccount.host).to.equal('localhost:9001')
229 it('Should unblock the remote account', async function () {
230 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
233 it('Should display its videos', async function () {
234 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
236 const videos: Video[] = res.body.data
237 expect(videos).to.have.lengthOf(3)
239 const v = videos.find(v => v.name === 'video user 2')
240 expect(v).not.to.be.undefined
243 it('Should unblock the local account', async function () {
244 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
247 it('Should display its comments', function () {
248 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
252 describe('When managing server blocklist', function () {
253 it('Should list all videos', function () {
254 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
257 it('Should list the comments', function () {
258 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
261 it('Should block a remote server', async function () {
262 await addServerToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
265 it('Should hide its videos', async function () {
266 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
268 const videos: Video[] = res.body.data
269 expect(videos).to.have.lengthOf(2)
271 const v1 = videos.find(v => v.name === 'video user 2')
272 const v2 = videos.find(v => v.name === 'video server 2')
274 expect(v1).to.be.undefined
275 expect(v2).to.be.undefined
278 it('Should list all the videos with another user', async function () {
279 return checkAllVideos(servers[ 0 ].url, userToken1)
282 it('Should hide its comments')
284 it('Should list blocked servers', async function () {
285 const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
286 const blocks: ServerBlock[] = res.body.data
288 expect(res.body.total).to.equal(1)
290 const block = blocks[ 0 ]
291 expect(block.byAccount.displayName).to.equal('root')
292 expect(block.byAccount.name).to.equal('root')
293 expect(block.blockedServer.host).to.equal('localhost:9002')
296 it('Should unblock the remote server', async function () {
297 await removeServerFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
300 it('Should display its videos', function () {
301 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
304 it('Should display its comments', function () {
305 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
310 describe('Server blocklist', function () {
312 describe('When managing account blocklist', function () {
313 it('Should list all videos', async function () {
314 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
315 await checkAllVideos(servers[ 0 ].url, token)
319 it('Should list the comments', async function () {
320 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
321 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
325 it('Should block a remote account', async function () {
326 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
329 it('Should hide its videos', async function () {
330 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
331 const res = await getVideosListWithToken(servers[ 0 ].url, token)
333 const videos: Video[] = res.body.data
334 expect(videos).to.have.lengthOf(3)
336 const v = videos.find(v => v.name === 'video user 2')
337 expect(v).to.be.undefined
341 it('Should block a local account', async function () {
342 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
345 it('Should hide its videos', async function () {
346 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
347 const res = await getVideosListWithToken(servers[ 0 ].url, token)
349 const videos: Video[] = res.body.data
350 expect(videos).to.have.lengthOf(2)
352 const v = videos.find(v => v.name === 'video user 1')
353 expect(v).to.be.undefined
357 it('Should hide its comments', async function () {
358 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
359 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', token)
361 const threads: VideoComment[] = resThreads.body.data
362 expect(threads).to.have.lengthOf(1)
363 expect(threads[ 0 ].totalReplies).to.equal(0)
365 const t = threads.find(t => t.text === 'comment user 1')
366 expect(t).to.be.undefined
368 for (const thread of threads) {
369 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, token)
371 const tree: VideoCommentThreadTree = res.body
372 expect(tree.children).to.have.lengthOf(0)
377 it('Should list blocked accounts', async function () {
379 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
380 const blocks: AccountBlock[] = res.body.data
382 expect(res.body.total).to.equal(2)
384 const block = blocks[ 0 ]
385 expect(block.byAccount.displayName).to.equal('peertube')
386 expect(block.byAccount.name).to.equal('peertube')
387 expect(block.blockedAccount.displayName).to.equal('user2')
388 expect(block.blockedAccount.name).to.equal('user2')
389 expect(block.blockedAccount.host).to.equal('localhost:9002')
393 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
394 const blocks: AccountBlock[] = res.body.data
396 expect(res.body.total).to.equal(2)
398 const block = blocks[ 0 ]
399 expect(block.byAccount.displayName).to.equal('peertube')
400 expect(block.byAccount.name).to.equal('peertube')
401 expect(block.blockedAccount.displayName).to.equal('user1')
402 expect(block.blockedAccount.name).to.equal('user1')
403 expect(block.blockedAccount.host).to.equal('localhost:9001')
407 it('Should unblock the remote account', async function () {
408 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
411 it('Should display its videos', async function () {
412 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
413 const res = await getVideosListWithToken(servers[ 0 ].url, token)
415 const videos: Video[] = res.body.data
416 expect(videos).to.have.lengthOf(3)
418 const v = videos.find(v => v.name === 'video user 2')
419 expect(v).not.to.be.undefined
423 it('Should unblock the local account', async function () {
424 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
427 it('Should display its comments', async function () {
428 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
429 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
434 describe('When managing server blocklist', function () {
435 it('Should list all videos', async function () {
436 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
437 await checkAllVideos(servers[ 0 ].url, token)
441 it('Should list the comments', async function () {
442 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
443 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
447 it('Should block a remote server', async function () {
448 await addServerToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
451 it('Should hide its videos', async function () {
452 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
453 const res1 = await getVideosList(servers[ 0 ].url)
454 const res2 = await getVideosListWithToken(servers[ 0 ].url, token)
456 for (const res of [ res1, res2 ]) {
457 const videos: Video[] = res.body.data
458 expect(videos).to.have.lengthOf(2)
460 const v1 = videos.find(v => v.name === 'video user 2')
461 const v2 = videos.find(v => v.name === 'video server 2')
463 expect(v1).to.be.undefined
464 expect(v2).to.be.undefined
469 it('Should hide its comments')
471 it('Should list blocked servers', async function () {
472 const res = await getServerBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
473 const blocks: ServerBlock[] = res.body.data
475 expect(res.body.total).to.equal(1)
477 const block = blocks[ 0 ]
478 expect(block.byAccount.displayName).to.equal('peertube')
479 expect(block.byAccount.name).to.equal('peertube')
480 expect(block.blockedServer.host).to.equal('localhost:9002')
483 it('Should unblock the remote server', async function () {
484 await removeServerFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
487 it('Should list all videos', async function () {
488 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
489 await checkAllVideos(servers[ 0 ].url, token)
493 it('Should list the comments', async function () {
494 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
495 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
502 killallServers(servers)