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