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