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