]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/follows-moderation.ts
Add ability for instances to follow any actor
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows-moderation.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 createMultipleServers,
8 FollowsCommand,
9 PeerTubeServer,
10 setAccessTokensToServers,
11 waitJobs
12 } from '@shared/extra-utils'
13
14 const expect = chai.expect
15
16 async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
17 const fns = [
18 servers[0].follows.getFollowings.bind(servers[0].follows),
19 servers[1].follows.getFollowers.bind(servers[1].follows)
20 ]
21
22 for (const fn of fns) {
23 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
24 expect(body.total).to.equal(1)
25
26 const follow = body.data[0]
27 expect(follow.state).to.equal(state)
28 expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube')
29 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
30 }
31 }
32
33 async function checkNoFollowers (servers: PeerTubeServer[]) {
34 const fns = [
35 servers[0].follows.getFollowings.bind(servers[0].follows),
36 servers[1].follows.getFollowers.bind(servers[1].follows)
37 ]
38
39 for (const fn of fns) {
40 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
41 expect(body.total).to.equal(0)
42 }
43 }
44
45 describe('Test follows moderation', function () {
46 let servers: PeerTubeServer[] = []
47 let commands: FollowsCommand[]
48
49 before(async function () {
50 this.timeout(30000)
51
52 servers = await createMultipleServers(3)
53
54 // Get the access tokens
55 await setAccessTokensToServers(servers)
56
57 commands = servers.map(s => s.follows)
58 })
59
60 it('Should have server 1 following server 2', async function () {
61 this.timeout(30000)
62
63 await commands[0].follow({ hosts: [ servers[1].url ] })
64
65 await waitJobs(servers)
66 })
67
68 it('Should have correct follows', async function () {
69 await checkServer1And2HasFollowers(servers)
70 })
71
72 it('Should remove follower on server 2', async function () {
73 this.timeout(10000)
74
75 await commands[1].removeFollower({ follower: servers[0] })
76
77 await waitJobs(servers)
78 })
79
80 it('Should not not have follows anymore', async function () {
81 await checkNoFollowers(servers)
82 })
83
84 it('Should disable followers on server 2', async function () {
85 this.timeout(10000)
86
87 const subConfig = {
88 followers: {
89 instance: {
90 enabled: false,
91 manualApproval: false
92 }
93 }
94 }
95
96 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
97
98 await commands[0].follow({ hosts: [ servers[1].url ] })
99 await waitJobs(servers)
100
101 await checkNoFollowers(servers)
102 })
103
104 it('Should re enable followers on server 2', async function () {
105 this.timeout(10000)
106
107 const subConfig = {
108 followers: {
109 instance: {
110 enabled: true,
111 manualApproval: false
112 }
113 }
114 }
115
116 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
117
118 await commands[0].follow({ hosts: [ servers[1].url ] })
119 await waitJobs(servers)
120
121 await checkServer1And2HasFollowers(servers)
122 })
123
124 it('Should manually approve followers', async function () {
125 this.timeout(20000)
126
127 await commands[1].removeFollower({ follower: servers[0] })
128 await waitJobs(servers)
129
130 const subConfig = {
131 followers: {
132 instance: {
133 enabled: true,
134 manualApproval: true
135 }
136 }
137 }
138
139 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
140 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
141
142 await commands[0].follow({ hosts: [ servers[1].url ] })
143 await waitJobs(servers)
144
145 await checkServer1And2HasFollowers(servers, 'pending')
146 })
147
148 it('Should accept a follower', async function () {
149 this.timeout(10000)
150
151 await commands[1].acceptFollower({ follower: 'peertube@localhost:' + servers[0].port })
152 await waitJobs(servers)
153
154 await checkServer1And2HasFollowers(servers)
155 })
156
157 it('Should reject another follower', async function () {
158 this.timeout(20000)
159
160 await commands[0].follow({ hosts: [ servers[2].url ] })
161 await waitJobs(servers)
162
163 {
164 const body = await commands[0].getFollowings({ start: 0, count: 5, sort: 'createdAt' })
165 expect(body.total).to.equal(2)
166 }
167
168 {
169 const body = await commands[1].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
170 expect(body.total).to.equal(1)
171 }
172
173 {
174 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
175 expect(body.total).to.equal(1)
176 }
177
178 await commands[2].rejectFollower({ follower: 'peertube@localhost:' + servers[0].port })
179 await waitJobs(servers)
180
181 await checkServer1And2HasFollowers(servers)
182
183 {
184 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
185 expect(body.total).to.equal(0)
186 }
187 })
188
189 after(async function () {
190 await cleanupTests(servers)
191 })
192 })