]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/follows-moderation.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows-moderation.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
0e9c48c2 2
0e9c48c2 3import 'mocha'
c3d29f69 4import * as chai from 'chai'
5b9c965d 5import {
7243f84d 6 cleanupTests,
254d3579 7 createMultipleServers,
c3d29f69 8 FollowsCommand,
254d3579 9 PeerTubeServer,
5b9c965d 10 setAccessTokensToServers,
c3d29f69
C
11 waitJobs
12} from '@shared/extra-utils'
0e9c48c2
C
13
14const expect = chai.expect
15
254d3579 16async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
c3d29f69 17 const fns = [
89d241a7
C
18 servers[0].follows.getFollowings.bind(servers[0].follows),
19 servers[1].follows.getFollowers.bind(servers[1].follows)
c3d29f69 20 ]
5b9c965d 21
c3d29f69
C
22 for (const fn of fns) {
23 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
24 expect(body.total).to.equal(1)
5b9c965d 25
c3d29f69 26 const follow = body.data[0]
14893eb7 27 expect(follow.state).to.equal(state)
48f07b4a 28 expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube')
7243f84d 29 expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
5b9c965d
C
30 }
31}
32
254d3579 33async function checkNoFollowers (servers: PeerTubeServer[]) {
c3d29f69 34 const fns = [
89d241a7
C
35 servers[0].follows.getFollowings.bind(servers[0].follows),
36 servers[1].follows.getFollowers.bind(servers[1].follows)
c3d29f69
C
37 ]
38
39 for (const fn of fns) {
40 const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
41 expect(body.total).to.equal(0)
5b9c965d
C
42 }
43}
44
0e9c48c2 45describe('Test follows moderation', function () {
254d3579 46 let servers: PeerTubeServer[] = []
c3d29f69 47 let commands: FollowsCommand[]
0e9c48c2
C
48
49 before(async function () {
50 this.timeout(30000)
51
254d3579 52 servers = await createMultipleServers(3)
0e9c48c2
C
53
54 // Get the access tokens
55 await setAccessTokensToServers(servers)
c3d29f69 56
89d241a7 57 commands = servers.map(s => s.follows)
0e9c48c2
C
58 })
59
60 it('Should have server 1 following server 2', async function () {
61 this.timeout(30000)
62
c3d29f69 63 await commands[0].follow({ targets: [ servers[1].url ] })
0e9c48c2
C
64
65 await waitJobs(servers)
66 })
67
68 it('Should have correct follows', async function () {
14893eb7 69 await checkServer1And2HasFollowers(servers)
0e9c48c2
C
70 })
71
72 it('Should remove follower on server 2', async function () {
de94ac86
C
73 this.timeout(10000)
74
c3d29f69 75 await commands[1].removeFollower({ follower: servers[0] })
0e9c48c2
C
76
77 await waitJobs(servers)
78 })
79
80 it('Should not not have follows anymore', async function () {
5b9c965d
C
81 await checkNoFollowers(servers)
82 })
83
84 it('Should disable followers on server 2', async function () {
de94ac86
C
85 this.timeout(10000)
86
5b9c965d
C
87 const subConfig = {
88 followers: {
89 instance: {
14893eb7
C
90 enabled: false,
91 manualApproval: false
5b9c965d
C
92 }
93 }
0e9c48c2
C
94 }
95
89d241a7 96 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
5b9c965d 97
c3d29f69 98 await commands[0].follow({ targets: [ servers[1].url ] })
5b9c965d
C
99 await waitJobs(servers)
100
101 await checkNoFollowers(servers)
102 })
103
104 it('Should re enable followers on server 2', async function () {
de94ac86
C
105 this.timeout(10000)
106
5b9c965d
C
107 const subConfig = {
108 followers: {
109 instance: {
14893eb7
C
110 enabled: true,
111 manualApproval: false
5b9c965d
C
112 }
113 }
0e9c48c2 114 }
5b9c965d 115
89d241a7 116 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
5b9c965d 117
c3d29f69 118 await commands[0].follow({ targets: [ servers[1].url ] })
5b9c965d
C
119 await waitJobs(servers)
120
14893eb7
C
121 await checkServer1And2HasFollowers(servers)
122 })
123
124 it('Should manually approve followers', async function () {
125 this.timeout(20000)
126
c3d29f69 127 await commands[1].removeFollower({ follower: servers[0] })
14893eb7
C
128 await waitJobs(servers)
129
130 const subConfig = {
131 followers: {
132 instance: {
133 enabled: true,
134 manualApproval: true
135 }
136 }
137 }
138
89d241a7
C
139 await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
140 await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
14893eb7 141
c3d29f69 142 await commands[0].follow({ targets: [ servers[1].url ] })
14893eb7
C
143 await waitJobs(servers)
144
145 await checkServer1And2HasFollowers(servers, 'pending')
146 })
147
148 it('Should accept a follower', async function () {
de94ac86
C
149 this.timeout(10000)
150
c3d29f69 151 await commands[1].acceptFollower({ follower: 'peertube@localhost:' + servers[0].port })
14893eb7
C
152 await waitJobs(servers)
153
154 await checkServer1And2HasFollowers(servers)
155 })
156
157 it('Should reject another follower', async function () {
158 this.timeout(20000)
159
c3d29f69 160 await commands[0].follow({ targets: [ servers[2].url ] })
14893eb7
C
161 await waitJobs(servers)
162
163 {
c3d29f69
C
164 const body = await commands[0].getFollowings({ start: 0, count: 5, sort: 'createdAt' })
165 expect(body.total).to.equal(2)
14893eb7
C
166 }
167
168 {
c3d29f69
C
169 const body = await commands[1].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
170 expect(body.total).to.equal(1)
14893eb7
C
171 }
172
173 {
c3d29f69
C
174 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
175 expect(body.total).to.equal(1)
14893eb7
C
176 }
177
c3d29f69 178 await commands[2].rejectFollower({ follower: 'peertube@localhost:' + servers[0].port })
14893eb7
C
179 await waitJobs(servers)
180
181 await checkServer1And2HasFollowers(servers)
182
183 {
c3d29f69
C
184 const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
185 expect(body.total).to.equal(0)
14893eb7 186 }
0e9c48c2
C
187 })
188
7c3b7976
C
189 after(async function () {
190 await cleanupTests(servers)
0e9c48c2
C
191 })
192})