]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/activitypub/cleaner.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / api / activitypub / cleaner.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 waitJobs
13 } from '@shared/server-commands'
14
15 const expect = chai.expect
16
17 describe('Test AP cleaner', function () {
18 let servers: PeerTubeServer[] = []
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 }
33 servers = await createMultipleServers(3, config)
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
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
52
53 videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ]
54
55 await waitJobs(servers)
56
57 for (const server of servers) {
58 for (const uuid of videoUUIDs) {
59 await server.videos.rate({ id: uuid, rating: 'like' })
60 await server.comments.createThread({ videoId: uuid, text: 'comment' })
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) {
70 const video = await server.videos.get({ id: uuid })
71
72 expect(video.likes).to.equal(3)
73 expect(video.dislikes).to.equal(0)
74 }
75 }
76 })
77
78 it('Should destroy server 3 internal likes and correctly clean them', async function () {
79 this.timeout(20000)
80
81 await servers[2].sql.deleteAll('accountVideoRate')
82 for (const uuid of videoUUIDs) {
83 await servers[2].sql.setVideoField(uuid, 'likes', '0')
84 }
85
86 await wait(5000)
87 await waitJobs(servers)
88
89 // Updated rates of my video
90 {
91 const video = await servers[0].videos.get({ id: videoUUID1 })
92 expect(video.likes).to.equal(2)
93 expect(video.dislikes).to.equal(0)
94 }
95
96 // Did not update rates of a remote video
97 {
98 const video = await servers[0].videos.get({ id: videoUUID2 })
99 expect(video.likes).to.equal(3)
100 expect(video.dislikes).to.equal(0)
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) {
109 await server.videos.rate({ id: uuid, rating: 'dislike' })
110 }
111 }
112
113 await waitJobs(servers)
114
115 for (const server of servers) {
116 for (const uuid of videoUUIDs) {
117 const video = await server.videos.get({ id: uuid })
118 expect(video.likes).to.equal(0)
119 expect(video.dislikes).to.equal(3)
120 }
121 }
122 })
123
124 it('Should destroy server 3 internal dislikes and correctly clean them', async function () {
125 this.timeout(20000)
126
127 await servers[2].sql.deleteAll('accountVideoRate')
128
129 for (const uuid of videoUUIDs) {
130 await servers[2].sql.setVideoField(uuid, 'dislikes', '0')
131 }
132
133 await wait(5000)
134 await waitJobs(servers)
135
136 // Updated rates of my video
137 {
138 const video = await servers[0].videos.get({ id: videoUUID1 })
139 expect(video.likes).to.equal(0)
140 expect(video.dislikes).to.equal(2)
141 }
142
143 // Did not update rates of a remote video
144 {
145 const video = await servers[0].videos.get({ id: videoUUID2 })
146 expect(video.likes).to.equal(0)
147 expect(video.dislikes).to.equal(3)
148 }
149 })
150
151 it('Should destroy server 3 internal shares and correctly clean them', async function () {
152 this.timeout(20000)
153
154 const preCount = await servers[0].sql.getCount('videoShare')
155 expect(preCount).to.equal(6)
156
157 await servers[2].sql.deleteAll('videoShare')
158 await wait(5000)
159 await waitJobs(servers)
160
161 // Still 6 because we don't have remote shares on local videos
162 const postCount = await servers[0].sql.getCount('videoShare')
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 {
170 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID1 })
171 expect(total).to.equal(3)
172 }
173
174 await servers[2].sql.deleteAll('videoComment')
175
176 await wait(5000)
177 await waitJobs(servers)
178
179 {
180 const { total } = await servers[0].comments.listThreads({ videoId: videoUUID1 })
181 expect(total).to.equal(2)
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}'`
191 const res = await servers[0].sql.selectQuery(query)
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'`
220 await servers[1].sql.updateQuery(query)
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
237 const res = await servers[0].sql.selectQuery(query)
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'`
263 await servers[1].sql.updateQuery(query)
264
265 await wait(5000)
266 await waitJobs(servers)
267 }
268
269 await checkLocal()
270 await checkRemote('kyle')
271 })
272
273 after(async function () {
274 await cleanupTests(servers)
275 })
276 })