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