]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/auto-follows.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / auto-follows.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
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'
7
8 const expect = chai.expect
9
10 async function checkFollow (follower: PeerTubeServer, following: PeerTubeServer, exists: boolean) {
11 {
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')
14
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
17 }
18
19 {
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')
22
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
25 }
26 }
27
28 async function server1Follows2 (servers: PeerTubeServer[]) {
29 await servers[0].follows.follow({ hosts: [ servers[1].host ] })
30
31 await waitJobs(servers)
32 }
33
34 async function resetFollows (servers: PeerTubeServer[]) {
35 try {
36 await servers[0].follows.unfollow({ target: servers[1] })
37 await servers[1].follows.unfollow({ target: servers[0] })
38 } catch { /* empty */
39 }
40
41 await waitJobs(servers)
42
43 await checkFollow(servers[0], servers[1], false)
44 await checkFollow(servers[1], servers[0], false)
45 }
46
47 describe('Test auto follows', function () {
48 let servers: PeerTubeServer[] = []
49
50 before(async function () {
51 this.timeout(120000)
52
53 servers = await createMultipleServers(3)
54
55 // Get the access tokens
56 await setAccessTokensToServers(servers)
57 })
58
59 describe('Auto follow back', function () {
60
61 it('Should not auto follow back if the option is not enabled', async function () {
62 this.timeout(15000)
63
64 await server1Follows2(servers)
65
66 await checkFollow(servers[0], servers[1], true)
67 await checkFollow(servers[1], servers[0], false)
68
69 await resetFollows(servers)
70 })
71
72 it('Should auto follow back on auto accept if the option is enabled', async function () {
73 this.timeout(15000)
74
75 const config = {
76 followings: {
77 instance: {
78 autoFollowBack: { enabled: true }
79 }
80 }
81 }
82 await servers[1].config.updateCustomSubConfig({ newConfig: config })
83
84 await server1Follows2(servers)
85
86 await checkFollow(servers[0], servers[1], true)
87 await checkFollow(servers[1], servers[0], true)
88
89 await resetFollows(servers)
90 })
91
92 it('Should wait the acceptation before auto follow back', async function () {
93 this.timeout(30000)
94
95 const config = {
96 followings: {
97 instance: {
98 autoFollowBack: { enabled: true }
99 }
100 },
101 followers: {
102 instance: {
103 manualApproval: true
104 }
105 }
106 }
107 await servers[1].config.updateCustomSubConfig({ newConfig: config })
108
109 await server1Follows2(servers)
110
111 await checkFollow(servers[0], servers[1], false)
112 await checkFollow(servers[1], servers[0], false)
113
114 await servers[1].follows.acceptFollower({ follower: 'peertube@' + servers[0].host })
115 await waitJobs(servers)
116
117 await checkFollow(servers[0], servers[1], true)
118 await checkFollow(servers[1], servers[0], true)
119
120 await resetFollows(servers)
121
122 config.followings.instance.autoFollowBack.enabled = false
123 config.followers.instance.manualApproval = false
124 await servers[1].config.updateCustomSubConfig({ newConfig: config })
125 })
126 })
127
128 describe('Auto follow index', function () {
129 const instanceIndexServer = new MockInstancesIndex()
130 let port: number
131
132 before(async () => {
133 port = await instanceIndexServer.initialize()
134 })
135
136 it('Should not auto follow index if the option is not enabled', async function () {
137 this.timeout(30000)
138
139 await wait(5000)
140 await waitJobs(servers)
141
142 await checkFollow(servers[0], servers[1], false)
143 await checkFollow(servers[1], servers[0], false)
144 })
145
146 it('Should auto follow the index', async function () {
147 this.timeout(30000)
148
149 instanceIndexServer.addInstance(servers[1].host)
150
151 const config = {
152 followings: {
153 instance: {
154 autoFollowIndex: {
155 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
156 enabled: true
157 }
158 }
159 }
160 }
161 await servers[0].config.updateCustomSubConfig({ newConfig: config })
162
163 await wait(5000)
164 await waitJobs(servers)
165
166 await checkFollow(servers[0], servers[1], true)
167
168 await resetFollows(servers)
169 })
170
171 it('Should follow new added instances in the index but not old ones', async function () {
172 this.timeout(30000)
173
174 instanceIndexServer.addInstance(servers[2].host)
175
176 await wait(5000)
177 await waitJobs(servers)
178
179 await checkFollow(servers[0], servers[1], false)
180 await checkFollow(servers[0], servers[2], true)
181 })
182
183 after(async function () {
184 await instanceIndexServer.terminate()
185 })
186 })
187
188 after(async function () {
189 await cleanupTests(servers)
190 })
191 })