1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { MockInstancesIndex } from '@server/tests/shared'
6 import { wait } from '@shared/core-utils'
7 import { cleanupTests, createMultipleServers, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
9 const expect = chai.expect
11 async function checkFollow (follower: PeerTubeServer, following: PeerTubeServer, exists: boolean) {
13 const body = await following.follows.getFollowers({ start: 0, count: 5, sort: '-createdAt' })
14 const follow = body.data.find(f => f.follower.host === follower.host && f.state === 'accepted')
16 if (exists === true) expect(follow, `Follower ${follower.url} should exist on ${following.url}`).to.exist
17 else expect(follow, `Follower ${follower.url} should not exist on ${following.url}`).to.be.undefined
21 const body = await follower.follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
22 const follow = body.data.find(f => f.following.host === following.host && f.state === 'accepted')
24 if (exists === true) expect(follow, `Following ${following.url} should exist on ${follower.url}`).to.exist
25 else expect(follow, `Following ${following.url} should not exist on ${follower.url}`).to.be.undefined
29 async function server1Follows2 (servers: PeerTubeServer[]) {
30 await servers[0].follows.follow({ hosts: [ servers[1].host ] })
32 await waitJobs(servers)
35 async function resetFollows (servers: PeerTubeServer[]) {
37 await servers[0].follows.unfollow({ target: servers[1] })
38 await servers[1].follows.unfollow({ target: servers[0] })
42 await waitJobs(servers)
44 await checkFollow(servers[0], servers[1], false)
45 await checkFollow(servers[1], servers[0], false)
48 describe('Test auto follows', function () {
49 let servers: PeerTubeServer[] = []
51 before(async function () {
54 servers = await createMultipleServers(3)
56 // Get the access tokens
57 await setAccessTokensToServers(servers)
60 describe('Auto follow back', function () {
62 it('Should not auto follow back if the option is not enabled', async function () {
65 await server1Follows2(servers)
67 await checkFollow(servers[0], servers[1], true)
68 await checkFollow(servers[1], servers[0], false)
70 await resetFollows(servers)
73 it('Should auto follow back on auto accept if the option is enabled', async function () {
79 autoFollowBack: { enabled: true }
83 await servers[1].config.updateCustomSubConfig({ newConfig: config })
85 await server1Follows2(servers)
87 await checkFollow(servers[0], servers[1], true)
88 await checkFollow(servers[1], servers[0], true)
90 await resetFollows(servers)
93 it('Should wait the acceptation before auto follow back', async function () {
99 autoFollowBack: { enabled: true }
108 await servers[1].config.updateCustomSubConfig({ newConfig: config })
110 await server1Follows2(servers)
112 await checkFollow(servers[0], servers[1], false)
113 await checkFollow(servers[1], servers[0], false)
115 await servers[1].follows.acceptFollower({ follower: 'peertube@' + servers[0].host })
116 await waitJobs(servers)
118 await checkFollow(servers[0], servers[1], true)
119 await checkFollow(servers[1], servers[0], true)
121 await resetFollows(servers)
123 config.followings.instance.autoFollowBack.enabled = false
124 config.followers.instance.manualApproval = false
125 await servers[1].config.updateCustomSubConfig({ newConfig: config })
129 describe('Auto follow index', function () {
130 const instanceIndexServer = new MockInstancesIndex()
134 port = await instanceIndexServer.initialize()
137 it('Should not auto follow index if the option is not enabled', async function () {
141 await waitJobs(servers)
143 await checkFollow(servers[0], servers[1], false)
144 await checkFollow(servers[1], servers[0], false)
147 it('Should auto follow the index', async function () {
150 instanceIndexServer.addInstance(servers[1].host)
156 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
162 await servers[0].config.updateCustomSubConfig({ newConfig: config })
165 await waitJobs(servers)
167 await checkFollow(servers[0], servers[1], true)
169 await resetFollows(servers)
172 it('Should follow new added instances in the index but not old ones', async function () {
175 instanceIndexServer.addInstance(servers[2].host)
178 await waitJobs(servers)
180 await checkFollow(servers[0], servers[1], false)
181 await checkFollow(servers[0], servers[2], true)
184 after(async function () {
185 await instanceIndexServer.terminate()
189 after(async function () {
190 await cleanupTests(servers)