]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/auto-follows.ts
Use an object to represent a server
[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'
8424c402 5import {
8424c402 6 cleanupTests,
254d3579 7 createMultipleServers,
6f1b4fa4 8 MockInstancesIndex,
254d3579 9 PeerTubeServer,
8424c402 10 setAccessTokensToServers,
c3d29f69
C
11 wait,
12 waitJobs
13} from '@shared/extra-utils'
8424c402
C
14
15const expect = chai.expect
16
254d3579 17async function checkFollow (follower: PeerTubeServer, following: PeerTubeServer, exists: boolean) {
8424c402 18 {
89d241a7 19 const body = await following.follows.getFollowers({ start: 0, count: 5, sort: '-createdAt' })
c3d29f69 20 const follow = body.data.find(f => f.follower.host === follower.host && f.state === 'accepted')
8424c402 21
c3d29f69
C
22 if (exists === true) expect(follow).to.exist
23 else expect(follow).to.be.undefined
8424c402
C
24 }
25
26 {
89d241a7 27 const body = await follower.follows.getFollowings({ start: 0, count: 5, sort: '-createdAt' })
c3d29f69 28 const follow = body.data.find(f => f.following.host === following.host && f.state === 'accepted')
8424c402 29
c3d29f69
C
30 if (exists === true) expect(follow).to.exist
31 else expect(follow).to.be.undefined
8424c402
C
32 }
33}
34
254d3579 35async function server1Follows2 (servers: PeerTubeServer[]) {
89d241a7 36 await servers[0].follows.follow({ targets: [ servers[1].host ] })
8424c402
C
37
38 await waitJobs(servers)
39}
40
254d3579 41async function resetFollows (servers: PeerTubeServer[]) {
8424c402 42 try {
89d241a7
C
43 await servers[0].follows.unfollow({ target: servers[1] })
44 await servers[1].follows.unfollow({ target: servers[0] })
a1587156
C
45 } catch { /* empty */
46 }
8424c402
C
47
48 await waitJobs(servers)
49
50 await checkFollow(servers[0], servers[1], false)
51 await checkFollow(servers[1], servers[0], false)
52}
53
54describe('Test auto follows', function () {
254d3579 55 let servers: PeerTubeServer[] = []
8424c402
C
56
57 before(async function () {
58 this.timeout(30000)
59
254d3579 60 servers = await createMultipleServers(3)
8424c402
C
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 }
89d241a7 89 await servers[1].config.updateCustomSubConfig({ newConfig: config })
8424c402
C
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 }
89d241a7 114 await servers[1].config.updateCustomSubConfig({ newConfig: config })
8424c402
C
115
116 await server1Follows2(servers)
117
118 await checkFollow(servers[0], servers[1], false)
119 await checkFollow(servers[1], servers[0], false)
120
89d241a7 121 await servers[1].follows.acceptFollower({ follower: 'peertube@' + servers[0].host })
8424c402
C
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)
2ba613a5
C
128
129 config.followings.instance.autoFollowBack.enabled = false
130 config.followers.instance.manualApproval = false
89d241a7 131 await servers[1].config.updateCustomSubConfig({ newConfig: config })
8424c402
C
132 })
133 })
134
6f1b4fa4
C
135 describe('Auto follow index', function () {
136 const instanceIndexServer = new MockInstancesIndex()
f6500729 137 let port: number
6f1b4fa4
C
138
139 before(async () => {
f6500729 140 port = await instanceIndexServer.initialize()
6f1b4fa4
C
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
a1587156
C
149 await checkFollow(servers[0], servers[1], false)
150 await checkFollow(servers[1], servers[0], false)
6f1b4fa4
C
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: {
f6500729 162 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
6f1b4fa4
C
163 enabled: true
164 }
165 }
166 }
167 }
89d241a7 168 await servers[0].config.updateCustomSubConfig({ newConfig: config })
6f1b4fa4
C
169
170 await wait(5000)
171 await waitJobs(servers)
172
a1587156 173 await checkFollow(servers[0], servers[1], true)
6f1b4fa4
C
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
a1587156
C
186 await checkFollow(servers[0], servers[1], false)
187 await checkFollow(servers[0], servers[2], true)
6f1b4fa4
C
188 })
189 })
190
8424c402
C
191 after(async function () {
192 await cleanupTests(servers)
193 })
194})