aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/src/api/server/follows-moderation.ts
blob: 811dd5c221578c7fa080cb6aab30948137b88e03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { expectStartWith } from '@tests/shared/checks.js'
import { ActorFollow, FollowState } from '@peertube/peertube-models'
import {
  cleanupTests,
  createMultipleServers,
  FollowsCommand,
  PeerTubeServer,
  setAccessTokensToServers,
  waitJobs
} from '@peertube/peertube-server-commands'

async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
  const fns = [
    servers[0].follows.getFollowings.bind(servers[0].follows),
    servers[1].follows.getFollowers.bind(servers[1].follows)
  ]

  for (const fn of fns) {
    const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
    expect(body.total).to.equal(1)

    const follow = body.data[0]
    expect(follow.state).to.equal(state)
    expect(follow.follower.url).to.equal(servers[0].url + '/accounts/peertube')
    expect(follow.following.url).to.equal(servers[1].url + '/accounts/peertube')
  }
}

async function checkFollows (options: {
  follower: PeerTubeServer
  followerState: FollowState | 'deleted'

  following: PeerTubeServer
  followingState: FollowState | 'deleted'
}) {
  const { follower, followerState, followingState, following } = options

  const followerUrl = follower.url + '/accounts/peertube'
  const followingUrl = following.url + '/accounts/peertube'
  const finder = (d: ActorFollow) => d.follower.url === followerUrl && d.following.url === followingUrl

  {
    const { data } = await follower.follows.getFollowings()
    const follow = data.find(finder)

    if (followerState === 'deleted') {
      expect(follow).to.not.exist
    } else {
      expect(follow.state).to.equal(followerState)
      expect(follow.follower.url).to.equal(followerUrl)
      expect(follow.following.url).to.equal(followingUrl)
    }
  }

  {
    const { data } = await following.follows.getFollowers()
    const follow = data.find(finder)

    if (followingState === 'deleted') {
      expect(follow).to.not.exist
    } else {
      expect(follow.state).to.equal(followingState)
      expect(follow.follower.url).to.equal(followerUrl)
      expect(follow.following.url).to.equal(followingUrl)
    }
  }
}

async function checkNoFollowers (servers: PeerTubeServer[]) {
  const fns = [
    servers[0].follows.getFollowings.bind(servers[0].follows),
    servers[1].follows.getFollowers.bind(servers[1].follows)
  ]

  for (const fn of fns) {
    const body = await fn({ start: 0, count: 5, sort: 'createdAt', state: 'accepted' })
    expect(body.total).to.equal(0)
  }
}

describe('Test follows moderation', function () {
  let servers: PeerTubeServer[] = []
  let commands: FollowsCommand[]

  before(async function () {
    this.timeout(240000)

    servers = await createMultipleServers(3)

    // Get the access tokens
    await setAccessTokensToServers(servers)

    commands = servers.map(s => s.follows)
  })

  describe('Default behaviour', function () {

    it('Should have server 1 following server 2', async function () {
      this.timeout(30000)

      await commands[0].follow({ hosts: [ servers[1].url ] })

      await waitJobs(servers)
    })

    it('Should have correct follows', async function () {
      await checkServer1And2HasFollowers(servers)
    })

    it('Should remove follower on server 2', async function () {
      await commands[1].removeFollower({ follower: servers[0] })

      await waitJobs(servers)
    })

    it('Should not not have follows anymore', async function () {
      await checkNoFollowers(servers)
    })
  })

  describe('Disabled/Enabled followers', function () {

    it('Should disable followers on server 2', async function () {
      const subConfig = {
        followers: {
          instance: {
            enabled: false,
            manualApproval: false
          }
        }
      }

      await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })

      await commands[0].follow({ hosts: [ servers[1].url ] })
      await waitJobs(servers)

      await checkNoFollowers(servers)
    })

    it('Should re enable followers on server 2', async function () {
      const subConfig = {
        followers: {
          instance: {
            enabled: true,
            manualApproval: false
          }
        }
      }

      await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })

      await commands[0].follow({ hosts: [ servers[1].url ] })
      await waitJobs(servers)

      await checkServer1And2HasFollowers(servers)
    })
  })

  describe('Manual approbation', function () {

    it('Should manually approve followers', async function () {
      this.timeout(20000)

      await commands[0].unfollow({ target: servers[1] })
      await waitJobs(servers)

      const subConfig = {
        followers: {
          instance: {
            enabled: true,
            manualApproval: true
          }
        }
      }

      await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
      await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })

      await commands[0].follow({ hosts: [ servers[1].url ] })
      await waitJobs(servers)

      await checkServer1And2HasFollowers(servers, 'pending')
    })

    it('Should accept a follower', async function () {
      await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
      await waitJobs(servers)

      await checkServer1And2HasFollowers(servers)
    })

    it('Should reject another follower', async function () {
      this.timeout(20000)

      await commands[0].follow({ hosts: [ servers[2].url ] })
      await waitJobs(servers)

      {
        const body = await commands[0].getFollowings()
        expect(body.total).to.equal(2)
      }

      {
        const body = await commands[1].getFollowers()
        expect(body.total).to.equal(1)
      }

      {
        const body = await commands[2].getFollowers()
        expect(body.total).to.equal(1)
      }

      await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
      await waitJobs(servers)

      { // server 1
        {
          const { data } = await commands[0].getFollowings({ state: 'accepted' })
          expect(data).to.have.lengthOf(1)
        }

        {
          const { data } = await commands[0].getFollowings({ state: 'rejected' })
          expect(data).to.have.lengthOf(1)
          expectStartWith(data[0].following.url, servers[2].url)
        }
      }

      { // server 3
        {
          const { data } = await commands[2].getFollowers({ state: 'accepted' })
          expect(data).to.have.lengthOf(0)
        }

        {
          const { data } = await commands[2].getFollowers({ state: 'rejected' })
          expect(data).to.have.lengthOf(1)
          expectStartWith(data[0].follower.url, servers[0].url)
        }
      }
    })

    it('Should still auto accept channel followers', async function () {
      await commands[0].follow({ handles: [ 'root_channel@' + servers[1].host ] })

      await waitJobs(servers)

      const body = await commands[0].getFollowings()
      const follow = body.data[0]
      expect(follow.following.name).to.equal('root_channel')
      expect(follow.state).to.equal('accepted')
    })
  })

  describe('Accept/reject state', function () {

    it('Should not change the follow on refollow with and without auto accept', async function () {
      const run = async () => {
        await commands[0].follow({ hosts: [ servers[2].url ] })
        await waitJobs(servers)

        await checkFollows({
          follower: servers[0],
          followerState: 'rejected',
          following: servers[2],
          followingState: 'rejected'
        })
      }

      await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
      await run()

      await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
      await run()
    })

    it('Should not change the rejected status on unfollow', async function () {
      await commands[0].unfollow({ target: servers[2] })
      await waitJobs(servers)

      await checkFollows({
        follower: servers[0],
        followerState: 'deleted',
        following: servers[2],
        followingState: 'rejected'
      })
    })

    it('Should delete the follower and add again the follower', async function () {
      await commands[2].removeFollower({ follower: servers[0] })
      await waitJobs(servers)

      await commands[0].follow({ hosts: [ servers[2].url ] })
      await waitJobs(servers)

      await checkFollows({
        follower: servers[0],
        followerState: 'pending',
        following: servers[2],
        followingState: 'pending'
      })
    })

    it('Should be able to reject a previously accepted follower', async function () {
      await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host })
      await waitJobs(servers)

      await checkFollows({
        follower: servers[0],
        followerState: 'rejected',
        following: servers[1],
        followingState: 'rejected'
      })
    })

    it('Should be able to re accept a previously rejected follower', async function () {
      await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
      await waitJobs(servers)

      await checkFollows({
        follower: servers[0],
        followerState: 'accepted',
        following: servers[1],
        followingState: 'accepted'
      })
    })
  })

  describe('Muted servers', function () {

    it('Should ignore follow requests of muted servers', async function () {
      await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host })

      await commands[0].unfollow({ target: servers[1] })

      await waitJobs(servers)

      await checkFollows({
        follower: servers[0],
        followerState: 'deleted',
        following: servers[1],
        followingState: 'deleted'
      })

      await commands[0].follow({ hosts: [ servers[1].host ] })
      await waitJobs(servers)

      await checkFollows({
        follower: servers[0],
        followerState: 'rejected',
        following: servers[1],
        followingState: 'deleted'
      })
    })
  })

  after(async function () {
    await cleanupTests(servers)
  })
})