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