1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
7 flushAndRunMultipleServers,
10 setAccessTokensToServers,
11 updateCustomSubConfig,
13 } from '@shared/extra-utils'
15 const expect = chai.expect
17 async function checkServer1And2HasFollowers (servers: ServerInfo[], state = 'accepted') {
19 servers[0].followsCommand.getFollowings.bind(servers[0].followsCommand),
20 servers[1].followsCommand.getFollowers.bind(servers[1].followsCommand)
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('http://localhost:' + servers[0].port + '/accounts/peertube')
30 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
34 async function checkNoFollowers (servers: ServerInfo[]) {
36 servers[0].followsCommand.getFollowings.bind(servers[0].followsCommand),
37 servers[1].followsCommand.getFollowers.bind(servers[1].followsCommand)
40 for (const fn of fns) {
41 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
42 expect(body.total).to.equal(0)
46 describe('Test follows moderation', function () {
47 let servers: ServerInfo[] = []
48 let commands: FollowsCommand[]
50 before(async function () {
53 servers = await flushAndRunMultipleServers(3)
55 // Get the access tokens
56 await setAccessTokensToServers(servers)
58 commands = servers.map(s => s.followsCommand)
61 it('Should have server 1 following server 2', async function () {
64 await commands[0].follow({ targets: [ servers[1].url ] })
66 await waitJobs(servers)
69 it('Should have correct follows', async function () {
70 await checkServer1And2HasFollowers(servers)
73 it('Should remove follower on server 2', async function () {
76 await commands[1].removeFollower({ follower: servers[0] })
78 await waitJobs(servers)
81 it('Should not not have follows anymore', async function () {
82 await checkNoFollowers(servers)
85 it('Should disable followers on server 2', async function () {
97 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
99 await commands[0].follow({ targets: [ servers[1].url ] })
100 await waitJobs(servers)
102 await checkNoFollowers(servers)
105 it('Should re enable followers on server 2', async function () {
112 manualApproval: false
117 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
119 await commands[0].follow({ targets: [ servers[1].url ] })
120 await waitJobs(servers)
122 await checkServer1And2HasFollowers(servers)
125 it('Should manually approve followers', async function () {
128 await commands[1].removeFollower({ follower: servers[0] })
129 await waitJobs(servers)
140 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
141 await updateCustomSubConfig(servers[2].url, servers[2].accessToken, subConfig)
143 await commands[0].follow({ targets: [ servers[1].url ] })
144 await waitJobs(servers)
146 await checkServer1And2HasFollowers(servers, 'pending')
149 it('Should accept a follower', async function () {
152 await commands[1].acceptFollower({ follower: 'peertube@localhost:' + servers[0].port })
153 await waitJobs(servers)
155 await checkServer1And2HasFollowers(servers)
158 it('Should reject another follower', async function () {
161 await commands[0].follow({ targets: [ servers[2].url ] })
162 await waitJobs(servers)
165 const body = await commands[0].getFollowings({ start: 0, count: 5, sort: 'createdAt' })
166 expect(body.total).to.equal(2)
170 const body = await commands[1].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
171 expect(body.total).to.equal(1)
175 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
176 expect(body.total).to.equal(1)
179 await commands[2].rejectFollower({ follower: 'peertube@localhost:' + servers[0].port })
180 await waitJobs(servers)
182 await checkServer1And2HasFollowers(servers)
185 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
186 expect(body.total).to.equal(0)
190 after(async function () {
191 await cleanupTests(servers)