]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/follows-moderation.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows-moderation.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { expectStartWith } from '@server/tests/shared'
5 import { ActorFollow, FollowState } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 FollowsCommand,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 waitJobs
13 } from '@shared/server-commands'
14
15 async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
16 const fns = [
17 servers[0].follows.getFollowings.bind(servers[0].follows),
18 servers[1].follows.getFollowers.bind(servers[1].follows)
19 ]
20
21 for (const fn of fns) {
22 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
23 expect(body.total).to.equal(1)
24
25 const follow = body.data[0]
26 expect(follow.state).to.equal(state)
27 expect(follow.follower.url).to.equal(servers[0].url + '/accounts/peertube')
28 expect(follow.following.url).to.equal(servers[1].url + '/accounts/peertube')
29 }
30 }
31
32 async function checkFollows (options: {
33 follower: PeerTubeServer
34 followerState: FollowState | 'deleted'
35
36 following: PeerTubeServer
37 followingState: FollowState | 'deleted'
38 }) {
39 const { follower, followerState, followingState, following } = options
40
41 const followerUrl = follower.url + '/accounts/peertube'
42 const followingUrl = following.url + '/accounts/peertube'
43 const finder = (d: ActorFollow) => d.follower.url === followerUrl && d.following.url === followingUrl
44
45 {
46 const { data } = await follower.follows.getFollowings()
47 const follow = data.find(finder)
48
49 if (followerState === 'deleted') {
50 expect(follow).to.not.exist
51 } else {
52 expect(follow.state).to.equal(followerState)
53 expect(follow.follower.url).to.equal(followerUrl)
54 expect(follow.following.url).to.equal(followingUrl)
55 }
56 }
57
58 {
59 const { data } = await following.follows.getFollowers()
60 const follow = data.find(finder)
61
62 if (followingState === 'deleted') {
63 expect(follow).to.not.exist
64 } else {
65 expect(follow.state).to.equal(followingState)
66 expect(follow.follower.url).to.equal(followerUrl)
67 expect(follow.following.url).to.equal(followingUrl)
68 }
69 }
70 }
71
72 async function checkNoFollowers (servers: PeerTubeServer[]) {
73 const fns = [
74 servers[0].follows.getFollowings.bind(servers[0].follows),
75 servers[1].follows.getFollowers.bind(servers[1].follows)
76 ]
77
78 for (const fn of fns) {
79 const body = await fn({ start: 0, count: 5, sort: 'createdAt', state: 'accepted' })
80 expect(body.total).to.equal(0)
81 }
82 }
83
84 describe('Test follows moderation', function () {
85 let servers: PeerTubeServer[] = []
86 let commands: FollowsCommand[]
87
88 before(async function () {
89 this.timeout(240000)
90
91 servers = await createMultipleServers(3)
92
93 // Get the access tokens
94 await setAccessTokensToServers(servers)
95
96 commands = servers.map(s => s.follows)
97 })
98
99 describe('Default behaviour', function () {
100
101 it('Should have server 1 following server 2', async function () {
102 this.timeout(30000)
103
104 await commands[0].follow({ hosts: [ servers[1].url ] })
105
106 await waitJobs(servers)
107 })
108
109 it('Should have correct follows', async function () {
110 await checkServer1And2HasFollowers(servers)
111 })
112
113 it('Should remove follower on server 2', async function () {
114 this.timeout(10000)
115
116 await commands[1].removeFollower({ follower: servers[0] })
117
118 await waitJobs(servers)
119 })
120
121 it('Should not not have follows anymore', async function () {
122 await checkNoFollowers(servers)
123 })
124 })
125
126 describe('Disabled/Enabled followers', function () {
127
128 it('Should disable followers on server 2', async function () {
129 this.timeout(10000)
130
131 const subConfig = {
132 followers: {
133 instance: {
134 enabled: false,
135 manualApproval: false
136 }
137 }
138 }
139
140 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
141
142 await commands[0].follow({ hosts: [ servers[1].url ] })
143 await waitJobs(servers)
144
145 await checkNoFollowers(servers)
146 })
147
148 it('Should re enable followers on server 2', async function () {
149 this.timeout(10000)
150
151 const subConfig = {
152 followers: {
153 instance: {
154 enabled: true,
155 manualApproval: false
156 }
157 }
158 }
159
160 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
161
162 await commands[0].follow({ hosts: [ servers[1].url ] })
163 await waitJobs(servers)
164
165 await checkServer1And2HasFollowers(servers)
166 })
167 })
168
169 describe('Manual approbation', function () {
170
171 it('Should manually approve followers', async function () {
172 this.timeout(20000)
173
174 await commands[0].unfollow({ target: servers[1] })
175 await waitJobs(servers)
176
177 const subConfig = {
178 followers: {
179 instance: {
180 enabled: true,
181 manualApproval: true
182 }
183 }
184 }
185
186 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
187 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
188
189 await commands[0].follow({ hosts: [ servers[1].url ] })
190 await waitJobs(servers)
191
192 await checkServer1And2HasFollowers(servers, 'pending')
193 })
194
195 it('Should accept a follower', async function () {
196 this.timeout(10000)
197
198 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
199 await waitJobs(servers)
200
201 await checkServer1And2HasFollowers(servers)
202 })
203
204 it('Should reject another follower', async function () {
205 this.timeout(20000)
206
207 await commands[0].follow({ hosts: [ servers[2].url ] })
208 await waitJobs(servers)
209
210 {
211 const body = await commands[0].getFollowings()
212 expect(body.total).to.equal(2)
213 }
214
215 {
216 const body = await commands[1].getFollowers()
217 expect(body.total).to.equal(1)
218 }
219
220 {
221 const body = await commands[2].getFollowers()
222 expect(body.total).to.equal(1)
223 }
224
225 await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
226 await waitJobs(servers)
227
228 { // server 1
229 {
230 const { data } = await commands[0].getFollowings({ state: 'accepted' })
231 expect(data).to.have.lengthOf(1)
232 }
233
234 {
235 const { data } = await commands[0].getFollowings({ state: 'rejected' })
236 expect(data).to.have.lengthOf(1)
237 expectStartWith(data[0].following.url, servers[2].url)
238 }
239 }
240
241 { // server 3
242 {
243 const { data } = await commands[2].getFollowers({ state: 'accepted' })
244 expect(data).to.have.lengthOf(0)
245 }
246
247 {
248 const { data } = await commands[2].getFollowers({ state: 'rejected' })
249 expect(data).to.have.lengthOf(1)
250 expectStartWith(data[0].follower.url, servers[0].url)
251 }
252 }
253 })
254
255 it('Should still auto accept channel followers', async function () {
256 await commands[0].follow({ handles: [ 'root_channel@' + servers[1].host ] })
257
258 await waitJobs(servers)
259
260 const body = await commands[0].getFollowings()
261 const follow = body.data[0]
262 expect(follow.following.name).to.equal('root_channel')
263 expect(follow.state).to.equal('accepted')
264 })
265 })
266
267 describe('Accept/reject state', function () {
268
269 it('Should not change the follow on refollow with and without auto accept', async function () {
270 const run = async () => {
271 await commands[0].follow({ hosts: [ servers[2].url ] })
272 await waitJobs(servers)
273
274 await checkFollows({
275 follower: servers[0],
276 followerState: 'rejected',
277 following: servers[2],
278 followingState: 'rejected'
279 })
280 }
281
282 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
283 await run()
284
285 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
286 await run()
287 })
288
289 it('Should not change the rejected status on unfollow', async function () {
290 await commands[0].unfollow({ target: servers[2] })
291 await waitJobs(servers)
292
293 await checkFollows({
294 follower: servers[0],
295 followerState: 'deleted',
296 following: servers[2],
297 followingState: 'rejected'
298 })
299 })
300
301 it('Should delete the follower and add again the follower', async function () {
302 await commands[2].removeFollower({ follower: servers[0] })
303 await waitJobs(servers)
304
305 await commands[0].follow({ hosts: [ servers[2].url ] })
306 await waitJobs(servers)
307
308 await checkFollows({
309 follower: servers[0],
310 followerState: 'pending',
311 following: servers[2],
312 followingState: 'pending'
313 })
314 })
315
316 it('Should be able to reject a previously accepted follower', async function () {
317 await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host })
318 await waitJobs(servers)
319
320 await checkFollows({
321 follower: servers[0],
322 followerState: 'rejected',
323 following: servers[1],
324 followingState: 'rejected'
325 })
326 })
327
328 it('Should be able to re accept a previously rejected follower', async function () {
329 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
330 await waitJobs(servers)
331
332 await checkFollows({
333 follower: servers[0],
334 followerState: 'accepted',
335 following: servers[1],
336 followingState: 'accepted'
337 })
338 })
339 })
340
341 describe('Muted servers', function () {
342
343 it('Should ignore follow requests of muted servers', async function () {
344 await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host })
345
346 await commands[0].unfollow({ target: servers[1] })
347
348 await waitJobs(servers)
349
350 await checkFollows({
351 follower: servers[0],
352 followerState: 'deleted',
353 following: servers[1],
354 followingState: 'deleted'
355 })
356
357 await commands[0].follow({ hosts: [ servers[1].host ] })
358 await waitJobs(servers)
359
360 await checkFollows({
361 follower: servers[0],
362 followerState: 'rejected',
363 following: servers[1],
364 followingState: 'deleted'
365 })
366 })
367 })
368
369 after(async function () {
370 await cleanupTests(servers)
371 })
372 })