1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { MockInstancesIndex } from '@server/tests/shared'
5 import { wait } from '@shared/core-utils'
6 import { cleanupTests, createMultipleServers, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
8 const expect = chai.expect
10 async function checkFollow (follower: PeerTubeServer, following: PeerTubeServer, exists: boolean) {
12 const body = await following.follows.getFollowers({ start: 0, count: 5, sort: '-createdAt' })
13 const follow = body.data.find(f => f.follower.host === follower.host && f.state === 'accepted')
15 if (exists === true) expect(follow, `Follower ${follower.url} should exist on ${following.url}`).to.exist
16 else expect(follow, `Follower ${follower.url} should not exist on ${following.url}`).to.be.undefined
20 const body = await follower.follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
21 const follow = body.data.find(f => f.following.host === following.host && f.state === 'accepted')
23 if (exists === true) expect(follow, `Following ${following.url} should exist on ${follower.url}`).to.exist
24 else expect(follow, `Following ${following.url} should not exist on ${follower.url}`).to.be.undefined
28 async function server1Follows2 (servers: PeerTubeServer[]) {
29 await servers[0].follows.follow({ hosts: [ servers[1].host ] })
31 await waitJobs(servers)
34 async function resetFollows (servers: PeerTubeServer[]) {
36 await servers[0].follows.unfollow({ target: servers[1] })
37 await servers[1].follows.unfollow({ target: servers[0] })
41 await waitJobs(servers)
43 await checkFollow(servers[0], servers[1], false)
44 await checkFollow(servers[1], servers[0], false)
47 describe('Test auto follows', function () {
48 let servers: PeerTubeServer[] = []
50 before(async function () {
53 servers = await createMultipleServers(3)
55 // Get the access tokens
56 await setAccessTokensToServers(servers)
59 describe('Auto follow back', function () {
61 it('Should not auto follow back if the option is not enabled', async function () {
64 await server1Follows2(servers)
66 await checkFollow(servers[0], servers[1], true)
67 await checkFollow(servers[1], servers[0], false)
69 await resetFollows(servers)
72 it('Should auto follow back on auto accept if the option is enabled', async function () {
78 autoFollowBack: { enabled: true }
82 await servers[1].config.updateCustomSubConfig({ newConfig: config })
84 await server1Follows2(servers)
86 await checkFollow(servers[0], servers[1], true)
87 await checkFollow(servers[1], servers[0], true)
89 await resetFollows(servers)
92 it('Should wait the acceptation before auto follow back', async function () {
98 autoFollowBack: { enabled: true }
107 await servers[1].config.updateCustomSubConfig({ newConfig: config })
109 await server1Follows2(servers)
111 await checkFollow(servers[0], servers[1], false)
112 await checkFollow(servers[1], servers[0], false)
114 await servers[1].follows.acceptFollower({ follower: 'peertube@' + servers[0].host })
115 await waitJobs(servers)
117 await checkFollow(servers[0], servers[1], true)
118 await checkFollow(servers[1], servers[0], true)
120 await resetFollows(servers)
122 config.followings.instance.autoFollowBack.enabled = false
123 config.followers.instance.manualApproval = false
124 await servers[1].config.updateCustomSubConfig({ newConfig: config })
128 describe('Auto follow index', function () {
129 const instanceIndexServer = new MockInstancesIndex()
133 port = await instanceIndexServer.initialize()
136 it('Should not auto follow index if the option is not enabled', async function () {
140 await waitJobs(servers)
142 await checkFollow(servers[0], servers[1], false)
143 await checkFollow(servers[1], servers[0], false)
146 it('Should auto follow the index', async function () {
149 instanceIndexServer.addInstance(servers[1].host)
155 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
161 await servers[0].config.updateCustomSubConfig({ newConfig: config })
164 await waitJobs(servers)
166 await checkFollow(servers[0], servers[1], true)
168 await resetFollows(servers)
171 it('Should follow new added instances in the index but not old ones', async function () {
174 instanceIndexServer.addInstance(servers[2].host)
177 await waitJobs(servers)
179 await checkFollow(servers[0], servers[1], false)
180 await checkFollow(servers[0], servers[2], true)
183 after(async function () {
184 await instanceIndexServer.terminate()
188 after(async function () {
189 await cleanupTests(servers)