aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/external-plugins/auto-block-videos.ts
blob: 3b4b48bf01b1969ff7fb99b4feb34882aad6d315 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import { expect } from 'chai'
import {
  cleanupTests,
  doubleFollow,
  flushAndRunMultipleServers,
  killallServers,
  MockBlocklist,
  reRunServer,
  ServerInfo,
  setAccessTokensToServers,
  wait
} from '@shared/extra-utils'
import { Video } from '@shared/models'

async function check (server: ServerInfo, videoUUID: string, exists = true) {
  const { data } = await server.videos.list()

  const video = data.find(v => v.uuid === videoUUID)

  if (exists) expect(video).to.not.be.undefined
  else expect(video).to.be.undefined
}

describe('Official plugin auto-block videos', function () {
  let servers: ServerInfo[]
  let blocklistServer: MockBlocklist
  let server1Videos: Video[] = []
  let server2Videos: Video[] = []
  let port: number

  before(async function () {
    this.timeout(60000)

    servers = await flushAndRunMultipleServers(2)
    await setAccessTokensToServers(servers)

    for (const server of servers) {
      await server.plugins.install({ npmName: 'peertube-plugin-auto-block-videos' })
    }

    blocklistServer = new MockBlocklist()
    port = await blocklistServer.initialize()

    await await servers[0].videos.quickUpload({ name: 'video server 1' })
    await await servers[1].videos.quickUpload({ name: 'video server 2' })
    await await servers[1].videos.quickUpload({ name: 'video 2 server 2' })
    await await servers[1].videos.quickUpload({ name: 'video 3 server 2' })

    {
      const { data } = await servers[0].videos.list()
      server1Videos = data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
    }

    {
      const { data } = await servers[1].videos.list()
      server2Videos = data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
    }

    await doubleFollow(servers[0], servers[1])
  })

  it('Should update plugin settings', async function () {
    await servers[0].plugins.updateSettings({
      npmName: 'peertube-plugin-auto-block-videos',
      settings: {
        'blocklist-urls': `http://localhost:${port}/blocklist`,
        'check-seconds-interval': 1
      }
    })
  })

  it('Should auto block a video', async function () {
    this.timeout(10000)

    await check(servers[0], server2Videos[0].uuid, true)

    blocklistServer.replace({
      data: [
        {
          value: server2Videos[0].url
        }
      ]
    })

    await wait(2000)

    await check(servers[0], server2Videos[0].uuid, false)
  })

  it('Should have video in blacklists', async function () {
    const body = await servers[0].blacklist.list()

    const videoBlacklists = body.data
    expect(videoBlacklists).to.have.lengthOf(1)
    expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
    expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
  })

  it('Should not block a local video', async function () {
    this.timeout(10000)

    await check(servers[0], server1Videos[0].uuid, true)

    blocklistServer.replace({
      data: [
        {
          value: server1Videos[0].url
        }
      ]
    })

    await wait(2000)

    await check(servers[0], server1Videos[0].uuid, true)
  })

  it('Should remove a video block', async function () {
    this.timeout(10000)

    await check(servers[0], server2Videos[0].uuid, false)

    blocklistServer.replace({
      data: [
        {
          value: server2Videos[0].url,
          action: 'remove'
        }
      ]
    })

    await wait(2000)

    await check(servers[0], server2Videos[0].uuid, true)
  })

  it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
    this.timeout(20000)

    const video = server2Videos[1]

    await check(servers[0], video.uuid, true)

    blocklistServer.replace({
      data: [
        {
          value: video.url,
          updatedAt: new Date().toISOString()
        }
      ]
    })

    await wait(2000)

    await check(servers[0], video.uuid, false)

    await servers[0].blacklist.remove({ videoId: video.uuid })

    await check(servers[0], video.uuid, true)

    await killallServers([ servers[0] ])
    await reRunServer(servers[0])
    await wait(2000)

    await check(servers[0], video.uuid, true)
  })

  after(async function () {
    await blocklistServer.terminate()

    await cleanupTests(servers)
  })
})