]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/external-plugins/auto-block-videos.ts
Add signup approval API tests
[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 this.timeout(10000)
75
76 await check(servers[0], server2Videos[0].uuid, true)
77
78 blocklistServer.replace({
79 data: [
80 {
81 value: server2Videos[0].url
82 }
83 ]
84 })
85
86 await wait(2000)
87
88 await check(servers[0], server2Videos[0].uuid, false)
89 })
90
91 it('Should have video in blacklists', async function () {
92 const body = await servers[0].blacklist.list()
93
94 const videoBlacklists = body.data
95 expect(videoBlacklists).to.have.lengthOf(1)
96 expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
97 expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
98 })
99
100 it('Should not block a local video', async function () {
101 this.timeout(10000)
102
103 await check(servers[0], server1Videos[0].uuid, true)
104
105 blocklistServer.replace({
106 data: [
107 {
108 value: server1Videos[0].url
109 }
110 ]
111 })
112
113 await wait(2000)
114
115 await check(servers[0], server1Videos[0].uuid, true)
116 })
117
118 it('Should remove a video block', async function () {
119 this.timeout(10000)
120
121 await check(servers[0], server2Videos[0].uuid, false)
122
123 blocklistServer.replace({
124 data: [
125 {
126 value: server2Videos[0].url,
127 action: 'remove'
128 }
129 ]
130 })
131
132 await wait(2000)
133
134 await check(servers[0], server2Videos[0].uuid, true)
135 })
136
137 it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
138 this.timeout(20000)
139
140 const video = server2Videos[1]
141
142 await check(servers[0], video.uuid, true)
143
144 blocklistServer.replace({
145 data: [
146 {
147 value: video.url,
148 updatedAt: new Date().toISOString()
149 }
150 ]
151 })
152
153 await wait(2000)
154
155 await check(servers[0], video.uuid, false)
156
157 await servers[0].blacklist.remove({ videoId: video.uuid })
158
159 await check(servers[0], video.uuid, true)
160
161 await killallServers([ servers[0] ])
162 await servers[0].run()
163 await wait(2000)
164
165 await check(servers[0], video.uuid, true)
166 })
167
168 after(async function () {
169 await blocklistServer.terminate()
170
171 await cleanupTests(servers)
172 })
173 })