1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { expectStartWith } from '@server/tests/shared'
5 import { ActorFollow, FollowState } from '@shared/models'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
17 servers[0].follows.getFollowings.bind(servers[0].follows),
18 servers[1].follows.getFollowers.bind(servers[1].follows)
21 for (const fn of fns) {
22 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
23 expect(body.total).to.equal(1)
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')
32 async function checkFollows (options: {
33 follower: PeerTubeServer
34 followerState: FollowState | 'deleted'
36 following: PeerTubeServer
37 followingState: FollowState | 'deleted'
39 const { follower, followerState, followingState, following } = options
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
46 const { data } = await follower.follows.getFollowings()
47 const follow = data.find(finder)
49 if (followerState === 'deleted') {
50 expect(follow).to.not.exist
52 expect(follow.state).to.equal(followerState)
53 expect(follow.follower.url).to.equal(followerUrl)
54 expect(follow.following.url).to.equal(followingUrl)
59 const { data } = await following.follows.getFollowers()
60 const follow = data.find(finder)
62 if (followingState === 'deleted') {
63 expect(follow).to.not.exist
65 expect(follow.state).to.equal(followingState)
66 expect(follow.follower.url).to.equal(followerUrl)
67 expect(follow.following.url).to.equal(followingUrl)
72 async function checkNoFollowers (servers: PeerTubeServer[]) {
74 servers[0].follows.getFollowings.bind(servers[0].follows),
75 servers[1].follows.getFollowers.bind(servers[1].follows)
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)
84 describe('Test follows moderation', function () {
85 let servers: PeerTubeServer[] = []
86 let commands: FollowsCommand[]
88 before(async function () {
91 servers = await createMultipleServers(3)
93 // Get the access tokens
94 await setAccessTokensToServers(servers)
96 commands = servers.map(s => s.follows)
99 describe('Default behaviour', function () {
101 it('Should have server 1 following server 2', async function () {
104 await commands[0].follow({ hosts: [ servers[1].url ] })
106 await waitJobs(servers)
109 it('Should have correct follows', async function () {
110 await checkServer1And2HasFollowers(servers)
113 it('Should remove follower on server 2', async function () {
116 await commands[1].removeFollower({ follower: servers[0] })
118 await waitJobs(servers)
121 it('Should not not have follows anymore', async function () {
122 await checkNoFollowers(servers)
126 describe('Disabled/Enabled followers', function () {
128 it('Should disable followers on server 2', async function () {
135 manualApproval: false
140 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
142 await commands[0].follow({ hosts: [ servers[1].url ] })
143 await waitJobs(servers)
145 await checkNoFollowers(servers)
148 it('Should re enable followers on server 2', async function () {
155 manualApproval: false
160 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
162 await commands[0].follow({ hosts: [ servers[1].url ] })
163 await waitJobs(servers)
165 await checkServer1And2HasFollowers(servers)
169 describe('Manual approbation', function () {
171 it('Should manually approve followers', async function () {
174 await commands[0].unfollow({ target: servers[1] })
175 await waitJobs(servers)
186 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
187 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
189 await commands[0].follow({ hosts: [ servers[1].url ] })
190 await waitJobs(servers)
192 await checkServer1And2HasFollowers(servers, 'pending')
195 it('Should accept a follower', async function () {
198 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
199 await waitJobs(servers)
201 await checkServer1And2HasFollowers(servers)
204 it('Should reject another follower', async function () {
207 await commands[0].follow({ hosts: [ servers[2].url ] })
208 await waitJobs(servers)
211 const body = await commands[0].getFollowings()
212 expect(body.total).to.equal(2)
216 const body = await commands[1].getFollowers()
217 expect(body.total).to.equal(1)
221 const body = await commands[2].getFollowers()
222 expect(body.total).to.equal(1)
225 await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
226 await waitJobs(servers)
230 const { data } = await commands[0].getFollowings({ state: 'accepted' })
231 expect(data).to.have.lengthOf(1)
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)
243 const { data } = await commands[2].getFollowers({ state: 'accepted' })
244 expect(data).to.have.lengthOf(0)
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)
255 it('Should still auto accept channel followers', async function () {
256 await commands[0].follow({ handles: [ 'root_channel@' + servers[1].host ] })
258 await waitJobs(servers)
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')
267 describe('Accept/reject state', function () {
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)
275 follower: servers[0],
276 followerState: 'rejected',
277 following: servers[2],
278 followingState: 'rejected'
282 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
285 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
289 it('Should not change the rejected status on unfollow', async function () {
290 await commands[0].unfollow({ target: servers[2] })
291 await waitJobs(servers)
294 follower: servers[0],
295 followerState: 'deleted',
296 following: servers[2],
297 followingState: 'rejected'
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)
305 await commands[0].follow({ hosts: [ servers[2].url ] })
306 await waitJobs(servers)
309 follower: servers[0],
310 followerState: 'pending',
311 following: servers[2],
312 followingState: 'pending'
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)
321 follower: servers[0],
322 followerState: 'rejected',
323 following: servers[1],
324 followingState: 'rejected'
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)
333 follower: servers[0],
334 followerState: 'accepted',
335 following: servers[1],
336 followingState: 'accepted'
341 describe('Muted servers', function () {
343 it('Should ignore follow requests of muted servers', async function () {
344 await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host })
346 await commands[0].unfollow({ target: servers[1] })
348 await waitJobs(servers)
351 follower: servers[0],
352 followerState: 'deleted',
353 following: servers[1],
354 followingState: 'deleted'
357 await commands[0].follow({ hosts: [ servers[1].host ] })
358 await waitJobs(servers)
361 follower: servers[0],
362 followerState: 'rejected',
363 following: servers[1],
364 followingState: 'deleted'
369 after(async function () {
370 await cleanupTests(servers)