]>
Commit | Line | Data |
---|---|---|
8424c402 C |
1 | /* tslint:disable:no-unused-expression */ |
2 | ||
3 | import * as chai from 'chai' | |
4 | import 'mocha' | |
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 { | |
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 | await waitJobs(servers) | |
67 | ||
68 | await checkFollow(servers[0], servers[1], false) | |
69 | await checkFollow(servers[1], servers[0], false) | |
70 | } | |
71 | ||
72 | describe('Test auto follows', function () { | |
73 | let servers: ServerInfo[] = [] | |
74 | ||
75 | before(async function () { | |
76 | this.timeout(30000) | |
77 | ||
6f1b4fa4 | 78 | servers = await flushAndRunMultipleServers(3) |
8424c402 C |
79 | |
80 | // Get the access tokens | |
81 | await setAccessTokensToServers(servers) | |
82 | }) | |
83 | ||
84 | describe('Auto follow back', function () { | |
85 | ||
86 | it('Should not auto follow back if the option is not enabled', async function () { | |
87 | this.timeout(15000) | |
88 | ||
89 | await server1Follows2(servers) | |
90 | ||
91 | await checkFollow(servers[0], servers[1], true) | |
92 | await checkFollow(servers[1], servers[0], false) | |
93 | ||
94 | await resetFollows(servers) | |
95 | }) | |
96 | ||
97 | it('Should auto follow back on auto accept if the option is enabled', async function () { | |
98 | this.timeout(15000) | |
99 | ||
100 | const config = { | |
101 | followings: { | |
102 | instance: { | |
103 | autoFollowBack: { enabled: true } | |
104 | } | |
105 | } | |
106 | } | |
107 | await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) | |
108 | ||
109 | await server1Follows2(servers) | |
110 | ||
111 | await checkFollow(servers[0], servers[1], true) | |
112 | await checkFollow(servers[1], servers[0], true) | |
113 | ||
114 | await resetFollows(servers) | |
115 | }) | |
116 | ||
117 | it('Should wait the acceptation before auto follow back', async function () { | |
118 | this.timeout(30000) | |
119 | ||
120 | const config = { | |
121 | followings: { | |
122 | instance: { | |
123 | autoFollowBack: { enabled: true } | |
124 | } | |
125 | }, | |
126 | followers: { | |
127 | instance: { | |
128 | manualApproval: true | |
129 | } | |
130 | } | |
131 | } | |
132 | await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) | |
133 | ||
134 | await server1Follows2(servers) | |
135 | ||
136 | await checkFollow(servers[0], servers[1], false) | |
137 | await checkFollow(servers[1], servers[0], false) | |
138 | ||
139 | await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@' + servers[0].host) | |
140 | await waitJobs(servers) | |
141 | ||
142 | await checkFollow(servers[0], servers[1], true) | |
143 | await checkFollow(servers[1], servers[0], true) | |
144 | ||
145 | await resetFollows(servers) | |
2ba613a5 C |
146 | |
147 | config.followings.instance.autoFollowBack.enabled = false | |
148 | config.followers.instance.manualApproval = false | |
149 | await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) | |
8424c402 C |
150 | }) |
151 | }) | |
152 | ||
6f1b4fa4 C |
153 | describe('Auto follow index', function () { |
154 | const instanceIndexServer = new MockInstancesIndex() | |
155 | ||
156 | before(async () => { | |
157 | await instanceIndexServer.initialize() | |
158 | }) | |
159 | ||
160 | it('Should not auto follow index if the option is not enabled', async function () { | |
161 | this.timeout(30000) | |
162 | ||
163 | await wait(5000) | |
164 | await waitJobs(servers) | |
165 | ||
166 | await checkFollow(servers[ 0 ], servers[ 1 ], false) | |
167 | await checkFollow(servers[ 1 ], servers[ 0 ], false) | |
168 | }) | |
169 | ||
170 | it('Should auto follow the index', async function () { | |
171 | this.timeout(30000) | |
172 | ||
173 | instanceIndexServer.addInstance(servers[1].host) | |
174 | ||
175 | const config = { | |
176 | followings: { | |
177 | instance: { | |
178 | autoFollowIndex: { | |
179 | indexUrl: 'http://localhost:42100', | |
180 | enabled: true | |
181 | } | |
182 | } | |
183 | } | |
184 | } | |
185 | await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) | |
186 | ||
187 | await wait(5000) | |
188 | await waitJobs(servers) | |
189 | ||
190 | await checkFollow(servers[ 0 ], servers[ 1 ], true) | |
191 | ||
192 | await resetFollows(servers) | |
193 | }) | |
194 | ||
195 | it('Should follow new added instances in the index but not old ones', async function () { | |
196 | this.timeout(30000) | |
197 | ||
198 | instanceIndexServer.addInstance(servers[2].host) | |
199 | ||
200 | await wait(5000) | |
201 | await waitJobs(servers) | |
202 | ||
203 | await checkFollow(servers[ 0 ], servers[ 1 ], false) | |
204 | await checkFollow(servers[ 0 ], servers[ 2 ], true) | |
205 | }) | |
206 | }) | |
207 | ||
8424c402 C |
208 | after(async function () { |
209 | await cleanupTests(servers) | |
210 | }) | |
211 | }) |