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