]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/follows-moderation.ts
deeaac85a0859b5b51d680f2ef26a2c9172dc531
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows-moderation.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { expectStartWith } from '@server/tests/shared'
5 import { ActorFollow, FollowState } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 FollowsCommand,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 waitJobs
13 } from '@shared/server-commands'
14
15 async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
16 const fns = [
17 servers[0].follows.getFollowings.bind(servers[0].follows),
18 servers[1].follows.getFollowers.bind(servers[1].follows)
19 ]
20
21 for (const fn of fns) {
22 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
23 expect(body.total).to.equal(1)
24
25 const follow = body.data[0]
26 expect(follow.state).to.equal(state)
27 expect(follow.follower.url).to.equal(servers[0].url + '/accounts/peertube')
28 expect(follow.following.url).to.equal(servers[1].url + '/accounts/peertube')
29 }
30 }
31
32 async function checkFollows (options: {
33 follower: PeerTubeServer
34 followerState: FollowState | 'deleted'
35
36 following: PeerTubeServer
37 followingState: FollowState | 'deleted'
38 }) {
39 const { follower, followerState, followingState, following } = options
40
41 const followerUrl = follower.url + '/accounts/peertube'
42 const followingUrl = following.url + '/accounts/peertube'
43 const finder = (d: ActorFollow) => d.follower.url === followerUrl && d.following.url === followingUrl
44
45 {
46 const { data } = await follower.follows.getFollowings()
47 const follow = data.find(finder)
48
49 if (followerState === 'deleted') {
50 expect(follow).to.not.exist
51 } else {
52 expect(follow.state).to.equal(followerState)
53 expect(follow.follower.url).to.equal(followerUrl)
54 expect(follow.following.url).to.equal(followingUrl)
55 }
56 }
57
58 {
59 const { data } = await following.follows.getFollowers()
60 const follow = data.find(finder)
61
62 if (followingState === 'deleted') {
63 expect(follow).to.not.exist
64 } else {
65 expect(follow.state).to.equal(followingState)
66 expect(follow.follower.url).to.equal(followerUrl)
67 expect(follow.following.url).to.equal(followingUrl)
68 }
69 }
70 }
71
72 async function checkNoFollowers (servers: PeerTubeServer[]) {
73 const fns = [
74 servers[0].follows.getFollowings.bind(servers[0].follows),
75 servers[1].follows.getFollowers.bind(servers[1].follows)
76 ]
77
78 for (const fn of fns) {
79 const body = await fn({ start: 0, count: 5, sort: 'createdAt', state: 'accepted' })
80 expect(body.total).to.equal(0)
81 }
82 }
83
84 describe('Test follows moderation', function () {
85 let servers: PeerTubeServer[] = []
86 let commands: FollowsCommand[]
87
88 before(async function () {
89 this.timeout(240000)
90
91 servers = await createMultipleServers(3)
92
93 // Get the access tokens
94 await setAccessTokensToServers(servers)
95
96 commands = servers.map(s => s.follows)
97 })
98
99 it('Should have server 1 following server 2', async function () {
100 this.timeout(30000)
101
102 await commands[0].follow({ hosts: [ servers[1].url ] })
103
104 await waitJobs(servers)
105 })
106
107 it('Should have correct follows', async function () {
108 await checkServer1And2HasFollowers(servers)
109 })
110
111 it('Should remove follower on server 2', async function () {
112 this.timeout(10000)
113
114 await commands[1].removeFollower({ follower: servers[0] })
115
116 await waitJobs(servers)
117 })
118
119 it('Should not not have follows anymore', async function () {
120 await checkNoFollowers(servers)
121 })
122
123 it('Should disable followers on server 2', async function () {
124 this.timeout(10000)
125
126 const subConfig = {
127 followers: {
128 instance: {
129 enabled: false,
130 manualApproval: false
131 }
132 }
133 }
134
135 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
136
137 await commands[0].follow({ hosts: [ servers[1].url ] })
138 await waitJobs(servers)
139
140 await checkNoFollowers(servers)
141 })
142
143 it('Should re enable followers on server 2', async function () {
144 this.timeout(10000)
145
146 const subConfig = {
147 followers: {
148 instance: {
149 enabled: true,
150 manualApproval: false
151 }
152 }
153 }
154
155 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
156
157 await commands[0].follow({ hosts: [ servers[1].url ] })
158 await waitJobs(servers)
159
160 await checkServer1And2HasFollowers(servers)
161 })
162
163 it('Should manually approve followers', async function () {
164 this.timeout(20000)
165
166 await commands[0].unfollow({ target: servers[1] })
167 await waitJobs(servers)
168
169 const subConfig = {
170 followers: {
171 instance: {
172 enabled: true,
173 manualApproval: true
174 }
175 }
176 }
177
178 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
179 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
180
181 await commands[0].follow({ hosts: [ servers[1].url ] })
182 await waitJobs(servers)
183
184 await checkServer1And2HasFollowers(servers, 'pending')
185 })
186
187 it('Should accept a follower', async function () {
188 this.timeout(10000)
189
190 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
191 await waitJobs(servers)
192
193 await checkServer1And2HasFollowers(servers)
194 })
195
196 it('Should reject another follower', async function () {
197 this.timeout(20000)
198
199 await commands[0].follow({ hosts: [ servers[2].url ] })
200 await waitJobs(servers)
201
202 {
203 const body = await commands[0].getFollowings()
204 expect(body.total).to.equal(2)
205 }
206
207 {
208 const body = await commands[1].getFollowers()
209 expect(body.total).to.equal(1)
210 }
211
212 {
213 const body = await commands[2].getFollowers()
214 expect(body.total).to.equal(1)
215 }
216
217 await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
218 await waitJobs(servers)
219
220 { // server 1
221 {
222 const { data } = await commands[0].getFollowings({ state: 'accepted' })
223 expect(data).to.have.lengthOf(1)
224 }
225
226 {
227 const { data } = await commands[0].getFollowings({ state: 'rejected' })
228 expect(data).to.have.lengthOf(1)
229 expectStartWith(data[0].following.url, servers[2].url)
230 }
231 }
232
233 { // server 3
234 {
235 const { data } = await commands[2].getFollowers({ state: 'accepted' })
236 expect(data).to.have.lengthOf(0)
237 }
238
239 {
240 const { data } = await commands[2].getFollowers({ state: 'rejected' })
241 expect(data).to.have.lengthOf(1)
242 expectStartWith(data[0].follower.url, servers[0].url)
243 }
244 }
245 })
246
247 it('Should not change the follow on refollow with and without auto accept', async function () {
248 const run = async () => {
249 await commands[0].follow({ hosts: [ servers[2].url ] })
250 await waitJobs(servers)
251
252 await checkFollows({
253 follower: servers[0],
254 followerState: 'rejected',
255 following: servers[2],
256 followingState: 'rejected'
257 })
258 }
259
260 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
261 await run()
262
263 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
264 await run()
265 })
266
267 it('Should not change the rejected status on unfollow', async function () {
268 await commands[0].unfollow({ target: servers[2] })
269 await waitJobs(servers)
270
271 await checkFollows({
272 follower: servers[0],
273 followerState: 'deleted',
274 following: servers[2],
275 followingState: 'rejected'
276 })
277 })
278
279 it('Should delete the follower and add again the follower', async function () {
280 await commands[2].removeFollower({ follower: servers[0] })
281 await waitJobs(servers)
282
283 await commands[0].follow({ hosts: [ servers[2].url ] })
284 await waitJobs(servers)
285
286 await checkFollows({
287 follower: servers[0],
288 followerState: 'pending',
289 following: servers[2],
290 followingState: 'pending'
291 })
292 })
293
294 it('Should be able to reject a previously accepted follower', async function () {
295 await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host })
296 await waitJobs(servers)
297
298 await checkFollows({
299 follower: servers[0],
300 followerState: 'rejected',
301 following: servers[1],
302 followingState: 'rejected'
303 })
304 })
305
306 it('Should be able to re accept a previously rejected follower', async function () {
307 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
308 await waitJobs(servers)
309
310 await checkFollows({
311 follower: servers[0],
312 followerState: 'accepted',
313 following: servers[1],
314 followingState: 'accepted'
315 })
316 })
317
318 it('Should ignore follow requests of muted servers', async function () {
319 await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host })
320
321 await commands[0].unfollow({ target: servers[1] })
322
323 await waitJobs(servers)
324
325 await checkFollows({
326 follower: servers[0],
327 followerState: 'deleted',
328 following: servers[1],
329 followingState: 'deleted'
330 })
331
332 await commands[0].follow({ hosts: [ servers[1].host ] })
333 await waitJobs(servers)
334
335 await checkFollows({
336 follower: servers[0],
337 followerState: 'rejected',
338 following: servers[1],
339 followingState: 'deleted'
340 })
341 })
342
343 after(async function () {
344 await cleanupTests(servers)
345 })
346 })