]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/external-plugins/auto-mute.ts
Introduce blocklist command
[github/Chocobozzz/PeerTube.git] / server / tests / external-plugins / auto-mute.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
6 import {
7 cleanupTests,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 getVideosList,
11 killallServers,
12 makeGetRequest,
13 MockBlocklist,
14 reRunServer,
15 ServerInfo,
16 setAccessTokensToServers,
17 uploadVideoAndGetId,
18 wait
19 } from '@shared/extra-utils'
20
21 describe('Official plugin auto-mute', function () {
22 const autoMuteListPath = '/plugins/auto-mute/router/api/v1/mute-list'
23 let servers: ServerInfo[]
24 let blocklistServer: MockBlocklist
25 let port: number
26
27 before(async function () {
28 this.timeout(30000)
29
30 servers = await flushAndRunMultipleServers(2)
31 await setAccessTokensToServers(servers)
32
33 for (const server of servers) {
34 await server.pluginsCommand.install({ npmName: 'peertube-plugin-auto-mute' })
35 }
36
37 blocklistServer = new MockBlocklist()
38 port = await blocklistServer.initialize()
39
40 await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
41 await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
42
43 await doubleFollow(servers[0], servers[1])
44 })
45
46 it('Should update plugin settings', async function () {
47 await servers[0].pluginsCommand.updateSettings({
48 npmName: 'peertube-plugin-auto-mute',
49 settings: {
50 'blocklist-urls': `http://localhost:${port}/blocklist`,
51 'check-seconds-interval': 1
52 }
53 })
54 })
55
56 it('Should add a server blocklist', async function () {
57 this.timeout(10000)
58
59 blocklistServer.replace({
60 data: [
61 {
62 value: 'localhost:' + servers[1].port
63 }
64 ]
65 })
66
67 await wait(2000)
68
69 const res = await getVideosList(servers[0].url)
70 expect(res.body.total).to.equal(1)
71 })
72
73 it('Should remove a server blocklist', async function () {
74 this.timeout(10000)
75
76 blocklistServer.replace({
77 data: [
78 {
79 value: 'localhost:' + servers[1].port,
80 action: 'remove'
81 }
82 ]
83 })
84
85 await wait(2000)
86
87 const res = await getVideosList(servers[0].url)
88 expect(res.body.total).to.equal(2)
89 })
90
91 it('Should add an account blocklist', async function () {
92 this.timeout(10000)
93
94 blocklistServer.replace({
95 data: [
96 {
97 value: 'root@localhost:' + servers[1].port
98 }
99 ]
100 })
101
102 await wait(2000)
103
104 const res = await getVideosList(servers[0].url)
105 expect(res.body.total).to.equal(1)
106 })
107
108 it('Should remove an account blocklist', async function () {
109 this.timeout(10000)
110
111 blocklistServer.replace({
112 data: [
113 {
114 value: 'root@localhost:' + servers[1].port,
115 action: 'remove'
116 }
117 ]
118 })
119
120 await wait(2000)
121
122 const res = await getVideosList(servers[0].url)
123 expect(res.body.total).to.equal(2)
124 })
125
126 it('Should auto mute an account, manually unmute it and do not remute it automatically', async function () {
127 this.timeout(20000)
128
129 const account = 'root@localhost:' + servers[1].port
130
131 blocklistServer.replace({
132 data: [
133 {
134 value: account,
135 updatedAt: new Date().toISOString()
136 }
137 ]
138 })
139
140 await wait(2000)
141
142 {
143 const res = await getVideosList(servers[0].url)
144 expect(res.body.total).to.equal(1)
145 }
146
147 await servers[0].blocklistCommand.removeFromServerBlocklist({ account })
148
149 {
150 const res = await getVideosList(servers[0].url)
151 expect(res.body.total).to.equal(2)
152 }
153
154 killallServers([ servers[0] ])
155 await reRunServer(servers[0])
156 await wait(2000)
157
158 {
159 const res = await getVideosList(servers[0].url)
160 expect(res.body.total).to.equal(2)
161 }
162 })
163
164 it('Should not expose the auto mute list', async function () {
165 await makeGetRequest({
166 url: servers[0].url,
167 path: '/plugins/auto-mute/router/api/v1/mute-list',
168 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
169 })
170 })
171
172 it('Should enable auto mute list', async function () {
173 await servers[0].pluginsCommand.updateSettings({
174 npmName: 'peertube-plugin-auto-mute',
175 settings: {
176 'blocklist-urls': '',
177 'check-seconds-interval': 1,
178 'expose-mute-list': true
179 }
180 })
181
182 await makeGetRequest({
183 url: servers[0].url,
184 path: '/plugins/auto-mute/router/api/v1/mute-list',
185 statusCodeExpected: HttpStatusCode.OK_200
186 })
187 })
188
189 it('Should mute an account on server 1, and server 2 auto mutes it', async function () {
190 this.timeout(20000)
191
192 await servers[1].pluginsCommand.updateSettings({
193 npmName: 'peertube-plugin-auto-mute',
194 settings: {
195 'blocklist-urls': 'http://localhost:' + servers[0].port + autoMuteListPath,
196 'check-seconds-interval': 1,
197 'expose-mute-list': false
198 }
199 })
200
201 await servers[0].blocklistCommand.addToServerBlocklist({ account: 'root@localhost:' + servers[1].port })
202 await servers[0].blocklistCommand.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
203
204 const res = await makeGetRequest({
205 url: servers[0].url,
206 path: '/plugins/auto-mute/router/api/v1/mute-list',
207 statusCodeExpected: HttpStatusCode.OK_200
208 })
209
210 const data = res.body.data
211 expect(data).to.have.lengthOf(1)
212 expect(data[0].updatedAt).to.exist
213 expect(data[0].value).to.equal('root@localhost:' + servers[1].port)
214
215 await wait(2000)
216
217 for (const server of servers) {
218 const res = await getVideosList(server.url)
219 expect(res.body.total).to.equal(1)
220 }
221 })
222
223 after(async function () {
224 await blocklistServer.terminate()
225
226 await cleanupTests(servers)
227 })
228 })