]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
0e9c48c2 | 2 | |
86347717 | 3 | import { expect } from 'chai' |
927fa4b1 C |
4 | import { expectStartWith } from '@server/tests/shared' |
5 | import { ActorFollow, FollowState } from '@shared/models' | |
5b9c965d | 6 | import { |
7243f84d | 7 | cleanupTests, |
254d3579 | 8 | createMultipleServers, |
c3d29f69 | 9 | FollowsCommand, |
254d3579 | 10 | PeerTubeServer, |
5b9c965d | 11 | setAccessTokensToServers, |
c3d29f69 | 12 | waitJobs |
bf54587a | 13 | } from '@shared/server-commands' |
0e9c48c2 | 14 | |
254d3579 | 15 | async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') { |
c3d29f69 | 16 | const fns = [ |
89d241a7 C |
17 | servers[0].follows.getFollowings.bind(servers[0].follows), |
18 | servers[1].follows.getFollowers.bind(servers[1].follows) | |
c3d29f69 | 19 | ] |
5b9c965d | 20 | |
c3d29f69 C |
21 | for (const fn of fns) { |
22 | const body = await fn({ start: 0, count: 5, sort: 'createdAt' }) | |
23 | expect(body.total).to.equal(1) | |
5b9c965d | 24 | |
c3d29f69 | 25 | const follow = body.data[0] |
14893eb7 | 26 | expect(follow.state).to.equal(state) |
927fa4b1 C |
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: { | |
073deef8 C |
33 | follower: PeerTubeServer |
34 | followerState: FollowState | 'deleted' | |
35 | ||
36 | following: PeerTubeServer | |
37 | followingState: FollowState | 'deleted' | |
927fa4b1 | 38 | }) { |
073deef8 | 39 | const { follower, followerState, followingState, following } = options |
927fa4b1 | 40 | |
073deef8 C |
41 | const followerUrl = follower.url + '/accounts/peertube' |
42 | const followingUrl = following.url + '/accounts/peertube' | |
927fa4b1 C |
43 | const finder = (d: ActorFollow) => d.follower.url === followerUrl && d.following.url === followingUrl |
44 | ||
45 | { | |
073deef8 | 46 | const { data } = await follower.follows.getFollowings() |
927fa4b1 C |
47 | const follow = data.find(finder) |
48 | ||
073deef8 | 49 | if (followerState === 'deleted') { |
927fa4b1 C |
50 | expect(follow).to.not.exist |
51 | } else { | |
073deef8 | 52 | expect(follow.state).to.equal(followerState) |
927fa4b1 C |
53 | expect(follow.follower.url).to.equal(followerUrl) |
54 | expect(follow.following.url).to.equal(followingUrl) | |
55 | } | |
56 | } | |
57 | ||
58 | { | |
073deef8 | 59 | const { data } = await following.follows.getFollowers() |
927fa4b1 C |
60 | const follow = data.find(finder) |
61 | ||
073deef8 | 62 | if (followingState === 'deleted') { |
927fa4b1 C |
63 | expect(follow).to.not.exist |
64 | } else { | |
073deef8 | 65 | expect(follow.state).to.equal(followingState) |
927fa4b1 C |
66 | expect(follow.follower.url).to.equal(followerUrl) |
67 | expect(follow.following.url).to.equal(followingUrl) | |
68 | } | |
5b9c965d C |
69 | } |
70 | } | |
71 | ||
254d3579 | 72 | async function checkNoFollowers (servers: PeerTubeServer[]) { |
c3d29f69 | 73 | const fns = [ |
89d241a7 C |
74 | servers[0].follows.getFollowings.bind(servers[0].follows), |
75 | servers[1].follows.getFollowers.bind(servers[1].follows) | |
c3d29f69 C |
76 | ] |
77 | ||
78 | for (const fn of fns) { | |
927fa4b1 | 79 | const body = await fn({ start: 0, count: 5, sort: 'createdAt', state: 'accepted' }) |
c3d29f69 | 80 | expect(body.total).to.equal(0) |
5b9c965d C |
81 | } |
82 | } | |
83 | ||
0e9c48c2 | 84 | describe('Test follows moderation', function () { |
254d3579 | 85 | let servers: PeerTubeServer[] = [] |
c3d29f69 | 86 | let commands: FollowsCommand[] |
0e9c48c2 C |
87 | |
88 | before(async function () { | |
3b2006bb | 89 | this.timeout(240000) |
0e9c48c2 | 90 | |
254d3579 | 91 | servers = await createMultipleServers(3) |
0e9c48c2 C |
92 | |
93 | // Get the access tokens | |
94 | await setAccessTokensToServers(servers) | |
c3d29f69 | 95 | |
89d241a7 | 96 | commands = servers.map(s => s.follows) |
0e9c48c2 C |
97 | }) |
98 | ||
a6b26afc | 99 | describe('Default behaviour', function () { |
0e9c48c2 | 100 | |
a6b26afc C |
101 | it('Should have server 1 following server 2', async function () { |
102 | this.timeout(30000) | |
0e9c48c2 | 103 | |
a6b26afc | 104 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
0e9c48c2 | 105 | |
a6b26afc C |
106 | await waitJobs(servers) |
107 | }) | |
0e9c48c2 | 108 | |
a6b26afc C |
109 | it('Should have correct follows', async function () { |
110 | await checkServer1And2HasFollowers(servers) | |
111 | }) | |
de94ac86 | 112 | |
a6b26afc | 113 | it('Should remove follower on server 2', async function () { |
a6b26afc | 114 | await commands[1].removeFollower({ follower: servers[0] }) |
0e9c48c2 | 115 | |
a6b26afc C |
116 | await waitJobs(servers) |
117 | }) | |
118 | ||
119 | it('Should not not have follows anymore', async function () { | |
120 | await checkNoFollowers(servers) | |
121 | }) | |
5b9c965d C |
122 | }) |
123 | ||
a6b26afc | 124 | describe('Disabled/Enabled followers', function () { |
de94ac86 | 125 | |
a6b26afc | 126 | it('Should disable followers on server 2', async function () { |
a6b26afc C |
127 | const subConfig = { |
128 | followers: { | |
129 | instance: { | |
130 | enabled: false, | |
131 | manualApproval: false | |
132 | } | |
5b9c965d C |
133 | } |
134 | } | |
0e9c48c2 | 135 | |
a6b26afc | 136 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) |
5b9c965d | 137 | |
a6b26afc C |
138 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
139 | await waitJobs(servers) | |
5b9c965d | 140 | |
a6b26afc C |
141 | await checkNoFollowers(servers) |
142 | }) | |
5b9c965d | 143 | |
a6b26afc | 144 | it('Should re enable followers on server 2', async function () { |
a6b26afc C |
145 | const subConfig = { |
146 | followers: { | |
147 | instance: { | |
148 | enabled: true, | |
149 | manualApproval: false | |
150 | } | |
5b9c965d C |
151 | } |
152 | } | |
5b9c965d | 153 | |
a6b26afc | 154 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) |
5b9c965d | 155 | |
a6b26afc C |
156 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
157 | await waitJobs(servers) | |
5b9c965d | 158 | |
a6b26afc C |
159 | await checkServer1And2HasFollowers(servers) |
160 | }) | |
14893eb7 C |
161 | }) |
162 | ||
a6b26afc C |
163 | describe('Manual approbation', function () { |
164 | ||
165 | it('Should manually approve followers', async function () { | |
166 | this.timeout(20000) | |
14893eb7 | 167 | |
a6b26afc C |
168 | await commands[0].unfollow({ target: servers[1] }) |
169 | await waitJobs(servers) | |
14893eb7 | 170 | |
a6b26afc C |
171 | const subConfig = { |
172 | followers: { | |
173 | instance: { | |
174 | enabled: true, | |
175 | manualApproval: true | |
176 | } | |
14893eb7 C |
177 | } |
178 | } | |
de94ac86 | 179 | |
a6b26afc C |
180 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) |
181 | await servers[2].config.updateCustomSubConfig({ newConfig: subConfig }) | |
14893eb7 | 182 | |
a6b26afc C |
183 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
184 | await waitJobs(servers) | |
14893eb7 | 185 | |
a6b26afc C |
186 | await checkServer1And2HasFollowers(servers, 'pending') |
187 | }) | |
14893eb7 | 188 | |
a6b26afc | 189 | it('Should accept a follower', async function () { |
a6b26afc C |
190 | await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host }) |
191 | await waitJobs(servers) | |
14893eb7 | 192 | |
a6b26afc C |
193 | await checkServer1And2HasFollowers(servers) |
194 | }) | |
14893eb7 | 195 | |
a6b26afc C |
196 | it('Should reject another follower', async function () { |
197 | this.timeout(20000) | |
14893eb7 | 198 | |
a6b26afc C |
199 | await commands[0].follow({ hosts: [ servers[2].url ] }) |
200 | await waitJobs(servers) | |
14893eb7 | 201 | |
927fa4b1 | 202 | { |
a6b26afc C |
203 | const body = await commands[0].getFollowings() |
204 | expect(body.total).to.equal(2) | |
927fa4b1 | 205 | } |
14893eb7 | 206 | |
927fa4b1 | 207 | { |
a6b26afc C |
208 | const body = await commands[1].getFollowers() |
209 | expect(body.total).to.equal(1) | |
927fa4b1 | 210 | } |
927fa4b1 | 211 | |
927fa4b1 | 212 | { |
a6b26afc C |
213 | const body = await commands[2].getFollowers() |
214 | expect(body.total).to.equal(1) | |
927fa4b1 C |
215 | } |
216 | ||
a6b26afc C |
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 | } | |
927fa4b1 | 231 | } |
a6b26afc C |
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 | }) | |
0e9c48c2 C |
257 | }) |
258 | ||
a6b26afc C |
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] }) | |
927fa4b1 C |
283 | await waitJobs(servers) |
284 | ||
285 | await checkFollows({ | |
073deef8 | 286 | follower: servers[0], |
a6b26afc | 287 | followerState: 'deleted', |
073deef8 C |
288 | following: servers[2], |
289 | followingState: 'rejected' | |
927fa4b1 | 290 | }) |
927fa4b1 | 291 | }) |
927fa4b1 | 292 | |
a6b26afc C |
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) | |
927fa4b1 | 296 | |
a6b26afc C |
297 | await commands[0].follow({ hosts: [ servers[2].url ] }) |
298 | await waitJobs(servers) | |
927fa4b1 | 299 | |
a6b26afc C |
300 | await checkFollows({ |
301 | follower: servers[0], | |
302 | followerState: 'pending', | |
303 | following: servers[2], | |
304 | followingState: 'pending' | |
305 | }) | |
927fa4b1 | 306 | }) |
927fa4b1 | 307 | |
a6b26afc C |
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) | |
927fa4b1 | 311 | |
a6b26afc C |
312 | await checkFollows({ |
313 | follower: servers[0], | |
314 | followerState: 'rejected', | |
315 | following: servers[1], | |
316 | followingState: 'rejected' | |
317 | }) | |
927fa4b1 | 318 | }) |
927fa4b1 | 319 | |
a6b26afc C |
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) | |
927fa4b1 | 323 | |
a6b26afc C |
324 | await checkFollows({ |
325 | follower: servers[0], | |
326 | followerState: 'accepted', | |
327 | following: servers[1], | |
328 | followingState: 'accepted' | |
329 | }) | |
927fa4b1 C |
330 | }) |
331 | }) | |
332 | ||
a6b26afc | 333 | describe('Muted servers', function () { |
073deef8 | 334 | |
a6b26afc C |
335 | it('Should ignore follow requests of muted servers', async function () { |
336 | await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host }) | |
927fa4b1 | 337 | |
a6b26afc | 338 | await commands[0].unfollow({ target: servers[1] }) |
073deef8 | 339 | |
a6b26afc C |
340 | await waitJobs(servers) |
341 | ||
342 | await checkFollows({ | |
343 | follower: servers[0], | |
344 | followerState: 'deleted', | |
345 | following: servers[1], | |
346 | followingState: 'deleted' | |
347 | }) | |
073deef8 | 348 | |
a6b26afc C |
349 | await commands[0].follow({ hosts: [ servers[1].host ] }) |
350 | await waitJobs(servers) | |
073deef8 | 351 | |
a6b26afc C |
352 | await checkFollows({ |
353 | follower: servers[0], | |
354 | followerState: 'rejected', | |
355 | following: servers[1], | |
356 | followingState: 'deleted' | |
357 | }) | |
073deef8 | 358 | }) |
927fa4b1 C |
359 | }) |
360 | ||
7c3b7976 C |
361 | after(async function () { |
362 | await cleanupTests(servers) | |
0e9c48c2 C |
363 | }) |
364 | }) |