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