1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 flushAndRunMultipleServers,
10 setAccessTokensToServers,
11 updateCustomSubConfig,
14 } from '@shared/extra-utils'
16 const expect = chai.expect
18 async function checkFollow (follower: ServerInfo, following: ServerInfo, exists: boolean) {
20 const body = await following.followsCommand.getFollowers({ start: 0, count: 5, sort: '-createdAt' })
21 const follow = body.data.find(f => f.follower.host === follower.host && f.state === 'accepted')
23 if (exists === true) expect(follow).to.exist
24 else expect(follow).to.be.undefined
28 const body = await follower.followsCommand.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
29 const follow = body.data.find(f => f.following.host === following.host && f.state === 'accepted')
31 if (exists === true) expect(follow).to.exist
32 else expect(follow).to.be.undefined
36 async function server1Follows2 (servers: ServerInfo[]) {
37 await servers[0].followsCommand.follow({ targets: [ servers[1].host ] })
39 await waitJobs(servers)
42 async function resetFollows (servers: ServerInfo[]) {
44 await servers[0].followsCommand.unfollow({ target: servers[1] })
45 await servers[1].followsCommand.unfollow({ target: servers[0] })
49 await waitJobs(servers)
51 await checkFollow(servers[0], servers[1], false)
52 await checkFollow(servers[1], servers[0], false)
55 describe('Test auto follows', function () {
56 let servers: ServerInfo[] = []
58 before(async function () {
61 servers = await flushAndRunMultipleServers(3)
63 // Get the access tokens
64 await setAccessTokensToServers(servers)
67 describe('Auto follow back', function () {
69 it('Should not auto follow back if the option is not enabled', async function () {
72 await server1Follows2(servers)
74 await checkFollow(servers[0], servers[1], true)
75 await checkFollow(servers[1], servers[0], false)
77 await resetFollows(servers)
80 it('Should auto follow back on auto accept if the option is enabled', async function () {
86 autoFollowBack: { enabled: true }
90 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
92 await server1Follows2(servers)
94 await checkFollow(servers[0], servers[1], true)
95 await checkFollow(servers[1], servers[0], true)
97 await resetFollows(servers)
100 it('Should wait the acceptation before auto follow back', async function () {
106 autoFollowBack: { enabled: true }
115 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
117 await server1Follows2(servers)
119 await checkFollow(servers[0], servers[1], false)
120 await checkFollow(servers[1], servers[0], false)
122 await servers[1].followsCommand.acceptFollower({ follower: 'peertube@' + servers[0].host })
123 await waitJobs(servers)
125 await checkFollow(servers[0], servers[1], true)
126 await checkFollow(servers[1], servers[0], true)
128 await resetFollows(servers)
130 config.followings.instance.autoFollowBack.enabled = false
131 config.followers.instance.manualApproval = false
132 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
136 describe('Auto follow index', function () {
137 const instanceIndexServer = new MockInstancesIndex()
141 port = await instanceIndexServer.initialize()
144 it('Should not auto follow index if the option is not enabled', async function () {
148 await waitJobs(servers)
150 await checkFollow(servers[0], servers[1], false)
151 await checkFollow(servers[1], servers[0], false)
154 it('Should auto follow the index', async function () {
157 instanceIndexServer.addInstance(servers[1].host)
163 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
169 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
172 await waitJobs(servers)
174 await checkFollow(servers[0], servers[1], true)
176 await resetFollows(servers)
179 it('Should follow new added instances in the index but not old ones', async function () {
182 instanceIndexServer.addInstance(servers[2].host)
185 await waitJobs(servers)
187 await checkFollow(servers[0], servers[1], false)
188 await checkFollow(servers[0], servers[2], true)
192 after(async function () {
193 await cleanupTests(servers)