]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/activitypub/cleaner.ts
Introduce user command
[github/Chocobozzz/PeerTube.git] / server / tests / api / activitypub / cleaner.ts
CommitLineData
74d249bc
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
5import {
6 cleanupTests,
74d249bc 7 doubleFollow,
12edc149 8 flushAndRunMultipleServers,
12edc149
C
9 getVideo,
10 rateVideo,
12edc149
C
11 ServerInfo,
12 setAccessTokensToServers,
12edc149
C
13 uploadVideoAndGetId,
14 wait,
15 waitJobs
16} from '@shared/extra-utils'
74d249bc
C
17
18const expect = chai.expect
19
20describe('Test AP cleaner', function () {
21 let servers: ServerInfo[] = []
22 let videoUUID1: string
23 let videoUUID2: string
24 let videoUUID3: string
25
26 let videoUUIDs: string[]
27
28 before(async function () {
29 this.timeout(120000)
30
31 const config = {
32 federation: {
33 videos: { cleanup_remote_interactions: true }
34 }
35 }
36 servers = await flushAndRunMultipleServers(3, config)
37
38 // Get the access tokens
39 await setAccessTokensToServers(servers)
40
41 await Promise.all([
42 doubleFollow(servers[0], servers[1]),
43 doubleFollow(servers[1], servers[2]),
44 doubleFollow(servers[0], servers[2])
45 ])
46
47 // Update 1 local share, check 6 shares
48
49 // Create 1 comment per video
50 // Update 1 remote URL and 1 local URL on
51
52 videoUUID1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' })).uuid
53 videoUUID2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
54 videoUUID3 = (await uploadVideoAndGetId({ server: servers[2], videoName: 'server 3' })).uuid
55
56 videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ]
57
58 await waitJobs(servers)
59
60 for (const server of servers) {
61 for (const uuid of videoUUIDs) {
62 await rateVideo(server.url, server.accessToken, uuid, 'like')
12edc149 63 await server.commentsCommand.createThread({ videoId: uuid, text: 'comment' })
74d249bc
C
64 }
65 }
66
67 await waitJobs(servers)
68 })
69
70 it('Should have the correct likes', async function () {
71 for (const server of servers) {
72 for (const uuid of videoUUIDs) {
73 const res = await getVideo(server.url, uuid)
74 expect(res.body.likes).to.equal(3)
75 expect(res.body.dislikes).to.equal(0)
76 }
77 }
78 })
79
80 it('Should destroy server 3 internal likes and correctly clean them', async function () {
81 this.timeout(20000)
82
9293139f 83 await servers[2].sqlCommand.deleteAll('accountVideoRate')
74d249bc 84 for (const uuid of videoUUIDs) {
9293139f 85 await servers[2].sqlCommand.setVideoField(uuid, 'likes', '0')
74d249bc
C
86 }
87
88 await wait(5000)
89 await waitJobs(servers)
90
91 // Updated rates of my video
92 {
93 const res = await getVideo(servers[0].url, videoUUID1)
94 expect(res.body.likes).to.equal(2)
95 expect(res.body.dislikes).to.equal(0)
96 }
97
98 // Did not update rates of a remote video
99 {
100 const res = await getVideo(servers[0].url, videoUUID2)
101 expect(res.body.likes).to.equal(3)
102 expect(res.body.dislikes).to.equal(0)
103 }
104 })
105
106 it('Should update rates to dislikes', async function () {
107 this.timeout(20000)
108
109 for (const server of servers) {
110 for (const uuid of videoUUIDs) {
111 await rateVideo(server.url, server.accessToken, uuid, 'dislike')
112 }
113 }
114
115 await waitJobs(servers)
116
117 for (const server of servers) {
118 for (const uuid of videoUUIDs) {
119 const res = await getVideo(server.url, uuid)
120 expect(res.body.likes).to.equal(0)
121 expect(res.body.dislikes).to.equal(3)
122 }
123 }
124 })
125
126 it('Should destroy server 3 internal dislikes and correctly clean them', async function () {
127 this.timeout(20000)
128
9293139f 129 await servers[2].sqlCommand.deleteAll('accountVideoRate')
74d249bc
C
130
131 for (const uuid of videoUUIDs) {
9293139f 132 await servers[2].sqlCommand.setVideoField(uuid, 'dislikes', '0')
74d249bc
C
133 }
134
135 await wait(5000)
136 await waitJobs(servers)
137
138 // Updated rates of my video
139 {
140 const res = await getVideo(servers[0].url, videoUUID1)
141 expect(res.body.likes).to.equal(0)
142 expect(res.body.dislikes).to.equal(2)
143 }
144
145 // Did not update rates of a remote video
146 {
147 const res = await getVideo(servers[0].url, videoUUID2)
148 expect(res.body.likes).to.equal(0)
149 expect(res.body.dislikes).to.equal(3)
150 }
151 })
152
153 it('Should destroy server 3 internal shares and correctly clean them', async function () {
154 this.timeout(20000)
155
9293139f 156 const preCount = await servers[0].sqlCommand.getCount('videoShare')
74d249bc
C
157 expect(preCount).to.equal(6)
158
9293139f 159 await servers[2].sqlCommand.deleteAll('videoShare')
74d249bc
C
160 await wait(5000)
161 await waitJobs(servers)
162
163 // Still 6 because we don't have remote shares on local videos
9293139f 164 const postCount = await servers[0].sqlCommand.getCount('videoShare')
74d249bc
C
165 expect(postCount).to.equal(6)
166 })
167
168 it('Should destroy server 3 internal comments and correctly clean them', async function () {
169 this.timeout(20000)
170
171 {
12edc149
C
172 const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 })
173 expect(total).to.equal(3)
74d249bc
C
174 }
175
9293139f 176 await servers[2].sqlCommand.deleteAll('videoComment')
74d249bc
C
177
178 await wait(5000)
179 await waitJobs(servers)
180
181 {
12edc149
C
182 const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 })
183 expect(total).to.equal(2)
74d249bc
C
184 }
185 })
186
187 it('Should correctly update rate URLs', async function () {
188 this.timeout(30000)
189
190 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
191 const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` +
192 `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'`
9293139f 193 const res = await servers[0].sqlCommand.selectQuery(query)
74d249bc
C
194
195 for (const rate of res) {
196 const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`)
197 expect(rate.url).to.match(matcher)
198 }
199 }
200
201 async function checkLocal () {
202 const startsWith = 'http://' + servers[0].host + '%'
203 // On local videos
204 await check(startsWith, servers[0].url, '', 'false')
205 // On remote videos
206 await check(startsWith, servers[0].url, '', 'true')
207 }
208
209 async function checkRemote (suffix: string) {
210 const startsWith = 'http://' + servers[1].host + '%'
211 // On local videos
212 await check(startsWith, servers[1].url, suffix, 'false')
213 // On remote videos, we should not update URLs so no suffix
214 await check(startsWith, servers[1].url, '', 'true')
215 }
216
217 await checkLocal()
218 await checkRemote('')
219
220 {
221 const query = `UPDATE "accountVideoRate" SET url = url || 'stan'`
9293139f 222 await servers[1].sqlCommand.updateQuery(query)
74d249bc
C
223
224 await wait(5000)
225 await waitJobs(servers)
226 }
227
228 await checkLocal()
229 await checkRemote('stan')
230 })
231
232 it('Should correctly update comment URLs', async function () {
233 this.timeout(30000)
234
235 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
236 const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` +
237 `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'`
238
9293139f 239 const res = await servers[0].sqlCommand.selectQuery(query)
74d249bc
C
240
241 for (const comment of res) {
242 const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`)
243 expect(comment.url).to.match(matcher)
244 }
245 }
246
247 async function checkLocal () {
248 const startsWith = 'http://' + servers[0].host + '%'
249 // On local videos
250 await check(startsWith, servers[0].url, '', 'false')
251 // On remote videos
252 await check(startsWith, servers[0].url, '', 'true')
253 }
254
255 async function checkRemote (suffix: string) {
256 const startsWith = 'http://' + servers[1].host + '%'
257 // On local videos
258 await check(startsWith, servers[1].url, suffix, 'false')
259 // On remote videos, we should not update URLs so no suffix
260 await check(startsWith, servers[1].url, '', 'true')
261 }
262
263 {
264 const query = `UPDATE "videoComment" SET url = url || 'kyle'`
9293139f 265 await servers[1].sqlCommand.updateQuery(query)
74d249bc
C
266
267 await wait(5000)
268 await waitJobs(servers)
269 }
270
271 await checkLocal()
272 await checkRemote('kyle')
273 })
274
275 after(async function () {
276 await cleanupTests(servers)
74d249bc
C
277 })
278})