Add welcome modal
[oweals/peertube.git] / server / tests / api / users / blocklist.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
6 import {
7   cleanupTests,
8   createUser,
9   doubleFollow,
10   flushAndRunMultipleServers,
11   flushTests,
12   killallServers,
13   ServerInfo,
14   uploadVideo,
15   userLogin
16 } from '../../../../shared/extra-utils/index'
17 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
18 import { getVideosListWithToken, getVideosList } from '../../../../shared/extra-utils/videos/videos'
19 import {
20   addVideoCommentReply,
21   addVideoCommentThread,
22   getVideoCommentThreads,
23   getVideoThreadComments
24 } from '../../../../shared/extra-utils/videos/video-comments'
25 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
26 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
27 import {
28   addAccountToAccountBlocklist,
29   addAccountToServerBlocklist,
30   addServerToAccountBlocklist,
31   addServerToServerBlocklist,
32   getAccountBlocklistByAccount,
33   getAccountBlocklistByServer,
34   getServerBlocklistByAccount,
35   getServerBlocklistByServer,
36   removeAccountFromAccountBlocklist,
37   removeAccountFromServerBlocklist,
38   removeServerFromAccountBlocklist,
39   removeServerFromServerBlocklist
40 } from '../../../../shared/extra-utils/users/blocklist'
41
42 const expect = chai.expect
43
44 async function checkAllVideos (url: string, token: string) {
45   {
46     const res = await getVideosListWithToken(url, token)
47
48     expect(res.body.data).to.have.lengthOf(4)
49   }
50
51   {
52     const res = await getVideosList(url)
53
54     expect(res.body.data).to.have.lengthOf(4)
55   }
56 }
57
58 async function checkAllComments (url: string, token: string, videoUUID: string) {
59   const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
60
61   const threads: VideoComment[] = resThreads.body.data
62   expect(threads).to.have.lengthOf(2)
63
64   for (const thread of threads) {
65     const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
66
67     const tree: VideoCommentThreadTree = res.body
68     expect(tree.children).to.have.lengthOf(1)
69   }
70 }
71
72 describe('Test blocklist', function () {
73   let servers: ServerInfo[]
74   let videoUUID1: string
75   let videoUUID2: string
76   let userToken1: string
77   let userModeratorToken: string
78   let userToken2: string
79
80   before(async function () {
81     this.timeout(60000)
82
83     servers = await flushAndRunMultipleServers(2)
84     await setAccessTokensToServers(servers)
85
86     {
87       const user = { username: 'user1', password: 'password' }
88       await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
89
90       userToken1 = await userLogin(servers[0], user)
91       await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
92     }
93
94     {
95       const user = { username: 'moderator', password: 'password' }
96       await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
97
98       userModeratorToken = await userLogin(servers[0], user)
99     }
100
101     {
102       const user = { username: 'user2', password: 'password' }
103       await createUser({ url: servers[ 1 ].url, accessToken: servers[ 1 ].accessToken, username: user.username, password: user.password })
104
105       userToken2 = await userLogin(servers[1], user)
106       await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
107     }
108
109     {
110       const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
111       videoUUID1 = res.body.video.uuid
112     }
113
114     {
115       const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
116       videoUUID2 = res.body.video.uuid
117     }
118
119     await doubleFollow(servers[0], servers[1])
120
121     {
122       const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
123       const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
124       await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
125     }
126
127     {
128       const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
129       await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
130     }
131
132     await waitJobs(servers)
133   })
134
135   describe('User blocklist', function () {
136
137     describe('When managing account blocklist', function () {
138       it('Should list all videos', function () {
139         return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
140       })
141
142       it('Should list the comments', function () {
143         return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
144       })
145
146       it('Should block a remote account', async function () {
147         await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:' + servers[1].port)
148       })
149
150       it('Should hide its videos', async function () {
151         const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
152
153         const videos: Video[] = res.body.data
154         expect(videos).to.have.lengthOf(3)
155
156         const v = videos.find(v => v.name === 'video user 2')
157         expect(v).to.be.undefined
158       })
159
160       it('Should block a local account', async function () {
161         await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
162       })
163
164       it('Should hide its videos', async function () {
165         const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
166
167         const videos: Video[] = res.body.data
168         expect(videos).to.have.lengthOf(2)
169
170         const v = videos.find(v => v.name === 'video user 1')
171         expect(v).to.be.undefined
172       })
173
174       it('Should hide its comments', async function () {
175         const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', servers[ 0 ].accessToken)
176
177         const threads: VideoComment[] = resThreads.body.data
178         expect(threads).to.have.lengthOf(1)
179         expect(threads[ 0 ].totalReplies).to.equal(0)
180
181         const t = threads.find(t => t.text === 'comment user 1')
182         expect(t).to.be.undefined
183
184         for (const thread of threads) {
185           const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, servers[ 0 ].accessToken)
186
187           const tree: VideoCommentThreadTree = res.body
188           expect(tree.children).to.have.lengthOf(0)
189         }
190       })
191
192       it('Should list all the videos with another user', async function () {
193         return checkAllVideos(servers[ 0 ].url, userToken1)
194       })
195
196       it('Should list all the comments with another user', async function () {
197         return checkAllComments(servers[ 0 ].url, userToken1, videoUUID1)
198       })
199
200       it('Should list blocked accounts', async function () {
201         {
202           const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
203           const blocks: AccountBlock[] = res.body.data
204
205           expect(res.body.total).to.equal(2)
206
207           const block = blocks[ 0 ]
208           expect(block.byAccount.displayName).to.equal('root')
209           expect(block.byAccount.name).to.equal('root')
210           expect(block.blockedAccount.displayName).to.equal('user2')
211           expect(block.blockedAccount.name).to.equal('user2')
212           expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
213         }
214
215         {
216           const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
217           const blocks: AccountBlock[] = res.body.data
218
219           expect(res.body.total).to.equal(2)
220
221           const block = blocks[ 0 ]
222           expect(block.byAccount.displayName).to.equal('root')
223           expect(block.byAccount.name).to.equal('root')
224           expect(block.blockedAccount.displayName).to.equal('user1')
225           expect(block.blockedAccount.name).to.equal('user1')
226           expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
227         }
228       })
229
230       it('Should unblock the remote account', async function () {
231         await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:' + servers[1].port)
232       })
233
234       it('Should display its videos', async function () {
235         const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
236
237         const videos: Video[] = res.body.data
238         expect(videos).to.have.lengthOf(3)
239
240         const v = videos.find(v => v.name === 'video user 2')
241         expect(v).not.to.be.undefined
242       })
243
244       it('Should unblock the local account', async function () {
245         await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
246       })
247
248       it('Should display its comments', function () {
249         return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
250       })
251     })
252
253     describe('When managing server blocklist', function () {
254       it('Should list all videos', function () {
255         return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
256       })
257
258       it('Should list the comments', function () {
259         return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
260       })
261
262       it('Should block a remote server', async function () {
263         await addServerToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:' + servers[1].port)
264       })
265
266       it('Should hide its videos', async function () {
267         const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
268
269         const videos: Video[] = res.body.data
270         expect(videos).to.have.lengthOf(2)
271
272         const v1 = videos.find(v => v.name === 'video user 2')
273         const v2 = videos.find(v => v.name === 'video server 2')
274
275         expect(v1).to.be.undefined
276         expect(v2).to.be.undefined
277       })
278
279       it('Should list all the videos with another user', async function () {
280         return checkAllVideos(servers[ 0 ].url, userToken1)
281       })
282
283       it('Should hide its comments')
284
285       it('Should list blocked servers', async function () {
286         const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
287         const blocks: ServerBlock[] = res.body.data
288
289         expect(res.body.total).to.equal(1)
290
291         const block = blocks[ 0 ]
292         expect(block.byAccount.displayName).to.equal('root')
293         expect(block.byAccount.name).to.equal('root')
294         expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
295       })
296
297       it('Should unblock the remote server', async function () {
298         await removeServerFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:' + servers[1].port)
299       })
300
301       it('Should display its videos', function () {
302         return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
303       })
304
305       it('Should display its comments', function () {
306         return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
307       })
308     })
309   })
310
311   describe('Server blocklist', function () {
312
313     describe('When managing account blocklist', function () {
314       it('Should list all videos', async function () {
315         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
316           await checkAllVideos(servers[ 0 ].url, token)
317         }
318       })
319
320       it('Should list the comments', async function () {
321         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
322           await checkAllComments(servers[ 0 ].url, token, videoUUID1)
323         }
324       })
325
326       it('Should block a remote account', async function () {
327         await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:' + servers[1].port)
328       })
329
330       it('Should hide its videos', async function () {
331         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
332           const res = await getVideosListWithToken(servers[ 0 ].url, token)
333
334           const videos: Video[] = res.body.data
335           expect(videos).to.have.lengthOf(3)
336
337           const v = videos.find(v => v.name === 'video user 2')
338           expect(v).to.be.undefined
339         }
340       })
341
342       it('Should block a local account', async function () {
343         await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
344       })
345
346       it('Should hide its videos', async function () {
347         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
348           const res = await getVideosListWithToken(servers[ 0 ].url, token)
349
350           const videos: Video[] = res.body.data
351           expect(videos).to.have.lengthOf(2)
352
353           const v = videos.find(v => v.name === 'video user 1')
354           expect(v).to.be.undefined
355         }
356       })
357
358       it('Should hide its comments', async function () {
359         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
360           const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', token)
361
362           const threads: VideoComment[] = resThreads.body.data
363           expect(threads).to.have.lengthOf(1)
364           expect(threads[ 0 ].totalReplies).to.equal(0)
365
366           const t = threads.find(t => t.text === 'comment user 1')
367           expect(t).to.be.undefined
368
369           for (const thread of threads) {
370             const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, token)
371
372             const tree: VideoCommentThreadTree = res.body
373             expect(tree.children).to.have.lengthOf(0)
374           }
375         }
376       })
377
378       it('Should list blocked accounts', async function () {
379         {
380           const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
381           const blocks: AccountBlock[] = res.body.data
382
383           expect(res.body.total).to.equal(2)
384
385           const block = blocks[ 0 ]
386           expect(block.byAccount.displayName).to.equal('peertube')
387           expect(block.byAccount.name).to.equal('peertube')
388           expect(block.blockedAccount.displayName).to.equal('user2')
389           expect(block.blockedAccount.name).to.equal('user2')
390           expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
391         }
392
393         {
394           const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
395           const blocks: AccountBlock[] = res.body.data
396
397           expect(res.body.total).to.equal(2)
398
399           const block = blocks[ 0 ]
400           expect(block.byAccount.displayName).to.equal('peertube')
401           expect(block.byAccount.name).to.equal('peertube')
402           expect(block.blockedAccount.displayName).to.equal('user1')
403           expect(block.blockedAccount.name).to.equal('user1')
404           expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
405         }
406       })
407
408       it('Should unblock the remote account', async function () {
409         await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:' + servers[1].port)
410       })
411
412       it('Should display its videos', async function () {
413         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
414           const res = await getVideosListWithToken(servers[ 0 ].url, token)
415
416           const videos: Video[] = res.body.data
417           expect(videos).to.have.lengthOf(3)
418
419           const v = videos.find(v => v.name === 'video user 2')
420           expect(v).not.to.be.undefined
421         }
422       })
423
424       it('Should unblock the local account', async function () {
425         await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
426       })
427
428       it('Should display its comments', async function () {
429         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
430           await checkAllComments(servers[ 0 ].url, token, videoUUID1)
431         }
432       })
433     })
434
435     describe('When managing server blocklist', function () {
436       it('Should list all videos', async function () {
437         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
438           await checkAllVideos(servers[ 0 ].url, token)
439         }
440       })
441
442       it('Should list the comments', async function () {
443         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
444           await checkAllComments(servers[ 0 ].url, token, videoUUID1)
445         }
446       })
447
448       it('Should block a remote server', async function () {
449         await addServerToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:' + servers[1].port)
450       })
451
452       it('Should hide its videos', async function () {
453         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
454           const res1 = await getVideosList(servers[ 0 ].url)
455           const res2 = await getVideosListWithToken(servers[ 0 ].url, token)
456
457           for (const res of [ res1, res2 ]) {
458             const videos: Video[] = res.body.data
459             expect(videos).to.have.lengthOf(2)
460
461             const v1 = videos.find(v => v.name === 'video user 2')
462             const v2 = videos.find(v => v.name === 'video server 2')
463
464             expect(v1).to.be.undefined
465             expect(v2).to.be.undefined
466           }
467         }
468       })
469
470       it('Should hide its comments')
471
472       it('Should list blocked servers', async function () {
473         const res = await getServerBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
474         const blocks: ServerBlock[] = res.body.data
475
476         expect(res.body.total).to.equal(1)
477
478         const block = blocks[ 0 ]
479         expect(block.byAccount.displayName).to.equal('peertube')
480         expect(block.byAccount.name).to.equal('peertube')
481         expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
482       })
483
484       it('Should unblock the remote server', async function () {
485         await removeServerFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:' + servers[1].port)
486       })
487
488       it('Should list all videos', async function () {
489         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
490           await checkAllVideos(servers[ 0 ].url, token)
491         }
492       })
493
494       it('Should list the comments', async function () {
495         for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
496           await checkAllComments(servers[ 0 ].url, token, videoUUID1)
497         }
498       })
499     })
500   })
501
502   after(async function () {
503     await cleanupTests(servers)
504   })
505 })