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