blocklist.ts 30.1 KB
Newer Older
Chocobozzz's avatar
Chocobozzz committed
1
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3

import 'mocha'
Chocobozzz's avatar
Chocobozzz committed
4
import * as chai from 'chai'
5
import {
Chocobozzz's avatar
Chocobozzz committed
6
7
8
9
10
11
  addAccountToAccountBlocklist,
  addAccountToServerBlocklist,
  addServerToAccountBlocklist,
  addServerToServerBlocklist,
  addVideoCommentReply,
  addVideoCommentThread,
Chocobozzz's avatar
Chocobozzz committed
12
  cleanupTests,
Chocobozzz's avatar
Chocobozzz committed
13
14
  createUser,
  deleteVideoComment,
15
  doubleFollow,
Chocobozzz's avatar
Chocobozzz committed
16
  findCommentId,
17
  flushAndRunMultipleServers,
18
  follow,
19
20
21
22
  getAccountBlocklistByAccount,
  getAccountBlocklistByServer,
  getServerBlocklistByAccount,
  getServerBlocklistByServer,
Chocobozzz's avatar
Chocobozzz committed
23
24
25
26
27
  getUserNotifications,
  getVideoCommentThreads,
  getVideosList,
  getVideosListWithToken,
  getVideoThreadComments,
28
29
30
  removeAccountFromAccountBlocklist,
  removeAccountFromServerBlocklist,
  removeServerFromAccountBlocklist,
Chocobozzz's avatar
Chocobozzz committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  removeServerFromServerBlocklist,
  ServerInfo,
  setAccessTokensToServers,
  unfollow,
  uploadVideo,
  userLogin,
  waitJobs
} from '@shared/extra-utils'
import {
  AccountBlock,
  ServerBlock,
  UserNotification,
  UserNotificationType,
  Video,
  VideoComment,
  VideoCommentThreadTree
} from '@shared/models'
48
49
50
51

const expect = chai.expect

async function checkAllVideos (url: string, token: string) {
52
53
  {
    const res = await getVideosListWithToken(url, token)
54

55
    expect(res.body.data).to.have.lengthOf(5)
56
57
58
59
60
  }

  {
    const res = await getVideosList(url)

61
    expect(res.body.data).to.have.lengthOf(5)
62
  }
63
64
65
}

async function checkAllComments (url: string, token: string, videoUUID: string) {
Chocobozzz's avatar
Chocobozzz committed
66
  const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 25, '-createdAt', token)
67

Chocobozzz's avatar
Chocobozzz committed
68
69
  const allThreads: VideoComment[] = resThreads.body.data
  const threads = allThreads.filter(t => t.isDeleted === false)
70
71
72
73
74
75
76
77
78
79
  expect(threads).to.have.lengthOf(2)

  for (const thread of threads) {
    const res = await getVideoThreadComments(url, videoUUID, thread.id, token)

    const tree: VideoCommentThreadTree = res.body
    expect(tree.children).to.have.lengthOf(1)
  }
}

Chocobozzz's avatar
Chocobozzz committed
80
81
82
83
84
85
async function checkCommentNotification (
  mainServer: ServerInfo,
  comment: { server: ServerInfo, token: string, videoUUID: string, text: string },
  check: 'presence' | 'absence'
) {
  const resComment = await addVideoCommentThread(comment.server.url, comment.token, comment.videoUUID, comment.text)
86
87
88
  const created = resComment.body.comment as VideoComment
  const threadId = created.id
  const createdAt = created.createdAt
Chocobozzz's avatar
Chocobozzz committed
89

Chocobozzz's avatar
Chocobozzz committed
90
  await waitJobs([ mainServer, comment.server ])
Chocobozzz's avatar
Chocobozzz committed
91
92

  const res = await getUserNotifications(mainServer.url, mainServer.accessToken, 0, 30)
93
94
  const commentNotifications = (res.body.data as UserNotification[])
                                  .filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt)
Chocobozzz's avatar
Chocobozzz committed
95
96
97
98
99
100

  if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1)
  else expect(commentNotifications).to.have.lengthOf(0)

  await deleteVideoComment(comment.server.url, comment.token, comment.videoUUID, threadId)

Chocobozzz's avatar
Chocobozzz committed
101
  await waitJobs([ mainServer, comment.server ])
Chocobozzz's avatar
Chocobozzz committed
102
103
}

104
105
106
107
describe('Test blocklist', function () {
  let servers: ServerInfo[]
  let videoUUID1: string
  let videoUUID2: string
108
  let videoUUID3: string
109
110
111
112
113
  let userToken1: string
  let userModeratorToken: string
  let userToken2: string

  before(async function () {
114
    this.timeout(120000)
115

116
    servers = await flushAndRunMultipleServers(3)
117
118
119
120
    await setAccessTokensToServers(servers)

    {
      const user = { username: 'user1', password: 'password' }
Chocobozzz's avatar
Chocobozzz committed
121
      await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
122
123
124
125
126
127
128

      userToken1 = await userLogin(servers[0], user)
      await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
    }

    {
      const user = { username: 'moderator', password: 'password' }
Chocobozzz's avatar
Chocobozzz committed
129
      await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
130
131
132
133
134
135

      userModeratorToken = await userLogin(servers[0], user)
    }

    {
      const user = { username: 'user2', password: 'password' }
Chocobozzz's avatar
Chocobozzz committed
136
      await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

      userToken2 = await userLogin(servers[1], user)
      await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
    }

    {
      const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
      videoUUID1 = res.body.video.uuid
    }

    {
      const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
      videoUUID2 = res.body.video.uuid
    }

152
153
154
155
156
    {
      const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' })
      videoUUID3 = res.body.video.uuid
    }

157
    await doubleFollow(servers[0], servers[1])
158
    await doubleFollow(servers[0], servers[2])
159
160

    {
Chocobozzz's avatar
Chocobozzz committed
161
162
163
      const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID1, 'comment root 1')
      const resReply = await addVideoCommentReply(servers[0].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
      await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
164
165
166
    }

    {
Chocobozzz's avatar
Chocobozzz committed
167
168
      const resComment = await addVideoCommentThread(servers[0].url, userToken1, videoUUID1, 'comment user 1')
      await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
169
170
171
172
173
174
175
176
177
    }

    await waitJobs(servers)
  })

  describe('User blocklist', function () {

    describe('When managing account blocklist', function () {
      it('Should list all videos', function () {
Chocobozzz's avatar
Chocobozzz committed
178
        return checkAllVideos(servers[0].url, servers[0].accessToken)
179
180
181
      })

      it('Should list the comments', function () {
Chocobozzz's avatar
Chocobozzz committed
182
        return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
183
184
185
      })

      it('Should block a remote account', async function () {
Chocobozzz's avatar
Chocobozzz committed
186
        await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port)
187
188
189
      })

      it('Should hide its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
190
        const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
191
192

        const videos: Video[] = res.body.data
193
        expect(videos).to.have.lengthOf(4)
194
195
196
197
198
199

        const v = videos.find(v => v.name === 'video user 2')
        expect(v).to.be.undefined
      })

      it('Should block a local account', async function () {
Chocobozzz's avatar
Chocobozzz committed
200
        await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
201
202
203
      })

      it('Should hide its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
204
        const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
205
206

        const videos: Video[] = res.body.data
207
        expect(videos).to.have.lengthOf(3)
208
209
210
211
212
213

        const v = videos.find(v => v.name === 'video user 1')
        expect(v).to.be.undefined
      })

      it('Should hide its comments', async function () {
Chocobozzz's avatar
Chocobozzz committed
214
        const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 25, '-createdAt', servers[0].accessToken)
215
216
217

        const threads: VideoComment[] = resThreads.body.data
        expect(threads).to.have.lengthOf(1)
Chocobozzz's avatar
Chocobozzz committed
218
        expect(threads[0].totalReplies).to.equal(1)
219
220
221
222
223

        const t = threads.find(t => t.text === 'comment user 1')
        expect(t).to.be.undefined

        for (const thread of threads) {
Chocobozzz's avatar
Chocobozzz committed
224
          const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, servers[0].accessToken)
225
226
227
228
229
230

          const tree: VideoCommentThreadTree = res.body
          expect(tree.children).to.have.lengthOf(0)
        }
      })

Chocobozzz's avatar
Chocobozzz committed
231
232
233
234
      it('Should not have notifications from blocked accounts', async function () {
        this.timeout(20000)

        {
Chocobozzz's avatar
Chocobozzz committed
235
236
          const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
237
238
239
240
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
241
            server: servers[0],
Chocobozzz's avatar
Chocobozzz committed
242
243
            token: userToken1,
            videoUUID: videoUUID2,
Chocobozzz's avatar
Chocobozzz committed
244
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
245
          }
Chocobozzz's avatar
Chocobozzz committed
246
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
247
248
249
        }
      })

250
      it('Should list all the videos with another user', async function () {
Chocobozzz's avatar
Chocobozzz committed
251
        return checkAllVideos(servers[0].url, userToken1)
252
253
254
255
      })

      it('Should list blocked accounts', async function () {
        {
Chocobozzz's avatar
Chocobozzz committed
256
          const res = await getAccountBlocklistByAccount(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt')
257
258
259
260
          const blocks: AccountBlock[] = res.body.data

          expect(res.body.total).to.equal(2)

Chocobozzz's avatar
Chocobozzz committed
261
          const block = blocks[0]
262
263
264
265
          expect(block.byAccount.displayName).to.equal('root')
          expect(block.byAccount.name).to.equal('root')
          expect(block.blockedAccount.displayName).to.equal('user2')
          expect(block.blockedAccount.name).to.equal('user2')
266
          expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
267
268
269
        }

        {
Chocobozzz's avatar
Chocobozzz committed
270
          const res = await getAccountBlocklistByAccount(servers[0].url, servers[0].accessToken, 1, 2, 'createdAt')
271
272
273
274
          const blocks: AccountBlock[] = res.body.data

          expect(res.body.total).to.equal(2)

Chocobozzz's avatar
Chocobozzz committed
275
          const block = blocks[0]
276
277
278
279
          expect(block.byAccount.displayName).to.equal('root')
          expect(block.byAccount.name).to.equal('root')
          expect(block.blockedAccount.displayName).to.equal('user1')
          expect(block.blockedAccount.name).to.equal('user1')
280
          expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
281
282
283
        }
      })

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
      it('Should not allow a remote blocked user to comment my videos', async function () {
        this.timeout(60000)

        {
          await addVideoCommentThread(servers[1].url, userToken2, videoUUID3, 'comment user 2')
          await waitJobs(servers)

          await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID3, 'uploader')
          await waitJobs(servers)

          const commentId = await findCommentId(servers[1].url, videoUUID3, 'uploader')
          const message = 'reply by user 2'
          const resReply = await addVideoCommentReply(servers[1].url, userToken2, videoUUID3, commentId, message)
          await addVideoCommentReply(servers[1].url, servers[1].accessToken, videoUUID3, resReply.body.comment.id, 'another reply')

          await waitJobs(servers)
        }

        // Server 2 has all the comments
        {
          const resThreads = await getVideoCommentThreads(servers[1].url, videoUUID3, 0, 25, '-createdAt')
          const threads: VideoComment[] = resThreads.body.data

          expect(threads).to.have.lengthOf(2)
          expect(threads[0].text).to.equal('uploader')
          expect(threads[1].text).to.equal('comment user 2')

          const resReplies = await getVideoThreadComments(servers[1].url, videoUUID3, threads[0].id)

          const tree: VideoCommentThreadTree = resReplies.body
          expect(tree.children).to.have.lengthOf(1)
          expect(tree.children[0].comment.text).to.equal('reply by user 2')
          expect(tree.children[0].children).to.have.lengthOf(1)
          expect(tree.children[0].children[0].comment.text).to.equal('another reply')
        }

        // Server 1 and 3 should only have uploader comments
        for (const server of [ servers[0], servers[2] ]) {
          const resThreads = await getVideoCommentThreads(server.url, videoUUID3, 0, 25, '-createdAt')
          const threads: VideoComment[] = resThreads.body.data

          expect(threads).to.have.lengthOf(1)
          expect(threads[0].text).to.equal('uploader')

          const resReplies = await getVideoThreadComments(server.url, videoUUID3, threads[0].id)

          const tree: VideoCommentThreadTree = resReplies.body
          if (server.serverNumber === 1) {
            expect(tree.children).to.have.lengthOf(0)
          } else {
            expect(tree.children).to.have.lengthOf(1)
          }
        }
      })

339
      it('Should unblock the remote account', async function () {
Chocobozzz's avatar
Chocobozzz committed
340
        await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port)
341
342
343
      })

      it('Should display its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
344
        const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
345
346

        const videos: Video[] = res.body.data
347
        expect(videos).to.have.lengthOf(4)
348
349
350
351
352

        const v = videos.find(v => v.name === 'video user 2')
        expect(v).not.to.be.undefined
      })

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
      it('Should display its comments on my video', async function () {
        for (const server of servers) {
          const resThreads = await getVideoCommentThreads(server.url, videoUUID3, 0, 25, '-createdAt')
          const threads: VideoComment[] = resThreads.body.data

          // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment
          if (server.serverNumber === 3) {
            expect(threads).to.have.lengthOf(1)
            continue
          }

          expect(threads).to.have.lengthOf(2)
          expect(threads[0].text).to.equal('uploader')
          expect(threads[1].text).to.equal('comment user 2')

          const resReplies = await getVideoThreadComments(server.url, videoUUID3, threads[0].id)

          const tree: VideoCommentThreadTree = resReplies.body
          expect(tree.children).to.have.lengthOf(1)
          expect(tree.children[0].comment.text).to.equal('reply by user 2')
          expect(tree.children[0].children).to.have.lengthOf(1)
          expect(tree.children[0].children[0].comment.text).to.equal('another reply')
        }
      })

378
      it('Should unblock the local account', async function () {
Chocobozzz's avatar
Chocobozzz committed
379
        await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
380
381
382
      })

      it('Should display its comments', function () {
Chocobozzz's avatar
Chocobozzz committed
383
        return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
384
      })
Chocobozzz's avatar
Chocobozzz committed
385
386
387
388
389

      it('Should have a notification from a non blocked account', async function () {
        this.timeout(20000)

        {
Chocobozzz's avatar
Chocobozzz committed
390
391
          const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
392
393
394
395
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
396
            server: servers[0],
Chocobozzz's avatar
Chocobozzz committed
397
398
            token: userToken1,
            videoUUID: videoUUID2,
Chocobozzz's avatar
Chocobozzz committed
399
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
400
          }
Chocobozzz's avatar
Chocobozzz committed
401
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
402
403
        }
      })
404
405
406
407
    })

    describe('When managing server blocklist', function () {
      it('Should list all videos', function () {
Chocobozzz's avatar
Chocobozzz committed
408
        return checkAllVideos(servers[0].url, servers[0].accessToken)
409
410
411
      })

      it('Should list the comments', function () {
Chocobozzz's avatar
Chocobozzz committed
412
        return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
413
414
415
      })

      it('Should block a remote server', async function () {
Chocobozzz's avatar
Chocobozzz committed
416
        await addServerToAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port)
417
418
419
      })

      it('Should hide its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
420
        const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
421
422

        const videos: Video[] = res.body.data
423
        expect(videos).to.have.lengthOf(3)
424
425
426
427
428
429
430
431
432

        const v1 = videos.find(v => v.name === 'video user 2')
        const v2 = videos.find(v => v.name === 'video server 2')

        expect(v1).to.be.undefined
        expect(v2).to.be.undefined
      })

      it('Should list all the videos with another user', async function () {
Chocobozzz's avatar
Chocobozzz committed
433
        return checkAllVideos(servers[0].url, userToken1)
434
435
      })

Chocobozzz's avatar
Chocobozzz committed
436
437
438
      it('Should hide its comments', async function () {
        this.timeout(10000)

Chocobozzz's avatar
Chocobozzz committed
439
        const resThreads = await addVideoCommentThread(servers[1].url, userToken2, videoUUID1, 'hidden comment 2')
Chocobozzz's avatar
Chocobozzz committed
440
441
442
443
        const threadId = resThreads.body.comment.id

        await waitJobs(servers)

Chocobozzz's avatar
Chocobozzz committed
444
        await checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
Chocobozzz's avatar
Chocobozzz committed
445

Chocobozzz's avatar
Chocobozzz committed
446
        await deleteVideoComment(servers[1].url, userToken2, videoUUID1, threadId)
Chocobozzz's avatar
Chocobozzz committed
447
448
449
450
451
452
      })

      it('Should not have notifications from blocked server', async function () {
        this.timeout(20000)

        {
Chocobozzz's avatar
Chocobozzz committed
453
454
          const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
455
456
457
458
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
459
            server: servers[1],
Chocobozzz's avatar
Chocobozzz committed
460
461
            token: userToken2,
            videoUUID: videoUUID1,
Chocobozzz's avatar
Chocobozzz committed
462
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
463
          }
Chocobozzz's avatar
Chocobozzz committed
464
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
465
466
        }
      })
467
468

      it('Should list blocked servers', async function () {
Chocobozzz's avatar
Chocobozzz committed
469
        const res = await getServerBlocklistByAccount(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt')
470
471
472
473
        const blocks: ServerBlock[] = res.body.data

        expect(res.body.total).to.equal(1)

Chocobozzz's avatar
Chocobozzz committed
474
        const block = blocks[0]
475
476
        expect(block.byAccount.displayName).to.equal('root')
        expect(block.byAccount.name).to.equal('root')
477
        expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
478
479
480
      })

      it('Should unblock the remote server', async function () {
Chocobozzz's avatar
Chocobozzz committed
481
        await removeServerFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port)
482
483
484
      })

      it('Should display its videos', function () {
Chocobozzz's avatar
Chocobozzz committed
485
        return checkAllVideos(servers[0].url, servers[0].accessToken)
486
487
488
      })

      it('Should display its comments', function () {
Chocobozzz's avatar
Chocobozzz committed
489
        return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
490
      })
Chocobozzz's avatar
Chocobozzz committed
491
492
493
494
495

      it('Should have notification from unblocked server', async function () {
        this.timeout(20000)

        {
Chocobozzz's avatar
Chocobozzz committed
496
497
          const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
498
499
500
501
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
502
            server: servers[1],
Chocobozzz's avatar
Chocobozzz committed
503
504
            token: userToken2,
            videoUUID: videoUUID1,
Chocobozzz's avatar
Chocobozzz committed
505
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
506
          }
Chocobozzz's avatar
Chocobozzz committed
507
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
508
509
        }
      })
510
511
512
513
514
515
516
    })
  })

  describe('Server blocklist', function () {

    describe('When managing account blocklist', function () {
      it('Should list all videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
517
518
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          await checkAllVideos(servers[0].url, token)
519
520
521
522
        }
      })

      it('Should list the comments', async function () {
Chocobozzz's avatar
Chocobozzz committed
523
524
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          await checkAllComments(servers[0].url, token, videoUUID1)
525
526
527
528
        }
      })

      it('Should block a remote account', async function () {
Chocobozzz's avatar
Chocobozzz committed
529
        await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port)
530
531
532
      })

      it('Should hide its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
533
534
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          const res = await getVideosListWithToken(servers[0].url, token)
535
536

          const videos: Video[] = res.body.data
537
          expect(videos).to.have.lengthOf(4)
538
539
540
541
542
543
544

          const v = videos.find(v => v.name === 'video user 2')
          expect(v).to.be.undefined
        }
      })

      it('Should block a local account', async function () {
Chocobozzz's avatar
Chocobozzz committed
545
        await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'user1')
546
547
548
      })

      it('Should hide its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
549
550
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          const res = await getVideosListWithToken(servers[0].url, token)
551
552

          const videos: Video[] = res.body.data
553
          expect(videos).to.have.lengthOf(3)
554
555
556
557
558
559
560

          const v = videos.find(v => v.name === 'video user 1')
          expect(v).to.be.undefined
        }
      })

      it('Should hide its comments', async function () {
Chocobozzz's avatar
Chocobozzz committed
561
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
Chocobozzz's avatar
Chocobozzz committed
562
563
564
565
          const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 20, '-createdAt', token)

          let threads: VideoComment[] = resThreads.body.data
          threads = threads.filter(t => t.isDeleted === false)
566
567

          expect(threads).to.have.lengthOf(1)
Chocobozzz's avatar
Chocobozzz committed
568
          expect(threads[0].totalReplies).to.equal(1)
569
570
571
572
573

          const t = threads.find(t => t.text === 'comment user 1')
          expect(t).to.be.undefined

          for (const thread of threads) {
Chocobozzz's avatar
Chocobozzz committed
574
            const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, token)
575
576
577
578
579
580
581

            const tree: VideoCommentThreadTree = res.body
            expect(tree.children).to.have.lengthOf(0)
          }
        }
      })

Chocobozzz's avatar
Chocobozzz committed
582
583
584
585
      it('Should not have notification from blocked accounts by instance', async function () {
        this.timeout(20000)

        {
Chocobozzz's avatar
Chocobozzz committed
586
587
          const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'hidden comment' }
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
588
589
590
591
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
592
            server: servers[1],
Chocobozzz's avatar
Chocobozzz committed
593
594
            token: userToken2,
            videoUUID: videoUUID1,
Chocobozzz's avatar
Chocobozzz committed
595
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
596
          }
Chocobozzz's avatar
Chocobozzz committed
597
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
598
599
600
        }
      })

601
602
      it('Should list blocked accounts', async function () {
        {
Chocobozzz's avatar
Chocobozzz committed
603
          const res = await getAccountBlocklistByServer(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt')
604
605
606
607
          const blocks: AccountBlock[] = res.body.data

          expect(res.body.total).to.equal(2)

Chocobozzz's avatar
Chocobozzz committed
608
          const block = blocks[0]
609
610
611
612
          expect(block.byAccount.displayName).to.equal('peertube')
          expect(block.byAccount.name).to.equal('peertube')
          expect(block.blockedAccount.displayName).to.equal('user2')
          expect(block.blockedAccount.name).to.equal('user2')
613
          expect(block.blockedAccount.host).to.equal('localhost:' + servers[1].port)
614
615
616
        }

        {
Chocobozzz's avatar
Chocobozzz committed
617
          const res = await getAccountBlocklistByServer(servers[0].url, servers[0].accessToken, 1, 2, 'createdAt')
618
619
620
621
          const blocks: AccountBlock[] = res.body.data

          expect(res.body.total).to.equal(2)

Chocobozzz's avatar
Chocobozzz committed
622
          const block = blocks[0]
623
624
625
626
          expect(block.byAccount.displayName).to.equal('peertube')
          expect(block.byAccount.name).to.equal('peertube')
          expect(block.blockedAccount.displayName).to.equal('user1')
          expect(block.blockedAccount.name).to.equal('user1')
627
          expect(block.blockedAccount.host).to.equal('localhost:' + servers[0].port)
628
629
630
631
        }
      })

      it('Should unblock the remote account', async function () {
Chocobozzz's avatar
Chocobozzz committed
632
        await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:' + servers[1].port)
633
634
635
      })

      it('Should display its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
636
637
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          const res = await getVideosListWithToken(servers[0].url, token)
638
639

          const videos: Video[] = res.body.data
640
          expect(videos).to.have.lengthOf(4)
641
642
643
644
645
646
647

          const v = videos.find(v => v.name === 'video user 2')
          expect(v).not.to.be.undefined
        }
      })

      it('Should unblock the local account', async function () {
Chocobozzz's avatar
Chocobozzz committed
648
        await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'user1')
649
650
651
      })

      it('Should display its comments', async function () {
Chocobozzz's avatar
Chocobozzz committed
652
653
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          await checkAllComments(servers[0].url, token, videoUUID1)
654
655
        }
      })
Chocobozzz's avatar
Chocobozzz committed
656
657
658
659
660

      it('Should have notifications from unblocked accounts', async function () {
        this.timeout(20000)

        {
Chocobozzz's avatar
Chocobozzz committed
661
662
          const comment = { server: servers[0], token: userToken1, videoUUID: videoUUID1, text: 'displayed comment' }
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
663
664
665
666
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
667
            server: servers[1],
Chocobozzz's avatar
Chocobozzz committed
668
669
            token: userToken2,
            videoUUID: videoUUID1,
Chocobozzz's avatar
Chocobozzz committed
670
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
671
          }
Chocobozzz's avatar
Chocobozzz committed
672
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
673
674
        }
      })
675
676
677
678
    })

    describe('When managing server blocklist', function () {
      it('Should list all videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
679
680
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          await checkAllVideos(servers[0].url, token)
681
682
683
684
        }
      })

      it('Should list the comments', async function () {
Chocobozzz's avatar
Chocobozzz committed
685
686
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          await checkAllComments(servers[0].url, token, videoUUID1)
687
688
689
690
        }
      })

      it('Should block a remote server', async function () {
Chocobozzz's avatar
Chocobozzz committed
691
        await addServerToServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port)
692
693
694
      })

      it('Should hide its videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
695
696
697
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          const res1 = await getVideosList(servers[0].url)
          const res2 = await getVideosListWithToken(servers[0].url, token)
698

699
700
          for (const res of [ res1, res2 ]) {
            const videos: Video[] = res.body.data
701
            expect(videos).to.have.lengthOf(3)
702

703
704
            const v1 = videos.find(v => v.name === 'video user 2')
            const v2 = videos.find(v => v.name === 'video server 2')
705

706
707
708
            expect(v1).to.be.undefined
            expect(v2).to.be.undefined
          }
709
710
711
        }
      })

Chocobozzz's avatar
Chocobozzz committed
712
713
714
      it('Should hide its comments', async function () {
        this.timeout(10000)

Chocobozzz's avatar
Chocobozzz committed
715
        const resThreads = await addVideoCommentThread(servers[1].url, userToken2, videoUUID1, 'hidden comment 2')
Chocobozzz's avatar
Chocobozzz committed
716
717
718
719
        const threadId = resThreads.body.comment.id

        await waitJobs(servers)

Chocobozzz's avatar
Chocobozzz committed
720
        await checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
Chocobozzz's avatar
Chocobozzz committed
721

Chocobozzz's avatar
Chocobozzz committed
722
        await deleteVideoComment(servers[1].url, userToken2, videoUUID1, threadId)
Chocobozzz's avatar
Chocobozzz committed
723
724
725
      })

      it('Should not have notification from blocked instances by instance', async function () {
726
        this.timeout(50000)
Chocobozzz's avatar
Chocobozzz committed
727
728

        {
Chocobozzz's avatar
Chocobozzz committed
729
730
          const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'hidden comment' }
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
731
732
733
734
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
735
            server: servers[1],
Chocobozzz's avatar
Chocobozzz committed
736
737
            token: userToken2,
            videoUUID: videoUUID1,
Chocobozzz's avatar
Chocobozzz committed
738
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
739
          }
Chocobozzz's avatar
Chocobozzz committed
740
          await checkCommentNotification(servers[0], comment, 'absence')
Chocobozzz's avatar
Chocobozzz committed
741
        }
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

        {
          const now = new Date()
          await unfollow(servers[1].url, servers[1].accessToken, servers[0])
          await waitJobs(servers)
          await follow(servers[1].url, [ servers[0].host ], servers[1].accessToken)

          await waitJobs(servers)

          const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30)
          const commentNotifications = (res.body.data as UserNotification[])
                                          .filter(n => {
                                            return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER &&
                                            n.createdAt >= now.toISOString()
                                          })

          expect(commentNotifications).to.have.lengthOf(0)
        }
Chocobozzz's avatar
Chocobozzz committed
760
      })
761
762

      it('Should list blocked servers', async function () {
Chocobozzz's avatar
Chocobozzz committed
763
        const res = await getServerBlocklistByServer(servers[0].url, servers[0].accessToken, 0, 1, 'createdAt')
764
765
766
767
        const blocks: ServerBlock[] = res.body.data

        expect(res.body.total).to.equal(1)

Chocobozzz's avatar
Chocobozzz committed
768
        const block = blocks[0]
769
770
        expect(block.byAccount.displayName).to.equal('peertube')
        expect(block.byAccount.name).to.equal('peertube')
771
        expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port)
772
773
774
      })

      it('Should unblock the remote server', async function () {
Chocobozzz's avatar
Chocobozzz committed
775
        await removeServerFromServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port)
776
777
778
      })

      it('Should list all videos', async function () {
Chocobozzz's avatar
Chocobozzz committed
779
780
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          await checkAllVideos(servers[0].url, token)
781
782
783
784
        }
      })

      it('Should list the comments', async function () {
Chocobozzz's avatar
Chocobozzz committed
785
786
        for (const token of [ userModeratorToken, servers[0].accessToken ]) {
          await checkAllComments(servers[0].url, token, videoUUID1)
787
788
        }
      })
Chocobozzz's avatar
Chocobozzz committed
789
790

      it('Should have notification from unblocked instances', async function () {
791
        this.timeout(50000)
Chocobozzz's avatar
Chocobozzz committed
792
793

        {
Chocobozzz's avatar
Chocobozzz committed
794
795
          const comment = { server: servers[1], token: userToken2, videoUUID: videoUUID1, text: 'displayed comment' }
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
796
797
798
799
        }

        {
          const comment = {
Chocobozzz's avatar
Chocobozzz committed
800
            server: servers[1],
Chocobozzz's avatar
Chocobozzz committed
801
802
            token: userToken2,
            videoUUID: videoUUID1,
Chocobozzz's avatar
Chocobozzz committed
803
            text: 'hello @root@localhost:' + servers[0].port
Chocobozzz's avatar
Chocobozzz committed
804
          }
Chocobozzz's avatar
Chocobozzz committed
805
          await checkCommentNotification(servers[0], comment, 'presence')
Chocobozzz's avatar
Chocobozzz committed
806
        }
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824

        {
          const now = new Date()
          await unfollow(servers[1].url, servers[1].accessToken, servers[0])
          await waitJobs(servers)
          await follow(servers[1].url, [ servers[0].host ], servers[1].accessToken)

          await waitJobs(servers)

          const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30)
          const commentNotifications = (res.body.data as UserNotification[])
                                          .filter(n => {
                                            return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER &&
                                            n.createdAt >= now.toISOString()
                                          })

          expect(commentNotifications).to.have.lengthOf(1)
        }
Chocobozzz's avatar
Chocobozzz committed
825
      })
826
827
828
    })
  })

Chocobozzz's avatar
Chocobozzz committed
829
830
  after(async function () {
    await cleanupTests(servers)
831
832
  })
})