]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/auto-follows.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / auto-follows.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
8424c402 2
8424c402 3import 'mocha'
f6500729 4import * as chai from 'chai'
c55e3d72
C
5import { MockInstancesIndex } from '@server/tests/shared'
6import { wait } from '@shared/core-utils'
7import { cleanupTests, createMultipleServers, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
8424c402
C
8
9const expect = chai.expect
10
254d3579 11async function checkFollow (follower: PeerTubeServer, following: PeerTubeServer, exists: boolean) {
8424c402 12 {
89d241a7 13 const body = await following.follows.getFollowers({ start: 0, count: 5, sort: '-createdAt' })
c3d29f69 14 const follow = body.data.find(f => f.follower.host === follower.host && f.state === 'accepted')
8424c402 15
1cf0df02
C
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
8424c402
C
18 }
19
20 {
89d241a7 21 const body = await follower.follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
c3d29f69 22 const follow = body.data.find(f => f.following.host === following.host && f.state === 'accepted')
8424c402 23
1cf0df02
C
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
8424c402
C
26 }
27}
28
254d3579 29async function server1Follows2 (servers: PeerTubeServer[]) {
4d029ef8 30 await servers[0].follows.follow({ hosts: [ servers[1].host ] })
8424c402
C
31
32 await waitJobs(servers)
33}
34
254d3579 35async function resetFollows (servers: PeerTubeServer[]) {
8424c402 36 try {
89d241a7
C
37 await servers[0].follows.unfollow({ target: servers[1] })
38 await servers[1].follows.unfollow({ target: servers[0] })
a1587156
C
39 } catch { /* empty */
40 }
8424c402
C
41
42 await waitJobs(servers)
43
44 await checkFollow(servers[0], servers[1], false)
45 await checkFollow(servers[1], servers[0], false)
46}
47
48describe('Test auto follows', function () {
254d3579 49 let servers: PeerTubeServer[] = []
8424c402
C
50
51 before(async function () {
52 this.timeout(30000)
53
254d3579 54 servers = await createMultipleServers(3)
8424c402
C
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 }
89d241a7 83 await servers[1].config.updateCustomSubConfig({ newConfig: config })
8424c402
C
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 }
89d241a7 108 await servers[1].config.updateCustomSubConfig({ newConfig: config })
8424c402
C
109
110 await server1Follows2(servers)
111
112 await checkFollow(servers[0], servers[1], false)
113 await checkFollow(servers[1], servers[0], false)
114
89d241a7 115 await servers[1].follows.acceptFollower({ follower: 'peertube@' + servers[0].host })
8424c402
C
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)
2ba613a5
C
122
123 config.followings.instance.autoFollowBack.enabled = false
124 config.followers.instance.manualApproval = false
89d241a7 125 await servers[1].config.updateCustomSubConfig({ newConfig: config })
8424c402
C
126 })
127 })
128
6f1b4fa4
C
129 describe('Auto follow index', function () {
130 const instanceIndexServer = new MockInstancesIndex()
f6500729 131 let port: number
6f1b4fa4
C
132
133 before(async () => {
f6500729 134 port = await instanceIndexServer.initialize()
6f1b4fa4
C
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
a1587156
C
143 await checkFollow(servers[0], servers[1], false)
144 await checkFollow(servers[1], servers[0], false)
6f1b4fa4
C
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: {
f6500729 156 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
6f1b4fa4
C
157 enabled: true
158 }
159 }
160 }
161 }
89d241a7 162 await servers[0].config.updateCustomSubConfig({ newConfig: config })
6f1b4fa4
C
163
164 await wait(5000)
165 await waitJobs(servers)
166
a1587156 167 await checkFollow(servers[0], servers[1], true)
6f1b4fa4
C
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
a1587156
C
180 await checkFollow(servers[0], servers[1], false)
181 await checkFollow(servers[0], servers[2], true)
6f1b4fa4 182 })
cf0c8ee5
C
183
184 after(async function () {
185 await instanceIndexServer.terminate()
186 })
6f1b4fa4
C
187 })
188
8424c402
C
189 after(async function () {
190 await cleanupTests(servers)
191 })
192})