1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
8 flushAndRunMultipleServers,
10 setAccessTokensToServers,
12 } from '../../../../shared/extra-utils/index'
15 getFollowersListPaginationAndSort,
16 getFollowingListPaginationAndSort,
19 } from '../../../../shared/extra-utils/server/follows'
20 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21 import { ActorFollow } from '../../../../shared/models/actors'
23 const expect = chai.expect
25 async function checkServer1And2HasFollowers (servers: ServerInfo[], state = 'accepted') {
27 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: 'createdAt' })
28 expect(res.body.total).to.equal(1)
30 const follow = res.body.data[0] as ActorFollow
31 expect(follow.state).to.equal(state)
32 expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube')
33 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
37 const res = await getFollowersListPaginationAndSort({ url: servers[1].url, start: 0, count: 5, sort: 'createdAt' })
38 expect(res.body.total).to.equal(1)
40 const follow = res.body.data[0] as ActorFollow
41 expect(follow.state).to.equal(state)
42 expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube')
43 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
47 async function checkNoFollowers (servers: ServerInfo[]) {
49 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: 'createdAt' })
50 expect(res.body.total).to.equal(0)
54 const res = await getFollowersListPaginationAndSort({ url: servers[1].url, start: 0, count: 5, sort: 'createdAt' })
55 expect(res.body.total).to.equal(0)
59 describe('Test follows moderation', function () {
60 let servers: ServerInfo[] = []
62 before(async function () {
65 servers = await flushAndRunMultipleServers(3)
67 // Get the access tokens
68 await setAccessTokensToServers(servers)
71 it('Should have server 1 following server 2', async function () {
74 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
76 await waitJobs(servers)
79 it('Should have correct follows', async function () {
80 await checkServer1And2HasFollowers(servers)
83 it('Should remove follower on server 2', async function () {
86 await removeFollower(servers[1].url, servers[1].accessToken, servers[0])
88 await waitJobs(servers)
91 it('Should not not have follows anymore', async function () {
92 await checkNoFollowers(servers)
95 it('Should disable followers on server 2', async function () {
102 manualApproval: false
107 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
109 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
110 await waitJobs(servers)
112 await checkNoFollowers(servers)
115 it('Should re enable followers on server 2', async function () {
122 manualApproval: false
127 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
129 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
130 await waitJobs(servers)
132 await checkServer1And2HasFollowers(servers)
135 it('Should manually approve followers', async function () {
138 await removeFollower(servers[1].url, servers[1].accessToken, servers[0])
139 await waitJobs(servers)
150 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
151 await updateCustomSubConfig(servers[2].url, servers[2].accessToken, subConfig)
153 await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
154 await waitJobs(servers)
156 await checkServer1And2HasFollowers(servers, 'pending')
159 it('Should accept a follower', async function () {
162 await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@localhost:' + servers[0].port)
163 await waitJobs(servers)
165 await checkServer1And2HasFollowers(servers)
168 it('Should reject another follower', async function () {
171 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
172 await waitJobs(servers)
175 const res = await getFollowingListPaginationAndSort({ url: servers[0].url, start: 0, count: 5, sort: 'createdAt' })
176 expect(res.body.total).to.equal(2)
180 const res = await getFollowersListPaginationAndSort({ url: servers[1].url, start: 0, count: 5, sort: 'createdAt' })
181 expect(res.body.total).to.equal(1)
185 const res = await getFollowersListPaginationAndSort({ url: servers[2].url, start: 0, count: 5, sort: 'createdAt' })
186 expect(res.body.total).to.equal(1)
189 await rejectFollower(servers[2].url, servers[2].accessToken, 'peertube@localhost:' + servers[0].port)
190 await waitJobs(servers)
192 await checkServer1And2HasFollowers(servers)
195 const res = await getFollowersListPaginationAndSort({ url: servers[2].url, start: 0, count: 5, sort: 'createdAt' })
196 expect(res.body.total).to.equal(0)
200 after(async function () {
201 await cleanupTests(servers)