1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { expectStartWith } from '@server/tests/shared'
5 import { ActorFollow, FollowState } from '@shared/models'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
17 servers[0].follows.getFollowings.bind(servers[0].follows),
18 servers[1].follows.getFollowers.bind(servers[1].follows)
21 for (const fn of fns) {
22 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
23 expect(body.total).to.equal(1)
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')
32 async function checkFollows (options: {
33 follower: PeerTubeServer
34 followerState: FollowState | 'deleted'
36 following: PeerTubeServer
37 followingState: FollowState | 'deleted'
39 const { follower, followerState, followingState, following } = options
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
46 const { data } = await follower.follows.getFollowings()
47 const follow = data.find(finder)
49 if (followerState === 'deleted') {
50 expect(follow).to.not.exist
52 expect(follow.state).to.equal(followerState)
53 expect(follow.follower.url).to.equal(followerUrl)
54 expect(follow.following.url).to.equal(followingUrl)
59 const { data } = await following.follows.getFollowers()
60 const follow = data.find(finder)
62 if (followingState === 'deleted') {
63 expect(follow).to.not.exist
65 expect(follow.state).to.equal(followingState)
66 expect(follow.follower.url).to.equal(followerUrl)
67 expect(follow.following.url).to.equal(followingUrl)
72 async function checkNoFollowers (servers: PeerTubeServer[]) {
74 servers[0].follows.getFollowings.bind(servers[0].follows),
75 servers[1].follows.getFollowers.bind(servers[1].follows)
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)
84 describe('Test follows moderation', function () {
85 let servers: PeerTubeServer[] = []
86 let commands: FollowsCommand[]
88 before(async function () {
91 servers = await createMultipleServers(3)
93 // Get the access tokens
94 await setAccessTokensToServers(servers)
96 commands = servers.map(s => s.follows)
99 it('Should have server 1 following server 2', async function () {
102 await commands[0].follow({ hosts: [ servers[1].url ] })
104 await waitJobs(servers)
107 it('Should have correct follows', async function () {
108 await checkServer1And2HasFollowers(servers)
111 it('Should remove follower on server 2', async function () {
114 await commands[1].removeFollower({ follower: servers[0] })
116 await waitJobs(servers)
119 it('Should not not have follows anymore', async function () {
120 await checkNoFollowers(servers)
123 it('Should disable followers on server 2', async function () {
130 manualApproval: false
135 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
137 await commands[0].follow({ hosts: [ servers[1].url ] })
138 await waitJobs(servers)
140 await checkNoFollowers(servers)
143 it('Should re enable followers on server 2', async function () {
150 manualApproval: false
155 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
157 await commands[0].follow({ hosts: [ servers[1].url ] })
158 await waitJobs(servers)
160 await checkServer1And2HasFollowers(servers)
163 it('Should manually approve followers', async function () {
166 await commands[0].unfollow({ target: servers[1] })
167 await waitJobs(servers)
178 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
179 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
181 await commands[0].follow({ hosts: [ servers[1].url ] })
182 await waitJobs(servers)
184 await checkServer1And2HasFollowers(servers, 'pending')
187 it('Should accept a follower', async function () {
190 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
191 await waitJobs(servers)
193 await checkServer1And2HasFollowers(servers)
196 it('Should reject another follower', async function () {
199 await commands[0].follow({ hosts: [ servers[2].url ] })
200 await waitJobs(servers)
203 const body = await commands[0].getFollowings()
204 expect(body.total).to.equal(2)
208 const body = await commands[1].getFollowers()
209 expect(body.total).to.equal(1)
213 const body = await commands[2].getFollowers()
214 expect(body.total).to.equal(1)
217 await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
218 await waitJobs(servers)
222 const { data } = await commands[0].getFollowings({ state: 'accepted' })
223 expect(data).to.have.lengthOf(1)
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)
235 const { data } = await commands[2].getFollowers({ state: 'accepted' })
236 expect(data).to.have.lengthOf(0)
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)
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)
253 follower: servers[0],
254 followerState: 'rejected',
255 following: servers[2],
256 followingState: 'rejected'
260 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
263 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
267 it('Should not change the rejected status on unfollow', async function () {
268 await commands[0].unfollow({ target: servers[2] })
269 await waitJobs(servers)
272 follower: servers[0],
273 followerState: 'deleted',
274 following: servers[2],
275 followingState: 'rejected'
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)
283 await commands[0].follow({ hosts: [ servers[2].url ] })
284 await waitJobs(servers)
287 follower: servers[0],
288 followerState: 'pending',
289 following: servers[2],
290 followingState: 'pending'
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)
299 follower: servers[0],
300 followerState: 'rejected',
301 following: servers[1],
302 followingState: 'rejected'
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)
311 follower: servers[0],
312 followerState: 'accepted',
313 following: servers[1],
314 followingState: 'accepted'
318 it('Should ignore follow requests of muted servers', async function () {
319 await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host })
321 await commands[0].unfollow({ target: servers[1] })
323 await waitJobs(servers)
326 follower: servers[0],
327 followerState: 'deleted',
328 following: servers[1],
329 followingState: 'deleted'
332 await commands[0].follow({ hosts: [ servers[1].host ] })
333 await waitJobs(servers)
336 follower: servers[0],
337 followerState: 'rejected',
338 following: servers[1],
339 followingState: 'deleted'
343 after(async function () {
344 await cleanupTests(servers)