]>
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 | 5 | import { |
8424c402 C |
6 | cleanupTests, |
7 | flushAndRunMultipleServers, | |
6f1b4fa4 | 8 | MockInstancesIndex, |
8424c402 C |
9 | ServerInfo, |
10 | setAccessTokensToServers, | |
6f1b4fa4 | 11 | updateCustomSubConfig, |
c3d29f69 C |
12 | wait, |
13 | waitJobs | |
14 | } from '@shared/extra-utils' | |
8424c402 C |
15 | |
16 | const expect = chai.expect | |
17 | ||
18 | async function checkFollow (follower: ServerInfo, following: ServerInfo, exists: boolean) { | |
19 | { | |
c3d29f69 C |
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') | |
8424c402 | 22 | |
c3d29f69 C |
23 | if (exists === true) expect(follow).to.exist |
24 | else expect(follow).to.be.undefined | |
8424c402 C |
25 | } |
26 | ||
27 | { | |
c3d29f69 C |
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') | |
8424c402 | 30 | |
c3d29f69 C |
31 | if (exists === true) expect(follow).to.exist |
32 | else expect(follow).to.be.undefined | |
8424c402 C |
33 | } |
34 | } | |
35 | ||
36 | async function server1Follows2 (servers: ServerInfo[]) { | |
c3d29f69 | 37 | await servers[0].followsCommand.follow({ targets: [ servers[1].host ] }) |
8424c402 C |
38 | |
39 | await waitJobs(servers) | |
40 | } | |
41 | ||
42 | async function resetFollows (servers: ServerInfo[]) { | |
43 | try { | |
c3d29f69 C |
44 | await servers[0].followsCommand.unfollow({ target: servers[1] }) |
45 | await servers[1].followsCommand.unfollow({ target: servers[0] }) | |
a1587156 C |
46 | } catch { /* empty */ |
47 | } | |
8424c402 C |
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 | ||
6f1b4fa4 | 61 | servers = await flushAndRunMultipleServers(3) |
8424c402 C |
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 | ||
c3d29f69 | 122 | await servers[1].followsCommand.acceptFollower({ follower: 'peertube@' + servers[0].host }) |
8424c402 C |
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) | |
2ba613a5 C |
129 | |
130 | config.followings.instance.autoFollowBack.enabled = false | |
131 | config.followers.instance.manualApproval = false | |
132 | await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) | |
8424c402 C |
133 | }) |
134 | }) | |
135 | ||
6f1b4fa4 C |
136 | describe('Auto follow index', function () { |
137 | const instanceIndexServer = new MockInstancesIndex() | |
f6500729 | 138 | let port: number |
6f1b4fa4 C |
139 | |
140 | before(async () => { | |
f6500729 | 141 | port = await instanceIndexServer.initialize() |
6f1b4fa4 C |
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 | ||
a1587156 C |
150 | await checkFollow(servers[0], servers[1], false) |
151 | await checkFollow(servers[1], servers[0], false) | |
6f1b4fa4 C |
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: { | |
f6500729 | 163 | indexUrl: `http://localhost:${port}/api/v1/instances/hosts`, |
6f1b4fa4 C |
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 | ||
a1587156 | 174 | await checkFollow(servers[0], servers[1], true) |
6f1b4fa4 C |
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 | ||
a1587156 C |
187 | await checkFollow(servers[0], servers[1], false) |
188 | await checkFollow(servers[0], servers[2], true) | |
6f1b4fa4 C |
189 | }) |
190 | }) | |
191 | ||
8424c402 C |
192 | after(async function () { |
193 | await cleanupTests(servers) | |
194 | }) | |
195 | }) |