]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/auto-follows.ts
Implement auto follow in client
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / auto-follows.ts
CommitLineData
8424c402
C
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import {
6 acceptFollower,
7 cleanupTests,
8 flushAndRunMultipleServers,
6f1b4fa4 9 MockInstancesIndex,
8424c402
C
10 ServerInfo,
11 setAccessTokensToServers,
12 unfollow,
6f1b4fa4
C
13 updateCustomSubConfig,
14 wait
8424c402
C
15} from '../../../../shared/extra-utils/index'
16import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
17import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18import { ActorFollow } from '../../../../shared/models/actors'
19
20const expect = chai.expect
21
22async function checkFollow (follower: ServerInfo, following: ServerInfo, exists: boolean) {
23 {
24 const res = await getFollowersListPaginationAndSort(following.url, 0, 5, '-createdAt')
25 const follows = res.body.data as ActorFollow[]
26
6f1b4fa4
C
27 const follow = follows.find(f => {
28 return f.follower.host === follower.host && f.state === 'accepted'
29 })
8424c402 30
6f1b4fa4
C
31 if (exists === true) {
32 expect(follow).to.exist
8424c402 33 } else {
6f1b4fa4 34 expect(follow).to.be.undefined
8424c402
C
35 }
36 }
37
38 {
39 const res = await getFollowingListPaginationAndSort(follower.url, 0, 5, '-createdAt')
40 const follows = res.body.data as ActorFollow[]
41
6f1b4fa4
C
42 const follow = follows.find(f => {
43 return f.following.host === following.host && f.state === 'accepted'
44 })
8424c402 45
6f1b4fa4
C
46 if (exists === true) {
47 expect(follow).to.exist
8424c402 48 } else {
6f1b4fa4 49 expect(follow).to.be.undefined
8424c402
C
50 }
51 }
52}
53
54async function server1Follows2 (servers: ServerInfo[]) {
55 await follow(servers[0].url, [ servers[1].host ], servers[0].accessToken)
56
57 await waitJobs(servers)
58}
59
60async function resetFollows (servers: ServerInfo[]) {
61 try {
62 await unfollow(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ])
63 await unfollow(servers[ 1 ].url, servers[ 1 ].accessToken, servers[ 0 ])
64 } catch { /* empty */ }
65
66 await waitJobs(servers)
67
68 await checkFollow(servers[0], servers[1], false)
69 await checkFollow(servers[1], servers[0], false)
70}
71
72describe('Test auto follows', function () {
73 let servers: ServerInfo[] = []
74
75 before(async function () {
76 this.timeout(30000)
77
6f1b4fa4 78 servers = await flushAndRunMultipleServers(3)
8424c402
C
79
80 // Get the access tokens
81 await setAccessTokensToServers(servers)
82 })
83
84 describe('Auto follow back', function () {
85
86 it('Should not auto follow back if the option is not enabled', async function () {
87 this.timeout(15000)
88
89 await server1Follows2(servers)
90
91 await checkFollow(servers[0], servers[1], true)
92 await checkFollow(servers[1], servers[0], false)
93
94 await resetFollows(servers)
95 })
96
97 it('Should auto follow back on auto accept if the option is enabled', async function () {
98 this.timeout(15000)
99
100 const config = {
101 followings: {
102 instance: {
103 autoFollowBack: { enabled: true }
104 }
105 }
106 }
107 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
108
109 await server1Follows2(servers)
110
111 await checkFollow(servers[0], servers[1], true)
112 await checkFollow(servers[1], servers[0], true)
113
114 await resetFollows(servers)
115 })
116
117 it('Should wait the acceptation before auto follow back', async function () {
118 this.timeout(30000)
119
120 const config = {
121 followings: {
122 instance: {
123 autoFollowBack: { enabled: true }
124 }
125 },
126 followers: {
127 instance: {
128 manualApproval: true
129 }
130 }
131 }
132 await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
133
134 await server1Follows2(servers)
135
136 await checkFollow(servers[0], servers[1], false)
137 await checkFollow(servers[1], servers[0], false)
138
139 await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@' + servers[0].host)
140 await waitJobs(servers)
141
142 await checkFollow(servers[0], servers[1], true)
143 await checkFollow(servers[1], servers[0], true)
144
145 await resetFollows(servers)
146 })
147 })
148
6f1b4fa4
C
149 describe('Auto follow index', function () {
150 const instanceIndexServer = new MockInstancesIndex()
151
152 before(async () => {
153 await instanceIndexServer.initialize()
154 })
155
156 it('Should not auto follow index if the option is not enabled', async function () {
157 this.timeout(30000)
158
159 await wait(5000)
160 await waitJobs(servers)
161
162 await checkFollow(servers[ 0 ], servers[ 1 ], false)
163 await checkFollow(servers[ 1 ], servers[ 0 ], false)
164 })
165
166 it('Should auto follow the index', async function () {
167 this.timeout(30000)
168
169 instanceIndexServer.addInstance(servers[1].host)
170
171 const config = {
172 followings: {
173 instance: {
174 autoFollowIndex: {
175 indexUrl: 'http://localhost:42100',
176 enabled: true
177 }
178 }
179 }
180 }
181 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
182
183 await wait(5000)
184 await waitJobs(servers)
185
186 await checkFollow(servers[ 0 ], servers[ 1 ], true)
187
188 await resetFollows(servers)
189 })
190
191 it('Should follow new added instances in the index but not old ones', async function () {
192 this.timeout(30000)
193
194 instanceIndexServer.addInstance(servers[2].host)
195
196 await wait(5000)
197 await waitJobs(servers)
198
199 await checkFollow(servers[ 0 ], servers[ 1 ], false)
200 await checkFollow(servers[ 0 ], servers[ 2 ], true)
201 })
202 })
203
8424c402
C
204 after(async function () {
205 await cleanupTests(servers)
206 })
207})