aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/server/follows-moderation.ts
blob: 045024544a5de933db82bae437513f311e543188 (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import {
  cleanupTests,
  flushAndRunMultipleServers,
  FollowsCommand,
  ServerInfo,
  setAccessTokensToServers,
  waitJobs
} from '@shared/extra-utils'

const expect = chai.expect

async function checkServer1And2HasFollowers (servers: ServerInfo[], 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('http://localhost:' + servers[0].port + '/accounts/peertube')
    expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
  }
}

async function checkNoFollowers (servers: ServerInfo[]) {
  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(0)
  }
}

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

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

    servers = await flushAndRunMultipleServers(3)

    // Get the access tokens
    await setAccessTokensToServers(servers)

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

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

    await commands[0].follow({ targets: [ 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 () {
    this.timeout(10000)

    await commands[1].removeFollower({ follower: servers[0] })

    await waitJobs(servers)
  })

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

  it('Should disable followers on server 2', async function () {
    this.timeout(10000)

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

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

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

    await checkNoFollowers(servers)
  })

  it('Should re enable followers on server 2', async function () {
    this.timeout(10000)

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

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

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

    await checkServer1And2HasFollowers(servers)
  })

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

    await commands[1].removeFollower({ follower: servers[0] })
    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({ targets: [ servers[1].url ] })
    await waitJobs(servers)

    await checkServer1And2HasFollowers(servers, 'pending')
  })

  it('Should accept a follower', async function () {
    this.timeout(10000)

    await commands[1].acceptFollower({ follower: 'peertube@localhost:' + servers[0].port })
    await waitJobs(servers)

    await checkServer1And2HasFollowers(servers)
  })

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

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

    {
      const body = await commands[0].getFollowings({ start: 0, count: 5, sort: 'createdAt' })
      expect(body.total).to.equal(2)
    }

    {
      const body = await commands[1].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
      expect(body.total).to.equal(1)
    }

    {
      const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
      expect(body.total).to.equal(1)
    }

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

    await checkServer1And2HasFollowers(servers)

    {
      const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
      expect(body.total).to.equal(0)
    }
  })

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