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