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