]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
8424c402 | 2 | |
8424c402 | 3 | import 'mocha' |
f6500729 | 4 | import * as chai from 'chai' |
8424c402 C |
5 | import { |
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' |
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 | { | |
97ecddae | 24 | const res = await getFollowersListPaginationAndSort({ url: following.url, start: 0, count: 5, sort: '-createdAt' }) |
8424c402 C |
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 | { | |
97ecddae | 39 | const res = await getFollowingListPaginationAndSort({ url: follower.url, start: 0, count: 5, sort: '-createdAt' }) |
8424c402 C |
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 | ||
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 { | |
a1587156 C |
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 | } | |
8424c402 C |
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 | ||
6f1b4fa4 | 79 | servers = await flushAndRunMultipleServers(3) |
8424c402 C |
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) | |
2ba613a5 C |
147 | |
148 | config.followings.instance.autoFollowBack.enabled = false | |
149 | config.followers.instance.manualApproval = false | |
150 | await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) | |
8424c402 C |
151 | }) |
152 | }) | |
153 | ||
6f1b4fa4 C |
154 | describe('Auto follow index', function () { |
155 | const instanceIndexServer = new MockInstancesIndex() | |
f6500729 | 156 | let port: number |
6f1b4fa4 C |
157 | |
158 | before(async () => { | |
f6500729 | 159 | port = await instanceIndexServer.initialize() |
6f1b4fa4 C |
160 | }) |
161 | ||
162 | it('Should not auto follow index if the option is not enabled', async function () { | |
163 | this.timeout(30000) | |
164 | ||
165 | await wait(5000) | |
166 | await waitJobs(servers) | |
167 | ||
a1587156 C |
168 | await checkFollow(servers[0], servers[1], false) |
169 | await checkFollow(servers[1], servers[0], false) | |
6f1b4fa4 C |
170 | }) |
171 | ||
172 | it('Should auto follow the index', async function () { | |
173 | this.timeout(30000) | |
174 | ||
175 | instanceIndexServer.addInstance(servers[1].host) | |
176 | ||
177 | const config = { | |
178 | followings: { | |
179 | instance: { | |
180 | autoFollowIndex: { | |
f6500729 | 181 | indexUrl: `http://localhost:${port}/api/v1/instances/hosts`, |
6f1b4fa4 C |
182 | enabled: true |
183 | } | |
184 | } | |
185 | } | |
186 | } | |
187 | await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) | |
188 | ||
189 | await wait(5000) | |
190 | await waitJobs(servers) | |
191 | ||
a1587156 | 192 | await checkFollow(servers[0], servers[1], true) |
6f1b4fa4 C |
193 | |
194 | await resetFollows(servers) | |
195 | }) | |
196 | ||
197 | it('Should follow new added instances in the index but not old ones', async function () { | |
198 | this.timeout(30000) | |
199 | ||
200 | instanceIndexServer.addInstance(servers[2].host) | |
201 | ||
202 | await wait(5000) | |
203 | await waitJobs(servers) | |
204 | ||
a1587156 C |
205 | await checkFollow(servers[0], servers[1], false) |
206 | await checkFollow(servers[0], servers[2], true) | |
6f1b4fa4 C |
207 | }) |
208 | }) | |
209 | ||
8424c402 C |
210 | after(async function () { |
211 | await cleanupTests(servers) | |
212 | }) | |
213 | }) |