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