]>
Commit | Line | Data |
---|---|---|
74d249bc C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
c55e3d72 | 5 | import { wait } from '@shared/core-utils' |
74d249bc C |
6 | import { |
7 | cleanupTests, | |
254d3579 | 8 | createMultipleServers, |
4c7e60bc | 9 | doubleFollow, |
254d3579 | 10 | PeerTubeServer, |
12edc149 | 11 | setAccessTokensToServers, |
12edc149 | 12 | waitJobs |
bf54587a | 13 | } from '@shared/server-commands' |
74d249bc C |
14 | |
15 | const expect = chai.expect | |
16 | ||
17 | describe('Test AP cleaner', function () { | |
254d3579 | 18 | let servers: PeerTubeServer[] = [] |
74d249bc C |
19 | let videoUUID1: string |
20 | let videoUUID2: string | |
21 | let videoUUID3: string | |
22 | ||
23 | let videoUUIDs: string[] | |
24 | ||
25 | before(async function () { | |
26 | this.timeout(120000) | |
27 | ||
28 | const config = { | |
29 | federation: { | |
30 | videos: { cleanup_remote_interactions: true } | |
31 | } | |
32 | } | |
254d3579 | 33 | servers = await createMultipleServers(3, config) |
74d249bc C |
34 | |
35 | // Get the access tokens | |
36 | await setAccessTokensToServers(servers) | |
37 | ||
38 | await Promise.all([ | |
39 | doubleFollow(servers[0], servers[1]), | |
40 | doubleFollow(servers[1], servers[2]), | |
41 | doubleFollow(servers[0], servers[2]) | |
42 | ]) | |
43 | ||
44 | // Update 1 local share, check 6 shares | |
45 | ||
46 | // Create 1 comment per video | |
47 | // Update 1 remote URL and 1 local URL on | |
48 | ||
89d241a7 C |
49 | videoUUID1 = (await servers[0].videos.quickUpload({ name: 'server 1' })).uuid |
50 | videoUUID2 = (await servers[1].videos.quickUpload({ name: 'server 2' })).uuid | |
51 | videoUUID3 = (await servers[2].videos.quickUpload({ name: 'server 3' })).uuid | |
74d249bc C |
52 | |
53 | videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ] | |
54 | ||
55 | await waitJobs(servers) | |
56 | ||
57 | for (const server of servers) { | |
58 | for (const uuid of videoUUIDs) { | |
89d241a7 C |
59 | await server.videos.rate({ id: uuid, rating: 'like' }) |
60 | await server.comments.createThread({ videoId: uuid, text: 'comment' }) | |
74d249bc C |
61 | } |
62 | } | |
63 | ||
64 | await waitJobs(servers) | |
65 | }) | |
66 | ||
67 | it('Should have the correct likes', async function () { | |
68 | for (const server of servers) { | |
69 | for (const uuid of videoUUIDs) { | |
89d241a7 | 70 | const video = await server.videos.get({ id: uuid }) |
d23dd9fb C |
71 | |
72 | expect(video.likes).to.equal(3) | |
73 | expect(video.dislikes).to.equal(0) | |
74d249bc C |
74 | } |
75 | } | |
76 | }) | |
77 | ||
78 | it('Should destroy server 3 internal likes and correctly clean them', async function () { | |
79 | this.timeout(20000) | |
80 | ||
89d241a7 | 81 | await servers[2].sql.deleteAll('accountVideoRate') |
74d249bc | 82 | for (const uuid of videoUUIDs) { |
89d241a7 | 83 | await servers[2].sql.setVideoField(uuid, 'likes', '0') |
74d249bc C |
84 | } |
85 | ||
86 | await wait(5000) | |
87 | await waitJobs(servers) | |
88 | ||
89 | // Updated rates of my video | |
90 | { | |
89d241a7 | 91 | const video = await servers[0].videos.get({ id: videoUUID1 }) |
d23dd9fb C |
92 | expect(video.likes).to.equal(2) |
93 | expect(video.dislikes).to.equal(0) | |
74d249bc C |
94 | } |
95 | ||
96 | // Did not update rates of a remote video | |
97 | { | |
89d241a7 | 98 | const video = await servers[0].videos.get({ id: videoUUID2 }) |
d23dd9fb C |
99 | expect(video.likes).to.equal(3) |
100 | expect(video.dislikes).to.equal(0) | |
74d249bc C |
101 | } |
102 | }) | |
103 | ||
104 | it('Should update rates to dislikes', async function () { | |
105 | this.timeout(20000) | |
106 | ||
107 | for (const server of servers) { | |
108 | for (const uuid of videoUUIDs) { | |
89d241a7 | 109 | await server.videos.rate({ id: uuid, rating: 'dislike' }) |
74d249bc C |
110 | } |
111 | } | |
112 | ||
113 | await waitJobs(servers) | |
114 | ||
115 | for (const server of servers) { | |
116 | for (const uuid of videoUUIDs) { | |
89d241a7 | 117 | const video = await server.videos.get({ id: uuid }) |
d23dd9fb C |
118 | expect(video.likes).to.equal(0) |
119 | expect(video.dislikes).to.equal(3) | |
74d249bc C |
120 | } |
121 | } | |
122 | }) | |
123 | ||
124 | it('Should destroy server 3 internal dislikes and correctly clean them', async function () { | |
125 | this.timeout(20000) | |
126 | ||
89d241a7 | 127 | await servers[2].sql.deleteAll('accountVideoRate') |
74d249bc C |
128 | |
129 | for (const uuid of videoUUIDs) { | |
89d241a7 | 130 | await servers[2].sql.setVideoField(uuid, 'dislikes', '0') |
74d249bc C |
131 | } |
132 | ||
133 | await wait(5000) | |
134 | await waitJobs(servers) | |
135 | ||
136 | // Updated rates of my video | |
137 | { | |
89d241a7 | 138 | const video = await servers[0].videos.get({ id: videoUUID1 }) |
d23dd9fb C |
139 | expect(video.likes).to.equal(0) |
140 | expect(video.dislikes).to.equal(2) | |
74d249bc C |
141 | } |
142 | ||
143 | // Did not update rates of a remote video | |
144 | { | |
89d241a7 | 145 | const video = await servers[0].videos.get({ id: videoUUID2 }) |
d23dd9fb C |
146 | expect(video.likes).to.equal(0) |
147 | expect(video.dislikes).to.equal(3) | |
74d249bc C |
148 | } |
149 | }) | |
150 | ||
151 | it('Should destroy server 3 internal shares and correctly clean them', async function () { | |
152 | this.timeout(20000) | |
153 | ||
89d241a7 | 154 | const preCount = await servers[0].sql.getCount('videoShare') |
74d249bc C |
155 | expect(preCount).to.equal(6) |
156 | ||
89d241a7 | 157 | await servers[2].sql.deleteAll('videoShare') |
74d249bc C |
158 | await wait(5000) |
159 | await waitJobs(servers) | |
160 | ||
161 | // Still 6 because we don't have remote shares on local videos | |
89d241a7 | 162 | const postCount = await servers[0].sql.getCount('videoShare') |
74d249bc C |
163 | expect(postCount).to.equal(6) |
164 | }) | |
165 | ||
166 | it('Should destroy server 3 internal comments and correctly clean them', async function () { | |
167 | this.timeout(20000) | |
168 | ||
169 | { | |
89d241a7 | 170 | const { total } = await servers[0].comments.listThreads({ videoId: videoUUID1 }) |
12edc149 | 171 | expect(total).to.equal(3) |
74d249bc C |
172 | } |
173 | ||
89d241a7 | 174 | await servers[2].sql.deleteAll('videoComment') |
74d249bc C |
175 | |
176 | await wait(5000) | |
177 | await waitJobs(servers) | |
178 | ||
179 | { | |
89d241a7 | 180 | const { total } = await servers[0].comments.listThreads({ videoId: videoUUID1 }) |
12edc149 | 181 | expect(total).to.equal(2) |
74d249bc C |
182 | } |
183 | }) | |
184 | ||
185 | it('Should correctly update rate URLs', async function () { | |
186 | this.timeout(30000) | |
187 | ||
188 | async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') { | |
189 | const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` + | |
190 | `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'` | |
89d241a7 | 191 | const res = await servers[0].sql.selectQuery(query) |
74d249bc C |
192 | |
193 | for (const rate of res) { | |
194 | const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`) | |
195 | expect(rate.url).to.match(matcher) | |
196 | } | |
197 | } | |
198 | ||
199 | async function checkLocal () { | |
200 | const startsWith = 'http://' + servers[0].host + '%' | |
201 | // On local videos | |
202 | await check(startsWith, servers[0].url, '', 'false') | |
203 | // On remote videos | |
204 | await check(startsWith, servers[0].url, '', 'true') | |
205 | } | |
206 | ||
207 | async function checkRemote (suffix: string) { | |
208 | const startsWith = 'http://' + servers[1].host + '%' | |
209 | // On local videos | |
210 | await check(startsWith, servers[1].url, suffix, 'false') | |
211 | // On remote videos, we should not update URLs so no suffix | |
212 | await check(startsWith, servers[1].url, '', 'true') | |
213 | } | |
214 | ||
215 | await checkLocal() | |
216 | await checkRemote('') | |
217 | ||
218 | { | |
219 | const query = `UPDATE "accountVideoRate" SET url = url || 'stan'` | |
89d241a7 | 220 | await servers[1].sql.updateQuery(query) |
74d249bc C |
221 | |
222 | await wait(5000) | |
223 | await waitJobs(servers) | |
224 | } | |
225 | ||
226 | await checkLocal() | |
227 | await checkRemote('stan') | |
228 | }) | |
229 | ||
230 | it('Should correctly update comment URLs', async function () { | |
231 | this.timeout(30000) | |
232 | ||
233 | async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') { | |
234 | const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` + | |
235 | `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'` | |
236 | ||
89d241a7 | 237 | const res = await servers[0].sql.selectQuery(query) |
74d249bc C |
238 | |
239 | for (const comment of res) { | |
240 | const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`) | |
241 | expect(comment.url).to.match(matcher) | |
242 | } | |
243 | } | |
244 | ||
245 | async function checkLocal () { | |
246 | const startsWith = 'http://' + servers[0].host + '%' | |
247 | // On local videos | |
248 | await check(startsWith, servers[0].url, '', 'false') | |
249 | // On remote videos | |
250 | await check(startsWith, servers[0].url, '', 'true') | |
251 | } | |
252 | ||
253 | async function checkRemote (suffix: string) { | |
254 | const startsWith = 'http://' + servers[1].host + '%' | |
255 | // On local videos | |
256 | await check(startsWith, servers[1].url, suffix, 'false') | |
257 | // On remote videos, we should not update URLs so no suffix | |
258 | await check(startsWith, servers[1].url, '', 'true') | |
259 | } | |
260 | ||
261 | { | |
262 | const query = `UPDATE "videoComment" SET url = url || 'kyle'` | |
89d241a7 | 263 | await servers[1].sql.updateQuery(query) |
74d249bc C |
264 | |
265 | await wait(5000) | |
266 | await waitJobs(servers) | |
267 | } | |
268 | ||
269 | await checkLocal() | |
270 | await checkRemote('kyle') | |
271 | }) | |
272 | ||
f1569117 C |
273 | it('Should remove unavailable remote resources', async function () { |
274 | this.timeout(240000) | |
275 | ||
276 | async function expectNotDeleted () { | |
277 | { | |
278 | const video = await servers[0].videos.get({ id: uuid }) | |
279 | ||
280 | expect(video.likes).to.equal(3) | |
281 | expect(video.dislikes).to.equal(0) | |
282 | } | |
283 | ||
284 | { | |
285 | const { total } = await servers[0].comments.listThreads({ videoId: uuid }) | |
286 | expect(total).to.equal(3) | |
287 | } | |
288 | } | |
289 | ||
290 | async function expectDeleted () { | |
291 | { | |
292 | const video = await servers[0].videos.get({ id: uuid }) | |
293 | ||
10a72a7e | 294 | expect(video.likes).to.equal(2) |
f1569117 C |
295 | expect(video.dislikes).to.equal(0) |
296 | } | |
297 | ||
298 | { | |
10a72a7e | 299 | const { total } = await servers[0].comments.listThreads({ videoId: uuid }) |
f1569117 C |
300 | expect(total).to.equal(2) |
301 | } | |
302 | } | |
303 | ||
304 | const uuid = (await servers[0].videos.quickUpload({ name: 'server 1 video 2' })).uuid | |
305 | ||
306 | await waitJobs(servers) | |
307 | ||
308 | for (const server of servers) { | |
309 | await server.videos.rate({ id: uuid, rating: 'like' }) | |
310 | await server.comments.createThread({ videoId: uuid, text: 'comment' }) | |
311 | } | |
312 | ||
313 | await waitJobs(servers) | |
314 | ||
315 | await expectNotDeleted() | |
316 | ||
317 | await servers[1].kill() | |
318 | ||
319 | await wait(5000) | |
320 | await expectNotDeleted() | |
321 | ||
10a72a7e C |
322 | let continueWhile = true |
323 | ||
324 | do { | |
325 | try { | |
326 | await expectDeleted() | |
327 | continueWhile = false | |
328 | } catch { | |
329 | } | |
330 | } while (continueWhile) | |
f1569117 C |
331 | }) |
332 | ||
74d249bc C |
333 | after(async function () { |
334 | await cleanupTests(servers) | |
74d249bc C |
335 | }) |
336 | }) |