]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import { expect } from 'chai' | |
4 | import { expectStartWith } from '@server/tests/shared' | |
5 | import { ActorFollow, FollowState } from '@shared/models' | |
6 | import { | |
7 | cleanupTests, | |
8 | createMultipleServers, | |
9 | FollowsCommand, | |
10 | PeerTubeServer, | |
11 | setAccessTokensToServers, | |
12 | waitJobs | |
13 | } from '@shared/server-commands' | |
14 | ||
15 | async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') { | |
16 | const fns = [ | |
17 | servers[0].follows.getFollowings.bind(servers[0].follows), | |
18 | servers[1].follows.getFollowers.bind(servers[1].follows) | |
19 | ] | |
20 | ||
21 | for (const fn of fns) { | |
22 | const body = await fn({ start: 0, count: 5, sort: 'createdAt' }) | |
23 | expect(body.total).to.equal(1) | |
24 | ||
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') | |
29 | } | |
30 | } | |
31 | ||
32 | async function checkFollows (options: { | |
33 | follower: PeerTubeServer | |
34 | followerState: FollowState | 'deleted' | |
35 | ||
36 | following: PeerTubeServer | |
37 | followingState: FollowState | 'deleted' | |
38 | }) { | |
39 | const { follower, followerState, followingState, following } = options | |
40 | ||
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 | |
44 | ||
45 | { | |
46 | const { data } = await follower.follows.getFollowings() | |
47 | const follow = data.find(finder) | |
48 | ||
49 | if (followerState === 'deleted') { | |
50 | expect(follow).to.not.exist | |
51 | } else { | |
52 | expect(follow.state).to.equal(followerState) | |
53 | expect(follow.follower.url).to.equal(followerUrl) | |
54 | expect(follow.following.url).to.equal(followingUrl) | |
55 | } | |
56 | } | |
57 | ||
58 | { | |
59 | const { data } = await following.follows.getFollowers() | |
60 | const follow = data.find(finder) | |
61 | ||
62 | if (followingState === 'deleted') { | |
63 | expect(follow).to.not.exist | |
64 | } else { | |
65 | expect(follow.state).to.equal(followingState) | |
66 | expect(follow.follower.url).to.equal(followerUrl) | |
67 | expect(follow.following.url).to.equal(followingUrl) | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | async function checkNoFollowers (servers: PeerTubeServer[]) { | |
73 | const fns = [ | |
74 | servers[0].follows.getFollowings.bind(servers[0].follows), | |
75 | servers[1].follows.getFollowers.bind(servers[1].follows) | |
76 | ] | |
77 | ||
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) | |
81 | } | |
82 | } | |
83 | ||
84 | describe('Test follows moderation', function () { | |
85 | let servers: PeerTubeServer[] = [] | |
86 | let commands: FollowsCommand[] | |
87 | ||
88 | before(async function () { | |
89 | this.timeout(240000) | |
90 | ||
91 | servers = await createMultipleServers(3) | |
92 | ||
93 | // Get the access tokens | |
94 | await setAccessTokensToServers(servers) | |
95 | ||
96 | commands = servers.map(s => s.follows) | |
97 | }) | |
98 | ||
99 | describe('Default behaviour', function () { | |
100 | ||
101 | it('Should have server 1 following server 2', async function () { | |
102 | this.timeout(30000) | |
103 | ||
104 | await commands[0].follow({ hosts: [ servers[1].url ] }) | |
105 | ||
106 | await waitJobs(servers) | |
107 | }) | |
108 | ||
109 | it('Should have correct follows', async function () { | |
110 | await checkServer1And2HasFollowers(servers) | |
111 | }) | |
112 | ||
113 | it('Should remove follower on server 2', async function () { | |
114 | await commands[1].removeFollower({ follower: servers[0] }) | |
115 | ||
116 | await waitJobs(servers) | |
117 | }) | |
118 | ||
119 | it('Should not not have follows anymore', async function () { | |
120 | await checkNoFollowers(servers) | |
121 | }) | |
122 | }) | |
123 | ||
124 | describe('Disabled/Enabled followers', function () { | |
125 | ||
126 | it('Should disable followers on server 2', async function () { | |
127 | const subConfig = { | |
128 | followers: { | |
129 | instance: { | |
130 | enabled: false, | |
131 | manualApproval: false | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) | |
137 | ||
138 | await commands[0].follow({ hosts: [ servers[1].url ] }) | |
139 | await waitJobs(servers) | |
140 | ||
141 | await checkNoFollowers(servers) | |
142 | }) | |
143 | ||
144 | it('Should re enable followers on server 2', async function () { | |
145 | const subConfig = { | |
146 | followers: { | |
147 | instance: { | |
148 | enabled: true, | |
149 | manualApproval: false | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) | |
155 | ||
156 | await commands[0].follow({ hosts: [ servers[1].url ] }) | |
157 | await waitJobs(servers) | |
158 | ||
159 | await checkServer1And2HasFollowers(servers) | |
160 | }) | |
161 | }) | |
162 | ||
163 | describe('Manual approbation', function () { | |
164 | ||
165 | it('Should manually approve followers', async function () { | |
166 | this.timeout(20000) | |
167 | ||
168 | await commands[0].unfollow({ target: servers[1] }) | |
169 | await waitJobs(servers) | |
170 | ||
171 | const subConfig = { | |
172 | followers: { | |
173 | instance: { | |
174 | enabled: true, | |
175 | manualApproval: true | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) | |
181 | await servers[2].config.updateCustomSubConfig({ newConfig: subConfig }) | |
182 | ||
183 | await commands[0].follow({ hosts: [ servers[1].url ] }) | |
184 | await waitJobs(servers) | |
185 | ||
186 | await checkServer1And2HasFollowers(servers, 'pending') | |
187 | }) | |
188 | ||
189 | it('Should accept a follower', async function () { | |
190 | await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host }) | |
191 | await waitJobs(servers) | |
192 | ||
193 | await checkServer1And2HasFollowers(servers) | |
194 | }) | |
195 | ||
196 | it('Should reject another follower', async function () { | |
197 | this.timeout(20000) | |
198 | ||
199 | await commands[0].follow({ hosts: [ servers[2].url ] }) | |
200 | await waitJobs(servers) | |
201 | ||
202 | { | |
203 | const body = await commands[0].getFollowings() | |
204 | expect(body.total).to.equal(2) | |
205 | } | |
206 | ||
207 | { | |
208 | const body = await commands[1].getFollowers() | |
209 | expect(body.total).to.equal(1) | |
210 | } | |
211 | ||
212 | { | |
213 | const body = await commands[2].getFollowers() | |
214 | expect(body.total).to.equal(1) | |
215 | } | |
216 | ||
217 | await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host }) | |
218 | await waitJobs(servers) | |
219 | ||
220 | { // server 1 | |
221 | { | |
222 | const { data } = await commands[0].getFollowings({ state: 'accepted' }) | |
223 | expect(data).to.have.lengthOf(1) | |
224 | } | |
225 | ||
226 | { | |
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) | |
230 | } | |
231 | } | |
232 | ||
233 | { // server 3 | |
234 | { | |
235 | const { data } = await commands[2].getFollowers({ state: 'accepted' }) | |
236 | expect(data).to.have.lengthOf(0) | |
237 | } | |
238 | ||
239 | { | |
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) | |
243 | } | |
244 | } | |
245 | }) | |
246 | ||
247 | it('Should still auto accept channel followers', async function () { | |
248 | await commands[0].follow({ handles: [ 'root_channel@' + servers[1].host ] }) | |
249 | ||
250 | await waitJobs(servers) | |
251 | ||
252 | const body = await commands[0].getFollowings() | |
253 | const follow = body.data[0] | |
254 | expect(follow.following.name).to.equal('root_channel') | |
255 | expect(follow.state).to.equal('accepted') | |
256 | }) | |
257 | }) | |
258 | ||
259 | describe('Accept/reject state', function () { | |
260 | ||
261 | it('Should not change the follow on refollow with and without auto accept', async function () { | |
262 | const run = async () => { | |
263 | await commands[0].follow({ hosts: [ servers[2].url ] }) | |
264 | await waitJobs(servers) | |
265 | ||
266 | await checkFollows({ | |
267 | follower: servers[0], | |
268 | followerState: 'rejected', | |
269 | following: servers[2], | |
270 | followingState: 'rejected' | |
271 | }) | |
272 | } | |
273 | ||
274 | await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } }) | |
275 | await run() | |
276 | ||
277 | await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } }) | |
278 | await run() | |
279 | }) | |
280 | ||
281 | it('Should not change the rejected status on unfollow', async function () { | |
282 | await commands[0].unfollow({ target: servers[2] }) | |
283 | await waitJobs(servers) | |
284 | ||
285 | await checkFollows({ | |
286 | follower: servers[0], | |
287 | followerState: 'deleted', | |
288 | following: servers[2], | |
289 | followingState: 'rejected' | |
290 | }) | |
291 | }) | |
292 | ||
293 | it('Should delete the follower and add again the follower', async function () { | |
294 | await commands[2].removeFollower({ follower: servers[0] }) | |
295 | await waitJobs(servers) | |
296 | ||
297 | await commands[0].follow({ hosts: [ servers[2].url ] }) | |
298 | await waitJobs(servers) | |
299 | ||
300 | await checkFollows({ | |
301 | follower: servers[0], | |
302 | followerState: 'pending', | |
303 | following: servers[2], | |
304 | followingState: 'pending' | |
305 | }) | |
306 | }) | |
307 | ||
308 | it('Should be able to reject a previously accepted follower', async function () { | |
309 | await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host }) | |
310 | await waitJobs(servers) | |
311 | ||
312 | await checkFollows({ | |
313 | follower: servers[0], | |
314 | followerState: 'rejected', | |
315 | following: servers[1], | |
316 | followingState: 'rejected' | |
317 | }) | |
318 | }) | |
319 | ||
320 | it('Should be able to re accept a previously rejected follower', async function () { | |
321 | await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host }) | |
322 | await waitJobs(servers) | |
323 | ||
324 | await checkFollows({ | |
325 | follower: servers[0], | |
326 | followerState: 'accepted', | |
327 | following: servers[1], | |
328 | followingState: 'accepted' | |
329 | }) | |
330 | }) | |
331 | }) | |
332 | ||
333 | describe('Muted servers', function () { | |
334 | ||
335 | it('Should ignore follow requests of muted servers', async function () { | |
336 | await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host }) | |
337 | ||
338 | await commands[0].unfollow({ target: servers[1] }) | |
339 | ||
340 | await waitJobs(servers) | |
341 | ||
342 | await checkFollows({ | |
343 | follower: servers[0], | |
344 | followerState: 'deleted', | |
345 | following: servers[1], | |
346 | followingState: 'deleted' | |
347 | }) | |
348 | ||
349 | await commands[0].follow({ hosts: [ servers[1].host ] }) | |
350 | await waitJobs(servers) | |
351 | ||
352 | await checkFollows({ | |
353 | follower: servers[0], | |
354 | followerState: 'rejected', | |
355 | following: servers[1], | |
356 | followingState: 'deleted' | |
357 | }) | |
358 | }) | |
359 | }) | |
360 | ||
361 | after(async function () { | |
362 | await cleanupTests(servers) | |
363 | }) | |
364 | }) |