1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
9 flushAndRunMultipleServers,
15 } from '../../utils/index'
16 import { setAccessTokensToServers } from '../../utils/users/login'
17 import { getVideosListWithToken } from '../../utils/videos/videos'
20 addVideoCommentThread,
21 getVideoCommentThreads,
22 getVideoThreadComments
23 } from '../../utils/videos/video-comments'
24 import { waitJobs } from '../../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 '../../utils/users/blocklist'
41 const expect = chai.expect
43 async function checkAllVideos (url: string, token: string) {
44 const res = await getVideosListWithToken(url, token)
46 expect(res.body.data).to.have.lengthOf(4)
49 async function checkAllComments (url: string, token: string, videoUUID: string) {
50 const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
52 const threads: VideoComment[] = resThreads.body.data
53 expect(threads).to.have.lengthOf(2)
55 for (const thread of threads) {
56 const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
58 const tree: VideoCommentThreadTree = res.body
59 expect(tree.children).to.have.lengthOf(1)
63 describe('Test blocklist', function () {
64 let servers: ServerInfo[]
65 let videoUUID1: string
66 let videoUUID2: string
67 let userToken1: string
68 let userModeratorToken: string
69 let userToken2: string
71 before(async function () {
76 servers = await flushAndRunMultipleServers(2)
77 await setAccessTokensToServers(servers)
80 const user = { username: 'user1', password: 'password' }
81 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
83 userToken1 = await userLogin(servers[0], user)
84 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
88 const user = { username: 'moderator', password: 'password' }
89 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
91 userModeratorToken = await userLogin(servers[0], user)
95 const user = { username: 'user2', password: 'password' }
96 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
98 userToken2 = await userLogin(servers[1], user)
99 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
103 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
104 videoUUID1 = res.body.video.uuid
108 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
109 videoUUID2 = res.body.video.uuid
112 await doubleFollow(servers[0], servers[1])
115 const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
116 const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
117 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
121 const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
122 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
125 await waitJobs(servers)
128 describe('User blocklist', function () {
130 describe('When managing account blocklist', function () {
131 it('Should list all videos', function () {
132 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
135 it('Should list the comments', function () {
136 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
139 it('Should block a remote account', async function () {
140 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
143 it('Should hide its videos', async function () {
144 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
146 const videos: Video[] = res.body.data
147 expect(videos).to.have.lengthOf(3)
149 const v = videos.find(v => v.name === 'video user 2')
150 expect(v).to.be.undefined
153 it('Should block a local account', async function () {
154 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
157 it('Should hide its videos', async function () {
158 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
160 const videos: Video[] = res.body.data
161 expect(videos).to.have.lengthOf(2)
163 const v = videos.find(v => v.name === 'video user 1')
164 expect(v).to.be.undefined
167 it('Should hide its comments', async function () {
168 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', servers[ 0 ].accessToken)
170 const threads: VideoComment[] = resThreads.body.data
171 expect(threads).to.have.lengthOf(1)
172 expect(threads[ 0 ].totalReplies).to.equal(0)
174 const t = threads.find(t => t.text === 'comment user 1')
175 expect(t).to.be.undefined
177 for (const thread of threads) {
178 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, servers[ 0 ].accessToken)
180 const tree: VideoCommentThreadTree = res.body
181 expect(tree.children).to.have.lengthOf(0)
185 it('Should list all the videos with another user', async function () {
186 return checkAllVideos(servers[ 0 ].url, userToken1)
189 it('Should list all the comments with another user', async function () {
190 return checkAllComments(servers[ 0 ].url, userToken1, videoUUID1)
193 it('Should list blocked accounts', async function () {
195 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
196 const blocks: AccountBlock[] = res.body.data
198 expect(res.body.total).to.equal(2)
200 const block = blocks[ 0 ]
201 expect(block.byAccount.displayName).to.equal('root')
202 expect(block.byAccount.name).to.equal('root')
203 expect(block.blockedAccount.displayName).to.equal('user2')
204 expect(block.blockedAccount.name).to.equal('user2')
205 expect(block.blockedAccount.host).to.equal('localhost:9002')
209 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
210 const blocks: AccountBlock[] = res.body.data
212 expect(res.body.total).to.equal(2)
214 const block = blocks[ 0 ]
215 expect(block.byAccount.displayName).to.equal('root')
216 expect(block.byAccount.name).to.equal('root')
217 expect(block.blockedAccount.displayName).to.equal('user1')
218 expect(block.blockedAccount.name).to.equal('user1')
219 expect(block.blockedAccount.host).to.equal('localhost:9001')
223 it('Should unblock the remote account', async function () {
224 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
227 it('Should display its videos', async function () {
228 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
230 const videos: Video[] = res.body.data
231 expect(videos).to.have.lengthOf(3)
233 const v = videos.find(v => v.name === 'video user 2')
234 expect(v).not.to.be.undefined
237 it('Should unblock the local account', async function () {
238 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
241 it('Should display its comments', function () {
242 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
246 describe('When managing server blocklist', function () {
247 it('Should list all videos', function () {
248 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
251 it('Should list the comments', function () {
252 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
255 it('Should block a remote server', async function () {
256 await addServerToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
259 it('Should hide its videos', async function () {
260 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
262 const videos: Video[] = res.body.data
263 expect(videos).to.have.lengthOf(2)
265 const v1 = videos.find(v => v.name === 'video user 2')
266 const v2 = videos.find(v => v.name === 'video server 2')
268 expect(v1).to.be.undefined
269 expect(v2).to.be.undefined
272 it('Should list all the videos with another user', async function () {
273 return checkAllVideos(servers[ 0 ].url, userToken1)
276 it('Should hide its comments')
278 it('Should list blocked servers', async function () {
279 const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
280 const blocks: ServerBlock[] = res.body.data
282 expect(res.body.total).to.equal(1)
284 const block = blocks[ 0 ]
285 expect(block.byAccount.displayName).to.equal('root')
286 expect(block.byAccount.name).to.equal('root')
287 expect(block.blockedServer.host).to.equal('localhost:9002')
290 it('Should unblock the remote server', async function () {
291 await removeServerFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
294 it('Should display its videos', function () {
295 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
298 it('Should display its comments', function () {
299 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
304 describe('Server blocklist', function () {
306 describe('When managing account blocklist', function () {
307 it('Should list all videos', async function () {
308 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
309 await checkAllVideos(servers[ 0 ].url, token)
313 it('Should list the comments', async function () {
314 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
315 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
319 it('Should block a remote account', async function () {
320 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
323 it('Should hide its videos', async function () {
324 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
325 const res = await getVideosListWithToken(servers[ 0 ].url, token)
327 const videos: Video[] = res.body.data
328 expect(videos).to.have.lengthOf(3)
330 const v = videos.find(v => v.name === 'video user 2')
331 expect(v).to.be.undefined
335 it('Should block a local account', async function () {
336 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
339 it('Should hide its videos', async function () {
340 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
341 const res = await getVideosListWithToken(servers[ 0 ].url, token)
343 const videos: Video[] = res.body.data
344 expect(videos).to.have.lengthOf(2)
346 const v = videos.find(v => v.name === 'video user 1')
347 expect(v).to.be.undefined
351 it('Should hide its comments', async function () {
352 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
353 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', token)
355 const threads: VideoComment[] = resThreads.body.data
356 expect(threads).to.have.lengthOf(1)
357 expect(threads[ 0 ].totalReplies).to.equal(0)
359 const t = threads.find(t => t.text === 'comment user 1')
360 expect(t).to.be.undefined
362 for (const thread of threads) {
363 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, token)
365 const tree: VideoCommentThreadTree = res.body
366 expect(tree.children).to.have.lengthOf(0)
371 it('Should list blocked accounts', async function () {
373 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
374 const blocks: AccountBlock[] = res.body.data
376 expect(res.body.total).to.equal(2)
378 const block = blocks[ 0 ]
379 expect(block.byAccount.displayName).to.equal('peertube')
380 expect(block.byAccount.name).to.equal('peertube')
381 expect(block.blockedAccount.displayName).to.equal('user2')
382 expect(block.blockedAccount.name).to.equal('user2')
383 expect(block.blockedAccount.host).to.equal('localhost:9002')
387 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
388 const blocks: AccountBlock[] = res.body.data
390 expect(res.body.total).to.equal(2)
392 const block = blocks[ 0 ]
393 expect(block.byAccount.displayName).to.equal('peertube')
394 expect(block.byAccount.name).to.equal('peertube')
395 expect(block.blockedAccount.displayName).to.equal('user1')
396 expect(block.blockedAccount.name).to.equal('user1')
397 expect(block.blockedAccount.host).to.equal('localhost:9001')
401 it('Should unblock the remote account', async function () {
402 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
405 it('Should display its videos', async function () {
406 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
407 const res = await getVideosListWithToken(servers[ 0 ].url, token)
409 const videos: Video[] = res.body.data
410 expect(videos).to.have.lengthOf(3)
412 const v = videos.find(v => v.name === 'video user 2')
413 expect(v).not.to.be.undefined
417 it('Should unblock the local account', async function () {
418 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
421 it('Should display its comments', async function () {
422 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
423 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
428 describe('When managing server blocklist', function () {
429 it('Should list all videos', async function () {
430 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
431 await checkAllVideos(servers[ 0 ].url, token)
435 it('Should list the comments', async function () {
436 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
437 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
441 it('Should block a remote server', async function () {
442 await addServerToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
445 it('Should hide its videos', async function () {
446 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
447 const res = await getVideosListWithToken(servers[ 0 ].url, token)
449 const videos: Video[] = res.body.data
450 expect(videos).to.have.lengthOf(2)
452 const v1 = videos.find(v => v.name === 'video user 2')
453 const v2 = videos.find(v => v.name === 'video server 2')
455 expect(v1).to.be.undefined
456 expect(v2).to.be.undefined
460 it('Should hide its comments')
462 it('Should list blocked servers', async function () {
463 const res = await getServerBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
464 const blocks: ServerBlock[] = res.body.data
466 expect(res.body.total).to.equal(1)
468 const block = blocks[ 0 ]
469 expect(block.byAccount.displayName).to.equal('peertube')
470 expect(block.byAccount.name).to.equal('peertube')
471 expect(block.blockedServer.host).to.equal('localhost:9002')
474 it('Should unblock the remote server', async function () {
475 await removeServerFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
478 it('Should list all videos', async function () {
479 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
480 await checkAllVideos(servers[ 0 ].url, token)
484 it('Should list the comments', async function () {
485 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
486 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
492 after(async function () {
493 killallServers(servers)
495 // Keep the logs if the test failed