1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } 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 async function checkFollow (follower: PeerTubeServer, following: PeerTubeServer, exists: boolean) {
10 const body = await following.follows.getFollowers({ start: 0, count: 5, sort: '-createdAt' })
11 const follow = body.data.find(f => f.follower.host === follower.host && f.state === 'accepted')
13 if (exists === true) expect(follow, `Follower ${follower.url} should exist on ${following.url}`).to.exist
14 else expect(follow, `Follower ${follower.url} should not exist on ${following.url}`).to.be.undefined
18 const body = await follower.follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
19 const follow = body.data.find(f => f.following.host === following.host && f.state === 'accepted')
21 if (exists === true) expect(follow, `Following ${following.url} should exist on ${follower.url}`).to.exist
22 else expect(follow, `Following ${following.url} should not exist on ${follower.url}`).to.be.undefined
26 async function server1Follows2 (servers: PeerTubeServer[]) {
27 await servers[0].follows.follow({ hosts: [ servers[1].host ] })
29 await waitJobs(servers)
32 async function resetFollows (servers: PeerTubeServer[]) {
34 await servers[0].follows.unfollow({ target: servers[1] })
35 await servers[1].follows.unfollow({ target: servers[0] })
39 await waitJobs(servers)
41 await checkFollow(servers[0], servers[1], false)
42 await checkFollow(servers[1], servers[0], false)
45 describe('Test auto follows', function () {
46 let servers: PeerTubeServer[] = []
48 before(async function () {
51 servers = await createMultipleServers(3)
53 // Get the access tokens
54 await setAccessTokensToServers(servers)
57 describe('Auto follow back', function () {
59 it('Should not auto follow back if the option is not enabled', async function () {
62 await server1Follows2(servers)
64 await checkFollow(servers[0], servers[1], true)
65 await checkFollow(servers[1], servers[0], false)
67 await resetFollows(servers)
70 it('Should auto follow back on auto accept if the option is enabled', async function () {
76 autoFollowBack: { enabled: true }
80 await servers[1].config.updateCustomSubConfig({ newConfig: config })
82 await server1Follows2(servers)
84 await checkFollow(servers[0], servers[1], true)
85 await checkFollow(servers[1], servers[0], true)
87 await resetFollows(servers)
90 it('Should wait the acceptation before auto follow back', async function () {
96 autoFollowBack: { enabled: true }
105 await servers[1].config.updateCustomSubConfig({ newConfig: config })
107 await server1Follows2(servers)
109 await checkFollow(servers[0], servers[1], false)
110 await checkFollow(servers[1], servers[0], false)
112 await servers[1].follows.acceptFollower({ follower: 'peertube@' + servers[0].host })
113 await waitJobs(servers)
115 await checkFollow(servers[0], servers[1], true)
116 await checkFollow(servers[1], servers[0], true)
118 await resetFollows(servers)
120 config.followings.instance.autoFollowBack.enabled = false
121 config.followers.instance.manualApproval = false
122 await servers[1].config.updateCustomSubConfig({ newConfig: config })
126 describe('Auto follow index', function () {
127 const instanceIndexServer = new MockInstancesIndex()
130 before(async function () {
131 port = await instanceIndexServer.initialize()
134 it('Should not auto follow index if the option is not enabled', async function () {
138 await waitJobs(servers)
140 await checkFollow(servers[0], servers[1], false)
141 await checkFollow(servers[1], servers[0], false)
144 it('Should auto follow the index', async function () {
147 instanceIndexServer.addInstance(servers[1].host)
153 indexUrl: `http://127.0.0.1:${port}/api/v1/instances/hosts`,
159 await servers[0].config.updateCustomSubConfig({ newConfig: config })
162 await waitJobs(servers)
164 await checkFollow(servers[0], servers[1], true)
166 await resetFollows(servers)
169 it('Should follow new added instances in the index but not old ones', async function () {
172 instanceIndexServer.addInstance(servers[2].host)
175 await waitJobs(servers)
177 await checkFollow(servers[0], servers[1], false)
178 await checkFollow(servers[0], servers[2], true)
181 after(async function () {
182 await instanceIndexServer.terminate()
186 after(async function () {
187 await cleanupTests(servers)