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