]>
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 | ||
99 | it('Should have server 1 following server 2', async function () { | |
100 | this.timeout(30000) | |
101 | ||
4d029ef8 | 102 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
0e9c48c2 C |
103 | |
104 | await waitJobs(servers) | |
105 | }) | |
106 | ||
107 | it('Should have correct follows', async function () { | |
14893eb7 | 108 | await checkServer1And2HasFollowers(servers) |
0e9c48c2 C |
109 | }) |
110 | ||
111 | it('Should remove follower on server 2', async function () { | |
de94ac86 C |
112 | this.timeout(10000) |
113 | ||
c3d29f69 | 114 | await commands[1].removeFollower({ follower: servers[0] }) |
0e9c48c2 C |
115 | |
116 | await waitJobs(servers) | |
117 | }) | |
118 | ||
119 | it('Should not not have follows anymore', async function () { | |
5b9c965d C |
120 | await checkNoFollowers(servers) |
121 | }) | |
122 | ||
123 | it('Should disable followers on server 2', async function () { | |
de94ac86 C |
124 | this.timeout(10000) |
125 | ||
5b9c965d C |
126 | const subConfig = { |
127 | followers: { | |
128 | instance: { | |
14893eb7 C |
129 | enabled: false, |
130 | manualApproval: false | |
5b9c965d C |
131 | } |
132 | } | |
0e9c48c2 C |
133 | } |
134 | ||
89d241a7 | 135 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) |
5b9c965d | 136 | |
4d029ef8 | 137 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
5b9c965d C |
138 | await waitJobs(servers) |
139 | ||
140 | await checkNoFollowers(servers) | |
141 | }) | |
142 | ||
143 | it('Should re enable followers on server 2', async function () { | |
de94ac86 C |
144 | this.timeout(10000) |
145 | ||
5b9c965d C |
146 | const subConfig = { |
147 | followers: { | |
148 | instance: { | |
14893eb7 C |
149 | enabled: true, |
150 | manualApproval: false | |
5b9c965d C |
151 | } |
152 | } | |
0e9c48c2 | 153 | } |
5b9c965d | 154 | |
89d241a7 | 155 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) |
5b9c965d | 156 | |
4d029ef8 | 157 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
5b9c965d C |
158 | await waitJobs(servers) |
159 | ||
14893eb7 C |
160 | await checkServer1And2HasFollowers(servers) |
161 | }) | |
162 | ||
163 | it('Should manually approve followers', async function () { | |
164 | this.timeout(20000) | |
165 | ||
927fa4b1 | 166 | await commands[0].unfollow({ target: servers[1] }) |
14893eb7 C |
167 | await waitJobs(servers) |
168 | ||
169 | const subConfig = { | |
170 | followers: { | |
171 | instance: { | |
172 | enabled: true, | |
173 | manualApproval: true | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
89d241a7 C |
178 | await servers[1].config.updateCustomSubConfig({ newConfig: subConfig }) |
179 | await servers[2].config.updateCustomSubConfig({ newConfig: subConfig }) | |
14893eb7 | 180 | |
4d029ef8 | 181 | await commands[0].follow({ hosts: [ servers[1].url ] }) |
14893eb7 C |
182 | await waitJobs(servers) |
183 | ||
184 | await checkServer1And2HasFollowers(servers, 'pending') | |
185 | }) | |
186 | ||
187 | it('Should accept a follower', async function () { | |
de94ac86 C |
188 | this.timeout(10000) |
189 | ||
927fa4b1 | 190 | await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host }) |
14893eb7 C |
191 | await waitJobs(servers) |
192 | ||
193 | await checkServer1And2HasFollowers(servers) | |
194 | }) | |
195 | ||
196 | it('Should reject another follower', async function () { | |
197 | this.timeout(20000) | |
198 | ||
4d029ef8 | 199 | await commands[0].follow({ hosts: [ servers[2].url ] }) |
14893eb7 C |
200 | await waitJobs(servers) |
201 | ||
202 | { | |
927fa4b1 | 203 | const body = await commands[0].getFollowings() |
c3d29f69 | 204 | expect(body.total).to.equal(2) |
14893eb7 C |
205 | } |
206 | ||
207 | { | |
927fa4b1 | 208 | const body = await commands[1].getFollowers() |
c3d29f69 | 209 | expect(body.total).to.equal(1) |
14893eb7 C |
210 | } |
211 | ||
212 | { | |
927fa4b1 | 213 | const body = await commands[2].getFollowers() |
c3d29f69 | 214 | expect(body.total).to.equal(1) |
14893eb7 C |
215 | } |
216 | ||
927fa4b1 | 217 | await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host }) |
14893eb7 C |
218 | await waitJobs(servers) |
219 | ||
927fa4b1 C |
220 | { // server 1 |
221 | { | |
222 | const { data } = await commands[0].getFollowings({ state: 'accepted' }) | |
223 | expect(data).to.have.lengthOf(1) | |
224 | } | |
14893eb7 | 225 | |
927fa4b1 C |
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 | } | |
14893eb7 | 244 | } |
0e9c48c2 C |
245 | }) |
246 | ||
927fa4b1 C |
247 | it('Should not change the follow on refollow with and without auto accept', async function () { |
248 | const run = async () => { | |
249 | await commands[0].follow({ hosts: [ servers[2].url ] }) | |
250 | await waitJobs(servers) | |
251 | ||
252 | await checkFollows({ | |
073deef8 C |
253 | follower: servers[0], |
254 | followerState: 'rejected', | |
255 | following: servers[2], | |
256 | followingState: 'rejected' | |
927fa4b1 C |
257 | }) |
258 | } | |
259 | ||
260 | await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } }) | |
261 | await run() | |
262 | ||
263 | await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } }) | |
264 | await run() | |
265 | }) | |
266 | ||
267 | it('Should not change the rejected status on unfollow', async function () { | |
268 | await commands[0].unfollow({ target: servers[2] }) | |
269 | await waitJobs(servers) | |
270 | ||
271 | await checkFollows({ | |
073deef8 C |
272 | follower: servers[0], |
273 | followerState: 'deleted', | |
274 | following: servers[2], | |
275 | followingState: 'rejected' | |
927fa4b1 C |
276 | }) |
277 | }) | |
278 | ||
279 | it('Should delete the follower and add again the follower', async function () { | |
280 | await commands[2].removeFollower({ follower: servers[0] }) | |
281 | await waitJobs(servers) | |
282 | ||
283 | await commands[0].follow({ hosts: [ servers[2].url ] }) | |
284 | await waitJobs(servers) | |
285 | ||
286 | await checkFollows({ | |
073deef8 C |
287 | follower: servers[0], |
288 | followerState: 'pending', | |
289 | following: servers[2], | |
290 | followingState: 'pending' | |
927fa4b1 C |
291 | }) |
292 | }) | |
293 | ||
294 | it('Should be able to reject a previously accepted follower', async function () { | |
295 | await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host }) | |
296 | await waitJobs(servers) | |
297 | ||
298 | await checkFollows({ | |
073deef8 C |
299 | follower: servers[0], |
300 | followerState: 'rejected', | |
301 | following: servers[1], | |
302 | followingState: 'rejected' | |
927fa4b1 C |
303 | }) |
304 | }) | |
305 | ||
306 | it('Should be able to re accept a previously rejected follower', async function () { | |
307 | await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host }) | |
308 | await waitJobs(servers) | |
309 | ||
310 | await checkFollows({ | |
073deef8 C |
311 | follower: servers[0], |
312 | followerState: 'accepted', | |
313 | following: servers[1], | |
314 | followingState: 'accepted' | |
927fa4b1 C |
315 | }) |
316 | }) | |
317 | ||
318 | it('Should ignore follow requests of muted servers', async function () { | |
073deef8 C |
319 | await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host }) |
320 | ||
321 | await commands[0].unfollow({ target: servers[1] }) | |
927fa4b1 | 322 | |
073deef8 C |
323 | await waitJobs(servers) |
324 | ||
325 | await checkFollows({ | |
326 | follower: servers[0], | |
327 | followerState: 'deleted', | |
328 | following: servers[1], | |
329 | followingState: 'deleted' | |
330 | }) | |
331 | ||
332 | await commands[0].follow({ hosts: [ servers[1].host ] }) | |
333 | await waitJobs(servers) | |
334 | ||
335 | await checkFollows({ | |
336 | follower: servers[0], | |
337 | followerState: 'rejected', | |
338 | following: servers[1], | |
339 | followingState: 'deleted' | |
340 | }) | |
927fa4b1 C |
341 | }) |
342 | ||
7c3b7976 C |
343 | after(async function () { |
344 | await cleanupTests(servers) | |
0e9c48c2 C |
345 | }) |
346 | }) |