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