]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/external-plugins/auto-block-videos.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / tests / external-plugins / auto-block-videos.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { Video } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 killallServers,
11 PeerTubeServer,
12 setAccessTokensToServers
13 } from '@shared/server-commands'
14 import { MockBlocklist } from '../shared'
15
16 async function check (server: PeerTubeServer, videoUUID: string, exists = true) {
17 const { data } = await server.videos.list()
18
19 const video = data.find(v => v.uuid === videoUUID)
20
21 if (exists) expect(video).to.not.be.undefined
22 else expect(video).to.be.undefined
23 }
24
25 describe('Official plugin auto-block videos', function () {
26 let servers: PeerTubeServer[]
27 let blocklistServer: MockBlocklist
28 let server1Videos: Video[] = []
29 let server2Videos: Video[] = []
30 let port: number
31
32 before(async function () {
33 this.timeout(120000)
34
35 servers = await createMultipleServers(2)
36 await setAccessTokensToServers(servers)
37
38 for (const server of servers) {
39 await server.plugins.install({ npmName: 'peertube-plugin-auto-block-videos' })
40 }
41
42 blocklistServer = new MockBlocklist()
43 port = await blocklistServer.initialize()
44
45 await servers[0].videos.quickUpload({ name: 'video server 1' })
46 await servers[1].videos.quickUpload({ name: 'video server 2' })
47 await servers[1].videos.quickUpload({ name: 'video 2 server 2' })
48 await servers[1].videos.quickUpload({ name: 'video 3 server 2' })
49
50 {
51 const { data } = await servers[0].videos.list()
52 server1Videos = data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
53 }
54
55 {
56 const { data } = await servers[1].videos.list()
57 server2Videos = data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
58 }
59
60 await doubleFollow(servers[0], servers[1])
61 })
62
63 it('Should update plugin settings', async function () {
64 await servers[0].plugins.updateSettings({
65 npmName: 'peertube-plugin-auto-block-videos',
66 settings: {
67 'blocklist-urls': `http://127.0.0.1:${port}/blocklist`,
68 'check-seconds-interval': 1
69 }
70 })
71 })
72
73 it('Should auto block a video', async function () {
74 await check(servers[0], server2Videos[0].uuid, true)
75
76 blocklistServer.replace({
77 data: [
78 {
79 value: server2Videos[0].url
80 }
81 ]
82 })
83
84 await wait(2000)
85
86 await check(servers[0], server2Videos[0].uuid, false)
87 })
88
89 it('Should have video in blacklists', async function () {
90 const body = await servers[0].blacklist.list()
91
92 const videoBlacklists = body.data
93 expect(videoBlacklists).to.have.lengthOf(1)
94 expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
95 expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
96 })
97
98 it('Should not block a local video', async function () {
99 await check(servers[0], server1Videos[0].uuid, true)
100
101 blocklistServer.replace({
102 data: [
103 {
104 value: server1Videos[0].url
105 }
106 ]
107 })
108
109 await wait(2000)
110
111 await check(servers[0], server1Videos[0].uuid, true)
112 })
113
114 it('Should remove a video block', async function () {
115 await check(servers[0], server2Videos[0].uuid, false)
116
117 blocklistServer.replace({
118 data: [
119 {
120 value: server2Videos[0].url,
121 action: 'remove'
122 }
123 ]
124 })
125
126 await wait(2000)
127
128 await check(servers[0], server2Videos[0].uuid, true)
129 })
130
131 it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
132 this.timeout(20000)
133
134 const video = server2Videos[1]
135
136 await check(servers[0], video.uuid, true)
137
138 blocklistServer.replace({
139 data: [
140 {
141 value: video.url,
142 updatedAt: new Date().toISOString()
143 }
144 ]
145 })
146
147 await wait(2000)
148
149 await check(servers[0], video.uuid, false)
150
151 await servers[0].blacklist.remove({ videoId: video.uuid })
152
153 await check(servers[0], video.uuid, true)
154
155 await killallServers([ servers[0] ])
156 await servers[0].run()
157 await wait(2000)
158
159 await check(servers[0], video.uuid, true)
160 })
161
162 after(async function () {
163 await blocklistServer.terminate()
164
165 await cleanupTests(servers)
166 })
167 })