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