1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { expectStartWith } from '@server/tests/shared'
5 import { ActorFollow, FollowState } from '@shared/models'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 const expect = chai.expect
17 async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
19 servers[0].follows.getFollowings.bind(servers[0].follows),
20 servers[1].follows.getFollowers.bind(servers[1].follows)
23 for (const fn of fns) {
24 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
25 expect(body.total).to.equal(1)
27 const follow = body.data[0]
28 expect(follow.state).to.equal(state)
29 expect(follow.follower.url).to.equal(servers[0].url + '/accounts/peertube')
30 expect(follow.following.url).to.equal(servers[1].url + '/accounts/peertube')
34 async function checkFollows (options: {
35 follower: PeerTubeServer
36 followerState: FollowState | 'deleted'
38 following: PeerTubeServer
39 followingState: FollowState | 'deleted'
41 const { follower, followerState, followingState, following } = options
43 const followerUrl = follower.url + '/accounts/peertube'
44 const followingUrl = following.url + '/accounts/peertube'
45 const finder = (d: ActorFollow) => d.follower.url === followerUrl && d.following.url === followingUrl
48 const { data } = await follower.follows.getFollowings()
49 const follow = data.find(finder)
51 if (followerState === 'deleted') {
52 expect(follow).to.not.exist
54 expect(follow.state).to.equal(followerState)
55 expect(follow.follower.url).to.equal(followerUrl)
56 expect(follow.following.url).to.equal(followingUrl)
61 const { data } = await following.follows.getFollowers()
62 const follow = data.find(finder)
64 if (followingState === 'deleted') {
65 expect(follow).to.not.exist
67 expect(follow.state).to.equal(followingState)
68 expect(follow.follower.url).to.equal(followerUrl)
69 expect(follow.following.url).to.equal(followingUrl)
74 async function checkNoFollowers (servers: PeerTubeServer[]) {
76 servers[0].follows.getFollowings.bind(servers[0].follows),
77 servers[1].follows.getFollowers.bind(servers[1].follows)
80 for (const fn of fns) {
81 const body = await fn({ start: 0, count: 5, sort: 'createdAt', state: 'accepted' })
82 expect(body.total).to.equal(0)
86 describe('Test follows moderation', function () {
87 let servers: PeerTubeServer[] = []
88 let commands: FollowsCommand[]
90 before(async function () {
93 servers = await createMultipleServers(3)
95 // Get the access tokens
96 await setAccessTokensToServers(servers)
98 commands = servers.map(s => s.follows)
101 it('Should have server 1 following server 2', async function () {
104 await commands[0].follow({ hosts: [ servers[1].url ] })
106 await waitJobs(servers)
109 it('Should have correct follows', async function () {
110 await checkServer1And2HasFollowers(servers)
113 it('Should remove follower on server 2', async function () {
116 await commands[1].removeFollower({ follower: servers[0] })
118 await waitJobs(servers)
121 it('Should not not have follows anymore', async function () {
122 await checkNoFollowers(servers)
125 it('Should disable followers on server 2', async function () {
132 manualApproval: false
137 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
139 await commands[0].follow({ hosts: [ servers[1].url ] })
140 await waitJobs(servers)
142 await checkNoFollowers(servers)
145 it('Should re enable followers on server 2', async function () {
152 manualApproval: false
157 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
159 await commands[0].follow({ hosts: [ servers[1].url ] })
160 await waitJobs(servers)
162 await checkServer1And2HasFollowers(servers)
165 it('Should manually approve followers', async function () {
168 await commands[0].unfollow({ target: servers[1] })
169 await waitJobs(servers)
180 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
181 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
183 await commands[0].follow({ hosts: [ servers[1].url ] })
184 await waitJobs(servers)
186 await checkServer1And2HasFollowers(servers, 'pending')
189 it('Should accept a follower', async function () {
192 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
193 await waitJobs(servers)
195 await checkServer1And2HasFollowers(servers)
198 it('Should reject another follower', async function () {
201 await commands[0].follow({ hosts: [ servers[2].url ] })
202 await waitJobs(servers)
205 const body = await commands[0].getFollowings()
206 expect(body.total).to.equal(2)
210 const body = await commands[1].getFollowers()
211 expect(body.total).to.equal(1)
215 const body = await commands[2].getFollowers()
216 expect(body.total).to.equal(1)
219 await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
220 await waitJobs(servers)
224 const { data } = await commands[0].getFollowings({ state: 'accepted' })
225 expect(data).to.have.lengthOf(1)
229 const { data } = await commands[0].getFollowings({ state: 'rejected' })
230 expect(data).to.have.lengthOf(1)
231 expectStartWith(data[0].following.url, servers[2].url)
237 const { data } = await commands[2].getFollowers({ state: 'accepted' })
238 expect(data).to.have.lengthOf(0)
242 const { data } = await commands[2].getFollowers({ state: 'rejected' })
243 expect(data).to.have.lengthOf(1)
244 expectStartWith(data[0].follower.url, servers[0].url)
249 it('Should not change the follow on refollow with and without auto accept', async function () {
250 const run = async () => {
251 await commands[0].follow({ hosts: [ servers[2].url ] })
252 await waitJobs(servers)
255 follower: servers[0],
256 followerState: 'rejected',
257 following: servers[2],
258 followingState: 'rejected'
262 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
265 await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
269 it('Should not change the rejected status on unfollow', async function () {
270 await commands[0].unfollow({ target: servers[2] })
271 await waitJobs(servers)
274 follower: servers[0],
275 followerState: 'deleted',
276 following: servers[2],
277 followingState: 'rejected'
281 it('Should delete the follower and add again the follower', async function () {
282 await commands[2].removeFollower({ follower: servers[0] })
283 await waitJobs(servers)
285 await commands[0].follow({ hosts: [ servers[2].url ] })
286 await waitJobs(servers)
289 follower: servers[0],
290 followerState: 'pending',
291 following: servers[2],
292 followingState: 'pending'
296 it('Should be able to reject a previously accepted follower', async function () {
297 await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host })
298 await waitJobs(servers)
301 follower: servers[0],
302 followerState: 'rejected',
303 following: servers[1],
304 followingState: 'rejected'
308 it('Should be able to re accept a previously rejected follower', async function () {
309 await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
310 await waitJobs(servers)
313 follower: servers[0],
314 followerState: 'accepted',
315 following: servers[1],
316 followingState: 'accepted'
320 it('Should ignore follow requests of muted servers', async function () {
321 await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host })
323 await commands[0].unfollow({ target: servers[1] })
325 await waitJobs(servers)
328 follower: servers[0],
329 followerState: 'deleted',
330 following: servers[1],
331 followingState: 'deleted'
334 await commands[0].follow({ hosts: [ servers[1].host ] })
335 await waitJobs(servers)
338 follower: servers[0],
339 followerState: 'rejected',
340 following: servers[1],
341 followingState: 'deleted'
345 after(async function () {
346 await cleanupTests(servers)