]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/moderation/blocklist.ts
chore(refactor): remove shared folder dependencies to the server
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / blocklist.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
b44164bb 2
b44164bb 3import 'mocha'
2b02c520 4import * as chai from 'chai'
b44164bb 5import {
5f8bd4cb 6 BlocklistCommand,
7c3b7976 7 cleanupTests,
12edc149 8 CommentsCommand,
254d3579 9 createMultipleServers,
4c7e60bc 10 doubleFollow,
254d3579 11 PeerTubeServer,
2b02c520 12 setAccessTokensToServers,
2b02c520
C
13 waitJobs
14} from '@shared/extra-utils'
d23dd9fb 15import { UserNotificationType } from '@shared/models'
b44164bb
C
16
17const expect = chai.expect
18
254d3579 19async function checkAllVideos (server: PeerTubeServer, token: string) {
65b21c96 20 {
89d241a7 21 const { data } = await server.videos.listWithToken({ token })
d23dd9fb 22 expect(data).to.have.lengthOf(5)
65b21c96
C
23 }
24
25 {
89d241a7 26 const { data } = await server.videos.list()
d23dd9fb 27 expect(data).to.have.lengthOf(5)
65b21c96 28 }
b44164bb
C
29}
30
254d3579 31async function checkAllComments (server: PeerTubeServer, token: string, videoUUID: string) {
89d241a7 32 const { data } = await server.comments.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token })
b44164bb 33
12edc149 34 const threads = data.filter(t => t.isDeleted === false)
b44164bb
C
35 expect(threads).to.have.lengthOf(2)
36
37 for (const thread of threads) {
89d241a7 38 const tree = await server.comments.getThread({ videoId: videoUUID, threadId: thread.id, token })
b44164bb
C
39 expect(tree.children).to.have.lengthOf(1)
40 }
41}
42
dddc8b1f 43async function checkCommentNotification (
254d3579
C
44 mainServer: PeerTubeServer,
45 comment: { server: PeerTubeServer, token: string, videoUUID: string, text: string },
dddc8b1f
C
46 check: 'presence' | 'absence'
47) {
89d241a7 48 const command = comment.server.comments
12edc149
C
49
50 const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text })
dddc8b1f 51
a1587156 52 await waitJobs([ mainServer, comment.server ])
dddc8b1f 53
89d241a7 54 const { data } = await mainServer.notifications.list({ start: 0, count: 30 })
dd0ebb71 55 const commentNotifications = data.filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
dddc8b1f
C
56
57 if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
58 else expect(commentNotifications).to.have.lengthOf(0)
59
12edc149 60 await command.delete({ token: comment.token, videoId: comment.videoUUID, commentId: threadId })
dddc8b1f 61
a1587156 62 await waitJobs([ mainServer, comment.server ])
dddc8b1f
C
63}
64
b44164bb 65describe('Test blocklist', function () {
254d3579 66 let servers: PeerTubeServer[]
b44164bb
C
67 let videoUUID1: string
68 let videoUUID2: string
696d83fd 69 let videoUUID3: string
b44164bb
C
70 let userToken1: string
71 let userModeratorToken: string
72 let userToken2: string
73
5f8bd4cb 74 let command: BlocklistCommand
12edc149 75 let commentsCommand: CommentsCommand[]
5f8bd4cb 76
b44164bb 77 before(async function () {
61fd9834 78 this.timeout(120000)
b44164bb 79
254d3579 80 servers = await createMultipleServers(3)
b44164bb
C
81 await setAccessTokensToServers(servers)
82
89d241a7
C
83 command = servers[0].blocklist
84 commentsCommand = servers.map(s => s.comments)
12edc149 85
b44164bb
C
86 {
87 const user = { username: 'user1', password: 'password' }
89d241a7 88 await servers[0].users.create({ username: user.username, password: user.password })
b44164bb 89
89d241a7
C
90 userToken1 = await servers[0].login.getAccessToken(user)
91 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
b44164bb
C
92 }
93
94 {
95 const user = { username: 'moderator', password: 'password' }
89d241a7 96 await servers[0].users.create({ username: user.username, password: user.password })
b44164bb 97
89d241a7 98 userModeratorToken = await servers[0].login.getAccessToken(user)
b44164bb
C
99 }
100
101 {
102 const user = { username: 'user2', password: 'password' }
89d241a7 103 await servers[1].users.create({ username: user.username, password: user.password })
b44164bb 104
89d241a7
C
105 userToken2 = await servers[1].login.getAccessToken(user)
106 await servers[1].videos.upload({ token: userToken2, attributes: { name: 'video user 2' } })
b44164bb
C
107 }
108
109 {
89d241a7 110 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
d23dd9fb 111 videoUUID1 = uuid
b44164bb
C
112 }
113
114 {
89d241a7 115 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
d23dd9fb 116 videoUUID2 = uuid
b44164bb
C
117 }
118
696d83fd 119 {
89d241a7 120 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 2 server 1' } })
d23dd9fb 121 videoUUID3 = uuid
696d83fd
C
122 }
123
b44164bb 124 await doubleFollow(servers[0], servers[1])
696d83fd 125 await doubleFollow(servers[0], servers[2])
b44164bb
C
126
127 {
12edc149
C
128 const created = await commentsCommand[0].createThread({ videoId: videoUUID1, text: 'comment root 1' })
129 const reply = await commentsCommand[0].addReply({
130 token: userToken1,
131 videoId: videoUUID1,
132 toCommentId: created.id,
133 text: 'comment user 1'
134 })
135 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: reply.id, text: 'comment root 1' })
b44164bb
C
136 }
137
138 {
12edc149
C
139 const created = await commentsCommand[0].createThread({ token: userToken1, videoId: videoUUID1, text: 'comment user 1' })
140 await commentsCommand[0].addReply({ videoId: videoUUID1, toCommentId: created.id, text: 'comment root 1' })
b44164bb
C
141 }
142
143 await waitJobs(servers)
144 })
145
146 describe('User blocklist', function () {
147
148 describe('When managing account blocklist', function () {
149 it('Should list all videos', function () {
12edc149 150 return checkAllVideos(servers[0], servers[0].accessToken)
b44164bb
C
151 })
152
153 it('Should list the comments', function () {
12edc149 154 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
b44164bb
C
155 })
156
157 it('Should block a remote account', async function () {
5f8bd4cb 158 await command.addToMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
b44164bb
C
159 })
160
161 it('Should hide its videos', async function () {
89d241a7 162 const { data } = await servers[0].videos.listWithToken()
b44164bb 163
d23dd9fb 164 expect(data).to.have.lengthOf(4)
b44164bb 165
d23dd9fb 166 const v = data.find(v => v.name === 'video user 2')
b44164bb
C
167 expect(v).to.be.undefined
168 })
169
170 it('Should block a local account', async function () {
5f8bd4cb 171 await command.addToMyBlocklist({ account: 'user1' })
b44164bb
C
172 })
173
174 it('Should hide its videos', async function () {
89d241a7 175 const { data } = await servers[0].videos.listWithToken()
b44164bb 176
d23dd9fb 177 expect(data).to.have.lengthOf(3)
b44164bb 178
d23dd9fb 179 const v = data.find(v => v.name === 'video user 1')
b44164bb
C
180 expect(v).to.be.undefined
181 })
182
183 it('Should hide its comments', async function () {
12edc149
C
184 const { data } = await commentsCommand[0].listThreads({
185 token: servers[0].accessToken,
186 videoId: videoUUID1,
187 start: 0,
188 count: 25,
189 sort: '-createdAt'
190 })
191
192 expect(data).to.have.lengthOf(1)
193 expect(data[0].totalReplies).to.equal(1)
194
195 const t = data.find(t => t.text === 'comment user 1')
b44164bb
C
196 expect(t).to.be.undefined
197
12edc149
C
198 for (const thread of data) {
199 const tree = await commentsCommand[0].getThread({
200 videoId: videoUUID1,
201 threadId: thread.id,
202 token: servers[0].accessToken
203 })
b44164bb
C
204 expect(tree.children).to.have.lengthOf(0)
205 }
206 })
207
dddc8b1f
C
208 it('Should not have notifications from blocked accounts', async function () {
209 this.timeout(20000)
210
211 {
a1587156
C
212 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
213 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f
C
214 }
215
216 {
217 const comment = {
a1587156 218 server: servers[0],
dddc8b1f
C
219 token: userToken1,
220 videoUUID: videoUUID2,
a1587156 221 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 222 }
a1587156 223 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f
C
224 }
225 })
226
b44164bb 227 it('Should list all the videos with another user', async function () {
12edc149 228 return checkAllVideos(servers[0], userToken1)
b44164bb
C
229 })
230
b44164bb
C
231 it('Should list blocked accounts', async function () {
232 {
5f8bd4cb
C
233 const body = await command.listMyAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
234 expect(body.total).to.equal(2)
b44164bb 235
5f8bd4cb 236 const block = body.data[0]
b44164bb
C
237 expect(block.byAccount.displayName).to.equal('root')
238 expect(block.byAccount.name).to.equal('root')
239 expect(block.blockedAccount.displayName).to.equal('user2')
240 expect(block.blockedAccount.name).to.equal('user2')
7243f84d 241 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
b44164bb
C
242 }
243
244 {
5f8bd4cb
C
245 const body = await command.listMyAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
246 expect(body.total).to.equal(2)
b44164bb 247
5f8bd4cb 248 const block = body.data[0]
b44164bb
C
249 expect(block.byAccount.displayName).to.equal('root')
250 expect(block.byAccount.name).to.equal('root')
251 expect(block.blockedAccount.displayName).to.equal('user1')
252 expect(block.blockedAccount.name).to.equal('user1')
7243f84d 253 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
b44164bb
C
254 }
255 })
256
80badf49
C
257 it('Should get blocked status', async function () {
258 const remoteHandle = 'user2@' + servers[1].host
259 const localHandle = 'user1@' + servers[0].host
260 const unknownHandle = 'user5@' + servers[0].host
261
262 {
263 const status = await command.getStatus({ accounts: [ remoteHandle ] })
264 expect(Object.keys(status.accounts)).to.have.lengthOf(1)
265 expect(status.accounts[remoteHandle].blockedByUser).to.be.false
266 expect(status.accounts[remoteHandle].blockedByServer).to.be.false
267
268 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
269 }
270
271 {
272 const status = await command.getStatus({ token: servers[0].accessToken, accounts: [ remoteHandle ] })
273 expect(Object.keys(status.accounts)).to.have.lengthOf(1)
274 expect(status.accounts[remoteHandle].blockedByUser).to.be.true
275 expect(status.accounts[remoteHandle].blockedByServer).to.be.false
276
277 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
278 }
279
280 {
281 const status = await command.getStatus({ token: servers[0].accessToken, accounts: [ localHandle, remoteHandle, unknownHandle ] })
282 expect(Object.keys(status.accounts)).to.have.lengthOf(3)
283
284 for (const handle of [ localHandle, remoteHandle ]) {
285 expect(status.accounts[handle].blockedByUser).to.be.true
286 expect(status.accounts[handle].blockedByServer).to.be.false
287 }
288
289 expect(status.accounts[unknownHandle].blockedByUser).to.be.false
290 expect(status.accounts[unknownHandle].blockedByServer).to.be.false
291
292 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
293 }
294 })
295
696d83fd
C
296 it('Should not allow a remote blocked user to comment my videos', async function () {
297 this.timeout(60000)
298
299 {
12edc149 300 await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID3, text: 'comment user 2' })
696d83fd
C
301 await waitJobs(servers)
302
12edc149 303 await commentsCommand[0].createThread({ token: servers[0].accessToken, videoId: videoUUID3, text: 'uploader' })
696d83fd
C
304 await waitJobs(servers)
305
12edc149 306 const commentId = await commentsCommand[1].findCommentId({ videoId: videoUUID3, text: 'uploader' })
696d83fd 307 const message = 'reply by user 2'
12edc149
C
308 const reply = await commentsCommand[1].addReply({ token: userToken2, videoId: videoUUID3, toCommentId: commentId, text: message })
309 await commentsCommand[1].addReply({ videoId: videoUUID3, toCommentId: reply.id, text: 'another reply' })
696d83fd
C
310
311 await waitJobs(servers)
312 }
313
314 // Server 2 has all the comments
315 {
12edc149 316 const { data } = await commentsCommand[1].listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
696d83fd 317
12edc149
C
318 expect(data).to.have.lengthOf(2)
319 expect(data[0].text).to.equal('uploader')
320 expect(data[1].text).to.equal('comment user 2')
696d83fd 321
12edc149 322 const tree = await commentsCommand[1].getThread({ videoId: videoUUID3, threadId: data[0].id })
696d83fd
C
323 expect(tree.children).to.have.lengthOf(1)
324 expect(tree.children[0].comment.text).to.equal('reply by user 2')
325 expect(tree.children[0].children).to.have.lengthOf(1)
326 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
327 }
328
329 // Server 1 and 3 should only have uploader comments
330 for (const server of [ servers[0], servers[2] ]) {
89d241a7 331 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
696d83fd 332
12edc149
C
333 expect(data).to.have.lengthOf(1)
334 expect(data[0].text).to.equal('uploader')
696d83fd 335
89d241a7 336 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
696d83fd 337
12edc149
C
338 if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0)
339 else expect(tree.children).to.have.lengthOf(1)
696d83fd
C
340 }
341 })
342
b44164bb 343 it('Should unblock the remote account', async function () {
5f8bd4cb 344 await command.removeFromMyBlocklist({ account: 'user2@localhost:' + servers[1].port })
b44164bb
C
345 })
346
347 it('Should display its videos', async function () {
89d241a7 348 const { data } = await servers[0].videos.listWithToken()
d23dd9fb 349 expect(data).to.have.lengthOf(4)
b44164bb 350
d23dd9fb 351 const v = data.find(v => v.name === 'video user 2')
b44164bb
C
352 expect(v).not.to.be.undefined
353 })
354
696d83fd
C
355 it('Should display its comments on my video', async function () {
356 for (const server of servers) {
89d241a7 357 const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' })
696d83fd
C
358
359 // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
360 if (server.serverNumber === 3) {
12edc149 361 expect(data).to.have.lengthOf(1)
696d83fd
C
362 continue
363 }
364
12edc149
C
365 expect(data).to.have.lengthOf(2)
366 expect(data[0].text).to.equal('uploader')
367 expect(data[1].text).to.equal('comment user 2')
696d83fd 368
89d241a7 369 const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id })
696d83fd
C
370 expect(tree.children).to.have.lengthOf(1)
371 expect(tree.children[0].comment.text).to.equal('reply by user 2')
372 expect(tree.children[0].children).to.have.lengthOf(1)
373 expect(tree.children[0].children[0].comment.text).to.equal('another reply')
374 }
375 })
376
b44164bb 377 it('Should unblock the local account', async function () {
5f8bd4cb 378 await command.removeFromMyBlocklist({ account: 'user1' })
b44164bb
C
379 })
380
381 it('Should display its comments', function () {
12edc149 382 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
b44164bb 383 })
dddc8b1f
C
384
385 it('Should have a notification from a non blocked account', async function () {
386 this.timeout(20000)
387
388 {
a1587156
C
389 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
390 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f
C
391 }
392
393 {
394 const comment = {
a1587156 395 server: servers[0],
dddc8b1f
C
396 token: userToken1,
397 videoUUID: videoUUID2,
a1587156 398 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 399 }
a1587156 400 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f
C
401 }
402 })
b44164bb
C
403 })
404
405 describe('When managing server blocklist', function () {
5f8bd4cb 406
b44164bb 407 it('Should list all videos', function () {
12edc149 408 return checkAllVideos(servers[0], servers[0].accessToken)
b44164bb
C
409 })
410
411 it('Should list the comments', function () {
12edc149 412 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
b44164bb
C
413 })
414
415 it('Should block a remote server', async function () {
5f8bd4cb 416 await command.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
b44164bb
C
417 })
418
419 it('Should hide its videos', async function () {
89d241a7 420 const { data } = await servers[0].videos.listWithToken()
b44164bb 421
d23dd9fb 422 expect(data).to.have.lengthOf(3)
b44164bb 423
d23dd9fb
C
424 const v1 = data.find(v => v.name === 'video user 2')
425 const v2 = data.find(v => v.name === 'video server 2')
b44164bb
C
426
427 expect(v1).to.be.undefined
428 expect(v2).to.be.undefined
429 })
430
431 it('Should list all the videos with another user', async function () {
12edc149 432 return checkAllVideos(servers[0], userToken1)
b44164bb
C
433 })
434
dddc8b1f
C
435 it('Should hide its comments', async function () {
436 this.timeout(10000)
437
12edc149 438 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
dddc8b1f
C
439
440 await waitJobs(servers)
441
12edc149 442 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
dddc8b1f 443
12edc149 444 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
dddc8b1f
C
445 })
446
447 it('Should not have notifications from blocked server', async function () {
448 this.timeout(20000)
449
450 {
a1587156
C
451 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
452 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f
C
453 }
454
455 {
456 const comment = {
a1587156 457 server: servers[1],
dddc8b1f
C
458 token: userToken2,
459 videoUUID: videoUUID1,
a1587156 460 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 461 }
a1587156 462 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f
C
463 }
464 })
b44164bb
C
465
466 it('Should list blocked servers', async function () {
5f8bd4cb
C
467 const body = await command.listMyServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
468 expect(body.total).to.equal(1)
b44164bb 469
5f8bd4cb 470 const block = body.data[0]
b44164bb
C
471 expect(block.byAccount.displayName).to.equal('root')
472 expect(block.byAccount.name).to.equal('root')
7243f84d 473 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
b44164bb
C
474 })
475
80badf49
C
476 it('Should get blocklist status', async function () {
477 const blockedServer = servers[1].host
478 const notBlockedServer = 'example.com'
479
480 {
481 const status = await command.getStatus({ hosts: [ blockedServer, notBlockedServer ] })
482 expect(Object.keys(status.accounts)).to.have.lengthOf(0)
483
484 expect(Object.keys(status.hosts)).to.have.lengthOf(2)
485 expect(status.hosts[blockedServer].blockedByUser).to.be.false
486 expect(status.hosts[blockedServer].blockedByServer).to.be.false
487
488 expect(status.hosts[notBlockedServer].blockedByUser).to.be.false
489 expect(status.hosts[notBlockedServer].blockedByServer).to.be.false
490 }
491
492 {
493 const status = await command.getStatus({ token: servers[0].accessToken, hosts: [ blockedServer, notBlockedServer ] })
494 expect(Object.keys(status.accounts)).to.have.lengthOf(0)
495
496 expect(Object.keys(status.hosts)).to.have.lengthOf(2)
497 expect(status.hosts[blockedServer].blockedByUser).to.be.true
498 expect(status.hosts[blockedServer].blockedByServer).to.be.false
499
500 expect(status.hosts[notBlockedServer].blockedByUser).to.be.false
501 expect(status.hosts[notBlockedServer].blockedByServer).to.be.false
502 }
503 })
504
b44164bb 505 it('Should unblock the remote server', async function () {
5f8bd4cb 506 await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port })
b44164bb
C
507 })
508
509 it('Should display its videos', function () {
12edc149 510 return checkAllVideos(servers[0], servers[0].accessToken)
b44164bb
C
511 })
512
513 it('Should display its comments', function () {
12edc149 514 return checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
b44164bb 515 })
dddc8b1f
C
516
517 it('Should have notification from unblocked server', async function () {
518 this.timeout(20000)
519
520 {
a1587156
C
521 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
522 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f
C
523 }
524
525 {
526 const comment = {
a1587156 527 server: servers[1],
dddc8b1f
C
528 token: userToken2,
529 videoUUID: videoUUID1,
a1587156 530 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 531 }
a1587156 532 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f
C
533 }
534 })
b44164bb
C
535 })
536 })
537
538 describe('Server blocklist', function () {
539
540 describe('When managing account blocklist', function () {
541 it('Should list all videos', async function () {
a1587156 542 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149 543 await checkAllVideos(servers[0], token)
b44164bb
C
544 }
545 })
546
547 it('Should list the comments', async function () {
a1587156 548 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149 549 await checkAllComments(servers[0], token, videoUUID1)
b44164bb
C
550 }
551 })
552
553 it('Should block a remote account', async function () {
5f8bd4cb 554 await command.addToServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
b44164bb
C
555 })
556
557 it('Should hide its videos', async function () {
a1587156 558 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
89d241a7 559 const { data } = await servers[0].videos.listWithToken({ token })
b44164bb 560
d23dd9fb 561 expect(data).to.have.lengthOf(4)
b44164bb 562
d23dd9fb 563 const v = data.find(v => v.name === 'video user 2')
b44164bb
C
564 expect(v).to.be.undefined
565 }
566 })
567
568 it('Should block a local account', async function () {
5f8bd4cb 569 await command.addToServerBlocklist({ account: 'user1' })
b44164bb
C
570 })
571
572 it('Should hide its videos', async function () {
a1587156 573 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
89d241a7 574 const { data } = await servers[0].videos.listWithToken({ token })
b44164bb 575
d23dd9fb 576 expect(data).to.have.lengthOf(3)
b44164bb 577
d23dd9fb 578 const v = data.find(v => v.name === 'video user 1')
b44164bb
C
579 expect(v).to.be.undefined
580 }
581 })
582
583 it('Should hide its comments', async function () {
a1587156 584 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149
C
585 const { data } = await commentsCommand[0].listThreads({ videoId: videoUUID1, count: 20, sort: '-createdAt', token })
586 const threads = data.filter(t => t.isDeleted === false)
b44164bb 587
b44164bb 588 expect(threads).to.have.lengthOf(1)
9de33c6b 589 expect(threads[0].totalReplies).to.equal(1)
b44164bb
C
590
591 const t = threads.find(t => t.text === 'comment user 1')
592 expect(t).to.be.undefined
593
594 for (const thread of threads) {
12edc149 595 const tree = await commentsCommand[0].getThread({ videoId: videoUUID1, threadId: thread.id, token })
b44164bb
C
596 expect(tree.children).to.have.lengthOf(0)
597 }
598 }
599 })
600
dddc8b1f
C
601 it('Should not have notification from blocked accounts by instance', async function () {
602 this.timeout(20000)
603
604 {
a1587156
C
605 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
606 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f
C
607 }
608
609 {
610 const comment = {
a1587156 611 server: servers[1],
dddc8b1f
C
612 token: userToken2,
613 videoUUID: videoUUID1,
a1587156 614 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 615 }
a1587156 616 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f
C
617 }
618 })
619
b44164bb
C
620 it('Should list blocked accounts', async function () {
621 {
5f8bd4cb
C
622 const body = await command.listServerAccountBlocklist({ start: 0, count: 1, sort: 'createdAt' })
623 expect(body.total).to.equal(2)
b44164bb 624
5f8bd4cb 625 const block = body.data[0]
b44164bb
C
626 expect(block.byAccount.displayName).to.equal('peertube')
627 expect(block.byAccount.name).to.equal('peertube')
628 expect(block.blockedAccount.displayName).to.equal('user2')
629 expect(block.blockedAccount.name).to.equal('user2')
7243f84d 630 expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
b44164bb
C
631 }
632
633 {
5f8bd4cb
C
634 const body = await command.listServerAccountBlocklist({ start: 1, count: 2, sort: 'createdAt' })
635 expect(body.total).to.equal(2)
b44164bb 636
5f8bd4cb 637 const block = body.data[0]
b44164bb
C
638 expect(block.byAccount.displayName).to.equal('peertube')
639 expect(block.byAccount.name).to.equal('peertube')
640 expect(block.blockedAccount.displayName).to.equal('user1')
641 expect(block.blockedAccount.name).to.equal('user1')
7243f84d 642 expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
b44164bb
C
643 }
644 })
645
80badf49
C
646 it('Should get blocked status', async function () {
647 const remoteHandle = 'user2@' + servers[1].host
648 const localHandle = 'user1@' + servers[0].host
649 const unknownHandle = 'user5@' + servers[0].host
650
651 for (const token of [ undefined, servers[0].accessToken ]) {
652 const status = await command.getStatus({ token, accounts: [ localHandle, remoteHandle, unknownHandle ] })
653 expect(Object.keys(status.accounts)).to.have.lengthOf(3)
654
655 for (const handle of [ localHandle, remoteHandle ]) {
656 expect(status.accounts[handle].blockedByUser).to.be.false
657 expect(status.accounts[handle].blockedByServer).to.be.true
658 }
659
660 expect(status.accounts[unknownHandle].blockedByUser).to.be.false
661 expect(status.accounts[unknownHandle].blockedByServer).to.be.false
662
663 expect(Object.keys(status.hosts)).to.have.lengthOf(0)
664 }
665 })
666
b44164bb 667 it('Should unblock the remote account', async function () {
5f8bd4cb 668 await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port })
b44164bb
C
669 })
670
671 it('Should display its videos', async function () {
a1587156 672 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
89d241a7 673 const { data } = await servers[0].videos.listWithToken({ token })
d23dd9fb 674 expect(data).to.have.lengthOf(4)
b44164bb 675
d23dd9fb 676 const v = data.find(v => v.name === 'video user 2')
b44164bb
C
677 expect(v).not.to.be.undefined
678 }
679 })
680
681 it('Should unblock the local account', async function () {
5f8bd4cb 682 await command.removeFromServerBlocklist({ account: 'user1' })
b44164bb
C
683 })
684
685 it('Should display its comments', async function () {
a1587156 686 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149 687 await checkAllComments(servers[0], token, videoUUID1)
b44164bb
C
688 }
689 })
dddc8b1f
C
690
691 it('Should have notifications from unblocked accounts', async function () {
692 this.timeout(20000)
693
694 {
a1587156
C
695 const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'displayed comment' }
696 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f
C
697 }
698
699 {
700 const comment = {
a1587156 701 server: servers[1],
dddc8b1f
C
702 token: userToken2,
703 videoUUID: videoUUID1,
a1587156 704 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 705 }
a1587156 706 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f
C
707 }
708 })
b44164bb
C
709 })
710
711 describe('When managing server blocklist', function () {
80badf49 712
b44164bb 713 it('Should list all videos', async function () {
a1587156 714 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149 715 await checkAllVideos(servers[0], token)
b44164bb
C
716 }
717 })
718
719 it('Should list the comments', async function () {
a1587156 720 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149 721 await checkAllComments(servers[0], token, videoUUID1)
b44164bb
C
722 }
723 })
724
725 it('Should block a remote server', async function () {
5f8bd4cb 726 await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
b44164bb
C
727 })
728
729 it('Should hide its videos', async function () {
a1587156 730 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
d23dd9fb 731 const requests = [
89d241a7
C
732 servers[0].videos.list(),
733 servers[0].videos.listWithToken({ token })
d23dd9fb 734 ]
b44164bb 735
d23dd9fb
C
736 for (const req of requests) {
737 const { data } = await req
738 expect(data).to.have.lengthOf(3)
b44164bb 739
d23dd9fb
C
740 const v1 = data.find(v => v.name === 'video user 2')
741 const v2 = data.find(v => v.name === 'video server 2')
b44164bb 742
65b21c96
C
743 expect(v1).to.be.undefined
744 expect(v2).to.be.undefined
745 }
b44164bb
C
746 }
747 })
748
dddc8b1f
C
749 it('Should hide its comments', async function () {
750 this.timeout(10000)
751
12edc149 752 const { id } = await commentsCommand[1].createThread({ token: userToken2, videoId: videoUUID1, text: 'hidden comment 2' })
dddc8b1f
C
753
754 await waitJobs(servers)
755
12edc149 756 await checkAllComments(servers[0], servers[0].accessToken, videoUUID1)
dddc8b1f 757
12edc149 758 await commentsCommand[1].delete({ token: userToken2, videoId: videoUUID1, commentId: id })
dddc8b1f
C
759 })
760
761 it('Should not have notification from blocked instances by instance', async function () {
696d83fd 762 this.timeout(50000)
dddc8b1f
C
763
764 {
a1587156
C
765 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
766 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f
C
767 }
768
769 {
770 const comment = {
a1587156 771 server: servers[1],
dddc8b1f
C
772 token: userToken2,
773 videoUUID: videoUUID1,
a1587156 774 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 775 }
a1587156 776 await checkCommentNotification(servers[0], comment, 'absence')
dddc8b1f 777 }
696d83fd
C
778
779 {
780 const now = new Date()
89d241a7 781 await servers[1].follows.unfollow({ target: servers[0] })
696d83fd 782 await waitJobs(servers)
4d029ef8 783 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
696d83fd
C
784
785 await waitJobs(servers)
786
89d241a7 787 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
dd0ebb71
C
788 const commentNotifications = data.filter(n => {
789 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
790 })
696d83fd
C
791
792 expect(commentNotifications).to.have.lengthOf(0)
793 }
dddc8b1f 794 })
b44164bb
C
795
796 it('Should list blocked servers', async function () {
5f8bd4cb
C
797 const body = await command.listServerServerBlocklist({ start: 0, count: 1, sort: 'createdAt' })
798 expect(body.total).to.equal(1)
b44164bb 799
5f8bd4cb 800 const block = body.data[0]
b44164bb
C
801 expect(block.byAccount.displayName).to.equal('peertube')
802 expect(block.byAccount.name).to.equal('peertube')
7243f84d 803 expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
b44164bb
C
804 })
805
80badf49
C
806 it('Should get blocklist status', async function () {
807 const blockedServer = servers[1].host
808 const notBlockedServer = 'example.com'
809
810 for (const token of [ undefined, servers[0].accessToken ]) {
811 const status = await command.getStatus({ token, hosts: [ blockedServer, notBlockedServer ] })
812 expect(Object.keys(status.accounts)).to.have.lengthOf(0)
813
814 expect(Object.keys(status.hosts)).to.have.lengthOf(2)
815 expect(status.hosts[blockedServer].blockedByUser).to.be.false
816 expect(status.hosts[blockedServer].blockedByServer).to.be.true
817
818 expect(status.hosts[notBlockedServer].blockedByUser).to.be.false
819 expect(status.hosts[notBlockedServer].blockedByServer).to.be.false
820 }
821 })
822
b44164bb 823 it('Should unblock the remote server', async function () {
5f8bd4cb 824 await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
b44164bb
C
825 })
826
827 it('Should list all videos', async function () {
a1587156 828 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149 829 await checkAllVideos(servers[0], token)
b44164bb
C
830 }
831 })
832
833 it('Should list the comments', async function () {
a1587156 834 for (const token of [ userModeratorToken, servers[0].accessToken ]) {
12edc149 835 await checkAllComments(servers[0], token, videoUUID1)
b44164bb
C
836 }
837 })
dddc8b1f
C
838
839 it('Should have notification from unblocked instances', async function () {
696d83fd 840 this.timeout(50000)
dddc8b1f
C
841
842 {
a1587156
C
843 const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
844 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f
C
845 }
846
847 {
848 const comment = {
a1587156 849 server: servers[1],
dddc8b1f
C
850 token: userToken2,
851 videoUUID: videoUUID1,
a1587156 852 text: 'hello @root@localhost:' + servers[0].port
dddc8b1f 853 }
a1587156 854 await checkCommentNotification(servers[0], comment, 'presence')
dddc8b1f 855 }
696d83fd
C
856
857 {
858 const now = new Date()
89d241a7 859 await servers[1].follows.unfollow({ target: servers[0] })
696d83fd 860 await waitJobs(servers)
4d029ef8 861 await servers[1].follows.follow({ hosts: [ servers[0].host ] })
696d83fd
C
862
863 await waitJobs(servers)
864
89d241a7 865 const { data } = await servers[0].notifications.list({ start: 0, count: 30 })
dd0ebb71
C
866 const commentNotifications = data.filter(n => {
867 return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString()
868 })
696d83fd
C
869
870 expect(commentNotifications).to.have.lengthOf(1)
871 }
dddc8b1f 872 })
b44164bb
C
873 })
874 })
875
7c3b7976
C
876 after(async function () {
877 await cleanupTests(servers)
b44164bb
C
878 })
879})